thread_pool.h 3.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. /*
  2. * This file is part of the EasyDataManager Library.
  3. *
  4. * Copyright (C) 2013-2019 by Armink <armink.ztl@gmail.com>
  5. *
  6. * Function: a thread pool base on RT-Thread
  7. * Created on: 2013-11-14
  8. */
  9. #ifndef THREAD_POOL_H_
  10. #define THREAD_POOL_H_
  11. #include <rtthread.h>
  12. #include <string.h>
  13. #define THREAD_POOL_VER "4.0.1"
  14. #define THREAD_POOL_THREADS_INIT_TIME 30 /**< threads initialize average waiting time */
  15. #define THREAD_POOL_JOB_DEFAULT_PRIORITY 10 /**< thread poll job's priority in rt-thread */
  16. #define THREAD_POOL_JOB_TICK 5 /**< thread poll job's time slice in rt-thread */
  17. #define THREAD_POOL_NAME_MAX RT_NAME_MAX /**< thread poll max name length */
  18. /* thread pool error code */
  19. typedef enum {
  20. THREAD_POOL_NO_ERR, /**< no error */
  21. THREAD_POOL_ALREADY_SHUTDOWN_ERR, /**< thread pool already shutdown */
  22. THREAD_POOL_MEM_FULL_ERR, /**< memory full */
  23. } thread_pool_err;
  24. /* a task queue which run in thread pool */
  25. typedef struct _thread_pool_task {
  26. void (*process)(void *arg); /**< task callback function */
  27. void *arg; /**< task callback function's arguments */
  28. struct _thread_pool_task *next;
  29. } thread_pool_task, *thread_pool_task_t;
  30. /* thread pool struct */
  31. typedef struct _thread_pool {
  32. char name[THREAD_POOL_NAME_MAX + 1];/**< the name of ThreadPool, the end of name is include '\0' */
  33. thread_pool_task_t queue_head; /**< task queue which place all waiting task */
  34. rt_mutex_t user_lock; /**< a synchronized lock provided to user */
  35. rt_mutex_t queue_lock; /**< task queue mutex lock */
  36. rt_sem_t queue_ready; /**< a semaphore which for task queue ready */
  37. uint8_t is_shutdown; /**< shutdown state,if shutdown the value will equal TRUE */
  38. rt_thread_t* thread_id; /**< thread queue which in thread pool */
  39. uint8_t max_thread_num; /**< the thread max number in thread pool */
  40. uint8_t cur_wait_thread_num; /**< the current waiting thread number in thread pool */
  41. /**
  42. * This function will add a task to thread pool.
  43. *
  44. * @param pool the ThreadPool pointer
  45. * @param process task function pointer
  46. * @param arg task function arguments
  47. *
  48. * @return error code
  49. */
  50. thread_pool_err (*add_task)(struct _thread_pool* const pool,
  51. void (*process)(void *arg), void *arg);
  52. /**
  53. * This function will delete all task.
  54. *
  55. * @param pool
  56. *
  57. * @return error code
  58. */
  59. thread_pool_err (*del_all)(struct _thread_pool* const pool);
  60. /**
  61. * This function will destroy thread pool.
  62. *
  63. * @param pool the ThreadPool pointer
  64. *
  65. * @return error code
  66. */
  67. thread_pool_err (*destroy)(struct _thread_pool* pool);
  68. /**
  69. * This function will lock the synchronized lock.
  70. *
  71. * @param pool the ThreadPool pointer
  72. *
  73. */
  74. void (*lock)(struct _thread_pool* pool);
  75. /**
  76. * This function will unlock the synchronized lock.
  77. *
  78. * @param pool the ThreadPool pointer
  79. *
  80. */
  81. void (*unlock)(struct _thread_pool* pool);
  82. } thread_pool, *thread_pool_t;
  83. thread_pool_err init_thread_pool(thread_pool_t const pool, const char* name, uint8_t max_thread_num,
  84. uint32_t thread_stack_size);
  85. #endif /* THREAD_POOL_H_ */