workqueue.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501
  1. /*
  2. * Copyright (c) 2006-2023, 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. * 2022-01-16 Meco Man add rt_work_urgent()
  12. */
  13. #include <rthw.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_with_flag(rt_thread_self(), RT_UNINTERRUPTIBLE);
  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. level = rt_hw_interrupt_disable();
  83. /* remove list */
  84. rt_list_remove(&(work->list));
  85. work->flags &= ~RT_WORK_STATE_PENDING;
  86. if (ticks == 0)
  87. {
  88. rt_list_insert_after(queue->work_list.prev, &(work->list));
  89. work->flags |= RT_WORK_STATE_PENDING;
  90. work->workqueue = queue;
  91. /* whether the workqueue is doing work */
  92. if (queue->work_current == RT_NULL &&
  93. ((queue->work_thread->stat & RT_THREAD_SUSPEND_MASK) == RT_THREAD_SUSPEND_MASK))
  94. {
  95. /* resume work thread */
  96. rt_thread_resume(queue->work_thread);
  97. rt_hw_interrupt_enable(level);
  98. rt_schedule();
  99. }
  100. else
  101. {
  102. rt_hw_interrupt_enable(level);
  103. }
  104. return RT_EOK;
  105. }
  106. else if (ticks < RT_TICK_MAX / 2)
  107. {
  108. /* Timer started */
  109. if (work->flags & RT_WORK_STATE_SUBMITTING)
  110. {
  111. rt_timer_stop(&work->timer);
  112. rt_timer_control(&work->timer, RT_TIMER_CTRL_SET_TIME, &ticks);
  113. }
  114. else
  115. {
  116. rt_timer_init(&(work->timer), "work", _delayed_work_timeout_handler,
  117. work, ticks, RT_TIMER_FLAG_ONE_SHOT | RT_TIMER_FLAG_SOFT_TIMER);
  118. work->flags |= RT_WORK_STATE_SUBMITTING;
  119. }
  120. work->workqueue = queue;
  121. /* insert delay work list */
  122. rt_list_insert_after(queue->delayed_list.prev, &(work->list));
  123. rt_hw_interrupt_enable(level);
  124. rt_timer_start(&(work->timer));
  125. return RT_EOK;
  126. }
  127. rt_hw_interrupt_enable(level);
  128. return -RT_ERROR;
  129. }
  130. static rt_err_t _workqueue_cancel_work(struct rt_workqueue *queue, struct rt_work *work)
  131. {
  132. rt_base_t level;
  133. rt_err_t err;
  134. level = rt_hw_interrupt_disable();
  135. rt_list_remove(&(work->list));
  136. work->flags &= ~RT_WORK_STATE_PENDING;
  137. /* Timer started */
  138. if (work->flags & RT_WORK_STATE_SUBMITTING)
  139. {
  140. rt_timer_stop(&(work->timer));
  141. rt_timer_detach(&(work->timer));
  142. work->flags &= ~RT_WORK_STATE_SUBMITTING;
  143. }
  144. err = queue->work_current != work ? RT_EOK : -RT_EBUSY;
  145. work->workqueue = RT_NULL;
  146. rt_hw_interrupt_enable(level);
  147. return err;
  148. }
  149. static void _delayed_work_timeout_handler(void *parameter)
  150. {
  151. struct rt_work *work;
  152. struct rt_workqueue *queue;
  153. rt_base_t level;
  154. work = (struct rt_work *)parameter;
  155. queue = work->workqueue;
  156. RT_ASSERT(queue != RT_NULL);
  157. level = rt_hw_interrupt_disable();
  158. rt_timer_detach(&(work->timer));
  159. work->flags &= ~RT_WORK_STATE_SUBMITTING;
  160. /* remove delay list */
  161. rt_list_remove(&(work->list));
  162. /* insert work queue */
  163. if (queue->work_current != work)
  164. {
  165. rt_list_insert_after(queue->work_list.prev, &(work->list));
  166. work->flags |= RT_WORK_STATE_PENDING;
  167. }
  168. /* whether the workqueue is doing work */
  169. if (queue->work_current == RT_NULL &&
  170. ((queue->work_thread->stat & RT_THREAD_SUSPEND_MASK) == RT_THREAD_SUSPEND_MASK))
  171. {
  172. /* resume work thread */
  173. rt_thread_resume(queue->work_thread);
  174. rt_hw_interrupt_enable(level);
  175. rt_schedule();
  176. }
  177. else
  178. {
  179. rt_hw_interrupt_enable(level);
  180. }
  181. }
  182. /**
  183. * @brief Initialize a work item, binding with a callback function.
  184. *
  185. * @param work is a pointer to the work item object.
  186. *
  187. * @param work_func is a callback function that will be called when this work item is executed.
  188. *
  189. * @param work_data is a user data passed to the callback function as the second parameter.
  190. */
  191. void rt_work_init(struct rt_work *work,
  192. void (*work_func)(struct rt_work *work, void *work_data),
  193. void *work_data)
  194. {
  195. RT_ASSERT(work != RT_NULL);
  196. RT_ASSERT(work_func != RT_NULL);
  197. rt_list_init(&(work->list));
  198. work->work_func = work_func;
  199. work->work_data = work_data;
  200. work->workqueue = RT_NULL;
  201. work->flags = 0;
  202. work->type = 0;
  203. }
  204. /**
  205. * @brief Create a work queue with a thread inside.
  206. *
  207. * @param name is a name of the work queue thread.
  208. *
  209. * @param stack_size is stack size of the work queue thread.
  210. *
  211. * @param priority is a priority of the work queue thread.
  212. *
  213. * @return Return a pointer to the workqueue object. It will return RT_NULL if failed.
  214. */
  215. struct rt_workqueue *rt_workqueue_create(const char *name, rt_uint16_t stack_size, rt_uint8_t priority)
  216. {
  217. struct rt_workqueue *queue = RT_NULL;
  218. queue = (struct rt_workqueue *)RT_KERNEL_MALLOC(sizeof(struct rt_workqueue));
  219. if (queue != RT_NULL)
  220. {
  221. /* initialize work list */
  222. rt_list_init(&(queue->work_list));
  223. rt_list_init(&(queue->delayed_list));
  224. queue->work_current = RT_NULL;
  225. rt_sem_init(&(queue->sem), "wqueue", 0, RT_IPC_FLAG_FIFO);
  226. /* create the work thread */
  227. queue->work_thread = rt_thread_create(name, _workqueue_thread_entry, queue, stack_size, priority, 10);
  228. if (queue->work_thread == RT_NULL)
  229. {
  230. RT_KERNEL_FREE(queue);
  231. return RT_NULL;
  232. }
  233. rt_thread_startup(queue->work_thread);
  234. }
  235. return queue;
  236. }
  237. /**
  238. * @brief Destroy a work queue.
  239. *
  240. * @param queue is a pointer to the workqueue object.
  241. *
  242. * @return RT_EOK Success.
  243. */
  244. rt_err_t rt_workqueue_destroy(struct rt_workqueue *queue)
  245. {
  246. RT_ASSERT(queue != RT_NULL);
  247. rt_workqueue_cancel_all_work(queue);
  248. rt_thread_delete(queue->work_thread);
  249. rt_sem_detach(&(queue->sem));
  250. RT_KERNEL_FREE(queue);
  251. return RT_EOK;
  252. }
  253. /**
  254. * @brief Submit a work item to the work queue without delay.
  255. *
  256. * @param queue is a pointer to the workqueue object.
  257. *
  258. * @param work is a pointer to the work item object.
  259. *
  260. * @return RT_EOK Success.
  261. * -RT_EBUSY This work item is executing.
  262. */
  263. rt_err_t rt_workqueue_dowork(struct rt_workqueue *queue, struct rt_work *work)
  264. {
  265. RT_ASSERT(queue != RT_NULL);
  266. RT_ASSERT(work != RT_NULL);
  267. return _workqueue_submit_work(queue, work, 0);
  268. }
  269. /**
  270. * @brief Submit a work item to the work queue with a delay.
  271. *
  272. * @param queue is a pointer to the workqueue object.
  273. *
  274. * @param work is a pointer to the work item object.
  275. *
  276. * @param ticks is the delay ticks for the work item to be submitted to the work queue.
  277. *
  278. * NOTE: The max timeout tick should be no more than (RT_TICK_MAX/2 - 1)
  279. *
  280. * @return RT_EOK Success.
  281. * -RT_EBUSY This work item is executing.
  282. * -RT_ERROR The ticks parameter is invalid.
  283. */
  284. rt_err_t rt_workqueue_submit_work(struct rt_workqueue *queue, struct rt_work *work, rt_tick_t ticks)
  285. {
  286. RT_ASSERT(queue != RT_NULL);
  287. RT_ASSERT(work != RT_NULL);
  288. RT_ASSERT(ticks < RT_TICK_MAX / 2);
  289. return _workqueue_submit_work(queue, work, ticks);
  290. }
  291. /**
  292. * @brief Submit a work item to the work queue without delay. This work item will be executed after the current work item.
  293. *
  294. * @param queue is a pointer to the workqueue object.
  295. *
  296. * @param work is a pointer to the work item object.
  297. *
  298. * @return RT_EOK Success.
  299. */
  300. rt_err_t rt_workqueue_urgent_work(struct rt_workqueue *queue, struct rt_work *work)
  301. {
  302. rt_base_t level;
  303. RT_ASSERT(queue != RT_NULL);
  304. RT_ASSERT(work != RT_NULL);
  305. level = rt_hw_interrupt_disable();
  306. /* NOTE: the work MUST be initialized firstly */
  307. rt_list_remove(&(work->list));
  308. rt_list_insert_after(&queue->work_list, &(work->list));
  309. /* whether the workqueue is doing work */
  310. if (queue->work_current == RT_NULL &&
  311. ((queue->work_thread->stat & RT_THREAD_SUSPEND_MASK) == RT_THREAD_SUSPEND_MASK))
  312. {
  313. /* resume work thread */
  314. rt_thread_resume(queue->work_thread);
  315. rt_hw_interrupt_enable(level);
  316. rt_schedule();
  317. }
  318. else
  319. {
  320. rt_hw_interrupt_enable(level);
  321. }
  322. return RT_EOK;
  323. }
  324. /**
  325. * @brief Cancel a work item in the work queue.
  326. *
  327. * @param queue is a pointer to the workqueue object.
  328. *
  329. * @param work is a pointer to the work item object.
  330. *
  331. * @return RT_EOK Success.
  332. * -RT_EBUSY This work item is executing.
  333. */
  334. rt_err_t rt_workqueue_cancel_work(struct rt_workqueue *queue, struct rt_work *work)
  335. {
  336. RT_ASSERT(work != RT_NULL);
  337. RT_ASSERT(queue != RT_NULL);
  338. return _workqueue_cancel_work(queue, work);
  339. }
  340. /**
  341. * @brief Cancel a work item in the work queue. If the work item is executing, this function will block until it is done.
  342. *
  343. * @param queue is a pointer to the workqueue object.
  344. *
  345. * @param work is a pointer to the work item object.
  346. *
  347. * @return RT_EOK Success.
  348. */
  349. rt_err_t rt_workqueue_cancel_work_sync(struct rt_workqueue *queue, struct rt_work *work)
  350. {
  351. RT_ASSERT(queue != RT_NULL);
  352. RT_ASSERT(work != RT_NULL);
  353. if (queue->work_current == work) /* it's current work in the queue */
  354. {
  355. /* wait for work completion */
  356. rt_sem_take(&(queue->sem), RT_WAITING_FOREVER);
  357. }
  358. else
  359. {
  360. _workqueue_cancel_work(queue, work);
  361. }
  362. return RT_EOK;
  363. }
  364. /**
  365. * @brief This function will cancel all work items in work queue.
  366. *
  367. * @param queue is a pointer to the workqueue object.
  368. *
  369. * @return RT_EOK Success.
  370. */
  371. rt_err_t rt_workqueue_cancel_all_work(struct rt_workqueue *queue)
  372. {
  373. struct rt_work *work;
  374. RT_ASSERT(queue != RT_NULL);
  375. /* cancel work */
  376. rt_enter_critical();
  377. while (rt_list_isempty(&queue->work_list) == RT_FALSE)
  378. {
  379. work = rt_list_first_entry(&queue->work_list, struct rt_work, list);
  380. _workqueue_cancel_work(queue, work);
  381. }
  382. /* cancel delay work */
  383. while (rt_list_isempty(&queue->delayed_list) == RT_FALSE)
  384. {
  385. work = rt_list_first_entry(&queue->delayed_list, struct rt_work, list);
  386. _workqueue_cancel_work(queue, work);
  387. }
  388. rt_exit_critical();
  389. return RT_EOK;
  390. }
  391. #ifdef RT_USING_SYSTEM_WORKQUEUE
  392. static struct rt_workqueue *sys_workq; /* system work queue */
  393. /**
  394. * @brief Submit a work item to the system work queue with a delay.
  395. *
  396. * @param work is a pointer to the work item object.
  397. *
  398. * @param ticks is the delay OS ticks for the work item to be submitted to the work queue.
  399. *
  400. * NOTE: The max timeout tick should be no more than (RT_TICK_MAX/2 - 1)
  401. *
  402. * @return RT_EOK Success.
  403. * -RT_EBUSY This work item is executing.
  404. * -RT_ERROR The ticks parameter is invalid.
  405. */
  406. rt_err_t rt_work_submit(struct rt_work *work, rt_tick_t ticks)
  407. {
  408. return rt_workqueue_submit_work(sys_workq, work, ticks);
  409. }
  410. /**
  411. * @brief Submit a work item to the system work queue without delay. This work item will be executed after the current work item.
  412. *
  413. * @param work is a pointer to the work item object.
  414. *
  415. * @return RT_EOK Success.
  416. */
  417. rt_err_t rt_work_urgent(struct rt_work *work)
  418. {
  419. return rt_workqueue_urgent_work(sys_workq, work);
  420. }
  421. /**
  422. * @brief Cancel a work item in the system work queue.
  423. *
  424. * @param work is a pointer to the work item object.
  425. *
  426. * @return RT_EOK Success.
  427. * -RT_EBUSY This work item is executing.
  428. */
  429. rt_err_t rt_work_cancel(struct rt_work *work)
  430. {
  431. return rt_workqueue_cancel_work(sys_workq, work);
  432. }
  433. static int rt_work_sys_workqueue_init(void)
  434. {
  435. if (sys_workq != RT_NULL)
  436. return RT_EOK;
  437. sys_workq = rt_workqueue_create("sys workq", RT_SYSTEM_WORKQUEUE_STACKSIZE,
  438. RT_SYSTEM_WORKQUEUE_PRIORITY);
  439. RT_ASSERT(sys_workq != RT_NULL);
  440. return RT_EOK;
  441. }
  442. INIT_PREV_EXPORT(rt_work_sys_workqueue_init);
  443. #endif /* RT_USING_SYSTEM_WORKQUEUE */
  444. #endif /* RT_USING_HEAP */