2019-10-21 02:02:58 +00:00
|
|
|
/*
|
|
|
|
* 4coder_async_tasks.cpp - Implementation of the custom layer asynchronous task system.
|
|
|
|
*/
|
|
|
|
|
|
|
|
// TOP
|
|
|
|
|
2019-10-22 05:17:24 +00:00
|
|
|
// TODO(allen): put atomic wrappers in base type layer
|
|
|
|
#define atomic_write_b32(p,v) (*(p)=(v))
|
|
|
|
#define atomic_read_b32(p) (*(p))
|
|
|
|
|
|
|
|
global Async_System global_async_system = {};
|
2019-10-22 04:10:29 +00:00
|
|
|
|
|
|
|
function Async_Node*
|
2019-10-22 05:17:24 +00:00
|
|
|
async_pop_node(Async_System *async_system){
|
|
|
|
for (;async_system->task_count == 0;){
|
|
|
|
system_condition_variable_wait(async_system->cv, async_system->mutex);
|
2019-10-22 04:10:29 +00:00
|
|
|
}
|
2019-10-22 05:17:24 +00:00
|
|
|
Node *node = async_system->task_sent.next;
|
|
|
|
Assert(node != &async_system->task_sent);
|
|
|
|
dll_remove(node);
|
|
|
|
async_system->task_count -= 1;
|
|
|
|
Async_Node *a_node = CastFromMember(Async_Node, node, node);
|
|
|
|
a_node->next = 0;
|
|
|
|
return(a_node);
|
2019-10-22 04:10:29 +00:00
|
|
|
}
|
|
|
|
|
2019-10-29 01:28:44 +00:00
|
|
|
function Async_Node*
|
|
|
|
async_push_node__inner(Async_System *async_system, Async_Task_Function_Type *func,
|
|
|
|
Data data){
|
2019-10-22 05:17:24 +00:00
|
|
|
Async_Task result = async_system->task_id_counter;
|
|
|
|
async_system->task_id_counter += 1;
|
2019-10-22 04:10:29 +00:00
|
|
|
|
2019-10-22 05:17:24 +00:00
|
|
|
Async_Node *node = async_system->free_nodes;
|
2019-10-22 04:10:29 +00:00
|
|
|
if (node == 0){
|
2019-10-22 05:17:24 +00:00
|
|
|
node = push_array(&async_system->node_arena, Async_Node, 1);
|
2019-10-22 04:10:29 +00:00
|
|
|
}
|
|
|
|
else{
|
2019-10-22 05:17:24 +00:00
|
|
|
sll_stack_pop(async_system->free_nodes);
|
2019-10-22 04:10:29 +00:00
|
|
|
}
|
|
|
|
node->task = result;
|
|
|
|
node->thread = 0;
|
|
|
|
node->func = func;
|
2019-10-22 05:17:24 +00:00
|
|
|
node->data.data = (u8*)heap_allocate(&async_system->node_heap, data.size);
|
2019-10-22 04:10:29 +00:00
|
|
|
block_copy(node->data.data, data.data, data.size);
|
|
|
|
node->data.size = data.size;
|
2019-10-22 05:17:24 +00:00
|
|
|
dll_insert_back(&async_system->task_sent, &node->node);
|
|
|
|
async_system->task_count += 1;
|
|
|
|
system_condition_variable_signal(async_system->cv);
|
2019-10-22 04:10:29 +00:00
|
|
|
|
2019-10-29 01:28:44 +00:00
|
|
|
return(node);
|
|
|
|
}
|
|
|
|
|
|
|
|
function Async_Task
|
|
|
|
async_push_node(Async_System *async_system, Async_Task_Function_Type *func, Data data){
|
|
|
|
Async_Node *node = async_push_node__inner(async_system, func, data);
|
|
|
|
return(node->task);
|
2019-10-22 04:10:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function void
|
2019-10-22 05:17:24 +00:00
|
|
|
async_free_node(Async_System *async_system, Async_Node *node){
|
|
|
|
heap_free(&async_system->node_heap, node->data.data);
|
|
|
|
sll_stack_push(async_system->free_nodes, node);
|
2019-10-22 04:10:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function void
|
|
|
|
async_task_thread(void *thread_ptr){
|
|
|
|
Base_Allocator *allocator = get_base_allocator_system();
|
|
|
|
|
|
|
|
Thread_Context_Extra_Info tctx_info = {};
|
|
|
|
tctx_info.async_thread = thread_ptr;
|
|
|
|
|
|
|
|
Thread_Context tctx_ = {};
|
|
|
|
Thread_Context *tctx = &tctx_;
|
|
|
|
thread_ctx_init(tctx, ThreadKind_AsyncTasks, allocator, allocator);
|
|
|
|
|
|
|
|
Async_Thread *thread = (Async_Thread*)thread_ptr;
|
2019-10-22 05:17:24 +00:00
|
|
|
Async_System *async_system = thread->async_system;
|
2019-10-22 04:10:29 +00:00
|
|
|
|
|
|
|
Application_Links app = {};
|
|
|
|
app.tctx = tctx;
|
2019-10-22 05:17:24 +00:00
|
|
|
app.cmd_context = async_system->cmd_context;
|
2019-10-26 20:48:50 +00:00
|
|
|
|
|
|
|
Profile_Global_List *list = get_core_profile_list(&app);
|
|
|
|
ProfileThreadName(tctx, list, string_u8_litexpr("async"));
|
|
|
|
|
2019-10-22 04:10:29 +00:00
|
|
|
Async_Context ctx = {&app, thread};
|
|
|
|
|
|
|
|
for (;;){
|
2019-10-22 05:17:24 +00:00
|
|
|
system_mutex_acquire(async_system->mutex);
|
|
|
|
Async_Node *node = async_pop_node(async_system);
|
2019-10-22 04:10:29 +00:00
|
|
|
node->thread = thread;
|
|
|
|
thread->node = node;
|
|
|
|
thread->task = node->task;
|
2019-10-22 05:17:24 +00:00
|
|
|
thread->cancel_signal = false;
|
|
|
|
thread->join_signal = false;
|
|
|
|
system_mutex_release(async_system->mutex);
|
|
|
|
|
2019-10-22 04:10:29 +00:00
|
|
|
node->func(&ctx, node->data);
|
2019-10-22 05:17:24 +00:00
|
|
|
|
|
|
|
system_mutex_acquire(async_system->mutex);
|
|
|
|
if (thread->join_signal){
|
|
|
|
system_condition_variable_signal(async_system->join_cv);
|
|
|
|
}
|
|
|
|
node->thread = 0;
|
2019-10-22 04:10:29 +00:00
|
|
|
thread->node = 0;
|
|
|
|
thread->task = 0;
|
2019-10-22 05:17:24 +00:00
|
|
|
thread->cancel_signal = false;
|
|
|
|
thread->join_signal = false;
|
2019-10-31 21:42:11 +00:00
|
|
|
async_free_node(async_system, node);
|
2019-10-22 05:17:24 +00:00
|
|
|
system_mutex_release(async_system->mutex);
|
2019-10-22 04:10:29 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function Async_Node*
|
2019-10-22 05:17:24 +00:00
|
|
|
async_get_pending_node(Async_System *async_system, Async_Task task){
|
2019-10-22 04:10:29 +00:00
|
|
|
Async_Node *result = 0;
|
2019-10-22 05:17:24 +00:00
|
|
|
if (task != 0){
|
|
|
|
for (Node *node = async_system->task_sent.next;
|
|
|
|
node != &async_system->task_sent;
|
|
|
|
node = node->next){
|
|
|
|
Async_Node *a_node = CastFromMember(Async_Node, node, node);
|
|
|
|
if (a_node->task == task){
|
|
|
|
result = a_node;
|
|
|
|
break;
|
|
|
|
}
|
2019-10-22 04:10:29 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return(result);
|
|
|
|
}
|
|
|
|
|
|
|
|
function Async_Node*
|
2019-10-22 05:17:24 +00:00
|
|
|
async_get_running_node(Async_System *async_system, Async_Task task){
|
2019-10-22 04:10:29 +00:00
|
|
|
Async_Node *result = 0;
|
2019-10-22 05:17:24 +00:00
|
|
|
if (task != 0 && async_system->thread.task == task){
|
|
|
|
result = async_system->thread.node;
|
2019-10-22 04:10:29 +00:00
|
|
|
}
|
|
|
|
return(result);
|
|
|
|
}
|
|
|
|
|
|
|
|
////////////////////////////////
|
2019-10-21 02:02:58 +00:00
|
|
|
|
|
|
|
function void
|
2019-10-22 05:17:24 +00:00
|
|
|
async_task_handler_init(Application_Links *app, Async_System *async_system){
|
|
|
|
block_zero_struct(async_system);
|
|
|
|
async_system->cmd_context = app->cmd_context;
|
|
|
|
async_system->node_arena = make_arena_system(KB(4));
|
|
|
|
heap_init(&async_system->node_heap, &async_system->node_arena);
|
|
|
|
async_system->mutex = system_mutex_make();
|
|
|
|
async_system->cv = system_condition_variable_make();
|
|
|
|
async_system->join_cv = system_condition_variable_make();
|
|
|
|
dll_init_sentinel(&async_system->task_sent);
|
2019-10-22 05:53:47 +00:00
|
|
|
async_system->thread.async_system = async_system;
|
2019-10-22 05:17:24 +00:00
|
|
|
async_system->thread.thread = system_thread_launch(async_task_thread, &async_system->thread);
|
2019-10-21 02:02:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function Async_Task
|
2019-10-29 01:28:44 +00:00
|
|
|
async_task_no_dep(Async_System *async_system, Async_Task_Function_Type *func,
|
|
|
|
Data data){
|
2019-10-22 05:17:24 +00:00
|
|
|
system_mutex_acquire(async_system->mutex);
|
|
|
|
Async_Task result = async_push_node(async_system, func, data);
|
|
|
|
system_mutex_release(async_system->mutex);
|
|
|
|
return(result);
|
2019-10-21 02:02:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function b32
|
2019-10-22 05:17:24 +00:00
|
|
|
async_task_is_pending(Async_System *async_system, Async_Task task){
|
|
|
|
system_mutex_acquire(async_system->mutex);
|
|
|
|
Async_Node *node = async_get_pending_node(async_system, task);
|
|
|
|
system_mutex_release(async_system->mutex);
|
2019-10-22 04:10:29 +00:00
|
|
|
return(node != 0);
|
2019-10-21 02:02:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function b32
|
2019-10-22 05:17:24 +00:00
|
|
|
async_task_is_running(Async_System *async_system, Async_Task task){
|
|
|
|
system_mutex_acquire(async_system->mutex);
|
|
|
|
Async_Node *node = async_get_running_node(async_system, task);
|
|
|
|
system_mutex_release(async_system->mutex);
|
2019-10-22 04:10:29 +00:00
|
|
|
return(node != 0);
|
2019-10-21 02:02:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function b32
|
2019-10-22 05:17:24 +00:00
|
|
|
async_task_is_running_or_pending(Async_System *async_system, Async_Task task){
|
|
|
|
system_mutex_acquire(async_system->mutex);
|
|
|
|
Async_Node *node = async_get_pending_node(async_system, task);
|
2019-10-22 04:10:29 +00:00
|
|
|
if (node != 0){
|
2019-10-22 05:17:24 +00:00
|
|
|
node = async_get_running_node(async_system, task);
|
2019-10-22 04:10:29 +00:00
|
|
|
}
|
2019-10-22 05:17:24 +00:00
|
|
|
system_mutex_release(async_system->mutex);
|
2019-10-22 04:10:29 +00:00
|
|
|
return(node != 0);
|
2019-10-21 02:02:58 +00:00
|
|
|
}
|
|
|
|
|
2019-10-31 21:42:11 +00:00
|
|
|
// TODO(allen): ensure that the job is canceled before this returns.
|
2019-10-21 02:02:58 +00:00
|
|
|
function void
|
2019-10-22 05:17:24 +00:00
|
|
|
async_task_cancel(Async_System *async_system, Async_Task task){
|
|
|
|
system_mutex_acquire(async_system->mutex);
|
|
|
|
Async_Node *node = async_get_pending_node(async_system, task);
|
|
|
|
if (node != 0){
|
|
|
|
dll_remove(&node->node);
|
|
|
|
async_system->task_count -= 1;
|
|
|
|
async_free_node(async_system, node);
|
|
|
|
}
|
|
|
|
else{
|
|
|
|
node = async_get_running_node(async_system, task);
|
|
|
|
if (node != 0){
|
|
|
|
b32 *cancel_signal = &node->thread->cancel_signal;
|
|
|
|
atomic_write_b32(cancel_signal, true);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
system_mutex_release(async_system->mutex);
|
2019-10-21 02:02:58 +00:00
|
|
|
}
|
|
|
|
|
2019-10-22 05:17:24 +00:00
|
|
|
function b32
|
|
|
|
async_check_canceled(Async_Context *actx){
|
|
|
|
b32 *cancel_signal = &actx->thread->cancel_signal;
|
|
|
|
b32 result = atomic_read_b32(cancel_signal);
|
|
|
|
return(result);
|
2019-10-21 02:02:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// BOTTOM
|
|
|
|
|