From 84e63af3d2660aaea678b1eccfc9c18455e43c1b Mon Sep 17 00:00:00 2001 From: Khairul Azhar Kasmiran Date: Wed, 22 May 2024 12:03:35 +0800 Subject: [PATCH] rz_core_task_join: Return fine-grained error values (#4505) --- librz/core/task.c | 12 ++++++------ librz/include/rz_core.h | 10 +++++++++- test/unit/test_core_task.c | 4 ++-- 3 files changed, 17 insertions(+), 9 deletions(-) diff --git a/librz/core/task.c b/librz/core/task.c index f42adb2546c..b885b40dea1 100644 --- a/librz/core/task.c +++ b/librz/core/task.c @@ -94,21 +94,21 @@ static bool task_join(RzCoreTask *task) { return true; } -RZ_API bool rz_core_task_join(RzCoreTaskScheduler *scheduler, RzCoreTask *current, int id) { - bool ret = true; +RZ_API RzCoreTaskJoinErr rz_core_task_join(RzCoreTaskScheduler *scheduler, RzCoreTask *current, int id) { + RzCoreTaskJoinErr ret = RZ_CORE_TASK_JOIN_ERR_SUCCESS; if (current && id == current->id) { - return false; + return RZ_CORE_TASK_JOIN_ERR_CURRENT; } if (id >= 0) { RzCoreTask *task = rz_core_task_get_incref(scheduler, id); if (!task) { - return false; + return RZ_CORE_TASK_JOIN_ERR_NO_TASK; } if (current) { rz_core_task_sleep_begin(current); } if (!task_join(task)) { - ret = false; + ret = RZ_CORE_TASK_JOIN_ERR_ONE_NO_SEM; } if (current) { rz_core_task_sleep_end(current); @@ -136,7 +136,7 @@ RZ_API bool rz_core_task_join(RzCoreTaskScheduler *scheduler, RzCoreTask *curren rz_core_task_sleep_begin(current); } if (!task_join(task)) { - ret = false; + ret = RZ_CORE_TASK_JOIN_ERR_ALL_NO_SEM; } if (current) { rz_core_task_sleep_end(current); diff --git a/librz/include/rz_core.h b/librz/include/rz_core.h index 3a331718d3e..56a9754c4a7 100644 --- a/librz/include/rz_core.h +++ b/librz/include/rz_core.h @@ -1138,6 +1138,14 @@ typedef enum { RZ_CORE_TASK_STATE_DONE } RzTaskState; +typedef enum { + RZ_CORE_TASK_JOIN_ERR_SUCCESS = 0, ///< Join success! + RZ_CORE_TASK_JOIN_ERR_CURRENT, ///< Join on current task + RZ_CORE_TASK_JOIN_ERR_NO_TASK, ///< No task has given id + RZ_CORE_TASK_JOIN_ERR_ONE_NO_SEM, ///< No join semaphore when joining on one task + RZ_CORE_TASK_JOIN_ERR_ALL_NO_SEM ///< No join semaphore when joining on all tasks +} RzCoreTaskJoinErr; + /** * Main payload of a task, the function that should be executed asynchronously. */ @@ -1191,7 +1199,7 @@ RZ_API void rz_core_task_break(RzCoreTaskScheduler *scheduler, int id); RZ_API void rz_core_task_break_all(RzCoreTaskScheduler *scheduler); RZ_API int rz_core_task_del(RzCoreTaskScheduler *scheduler, int id); RZ_API RzCoreTask *rz_core_task_self(RzCoreTaskScheduler *scheduler); -RZ_API bool rz_core_task_join(RzCoreTaskScheduler *scheduler, RzCoreTask *current, int id); +RZ_API RzCoreTaskJoinErr rz_core_task_join(RzCoreTaskScheduler *scheduler, RzCoreTask *current, int id); typedef void (*inRangeCb)(RzCore *core, ut64 from, ut64 to, int vsize, void *cb_user); RZ_API int rz_core_search_value_in_range(RzCore *core, RzInterval search_itv, ut64 vmin, ut64 vmax, int vsize, inRangeCb cb, void *cb_user); diff --git a/test/unit/test_core_task.c b/test/unit/test_core_task.c index fbc6255ffd0..37b8a45d2ec 100644 --- a/test/unit/test_core_task.c +++ b/test/unit/test_core_task.c @@ -69,8 +69,8 @@ static bool test_core_task_finished_cb(void) { RzCoreTask *a = rz_core_cmd_task_new(core, "echo amor; echo vincit; echo omnia", finished_cb, &res_indir); rz_core_task_enqueue(&core->tasks, a); - bool task_joined = rz_core_task_join(&core->tasks, rz_core_task_self(&core->tasks), a->id); - mu_assert_true(task_joined, "task joined"); + RzCoreTaskJoinErr join_err = rz_core_task_join(&core->tasks, rz_core_task_self(&core->tasks), a->id); + mu_assert_eq(join_err, RZ_CORE_TASK_JOIN_ERR_SUCCESS, "task joined"); const char *cmd_result = rz_core_cmd_task_get_result(a); mu_assert_streq(cmd_result, "amor\nvincit\nomnia\n", "cmd result");