diff options
author | dec05eba <dec05eba@protonmail.com> | 2019-08-22 00:59:49 +0200 |
---|---|---|
committer | dec05eba <dec05eba@protonmail.com> | 2020-07-25 14:36:46 +0200 |
commit | d6f368a3f400fea3e89280262a8147e7ce5d855c (patch) | |
tree | 4eb64eb0d18dad1e40c70a5bff974fe8033fe389 /include/std | |
parent | df640dc7f55fef962b598562e10d8dd4d60fedc0 (diff) |
Move thread work from compiler/parser to thread_work file, fix use after free bug in multithreaded parser allocator
Diffstat (limited to 'include/std')
-rw-r--r-- | include/std/buffer.h | 2 | ||||
-rw-r--r-- | include/std/thread.h | 2 | ||||
-rw-r--r-- | include/std/thread_pool.h | 71 |
3 files changed, 73 insertions, 2 deletions
diff --git a/include/std/buffer.h b/include/std/buffer.h index ac722b1..d194881 100644 --- a/include/std/buffer.h +++ b/include/std/buffer.h @@ -30,7 +30,7 @@ CHECK_RESULT int buffer_append_empty(Buffer *self, usize size); CHECK_RESULT int buffer_expand(Buffer *self, usize size); void* buffer_get(Buffer *self, usize index, usize type_size); CHECK_RESULT int buffer_pop(Buffer *self, void *data, usize size); -/* Set buffer size to 0, doesn't reallocate */ +/* Set buffer size to 0, doesn't change the capacity */ void buffer_clear(Buffer *self); void* buffer_begin(Buffer *self); void* buffer_end(Buffer *self); diff --git a/include/std/thread.h b/include/std/thread.h index 356ebf0..2765204 100644 --- a/include/std/thread.h +++ b/include/std/thread.h @@ -47,7 +47,7 @@ bool amal_thread_is_main(); /* Returns 0 if the number of usable threads is unknown */ int amal_get_usable_thread_count(); -void amal_mutex_init(amal_mutex *self); +CHECK_RESULT int amal_mutex_init(amal_mutex *self); void amal_mutex_deinit(amal_mutex *self); CHECK_RESULT int amal_mutex_lock(amal_mutex *self, const char *lock_identifier); /* Safe to call unlock when another thread owns the lock or if the lock is not locked */ diff --git a/include/std/thread_pool.h b/include/std/thread_pool.h new file mode 100644 index 0000000..f8acf83 --- /dev/null +++ b/include/std/thread_pool.h @@ -0,0 +1,71 @@ +#ifndef AMAL_THREAD_POOL_H +#define AMAL_THREAD_POOL_H + +#include "thread.h" +#include "buffer.h" +#include "buffer_view.h" + +/* + Return 0 if there is no error, otherwise return a non-0 value. + If the result is not 0, then all additional tasks will be stopped. +*/ +typedef int(*amal_thread_job_callback)(void *userdata); + +typedef enum { + THREAD_POOL_THREAD_STATUS_NEW, + THREAD_POOL_THREAD_STATUS_IDLE, + THREAD_POOL_THREAD_STATUS_RUNNING +} amal_thread_pool_thread_status; + +typedef struct { + amal_thread thread; + amal_thread_pool_thread_status status; +} amal_thread_pool_thread; + +typedef struct { + amal_thread_job_callback callback; + void *userdata; +} amal_thread_pool_task; + +typedef struct { + int num_threads; + amal_thread_pool_thread *threads; /* Size of @threads is @num_threads */ + amal_mutex task_select_mutex; + Buffer queued_tasks; + bool dead; + int num_finished_queued_tasks; +} amal_thread_pool; + +typedef struct { + amal_thread_pool *thread_pool; + amal_thread_pool_thread *thread_pool_thread; + amal_thread_job_callback callback; + void *userdata; +} amal_thread_pool_callback_data; + +/* + If @num_threads is 0, then the number of threads selected will the number + of physical threads, or 1 +*/ +CHECK_RESULT int thread_pool_init(amal_thread_pool *self, int num_threads); +void thread_pool_deinit(amal_thread_pool *self); +/* + Thread-safe. Will fail if one task has failed, in which case it can only be used + once @thread_pool_join_all_tasks has been called +*/ +CHECK_RESULT int thread_pool_add_task(amal_thread_pool *self, amal_thread_job_callback callback, void *userdata); +/* + Wait until all tasks have finished. Should only be called from one thread. + Returns true if all tasks finished without any issues (if @thread_pool_mark_dead was not called) +*/ +CHECK_RESULT bool thread_pool_join_all_tasks(amal_thread_pool *self); +/* + Stop tasks as soon as possible and stop dispatch of new tasks. + This can be used when work on one thread has failed and you want to + stop tasks on all threads to report errors and stop additional tasks. +*/ +void thread_pool_mark_dead(amal_thread_pool *self); + +BufferView/*<amal_thread_pool_thread>*/ thread_pool_get_threads(amal_thread_pool *self); + +#endif |