Skip to content

Commit

Permalink
Now adds a self reference to prevent GC until after close has been ca…
Browse files Browse the repository at this point in the history
…lled and

any queued messages have been flushed.
  • Loading branch information
pjsg committed Apr 24, 2024
1 parent f12b6cb commit f017089
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 0 deletions.
33 changes: 33 additions & 0 deletions components/modules/rotary.c
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ typedef struct {
uint32_t last_event_time;
int callback[CALLBACK_COUNT];
esp_timer_handle_t timer_handle;
int self_ref;
} DATA;

static task_handle_t tasknumber;
Expand Down Expand Up @@ -141,6 +142,8 @@ static int lrotary_setup( lua_State* L )
d->callback[i] = LUA_NOREF;
}

d->self_ref = LUA_NOREF;

d->click_delay_us = CLICK_DELAY_US;
d->longpress_delay_us = LONGPRESS_DELAY_US;

Expand Down Expand Up @@ -173,6 +176,27 @@ static int lrotary_setup( lua_State* L )
return 1;
}

static void update_self_ref(lua_State *L, DATA *d, int argnum) {
bool have_callback = false;
for (int i = 0; i < CALLBACK_COUNT; i++) {
if (d->callback[i] != LUA_NOREF) {
have_callback = true;
break;
}
}
if (have_callback) {
if (d->self_ref == LUA_NOREF && argnum > 0) {
lua_pushvalue(L, argnum);
d->self_ref = luaL_ref(L, LUA_REGISTRYINDEX);
}
} else {
if (d->self_ref != LUA_NOREF) {
luaL_unref(L, LUA_REGISTRYINDEX, d->self_ref);
d->self_ref = LUA_NOREF;
}
}
}

// Lua: close( )
static int lrotary_close( lua_State* L )
{
Expand All @@ -181,6 +205,10 @@ static int lrotary_close( lua_State* L )
if (d->handle) {
callback_free(L, d, ROTARY_ALL);

if (!rotary_has_queued_task(d->handle)) {
update_self_ref(L, d, 1);
}

if (rotary_close( d->handle )) {
return luaL_error( L, "Unable to close switch." );
}
Expand All @@ -193,6 +221,7 @@ static int lrotary_close( lua_State* L )
esp_timer_delete(d->timer_handle);
d->timer_handle = NULL;
}

return 0;
}

Expand All @@ -211,6 +240,8 @@ static int lrotary_on( lua_State* L )
callback_free(L, d, mask);
}

update_self_ref(L, d, 1);

return 0;
}

Expand Down Expand Up @@ -347,6 +378,8 @@ static void lrotary_task(task_param_t param, task_prio_t prio)
if (need_to_post) {
// If there is pending stuff, queue another task
task_post_medium(tasknumber, param);
} else if (d) {
update_self_ref(L, d, -1);
}
}

Expand Down
9 changes: 9 additions & 0 deletions components/modules/rotary_driver.c
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,15 @@ void rotary_event_handled(rotary_driver_handle_t d)
d->task_queued = 0;
}

bool rotary_has_queued_task(rotary_driver_handle_t d) {
if (!d) {
return false;
}

return d->task_queued;
}


// The pin numbers are actual platform GPIO numbers
rotary_driver_handle_t rotary_setup(int phase_a, int phase_b, int press,
task_handle_t tasknumber, void *arg) {
Expand Down
2 changes: 2 additions & 0 deletions components/modules/rotary_driver.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,6 @@ int rotary_close(struct rotary_driver_handle *handle);

void rotary_event_handled(struct rotary_driver_handle *handle);

bool rotary_has_queued_task(struct rotary_driver_handle *handle);

#endif

0 comments on commit f017089

Please sign in to comment.