workqueue.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456
  1. /*
  2. * Copyright (c) 2006-2021, RT-Thread Development Team
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. *
  6. * Change Logs:
  7. * Date Author Notes
  8. * 2017-02-27 Bernard fix the re-work issue.
  9. * 2021-08-01 Meco Man remove rt_delayed_work_init()
  10. * 2021-08-14 Jackistang add comments for function interface.
  11. */
  12. #include <rthw.h>
  13. #include <rtthread.h>
  14. #include <rtdevice.h>
  15. #ifdef RT_USING_HEAP
  16. static void _delayed_work_timeout_handler(void *parameter);
  17. rt_inline rt_err_t _workqueue_work_completion(struct rt_workqueue *queue)
  18. {
  19. rt_err_t result;
  20. rt_enter_critical();
  21. while (1)
  22. {
  23. /* try to take condition semaphore */
  24. result = rt_sem_trytake(&(queue->sem));
  25. if (result == -RT_ETIMEOUT)
  26. {
  27. /* it's timeout, release this semaphore */
  28. rt_sem_release(&(queue->sem));
  29. }
  30. else if (result == RT_EOK)
  31. {
  32. /* keep the sem value = 0 */
  33. result = RT_EOK;
  34. break;
  35. }
  36. else
  37. {
  38. result = -RT_ERROR;
  39. break;
  40. }
  41. }
  42. rt_exit_critical();
  43. return result;
  44. }
  45. static void _workqueue_thread_entry(void *parameter)
  46. {
  47. rt_base_t level;
  48. struct rt_work *work;
  49. struct rt_workqueue *queue;
  50. queue = (struct rt_workqueue *) parameter;
  51. RT_ASSERT(queue != RT_NULL);
  52. while (1)
  53. {
  54. level = rt_hw_interrupt_disable();
  55. if (rt_list_isempty(&(queue->work_list)))
  56. {
  57. /* no software timer exist, suspend self. */
  58. rt_thread_suspend(rt_thread_self());
  59. rt_hw_interrupt_enable(level);
  60. rt_schedule();
  61. continue;
  62. }
  63. /* we have work to do with. */
  64. work = rt_list_entry(queue->work_list.next, struct rt_work, list);
  65. rt_list_remove(&(work->list));
  66. queue->work_current = work;
  67. work->flags &= ~RT_WORK_STATE_PENDING;
  68. work->workqueue = RT_NULL;
  69. rt_hw_interrupt_enable(level);
  70. /* do work */
  71. work->work_func(work, work->work_data);
  72. /* clean current work */
  73. queue->work_current = RT_NULL;
  74. /* ack work completion */
  75. _workqueue_work_completion(queue);
  76. }
  77. }
  78. static rt_err_t _workqueue_submit_work(struct rt_workqueue *queue,
  79. struct rt_work *work, rt_tick_t ticks)
  80. {
  81. rt_base_t level;
  82. rt_err_t err;
  83. level = rt_hw_interrupt_disable();
  84. /* remove list */
  85. rt_list_remove(&(work->list));
  86. work->flags &= ~RT_WORK_STATE_PENDING;
  87. /* */
  88. if (ticks == 0)
  89. {
  90. if (queue->work_current != work)
  91. {
  92. rt_list_insert_after(queue->work_list.prev, &(work->list));
  93. work->flags |= RT_WORK_STATE_PENDING;
  94. work->workqueue = queue;
  95. err = RT_EOK;
  96. }
  97. else
  98. {
  99. err = -RT_EBUSY;
  100. }
  101. /* whether the workqueue is doing work */
  102. if (queue->work_current == RT_NULL &&
  103. ((queue->work_thread->stat & RT_THREAD_STAT_MASK) == RT_THREAD_SUSPEND))
  104. {
  105. /* resume work thread */
  106. rt_thread_resume(queue->work_thread);
  107. rt_hw_interrupt_enable(level);
  108. rt_schedule();
  109. }
  110. else
  111. {
  112. rt_hw_interrupt_enable(level);
  113. }
  114. return err;
  115. }
  116. else if (ticks < RT_TICK_MAX / 2)
  117. {
  118. /* Timer started */
  119. if (work->flags & RT_WORK_STATE_SUBMITTING)
  120. {
  121. rt_timer_stop(&work->timer);
  122. rt_timer_control(&work->timer, RT_TIMER_CTRL_SET_TIME, &ticks);
  123. }
  124. else
  125. {
  126. rt_timer_init(&(work->timer), "work", _delayed_work_timeout_handler,
  127. work, ticks, RT_TIMER_FLAG_ONE_SHOT | RT_TIMER_FLAG_SOFT_TIMER);
  128. work->flags |= RT_WORK_STATE_SUBMITTING;
  129. }
  130. work->workqueue = queue;
  131. /* insert delay work list */
  132. rt_list_insert_after(queue->delayed_list.prev, &(work->list));
  133. rt_hw_interrupt_enable(level);
  134. rt_timer_start(&(work->timer));
  135. return RT_EOK;
  136. }
  137. rt_hw_interrupt_enable(level);
  138. return -RT_ERROR;
  139. }
  140. static rt_err_t _workqueue_cancel_work(struct rt_workqueue *queue, struct rt_work *work)
  141. {
  142. rt_base_t level;
  143. rt_err_t err;
  144. level = rt_hw_interrupt_disable();
  145. rt_list_remove(&(work->list));
  146. work->flags &= ~RT_WORK_STATE_PENDING;
  147. /* Timer started */
  148. if (work->flags & RT_WORK_STATE_SUBMITTING)
  149. {
  150. rt_timer_stop(&(work->timer));
  151. rt_timer_detach(&(work->timer));
  152. work->flags &= ~RT_WORK_STATE_SUBMITTING;
  153. }
  154. err = queue->work_current != work ? RT_EOK : -RT_EBUSY;
  155. work->workqueue = RT_NULL;
  156. rt_hw_interrupt_enable(level);
  157. return err;
  158. }
  159. static void _delayed_work_timeout_handler(void *parameter)
  160. {
  161. struct rt_work *work;
  162. struct rt_workqueue *queue;
  163. rt_base_t level;
  164. work = (struct rt_work *)parameter;
  165. queue = work->workqueue;
  166. RT_ASSERT(queue != RT_NULL);
  167. level = rt_hw_interrupt_disable();
  168. rt_timer_detach(&(work->timer));
  169. work->flags &= ~RT_WORK_STATE_SUBMITTING;
  170. /* remove delay list */
  171. rt_list_remove(&(work->list));
  172. /* insert work queue */
  173. if (queue->work_current != work)
  174. {
  175. rt_list_insert_after(queue->work_list.prev, &(work->list));
  176. work->flags |= RT_WORK_STATE_PENDING;
  177. }
  178. /* whether the workqueue is doing work */
  179. if (queue->work_current == RT_NULL &&
  180. ((queue->work_thread->stat & RT_THREAD_STAT_MASK) == RT_THREAD_SUSPEND))
  181. {
  182. /* resume work thread */
  183. rt_thread_resume(queue->work_thread);
  184. rt_hw_interrupt_enable(level);
  185. rt_schedule();
  186. }
  187. else
  188. {
  189. rt_hw_interrupt_enable(level);
  190. }
  191. }
  192. /**
  193. * @brief Create a work queue with a thread inside.
  194. *
  195. * @param name The name of the work queue thread.
  196. * @param stack_size The stack size of the work queue thread.
  197. * @param priority The priority of the work queue thread.
  198. *
  199. * @return Return A pointer to the workqueue object. It will return RT_NULL if failed.
  200. */
  201. struct rt_workqueue *rt_workqueue_create(const char *name, rt_uint16_t stack_size, rt_uint8_t priority)
  202. {
  203. struct rt_workqueue *queue = RT_NULL;
  204. queue = (struct rt_workqueue *)RT_KERNEL_MALLOC(sizeof(struct rt_workqueue));
  205. if (queue != RT_NULL)
  206. {
  207. /* initialize work list */
  208. rt_list_init(&(queue->work_list));
  209. rt_list_init(&(queue->delayed_list));
  210. queue->work_current = RT_NULL;
  211. rt_sem_init(&(queue->sem), "wqueue", 0, RT_IPC_FLAG_FIFO);
  212. /* create the work thread */
  213. queue->work_thread = rt_thread_create(name, _workqueue_thread_entry, queue, stack_size, priority, 10);
  214. if (queue->work_thread == RT_NULL)
  215. {
  216. RT_KERNEL_FREE(queue);
  217. return RT_NULL;
  218. }
  219. rt_thread_startup(queue->work_thread);
  220. }
  221. return queue;
  222. }
  223. /**
  224. * @brief Destroy a work queue.
  225. *
  226. * @param queue A pointer to the workqueue object.
  227. *
  228. * @return RT_EOK Success.
  229. */
  230. rt_err_t rt_workqueue_destroy(struct rt_workqueue *queue)
  231. {
  232. RT_ASSERT(queue != RT_NULL);
  233. rt_workqueue_cancel_all_work(queue);
  234. rt_thread_delete(queue->work_thread);
  235. rt_sem_detach(&(queue->sem));
  236. RT_KERNEL_FREE(queue);
  237. return RT_EOK;
  238. }
  239. /**
  240. * @brief Submit a work item to the work queue without delay.
  241. *
  242. * @param queue A pointer to the workqueue object.
  243. * @param work A pointer to the work item object.
  244. *
  245. * @return RT_EOK Success.
  246. * @return -RT_EBUSY This work item is executing.
  247. */
  248. rt_err_t rt_workqueue_dowork(struct rt_workqueue *queue, struct rt_work *work)
  249. {
  250. RT_ASSERT(queue != RT_NULL);
  251. RT_ASSERT(work != RT_NULL);
  252. return _workqueue_submit_work(queue, work, 0);
  253. }
  254. /**
  255. * @brief Submit a work item to the work queue with a delay.
  256. *
  257. * @param queue A pointer to the workqueue object.
  258. * @param work A pointer to the work item object.
  259. * @param time The delay time (unit: OS ticks) for the work item to be submitted to the work queue.
  260. *
  261. * @return RT_EOK Success.
  262. * @return -RT_EBUSY This work item is executing.
  263. * @return -RT_ERROR The time parameter is invalid.
  264. */
  265. rt_err_t rt_workqueue_submit_work(struct rt_workqueue *queue, struct rt_work *work, rt_tick_t time)
  266. {
  267. RT_ASSERT(queue != RT_NULL);
  268. RT_ASSERT(work != RT_NULL);
  269. return _workqueue_submit_work(queue, work, time);
  270. }
  271. /**
  272. * @brief Submit a work item to the work queue without delay. This work item will be executed after the current work item is executed.
  273. *
  274. * @param queue A pointer to the workqueue object.
  275. * @param work A pointer to the work item object.
  276. *
  277. * @return RT_EOK Success.
  278. */
  279. rt_err_t rt_workqueue_critical_work(struct rt_workqueue *queue, struct rt_work *work)
  280. {
  281. rt_base_t level;
  282. RT_ASSERT(queue != RT_NULL);
  283. RT_ASSERT(work != RT_NULL);
  284. level = rt_hw_interrupt_disable();
  285. /* NOTE: the work MUST be initialized firstly */
  286. rt_list_remove(&(work->list));
  287. rt_list_insert_after(&queue->work_list, &(work->list));
  288. /* whether the workqueue is doing work */
  289. if (queue->work_current == RT_NULL &&
  290. ((queue->work_thread->stat & RT_THREAD_STAT_MASK) == RT_THREAD_SUSPEND))
  291. {
  292. /* resume work thread */
  293. rt_thread_resume(queue->work_thread);
  294. rt_hw_interrupt_enable(level);
  295. rt_schedule();
  296. }
  297. else
  298. {
  299. rt_hw_interrupt_enable(level);
  300. }
  301. return RT_EOK;
  302. }
  303. /**
  304. * @brief Cancel a work item in the work queue.
  305. *
  306. * @param queue A pointer to the workqueue object.
  307. * @param work A pointer to the work item object.
  308. *
  309. * @return RT_EOK Success.
  310. * @return -RT_EBUSY This work item is executing.
  311. */
  312. rt_err_t rt_workqueue_cancel_work(struct rt_workqueue *queue, struct rt_work *work)
  313. {
  314. RT_ASSERT(work != RT_NULL);
  315. RT_ASSERT(queue != RT_NULL);
  316. return _workqueue_cancel_work(queue, work);
  317. }
  318. /**
  319. * @brief Cancel a work item in the work queue. If the work item is executing, this function will block until it is done.
  320. *
  321. * @param queue A pointer to the workqueue object.
  322. * @param work A pointer to the work item object.
  323. *
  324. * @return RT_EOK Success.
  325. */
  326. rt_err_t rt_workqueue_cancel_work_sync(struct rt_workqueue *queue, struct rt_work *work)
  327. {
  328. RT_ASSERT(queue != RT_NULL);
  329. RT_ASSERT(work != RT_NULL);
  330. if (queue->work_current == work) /* it's current work in the queue */
  331. {
  332. /* wait for work completion */
  333. rt_sem_take(&(queue->sem), RT_WAITING_FOREVER);
  334. }
  335. else
  336. {
  337. _workqueue_cancel_work(queue, work);
  338. }
  339. return RT_EOK;
  340. }
  341. /**
  342. * @brief This function will cancel all work items in work queue.
  343. *
  344. * @param queue A pointer to the workqueue object.
  345. *
  346. * @return RT_EOK Success.
  347. */
  348. rt_err_t rt_workqueue_cancel_all_work(struct rt_workqueue *queue)
  349. {
  350. struct rt_work *work;
  351. RT_ASSERT(queue != RT_NULL);
  352. /* cancel work */
  353. rt_enter_critical();
  354. while (rt_list_isempty(&queue->work_list) == RT_FALSE)
  355. {
  356. work = rt_list_first_entry(&queue->work_list, struct rt_work, list);
  357. _workqueue_cancel_work(queue, work);
  358. }
  359. /* cancel delay work */
  360. while (rt_list_isempty(&queue->delayed_list) == RT_FALSE)
  361. {
  362. work = rt_list_first_entry(&queue->delayed_list, struct rt_work, list);
  363. _workqueue_cancel_work(queue, work);
  364. }
  365. rt_exit_critical();
  366. return RT_EOK;
  367. }
  368. #ifdef RT_USING_SYSTEM_WORKQUEUE
  369. static struct rt_workqueue *sys_workq;
  370. /**
  371. * @brief Submit a work item to the system work queue with a delay.
  372. *
  373. * @param work A pointer to the work item object.
  374. * @param time The delay time (unit: OS ticks) for the work item to be submitted to the work queue.
  375. *
  376. * @return RT_EOK Success.
  377. * @return -RT_EBUSY This work item is executing.
  378. * @return -RT_ERROR The time parameter is invalid.
  379. */
  380. rt_err_t rt_work_submit(struct rt_work *work, rt_tick_t time)
  381. {
  382. return rt_workqueue_submit_work(sys_workq, work, time);
  383. }
  384. /**
  385. * @brief Cancel a work item in the system work queue.
  386. *
  387. * @param work A pointer to the work item object.
  388. *
  389. * @return RT_EOK Success.
  390. * @return -RT_EBUSY This work item is executing.
  391. */
  392. rt_err_t rt_work_cancel(struct rt_work *work)
  393. {
  394. return rt_workqueue_cancel_work(sys_workq, work);
  395. }
  396. static int rt_work_sys_workqueue_init(void)
  397. {
  398. if (sys_workq != RT_NULL)
  399. return RT_EOK;
  400. sys_workq = rt_workqueue_create("sys_work", RT_SYSTEM_WORKQUEUE_STACKSIZE,
  401. RT_SYSTEM_WORKQUEUE_PRIORITY);
  402. RT_ASSERT(sys_workq != RT_NULL);
  403. return RT_EOK;
  404. }
  405. INIT_PREV_EXPORT(rt_work_sys_workqueue_init);
  406. #endif /* RT_USING_SYSTEM_WORKQUEUE */
  407. #endif /* RT_USING_HEAP */