diff options
author | sss <sss@dark-alexandr.net> | 2023-01-17 00:38:19 +0300 |
---|---|---|
committer | sss <sss@dark-alexandr.net> | 2023-01-17 00:38:19 +0300 |
commit | cc3f33db7a8d3c4ad373e646b199808e01bc5d9b (patch) | |
tree | ec09d690c7656ab5f2cc72607e05fb359c24d8b2 /src/core/wrdp_thpool.h |
added webrdp public code
Diffstat (limited to 'src/core/wrdp_thpool.h')
-rw-r--r-- | src/core/wrdp_thpool.h | 150 |
1 files changed, 150 insertions, 0 deletions
diff --git a/src/core/wrdp_thpool.h b/src/core/wrdp_thpool.h new file mode 100644 index 0000000..d6bbe38 --- /dev/null +++ b/src/core/wrdp_thpool.h @@ -0,0 +1,150 @@ +/* BSD-2-Clause license + * + * Copyright (c) 2018-2023 NST <www.newinfosec.ru>, sss <sss at dark-alexandr dot net>. + * + */ + +#pragma once + +#include <stdint.h> +#include <ev.h> + +struct wrdp_thpool_s; +typedef struct wrdp_thpool_s wrdp_thpool; + +struct wrdp_thpool_thread_s; +typedef struct wrdp_thpool_thread_s wrdp_thpool_thread; + +struct wrdp_thpool_task_s; +typedef struct wrdp_thpool_task_s wrdp_thpool_task; + +/* initialize thread pool */ + +/* function called in each thread to do additional initialization + * of user_pool_data +void (*custom_thread_init) (void *user_pool_data); +*/ + +/* function called in each thread to do additional cleanup of + * user_pool_data +void (*custom_thread_deinit) (void *user_pool_data); +*/ + +/* function called in wrdp_thpool_create to do additional initialization + * of user_pool_data +void (*custom_pool_create) (void *user_pool_data); +*/ + +/* function called in wrdp_thpool_destroy to do additional cleanup of + * user_pool_data +void (*custom_pool_destroy) (void *user_pool_data); +*/ +/* function called on incomming mesdage with "void *user_data" directed to + * pool +void (*pool_message_handler) (void *user_data); + */ + +/* function called on incomming message with "void *user_data" directed to + * thrad +void (*thread_message_handler) (void *user_data); + */ + +/* struct ev_loop* loop + * it's possible to use specified ev_loop instead of EV_DEFAULT + */ + +wrdp_thpool *wrdp_thpool_create(uint16_t thread_count, + uint64_t max_tasks_per_thread, + void (*custom_thread_init)(void *user_pool_data, wrdp_thpool_thread *t), + void (*custom_thread_deinit)(void *user_pool_data, wrdp_thpool_thread *t), + void (*custom_pool_create)(void *user_pool_data), + void (*custom_pool_destroy)(void *user_pool_data), + void (*pool_message_handler)(void *user_data), + void (*thread_message_handler)(void *user_data), struct ev_loop *loop, + void *user_pool_data); + +/* deinitialize thread pool */ +void wrdp_thpool_destroy(wrdp_thpool *pool); + +/* + * Add task to thread pool. + * task will be added to thread with minimal tasks count + * Not thread safe. + * Must be called from thread which created thread pool only. + * + * pool: fully initialized thread pool + * run_task: task entry point callback + * must not block but set libev watcher instead + * + * userdata: user specified data pointer + */ +bool wrdp_thread_pool_add_task(wrdp_thpool *pool, + void (*run_task)(wrdp_thpool_task *task, void *userdata), + void (*task_init_cb)(wrdp_thpool_task *task, void *userdata), + void *userdata); + +/* + * Add task to thread pool to specified thread. + * task will be added to specified thread + * Not thread safe. + * Must be called from thread which created thread pool only. + * + * pool: fully initialized thread pool + * + * thread_id: id of the thread to run task in + * + * run_task: task entry point callback + * must not block but set libev watcher instead + * + * userdata: user specified data pointer + */ +bool wrdp_thread_pool_add_task_to_thread(wrdp_thpool *pool, + void (*run_task)(wrdp_thpool_task *task, void *userdata), + uint32_t thread_id, + void (*task_init_cb)(wrdp_thpool_task *task, void *userdata), + void *userdata); + +/* + * Move task to specified thread. + * task will be moved to specified thread + * + * (technically new task in specified thread will + * be created, and current task will be deleted + * preserving task internal state) + * + * pool: fully initialized thread pool + * + * thread_id: id of the thread to run task in + * + * run_task: task entry point callback + * must not block but set libev watcher instead + * + * stop_task: callback function used to prepare task for ruining in new thread + * + * userdata: user specified data pointer + */ +bool wrdp_thread_pool_move_task_to_thread(wrdp_thpool *pool, + void (*run_task)(wrdp_thpool_task *task, void *userdata), + void (*stop_task)(wrdp_thpool_task *current_task, void *userdata), + uint32_t thread_id, + void (*task_init_cb)(wrdp_thpool_task *task, void *userdata), + wrdp_thpool_task *current_task, void *userdata); + +/* + * it is possible to send message with "void *user_data" to specified thread, + * or to pool main thread + * NOTE: custom message handler function must also be set during pool creation + */ + +bool wrdp_thpool_send_msg_to_thread( + wrdp_thpool *pool, uint32_t thread_id, void *user_data); + +bool wrdp_thpool_send_msg_to_pool(wrdp_thpool *pool, void *user_data); + +/* + * Destroy runing task. + * Custom destroy is function ptr "void(*callable)(wrdp_thpool_task* task)". + */ + +void wrdp_thread_pool_destroy_task( + wrdp_thpool_task *task, void (*on_task_destroy)(wrdp_thpool_task *task)); |