FS-11801 [core] Update scheduler to allow destruction of running task

This commit is contained in:
Chris Rienzo 2019-04-22 17:10:46 -04:00 committed by Andrey Volk
parent 8aa07b677d
commit 6f2466eb35

View File

@ -38,6 +38,7 @@ struct switch_scheduler_task_container {
int in_thread; int in_thread;
int destroyed; int destroyed;
int running; int running;
int destroy_requested;
switch_scheduler_func_t func; switch_scheduler_func_t func;
switch_memory_pool_t *pool; switch_memory_pool_t *pool;
uint32_t flags; uint32_t flags;
@ -62,11 +63,12 @@ static void switch_scheduler_execute(switch_scheduler_task_container_t *tp)
tp->func(&tp->task); tp->func(&tp->task);
switch_mutex_lock(globals.task_mutex);
if (tp->task.repeat) { if (tp->task.repeat) {
tp->task.runtime = switch_epoch_time_now(NULL) + tp->task.repeat; tp->task.runtime = switch_epoch_time_now(NULL) + tp->task.repeat;
} }
if (tp->task.runtime > tp->executed) { if (!tp->destroy_requested && tp->task.runtime > tp->executed) {
tp->executed = 0; tp->executed = 0;
if (switch_event_create(&event, SWITCH_EVENT_RE_SCHEDULE) == SWITCH_STATUS_SUCCESS) { if (switch_event_create(&event, SWITCH_EVENT_RE_SCHEDULE) == SWITCH_STATUS_SUCCESS) {
switch_event_add_header(event, SWITCH_STACK_BOTTOM, "Task-ID", "%u", tp->task.task_id); switch_event_add_header(event, SWITCH_STACK_BOTTOM, "Task-ID", "%u", tp->task.task_id);
@ -79,6 +81,7 @@ static void switch_scheduler_execute(switch_scheduler_task_container_t *tp)
} else { } else {
tp->destroyed = 1; tp->destroyed = 1;
} }
switch_mutex_unlock(globals.task_mutex);
} }
static void *SWITCH_THREAD_FUNC task_own_thread(switch_thread_t *thread, void *obj) static void *SWITCH_THREAD_FUNC task_own_thread(switch_thread_t *thread, void *obj)
@ -275,12 +278,13 @@ SWITCH_DECLARE(uint32_t) switch_scheduler_del_task_id(uint32_t task_id)
} }
if (tp->running) { if (tp->running) {
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_WARNING, "Attempt made to delete running task #%u (group %s)\n", switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_INFO, "Attempt made to delete running task #%u (group %s)\n",
tp->task.task_id, tp->task.group); tp->task.task_id, tp->task.group);
break; tp->destroy_requested++;
} else {
tp->destroyed++;
} }
tp->destroyed++;
delcnt++; delcnt++;
break; break;
} }
@ -314,7 +318,13 @@ SWITCH_DECLARE(uint32_t) switch_scheduler_del_task_group(const char *group)
tp->task.task_id, group); tp->task.task_id, group);
continue; continue;
} }
tp->destroyed++; if (tp->running) {
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_INFO, "Attempt made to delete running task #%u (group %s)\n",
tp->task.task_id, tp->task.group);
tp->destroy_requested++;
} else {
tp->destroyed++;
}
delcnt++; delcnt++;
} }
} }