workqueue.c 14 KB

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