From a0cfbd821c43e3ed78a5f6ed2bcfe601674eed75 Mon Sep 17 00:00:00 2001 From: akrivx Date: Thu, 29 Jan 2026 14:54:01 +0000 Subject: [PATCH 1/2] Refactor task handle interface - Make result blocking - Introduce non-blocking try_result - Implement wait in terms of result --- examples/texec_example.c | 2 -- include/texec/task_handle.h | 8 ++++++-- src/task_handle.c | 26 +++++++++++++------------- 3 files changed, 19 insertions(+), 17 deletions(-) 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..db22d8e 100644 --- a/src/task_handle.c +++ b/src/task_handle.c @@ -108,26 +108,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) { + if (!h || !out_result) return TEXEC_STATUS_INVALID_ARGUMENT; mtx_lock(&h->mtx); while (!h->done) { cnd_wait(&h->cv, &h->mtx); } + *out_result = h->result; mtx_unlock(&h->mtx); + return TEXEC_STATUS_OK; } -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) { +texec_status_t texec_task_handle_try_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); @@ -135,3 +127,11 @@ texec_status_t texec_task_handle_result(texec_task_handle_t* h, int* out_result) mtx_unlock(&h->mtx); return TEXEC_STATUS_OK; } + +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; +} From e7d3ea2f0e4d6d0c12491914192f9c7c5896b772 Mon Sep 17 00:00:00 2001 From: akrivx Date: Thu, 29 Jan 2026 15:00:33 +0000 Subject: [PATCH 2/2] Introduce get_result helper --- src/task_handle.c | 29 +++++++++++++++-------------- 1 file changed, 15 insertions(+), 14 deletions(-) diff --git a/src/task_handle.c b/src/task_handle.c index db22d8e..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)) { @@ -109,23 +122,11 @@ void texec_task_handle_release(texec_task_handle_t* h) { } 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); - while (!h->done) { - cnd_wait(&h->cv, &h->mtx); - } - *out_result = h->result; - mtx_unlock(&h->mtx); - return TEXEC_STATUS_OK; + return task_handle_get_result(h, out_result, true); } texec_status_t texec_task_handle_try_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; + return task_handle_get_result(h, out_result, false); } bool texec_task_handle_is_done(texec_task_handle_t* h) {