threadpool.h 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. #ifndef THREADPOOL_H
  2. #define THREADPOOL_H
  3. #pragma once
  4. #include "config.h"
  5. #include "memutil.h"
  6. #ifdef __cplusplus
  7. extern "C" {
  8. #endif
  9. typedef struct threadpool_t threadpool_t;
  10. typedef struct strand_t strand_t;
  11. typedef void (*threadpool_workitem_proc)(threadpool_t *threadpool, void *arg);
  12. typedef void (*threadpool_workitem_proc2)(threadpool_t *threadpool, void *arg, param_size_t param1, param_size_t param2);
  13. typedef void (*threadpool_decorator_callback)(threadpool_t *threadpool, void *user_data);
  14. TOOLKIT_API int threadpool_create(threadpool_t **p_threadpool);
  15. TOOLKIT_API int threadpool_destroy(threadpool_t *threadpool);
  16. TOOLKIT_API int threadpool_set_thread_decorator(threadpool_t *threadpool,
  17. threadpool_decorator_callback dc_init,
  18. threadpool_decorator_callback dc_term,
  19. void *user_data);
  20. TOOLKIT_API int threadpool_start(threadpool_t *threadpool,
  21. int num_fix_thread,
  22. int max_tmp_thread,
  23. int tmp_thread_ttl,
  24. int stack_size); // zero for default
  25. TOOLKIT_API int threadpool_stop(threadpool_t *threadpool);
  26. TOOLKIT_API int threadpool_queue_workitem(threadpool_t *threadpool,
  27. strand_t *strand,
  28. threadpool_workitem_proc workitem,
  29. void *arg);
  30. TOOLKIT_API int threadpool_queue_workitem2(threadpool_t *threadpool,
  31. strand_t *strand,
  32. threadpool_workitem_proc2 workitem,
  33. void *arg,
  34. param_size_t param1,
  35. param_size_t param2);
  36. #define threadpool_post_workitem_fifo threadpool_queue_workitem
  37. #define threadpool_post_workitem_fifo2 threadpool_queue_workitem2
  38. TOOLKIT_API int threadpool_post_workitem_lifo(threadpool_t *threadpool,
  39. strand_t *strand,
  40. threadpool_workitem_proc workitem,
  41. void *arg);
  42. TOOLKIT_API int threadpool_post_workitem_lifo2(threadpool_t *threadpool,
  43. strand_t *strand,
  44. threadpool_workitem_proc2 workitem,
  45. void *arg,
  46. param_size_t param1,
  47. param_size_t param2);
  48. TOOLKIT_API void threadpool_set_user_data(threadpool_t *threadpool, void *user_data);
  49. TOOLKIT_API void *threadpool_get_user_data(threadpool_t *threadpool);
  50. typedef void (*log_func)(threadpool_t *threadpool, const char*);
  51. TOOLKIT_API void threadpool_set_log(threadpool_t *threadpool, log_func func);
  52. TOOLKIT_API strand_t *strand_create();
  53. TOOLKIT_API int strand_dec_ref(strand_t *strand);
  54. TOOLKIT_API int strand_inc_ref(strand_t *strand);
  55. TOOLKIT_API int strand_ref_cnt(strand_t *strand);
  56. TOOLKIT_API void strand_lock(strand_t *strand);
  57. TOOLKIT_API void strand_unlock(strand_t *strand);
  58. static void strand_destroy(strand_t *strand) { strand_dec_ref(strand); }
  59. #ifdef __cplusplus
  60. } // extern "C" {
  61. #endif
  62. #endif // THREADPOOL_H