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

async: make work handler and callback optional #481

Merged
merged 1 commit into from
Aug 20, 2022
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
2 changes: 1 addition & 1 deletion include/re_async.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ typedef int (re_async_work_h)(void *arg);
typedef void (re_async_h)(int err, void *arg);

int re_async_alloc(struct re_async **asyncp, uint16_t workers);
int re_async(struct re_async *a, re_async_work_h *work, re_async_h *cb,
int re_async(struct re_async *a, re_async_work_h *workh, re_async_h *cb,
void *arg);

#endif
20 changes: 11 additions & 9 deletions src/async/async.c
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@

struct async_work {
struct le le;
re_async_work_h *work;
re_async_work_h *workh;
re_async_h *cb;
void *arg;
int err;
Expand Down Expand Up @@ -64,8 +64,9 @@ static int worker_thread(void *arg)
list_move(le, &a->curl);
mtx_unlock(&a->mtx);

work = le->data;
work->err = work->work(work->arg);
work = le->data;
if (work->workh)
work->err = work->workh(work->arg);

mtx_lock(&a->mtx);
mqueue_push(a->mqueue, 0, work);
Expand Down Expand Up @@ -125,7 +126,8 @@ static void queueh(int id, void *data, void *arg)
struct re_async *async = arg;
(void)id;

work->cb(work->err, work->arg);
if (work->cb)
work->cb(work->err, work->arg);

mtx_lock(&async->mtx);
list_move(&work->le, &async->freel);
Expand Down Expand Up @@ -214,13 +216,13 @@ int re_async_alloc(struct re_async **asyncp, uint16_t workers)
*
* @return 0 if success, otherwise errorcode
*/
int re_async(struct re_async *async, re_async_work_h *work, re_async_h *cb,
int re_async(struct re_async *async, re_async_work_h *workh, re_async_h *cb,
void *arg)
{
int err = 0;
struct async_work *async_work;

if (unlikely(!async || !work || !cb))
if (unlikely(!async))
return EINVAL;

if (unlikely(list_isempty(&async->freel))) {
Expand All @@ -233,9 +235,9 @@ int re_async(struct re_async *async, re_async_work_h *work, re_async_h *cb,
list_unlink(&async_work->le);
}

async_work->work = work;
async_work->cb = cb;
async_work->arg = arg;
async_work->workh = workh;
async_work->cb = cb;
async_work->arg = arg;

mtx_lock(&async->mtx);
list_append(&async->workl, &async_work->le, async_work);
Expand Down