diff options
-rw-r--r-- | http_parser.c | 135 | ||||
-rw-r--r-- | http_parser.h | 39 | ||||
-rw-r--r-- | io_muxer.c | 70 | ||||
-rw-r--r-- | io_muxer.h | 30 | ||||
-rw-r--r-- | thread_pool.c | 46 | ||||
-rw-r--r-- | thread_pool.h | 17 | ||||
-rw-r--r-- | utils.h | 6 |
7 files changed, 181 insertions, 162 deletions
diff --git a/http_parser.c b/http_parser.c index b668a92..99344cb 100644 --- a/http_parser.c +++ b/http_parser.c @@ -17,6 +17,7 @@ #include "http_parser.h" +#include <stdbool.h> #include <stddef.h> #include <stdint.h> @@ -41,8 +42,8 @@ */ // mburakov: RFC9110 5.6.2 Tokens -static inline _Bool IsTchar(char item) { - static const _Bool kAllowed[] = { +static inline bool IsTchar(char item) { + static const bool kAllowed[] = { _(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0), _(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0), _(0, 1, 0, 1, 1, 1, 1, 1, 0, 0, 1, 1, 0, 1, 1, 0), @@ -64,8 +65,8 @@ static inline _Bool IsTchar(char item) { } // mburakov: RFC3986 3.3 Path -static inline _Bool IsPchar(char item) { - static const _Bool kAllowed[] = { +static inline bool IsPchar(char item) { + static const bool kAllowed[] = { _(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0), _(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0), _(0, 1, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1), @@ -87,41 +88,41 @@ static inline _Bool IsPchar(char item) { } // mburakov: RFC9110 5.6.3 Whitespace -static inline _Bool IsOws(char item) { - _Bool result = item == ' ' || item == '\t'; +static inline bool IsOws(char item) { + bool result = item == ' ' || item == '\t'; return result; } // mburakov: RFC9110 5.5 Field Values -static inline _Bool IsVchar(char item) { +static inline bool IsVchar(char item) { uint8_t octet = (uint8_t)item; return octet > ' ' && octet < 0xff; } -static enum HttpParser_Result ParseMethod( - struct HttpParser_State* state, const void* buffer, size_t buffer_size, - const struct HttpParser_Callbacks* callbacks, void* user); -static enum HttpParser_Result ParseTarget( - struct HttpParser_State* state, const void* buffer, size_t buffer_size, - const struct HttpParser_Callbacks* callbacks, void* user); -static enum HttpParser_Result ParseVersion( - struct HttpParser_State* state, const void* buffer, size_t buffer_size, - const struct HttpParser_Callbacks* callbacks, void* user); -static enum HttpParser_Result ParseFieldName( - struct HttpParser_State* state, const void* buffer, size_t buffer_size, - const struct HttpParser_Callbacks* callbacks, void* user); -static enum HttpParser_Result ParseFieldValue( - struct HttpParser_State* state, const void* buffer, size_t buffer_size, - const struct HttpParser_Callbacks* callbacks, void* user); +static enum HttpParserResult ParseMethod( + struct HttpParserState* state, const void* buffer, size_t buffer_size, + const struct HttpParserCallbacks* callbacks, void* user); +static enum HttpParserResult ParseTarget( + struct HttpParserState* state, const void* buffer, size_t buffer_size, + const struct HttpParserCallbacks* callbacks, void* user); +static enum HttpParserResult ParseVersion( + struct HttpParserState* state, const void* buffer, size_t buffer_size, + const struct HttpParserCallbacks* callbacks, void* user); +static enum HttpParserResult ParseFieldName( + struct HttpParserState* state, const void* buffer, size_t buffer_size, + const struct HttpParserCallbacks* callbacks, void* user); +static enum HttpParserResult ParseFieldValue( + struct HttpParserState* state, const void* buffer, size_t buffer_size, + const struct HttpParserCallbacks* callbacks, void* user); -static enum HttpParser_Result ParseMethod( - struct HttpParser_State* state, const void* buffer, size_t buffer_size, - const struct HttpParser_Callbacks* callbacks, void* user) { +static enum HttpParserResult ParseMethod( + struct HttpParserState* state, const void* buffer, size_t buffer_size, + const struct HttpParserCallbacks* callbacks, void* user) { (void)callbacks; (void)user; for (const char* data = buffer;;) { if (state->parsing_offset == buffer_size) { - return kHttpParser_ResultWantMore; + return kHttpParserResultWantMore; } if (IsTchar(data[state->parsing_offset])) { state->parsing_offset++; @@ -132,21 +133,21 @@ static enum HttpParser_Result ParseMethod( state->parsing_offset++; state->first_size = state->parsing_offset - 1 - state->first_offset; state->second_offset = state->parsing_offset; - return kHttpParser_ResultFinished; + return kHttpParserResultFinished; } state->stage = 0; - return kHttpParser_ResultError; + return kHttpParserResultError; } } -static enum HttpParser_Result ParseTarget( - struct HttpParser_State* state, const void* buffer, size_t buffer_size, - const struct HttpParser_Callbacks* callbacks, void* user) { +static enum HttpParserResult ParseTarget( + struct HttpParserState* state, const void* buffer, size_t buffer_size, + const struct HttpParserCallbacks* callbacks, void* user) { (void)callbacks; (void)user; for (const char* data = buffer;;) { if (state->parsing_offset == buffer_size) { - return kHttpParser_ResultWantMore; + return kHttpParserResultWantMore; } if (IsPchar(data[state->parsing_offset])) { state->parsing_offset++; @@ -157,16 +158,16 @@ static enum HttpParser_Result ParseTarget( state->parsing_offset++; state->second_size = state->parsing_offset - 1 - state->second_offset; state->first_offset = state->parsing_offset; - return kHttpParser_ResultFinished; + return kHttpParserResultFinished; } state->stage = 0; - return kHttpParser_ResultError; + return kHttpParserResultError; } } -static enum HttpParser_Result ParseVersion( - struct HttpParser_State* state, const void* buffer, size_t buffer_size, - const struct HttpParser_Callbacks* callbacks, void* user) { +static enum HttpParserResult ParseVersion( + struct HttpParserState* state, const void* buffer, size_t buffer_size, + const struct HttpParserCallbacks* callbacks, void* user) { static const char kReferences[][10] = { {'H', 'T', 'T', 'P', '/', '1', '.', '1', '\r', '\n'}, {'H', 'T', 'T', 'P', '/', '1', '.', '0', '\r', '\n'}}; @@ -180,10 +181,10 @@ static enum HttpParser_Result ParseVersion( state->stage = ParseFieldName; state->first_offset = state->parsing_offset; state->second_size = 0; - return kHttpParser_ResultFinished; + return kHttpParserResultFinished; } if (state->parsing_offset == buffer_size) { - return kHttpParser_ResultWantMore; + return kHttpParserResultWantMore; } if (data[state->parsing_offset] == kReferences[0][reference_index] || data[state->parsing_offset] == kReferences[1][reference_index]) { @@ -191,20 +192,20 @@ static enum HttpParser_Result ParseVersion( continue; } state->stage = 0; - return kHttpParser_ResultError; + return kHttpParserResultError; } } -static enum HttpParser_Result ParseFieldName( - struct HttpParser_State* state, const void* buffer, size_t buffer_size, - const struct HttpParser_Callbacks* callbacks, void* user) { +static enum HttpParserResult ParseFieldName( + struct HttpParserState* state, const void* buffer, size_t buffer_size, + const struct HttpParserCallbacks* callbacks, void* user) { for (const char* data = buffer;;) { if (state->parsing_offset == buffer_size) { - return kHttpParser_ResultWantMore; + return kHttpParserResultWantMore; } if (data[state->parsing_offset] == '\r' && state->parsing_offset == state->first_offset) { - state->maybe_eol = 1; + state->maybe_eol = true; state->parsing_offset++; continue; } @@ -214,11 +215,11 @@ static enum HttpParser_Result ParseFieldName( if (callbacks && callbacks->on_finished) { callbacks->on_finished(user, state->parsing_offset + 1); } - return kHttpParser_ResultFinished; + return kHttpParserResultFinished; } if (state->maybe_eol) { state->stage = 0; - return kHttpParser_ResultError; + return kHttpParserResultError; } if (IsTchar(data[state->parsing_offset])) { state->parsing_offset++; @@ -228,19 +229,19 @@ static enum HttpParser_Result ParseFieldName( state->stage = ParseFieldValue; state->parsing_offset++; state->first_size = state->parsing_offset - 1 - state->first_offset; - if (state->first_size) return kHttpParser_ResultFinished; + if (state->first_size) return kHttpParserResultFinished; } state->stage = 0; - return kHttpParser_ResultError; + return kHttpParserResultError; } } -static enum HttpParser_Result ParseFieldValue( - struct HttpParser_State* state, const void* buffer, size_t buffer_size, - const struct HttpParser_Callbacks* callbacks, void* user) { +static enum HttpParserResult ParseFieldValue( + struct HttpParserState* state, const void* buffer, size_t buffer_size, + const struct HttpParserCallbacks* callbacks, void* user) { for (const char* data = buffer;;) { if (state->parsing_offset == buffer_size) { - return kHttpParser_ResultWantMore; + return kHttpParserResultWantMore; } if (!state->second_size) { if (IsOws(data[state->parsing_offset])) { @@ -253,31 +254,31 @@ static enum HttpParser_Result ParseFieldValue( if (data[state->parsing_offset] == '\r') { if (!state->second_size) { state->stage = 0; - return kHttpParser_ResultError; + return kHttpParserResultError; } - state->maybe_eol = 1; + state->maybe_eol = true; state->parsing_offset++; continue; } if (data[state->parsing_offset] == '\n') { if (!state->maybe_eol) { state->stage = 0; - return kHttpParser_ResultError; + return kHttpParserResultError; } if (callbacks && callbacks->on_field) { callbacks->on_field(user, data + state->first_offset, state->first_size, data + state->second_offset, state->second_size); } state->stage = ParseFieldName; - state->maybe_eol = 0; + state->maybe_eol = false; state->parsing_offset++; state->first_offset = state->parsing_offset; state->second_size = 0; - return kHttpParser_ResultFinished; + return kHttpParserResultFinished; } if (state->maybe_eol) { state->stage = 0; - return kHttpParser_ResultError; + return kHttpParserResultError; } if (IsVchar(data[state->parsing_offset])) { state->parsing_offset++; @@ -289,20 +290,20 @@ static enum HttpParser_Result ParseFieldValue( continue; } state->stage = 0; - return kHttpParser_ResultError; + return kHttpParserResultError; } } -void HttpParser_Reset(struct HttpParser_State* state) { - struct HttpParser_State reset = {.stage = ParseMethod}; +void HttpParserReset(struct HttpParserState* state) { + struct HttpParserState reset = {.stage = ParseMethod}; *state = reset; } -enum HttpParser_Result HttpParser_Parse( - struct HttpParser_State* state, const void* buffer, size_t buffer_size, - const struct HttpParser_Callbacks* callbacks, void* user) { - enum HttpParser_Result result = kHttpParser_ResultFinished; - while (result == kHttpParser_ResultFinished && state->stage) +enum HttpParserResult HttpParserParse( + struct HttpParserState* state, const void* buffer, size_t buffer_size, + const struct HttpParserCallbacks* callbacks, void* user) { + enum HttpParserResult result = kHttpParserResultFinished; + while (result == kHttpParserResultFinished && state->stage) result = (state->stage)(state, buffer, buffer_size, callbacks, user); return result; } diff --git a/http_parser.h b/http_parser.h index 9dffe6b..3c197a1 100644 --- a/http_parser.h +++ b/http_parser.h @@ -15,22 +15,23 @@ * along with toolbox. If not, see <https://www.gnu.org/licenses/>. */ -#ifndef HTTP_PARSER_H_ -#define HTTP_PARSER_H_ +#ifndef TOOLBOX_HTTP_PARSER_H_ +#define TOOLBOX_HTTP_PARSER_H_ +#include <stdbool.h> #include <stddef.h> #ifdef __cplusplus extern "C" { #endif // __cplusplus -enum HttpParser_Result { - kHttpParser_ResultFinished = 0, - kHttpParser_ResultWantMore, - kHttpParser_ResultError, +enum HttpParserResult { + kHttpParserResultFinished = 0, + kHttpParserResultWantMore, + kHttpParserResultError, }; -struct HttpParser_Callbacks { +struct HttpParserCallbacks { void (*on_request)(void* user, const char* method, size_t method_size, const char* target, size_t target_size); void (*on_field)(void* user, const char* name, size_t name_size, @@ -38,15 +39,15 @@ struct HttpParser_Callbacks { void (*on_finished)(void* user, size_t offset); }; -struct HttpParser_State; +struct HttpParserState; -typedef enum HttpParser_Result (*HttpParser_Handler)( - struct HttpParser_State*, const void*, size_t, - const struct HttpParser_Callbacks*, void*); +typedef enum HttpParserResult (*HttpParserHandler)( + struct HttpParserState*, const void*, size_t, + const struct HttpParserCallbacks*, void*); -struct HttpParser_State { - HttpParser_Handler stage; - int maybe_eol; +struct HttpParserState { + HttpParserHandler stage; + bool maybe_eol; size_t parsing_offset; size_t first_offset; size_t first_size; @@ -54,13 +55,13 @@ struct HttpParser_State { size_t second_size; }; -void HttpParser_Reset(struct HttpParser_State* state); -enum HttpParser_Result HttpParser_Parse( - struct HttpParser_State* state, const void* buffer, size_t buffer_size, - const struct HttpParser_Callbacks* callbacks, void* user); +void HttpParserReset(struct HttpParserState* state); +enum HttpParserResult HttpParserParse( + struct HttpParserState* state, const void* buffer, size_t buffer_size, + const struct HttpParserCallbacks* callbacks, void* user); #ifdef __cplusplus } // extern "C" #endif // __cplusplus -#endif // HTTP_PARSER_H_ +#endif // TOOLBOX_HTTP_PARSER_H_ @@ -18,27 +18,28 @@ #include "io_muxer.h" #include <poll.h> +#include <stdbool.h> #include <stddef.h> #include <stdlib.h> #include <string.h> -struct IoMuxer_Closure { - void (*callback)(void*); +struct IoMuxerTask { + void (*fun)(void*); void* user; }; -static _Bool AppendRecord(struct IoMuxer* io_muxer) { - if (io_muxer->size < io_muxer->alloc) return 1; +static bool AppendRecord(struct IoMuxer* io_muxer) { + if (io_muxer->size < io_muxer->alloc) return true; size_t alloc = io_muxer->alloc + 1; struct pollfd* pfds = realloc(io_muxer->pfds, alloc * sizeof(struct pollfd)); - if (!pfds) return 0; + if (!pfds) return false; io_muxer->pfds = pfds; - struct IoMuxer_Closure* closures = - realloc(io_muxer->closures, alloc * sizeof(struct IoMuxer_Closure)); - if (!closures) return 0; - io_muxer->closures = closures; + struct IoMuxerTask* tasks = + realloc(io_muxer->tasks, alloc * sizeof(struct IoMuxerTask)); + if (!tasks) return false; + io_muxer->tasks = tasks; io_muxer->alloc = alloc; - return 1; + return true; } static void CompressRecords(struct IoMuxer* io_muxer) { @@ -53,51 +54,58 @@ static void CompressRecords(struct IoMuxer* io_muxer) { struct pollfd pfd = io_muxer->pfds[forward]; io_muxer->pfds[forward] = io_muxer->pfds[reverse]; io_muxer->pfds[reverse] = pfd; - struct IoMuxer_Closure closure = io_muxer->closures[forward]; - io_muxer->closures[forward] = io_muxer->closures[reverse]; - io_muxer->closures[reverse] = closure; + struct IoMuxerTask task = io_muxer->tasks[forward]; + io_muxer->tasks[forward] = io_muxer->tasks[reverse]; + io_muxer->tasks[reverse] = task; } } -void IoMuxer_Create(struct IoMuxer* io_muxer) { +void IoMuxerCreate(struct IoMuxer* io_muxer) { memset(io_muxer, 0, sizeof(struct IoMuxer)); } -int IoMuxer_OnRead(struct IoMuxer* io_muxer, int fd, - void (*read_callback)(void*), void* user) { - if (!AppendRecord(io_muxer)) return -1; +bool IoMuxerOnRead(struct IoMuxer* io_muxer, int fd, void (*fun)(void*), + void* user) { + if (!AppendRecord(io_muxer)) return false; struct pollfd pfd = {.fd = fd, .events = POLLIN}; - struct IoMuxer_Closure closure = {.callback = read_callback, .user = user}; + struct IoMuxerTask task = {.fun = fun, .user = user}; io_muxer->pfds[io_muxer->size] = pfd; - io_muxer->closures[io_muxer->size] = closure; + io_muxer->tasks[io_muxer->size] = task; io_muxer->size++; - return 0; + return true; } -int IoMuxer_OnWrite(struct IoMuxer* io_muxer, int fd, - void (*write_callback)(void*), void* user) { - if (!AppendRecord(io_muxer)) return -1; +bool IoMuxerOnWrite(struct IoMuxer* io_muxer, int fd, void (*fun)(void*), + void* user) { + if (!AppendRecord(io_muxer)) return false; struct pollfd pfd = {.fd = fd, .events = POLLOUT}; - struct IoMuxer_Closure closure = {.callback = write_callback, .user = user}; + struct IoMuxerTask task = {.fun = fun, .user = user}; io_muxer->pfds[io_muxer->size] = pfd; - io_muxer->closures[io_muxer->size] = closure; + io_muxer->tasks[io_muxer->size] = task; io_muxer->size++; - return 0; + return true; } -int IoMuxer_Iterate(struct IoMuxer* io_muxer, int timeout) { +enum IoMuxerResult IoMuxerIterate(struct IoMuxer* io_muxer, int timeout) { for (;;) { CompressRecords(io_muxer); int result = poll(io_muxer->pfds, io_muxer->size, timeout); - if (result <= 0) return result; + switch (result) { + case -1: + return kIoMuxerResultError; + case 0: + return kIoMuxerResultTimeout; + default: + break; + } for (size_t i = 0; i < io_muxer->size; i++) { if (io_muxer->pfds[i].revents) - io_muxer->closures[i].callback(io_muxer->closures[i].user); + io_muxer->tasks[i].fun(io_muxer->tasks[i].user); } } } -void IoMuxer_Destroy(struct IoMuxer* io_muxer) { +void IoMuxerDestroy(struct IoMuxer* io_muxer) { free(io_muxer->pfds); - free(io_muxer->closures); + free(io_muxer->tasks); } @@ -15,9 +15,10 @@ * along with toolbox. If not, see <https://www.gnu.org/licenses/>. */ -#ifndef IO_MUXER_H_ -#define IO_MUXER_H_ +#ifndef TOOLBOX_IO_MUXER_H_ +#define TOOLBOX_IO_MUXER_H_ +#include <stdbool.h> #include <stddef.h> #ifdef __cplusplus @@ -25,25 +26,30 @@ extern "C" { #endif // __cplusplus struct pollfd; -struct IoMuxer_Closure; +struct IoMuxerTask; struct IoMuxer { struct pollfd* pfds; - struct IoMuxer_Closure* closures; + struct IoMuxerTask* tasks; size_t alloc; size_t size; }; -void IoMuxer_Create(struct IoMuxer* io_muxer); -int IoMuxer_OnRead(struct IoMuxer* io_muxer, int fd, - void (*read_callback)(void*), void* user); -int IoMuxer_OnWrite(struct IoMuxer* io_muxer, int fd, - void (*write_callback)(void*), void* user); -int IoMuxer_Iterate(struct IoMuxer* io_muxer, int timeout); -void IoMuxer_Destroy(struct IoMuxer* io_muxer); +enum IoMuxerResult { + kIoMuxerResultError = 0, + kIoMuxerResultTimeout, +}; + +void IoMuxerCreate(struct IoMuxer* io_muxer); +bool IoMuxerOnRead(struct IoMuxer* io_muxer, int fd, void (*fun)(void*), + void* user); +bool IoMuxerOnWrite(struct IoMuxer* io_muxer, int fd, void (*fun)(void*), + void* user); +enum IoMuxerResult IoMuxerIterate(struct IoMuxer* io_muxer, int timeout); +void IoMuxerDestroy(struct IoMuxer* io_muxer); #ifdef __cplusplus } // extern "C" #endif // __cplusplus -#endif // IO_MUXER_H_ +#endif // TOOLBOX_IO_MUXER_H_ diff --git a/thread_pool.c b/thread_pool.c index fa44ef1..0e04439 100644 --- a/thread_pool.c +++ b/thread_pool.c @@ -18,45 +18,46 @@ #include "thread_pool.h" #include <stdatomic.h> +#include <stdbool.h> #include <stddef.h> #include <stdlib.h> #include <threads.h> -struct ThreadPool_Task { +struct ThreadPoolTask { void (*fun)(void*); void* user; }; -static _Bool FetchTask(struct ThreadPool* thread_pool, - struct ThreadPool_Task* task) { +static bool FetchTask(struct ThreadPool* thread_pool, + struct ThreadPoolTask* task) { for (size_t i = 0; i < thread_pool->tasks_count; i++) { if (thread_pool->tasks[i].fun) { *task = thread_pool->tasks[i]; thread_pool->tasks[i].fun = NULL; - return 1; + return true; } } - return 0; + return false; } -static _Bool StoreTask(struct ThreadPool* thread_pool, - const struct ThreadPool_Task* task) { +static bool StoreTask(struct ThreadPool* thread_pool, + const struct ThreadPoolTask* task) { for (size_t i = 0; i < thread_pool->tasks_count; i++) { if (!thread_pool->tasks[i].fun) { thread_pool->tasks[i] = *task; - return 1; + return true; } } size_t tasks_count = thread_pool->tasks_count + 1; - struct ThreadPool_Task* tasks = - realloc(thread_pool->tasks, tasks_count * sizeof(struct ThreadPool_Task)); - if (!tasks) return 0; + struct ThreadPoolTask* tasks = + realloc(thread_pool->tasks, tasks_count * sizeof(struct ThreadPoolTask)); + if (!tasks) return false; tasks[thread_pool->tasks_count] = *task; thread_pool->tasks = tasks; thread_pool->tasks_count = tasks_count; - return 1; + return true; } static int ThreadProc(void* arg) { @@ -73,7 +74,7 @@ static int ThreadProc(void* arg) { mtx_unlock(&thread_pool->tasks_mutex); return 0; } - struct ThreadPool_Task task; + struct ThreadPoolTask task; if (FetchTask(thread_pool, &task)) { mtx_unlock(&thread_pool->tasks_mutex); task.fun(task.user); @@ -88,7 +89,7 @@ static int ThreadProc(void* arg) { int ThreadPool_Create(struct ThreadPool* thread_pool, size_t threads_count) { atomic_init(&thread_pool->running, 1); thread_pool->threads = malloc(threads_count * sizeof(thrd_t)); - if (!thread_pool->threads) return -1; + if (!thread_pool->threads) return false; thread_pool->threads_count = 0; if (cnd_init(&thread_pool->tasks_cond) != thrd_success) goto rollback_threads; @@ -103,7 +104,7 @@ int ThreadPool_Create(struct ThreadPool* thread_pool, size_t threads_count) { if (thrd_create(thread, ThreadProc, thread_pool) != thrd_success) goto rollback_running; } - return 0; + return true; rollback_running: atomic_store_explicit(&thread_pool->running, 0, memory_order_relaxed); @@ -115,20 +116,20 @@ rollback_tasks_cond: cnd_destroy(&thread_pool->tasks_cond); rollback_threads: free(thread_pool->threads); - return -1; + return false; } -int ThreadPool_Schedule(struct ThreadPool* thread_pool, void (*fun)(void*), +bool ThreadPoolSchedule(struct ThreadPool* thread_pool, void (*fun)(void*), void* user) { - if (mtx_lock(&thread_pool->tasks_mutex) != thrd_success) return -1; - struct ThreadPool_Task task = {.fun = fun, .user = user}; - _Bool result = StoreTask(thread_pool, &task); + if (mtx_lock(&thread_pool->tasks_mutex) != thrd_success) return false; + struct ThreadPoolTask task = {.fun = fun, .user = user}; + bool result = StoreTask(thread_pool, &task); if (result) cnd_broadcast(&thread_pool->tasks_cond); mtx_unlock(&thread_pool->tasks_mutex); - return result ? 0 : -1; + return result; } -void ThreadPool_Destroy(struct ThreadPool* thread_pool) { +void ThreadPoolDestroy(struct ThreadPool* thread_pool) { atomic_store_explicit(&thread_pool->running, 0, memory_order_relaxed); cnd_broadcast(&thread_pool->tasks_cond); while (thread_pool->threads_count-- > 0) @@ -136,4 +137,5 @@ void ThreadPool_Destroy(struct ThreadPool* thread_pool) { mtx_destroy(&thread_pool->tasks_mutex); cnd_destroy(&thread_pool->tasks_cond); free(thread_pool->threads); + free(thread_pool->tasks); } diff --git a/thread_pool.h b/thread_pool.h index 496fd9c..9954846 100644 --- a/thread_pool.h +++ b/thread_pool.h @@ -15,14 +15,15 @@ * along with toolbox. If not, see <https://www.gnu.org/licenses/>. */ -#ifndef THREAD_POOL_H_ -#define THREAD_POOL_H_ +#ifndef TOOLBOX_THREAD_POOL_H_ +#define TOOLBOX_THREAD_POOL_H_ #include <stdatomic.h> +#include <stdbool.h> #include <stddef.h> #include <threads.h> -struct ThreadPool_Task; +struct ThreadPoolTask; struct ThreadPool { atomic_bool running; @@ -30,13 +31,13 @@ struct ThreadPool { size_t threads_count; cnd_t tasks_cond; mtx_t tasks_mutex; - struct ThreadPool_Task* tasks; + struct ThreadPoolTask* tasks; size_t tasks_count; }; -int ThreadPool_Create(struct ThreadPool* thread_pool, size_t threads_count); -int ThreadPool_Schedule(struct ThreadPool* thread_pool, void (*fun)(void*), +bool ThreadPoolCreate(struct ThreadPool* thread_pool, size_t threads_count); +bool ThreadPoolSchedule(struct ThreadPool* thread_pool, void (*fun)(void*), void* user); -void ThreadPool_Destroy(struct ThreadPool* thread_pool); +void ThreadPoolDestroy(struct ThreadPool* thread_pool); -#endif // THREAD_POOL_H_ +#endif // TOOLBOX_THREAD_POOL_H_ @@ -15,8 +15,8 @@ * along with toolbox. If not, see <https://www.gnu.org/licenses/>. */ -#ifndef UTILS_H_ -#define UTILS_H_ +#ifndef TOOLBOX_UTILS_H_ +#define TOOLBOX_UTILS_H_ #ifdef __cplusplus extern "C" { @@ -35,4 +35,4 @@ unsigned long long MillisNow(void); } // extern "C" #endif // __cplusplus -#endif // UTILS_H_ +#endif // TOOLBOX_UTILS_H_ |