Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

main: add re_thread_async_main_id and re_thread_async_main_cancel #697

Merged
merged 1 commit into from
Feb 17, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions include/re_main.h
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,10 @@ int re_thread_async(re_async_work_h *work, re_async_h *cb, void *arg);
int re_thread_async_main(re_async_work_h *work, re_async_h *cb, void *arg);
int re_thread_async_id(intptr_t id, re_async_work_h *work, re_async_h *cb,
void *arg);
int re_thread_async_main_id(intptr_t id, re_async_work_h *work, re_async_h *cb,
void *arg);
void re_thread_async_cancel(intptr_t id);
void re_thread_async_main_cancel(intptr_t id);

void re_set_mutex(void *mutexp);

Expand Down
54 changes: 50 additions & 4 deletions src/main/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -1457,7 +1457,7 @@ int re_thread_async_main(re_async_work_h *work, re_async_h *cb, void *arg)


/**
* Execute work handler for current event loop
* Execute work handler for current event loop with identifier
*
* @param id Work identifier
* @param work Work handler
Expand Down Expand Up @@ -1488,6 +1488,38 @@ int re_thread_async_id(intptr_t id, re_async_work_h *work, re_async_h *cb,
}


/**
* Execute work handler for re_global main event loop with identifier
*
* @param id Work identifier
* @param work Work handler
* @param cb Callback handler (called by re poll thread)
* @param arg Handler argument (has to be thread-safe and mem_deref-safe)
*
* @return 0 if success, otherwise errorcode
*/
int re_thread_async_main_id(intptr_t id, re_async_work_h *work, re_async_h *cb,
void *arg)
{
struct re *re = re_global;
int err;

if (unlikely(!re)) {
DEBUG_WARNING("re_thread_async_id: re not ready\n");
return EAGAIN;
}

if (unlikely(!re->async)) {
/* fallback needed for internal libre functions */
err = re_async_alloc(&re->async, RE_THREAD_WORKERS);
if (err)
return err;
}

return re_async(re->async, id, work, cb, arg);
}


/**
* Cancel pending async work and callback
*
Expand All @@ -1502,9 +1534,23 @@ void re_thread_async_cancel(intptr_t id)
return;
}

#ifndef RELEASE
re_thread_check();
#endif
re_async_cancel(re->async, id);
}


/**
* Cancel pending async work and callback for re_global main event loop
*
* @param id Work identifier
*/
void re_thread_async_main_cancel(intptr_t id)
{
struct re *re = re_global;

if (unlikely(!re)) {
DEBUG_WARNING("re_thread_async_cancel: re not ready\n");
return;
}

re_async_cancel(re->async, id);
}