diff --git a/examples/texec_example.c b/examples/texec_example.c index 8fbd8c8..fc902c5 100644 --- a/examples/texec_example.c +++ b/examples/texec_example.c @@ -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) { diff --git a/include/texec/task_handle.h b/include/texec/task_handle.h index e98828b..455bf54 100644 --- a/include/texec/task_handle.h +++ b/include/texec/task_handle.h @@ -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 } diff --git a/src/task_handle.c b/src/task_handle.c index 6efb51b..d0c1caf 100644 --- a/src/task_handle.c +++ b/src/task_handle.c @@ -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)) { @@ -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; -}