workqueue.c 13 KB

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