From 1e487de28840b464888f8bb4823f30c78669b04a Mon Sep 17 00:00:00 2001 From: Marcus Winter Date: Thu, 7 Apr 2016 21:22:30 +0200 Subject: [PATCH] mtapi_c: fixed bug causing mtapi_task_wait to hang when a task was cancelled before it was running --- mtapi_c/src/embb_mtapi_scheduler_t.c | 26 +++++++++++++++++++++++++- mtapi_c/src/embb_mtapi_task_t.c | 7 ++++++- 2 files changed, 31 insertions(+), 2 deletions(-) diff --git a/mtapi_c/src/embb_mtapi_scheduler_t.c b/mtapi_c/src/embb_mtapi_scheduler_t.c index 844321d..d7549c6 100644 --- a/mtapi_c/src/embb_mtapi_scheduler_t.c +++ b/mtapi_c/src/embb_mtapi_scheduler_t.c @@ -40,6 +40,7 @@ #include #include #include +#include /* ---- CLASS MEMBERS ------------------------------------------------------ */ @@ -293,6 +294,8 @@ int embb_mtapi_scheduler_worker(void * arg) { /* check if there was work */ if (MTAPI_NULL != task) { embb_mtapi_queue_t * local_queue = MTAPI_NULL; + embb_mtapi_group_t * local_group = MTAPI_NULL; + embb_mtapi_action_t * local_action = MTAPI_NULL; /* is task associated with a queue? */ if (embb_mtapi_queue_pool_is_handle_valid( @@ -302,6 +305,21 @@ int embb_mtapi_scheduler_worker(void * arg) { node->queue_pool, task->queue); } + /* is task associated with a group? */ + if (embb_mtapi_group_pool_is_handle_valid( + node->group_pool, task->group)) { + local_group = + embb_mtapi_group_pool_get_storage_for_handle( + node->group_pool, task->group); + } + + if (embb_mtapi_action_pool_is_handle_valid( + node->action_pool, task->action)) { + local_action = + embb_mtapi_action_pool_get_storage_for_handle( + node->action_pool, task->action); + } + switch (embb_atomic_load_int(&task->state)) { case MTAPI_TASK_SCHEDULED: /* multi-instance task, another instance might be running */ @@ -328,7 +346,7 @@ int embb_mtapi_scheduler_worker(void * arg) { break; case MTAPI_TASK_CANCELLED: - /* set return value to canceled */ + /* set return value to cancelled */ task->error_code = MTAPI_ERR_ACTION_CANCELLED; if (embb_atomic_fetch_and_add_unsigned_int( &task->instances_todo, (unsigned int)-1) == 0) { @@ -336,6 +354,12 @@ int embb_mtapi_scheduler_worker(void * arg) { if (MTAPI_NULL != local_queue) { embb_mtapi_queue_task_finished(local_queue); } + if (MTAPI_NULL != local_group) { + embb_mtapi_task_queue_push(&local_group->queue, task); + } + } + if (MTAPI_NULL != local_action) { + embb_atomic_fetch_and_add_int(&local_action->num_tasks, -1); } break; diff --git a/mtapi_c/src/embb_mtapi_task_t.c b/mtapi_c/src/embb_mtapi_task_t.c index 4323a13..9abc3ca 100644 --- a/mtapi_c/src/embb_mtapi_task_t.c +++ b/mtapi_c/src/embb_mtapi_task_t.c @@ -501,7 +501,6 @@ void mtapi_task_cancel( if (embb_mtapi_task_pool_is_handle_valid(node->task_pool, task)) { embb_mtapi_task_t* local_task = embb_mtapi_task_pool_get_storage_for_handle(node->task_pool, task); - embb_mtapi_task_set_state(local_task, MTAPI_TASK_CANCELLED); /* call plugin action cancel function */ if (embb_mtapi_action_pool_is_handle_valid( @@ -511,8 +510,14 @@ void mtapi_task_cancel( node->action_pool, local_task->action); if (local_action->is_plugin_action) { local_action->plugin_task_cancel_function(task, &local_status); + } else { + embb_mtapi_task_set_state(local_task, MTAPI_TASK_CANCELLED); + local_task->error_code = MTAPI_ERR_ACTION_CANCELLED; + local_status = MTAPI_SUCCESS; } } else { + embb_mtapi_task_set_state(local_task, MTAPI_TASK_CANCELLED); + local_task->error_code = MTAPI_ERR_ACTION_CANCELLED; local_status = MTAPI_SUCCESS; } } else { -- libgit2 0.26.0