Skip to content
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: 0 additions & 2 deletions examples/texec_example.c
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,6 @@ int main(void) {

int ret = 0;
if (st == TEXEC_STATUS_OK) {
texec_task_handle_wait(handle);

int result = -1;
st = texec_task_handle_result(handle, &result);
if (st == TEXEC_STATUS_OK) {
Expand Down
8 changes: 6 additions & 2 deletions include/texec/task_handle.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,14 @@ typedef struct texec_task_handle texec_task_handle_t;
texec_status_t texec_task_handle_retain(texec_task_handle_t* h);
void texec_task_handle_release(texec_task_handle_t* h);

void texec_task_handle_wait(texec_task_handle_t* h);
texec_status_t texec_task_handle_try_result(texec_task_handle_t* h, int* out_result);
texec_status_t texec_task_handle_result(texec_task_handle_t* h, int* out_result);
bool texec_task_handle_is_done(texec_task_handle_t* h);

texec_status_t texec_task_handle_result(texec_task_handle_t* h, int* out_result);
static inline texec_status_t texec_task_handle_wait(texec_task_handle_t* h) {
int result;
return texec_task_handle_result(h, &result);
}

#ifdef __cplusplus
}
Expand Down
35 changes: 18 additions & 17 deletions src/task_handle.c
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,19 @@ static inline texec_status_t task_handle_unlock_return(texec_task_handle_t* h, t
return st;
}

static inline texec_status_t task_handle_get_result(texec_task_handle_t* h, int* out_result, bool block)
{
if (!h || !out_result) return TEXEC_STATUS_INVALID_ARGUMENT;
mtx_lock(&h->mtx);
while (!h->done) {
if (!block) return task_handle_unlock_return(h, TEXEC_STATUS_NOT_READY);
cnd_wait(&h->cv, &h->mtx);
}
*out_result = h->result;
mtx_unlock(&h->mtx);
return TEXEC_STATUS_OK;
}

texec_task_handle_t* texec_task_handle_create(const texec_allocator_t* alloc) {
texec_task_handle_t* h = texec_allocate(alloc, sizeof(*h), _Alignof(texec_task_handle_t));
if (!task_handle_init(h, alloc)) {
Expand Down Expand Up @@ -108,30 +121,18 @@ void texec_task_handle_release(texec_task_handle_t* h) {
}
}

void texec_task_handle_wait(texec_task_handle_t* h) {
if (!h) return;
texec_status_t texec_task_handle_result(texec_task_handle_t* h, int* out_result) {
return task_handle_get_result(h, out_result, true);
}

mtx_lock(&h->mtx);
while (!h->done) {
cnd_wait(&h->cv, &h->mtx);
}
mtx_unlock(&h->mtx);
texec_status_t texec_task_handle_try_result(texec_task_handle_t* h, int* out_result) {
return task_handle_get_result(h, out_result, false);
}

bool texec_task_handle_is_done(texec_task_handle_t* h) {
if (!h) return false;

mtx_lock(&h->mtx);
const bool done = h->done;
mtx_unlock(&h->mtx);
return done;
}

texec_status_t texec_task_handle_result(texec_task_handle_t* h, int* out_result) {
if (!h || !out_result) return TEXEC_STATUS_INVALID_ARGUMENT;
mtx_lock(&h->mtx);
if (!h->done) return task_handle_unlock_return(h, TEXEC_STATUS_NOT_READY);
*out_result = h->result;
mtx_unlock(&h->mtx);
return TEXEC_STATUS_OK;
}