workqueue.c 14 KB

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