workqueue.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510
  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(rt_thread_self());
  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. rt_err_t err;
  84. level = rt_hw_interrupt_disable();
  85. /* remove list */
  86. rt_list_remove(&(work->list));
  87. work->flags &= ~RT_WORK_STATE_PENDING;
  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 Initialize a work item, binding with a callback function.
  194. *
  195. * @param work is a pointer to the work item object.
  196. *
  197. * @param work_func is a callback function that will be called when this work item is executed.
  198. *
  199. * @param work_data is a user data passed to the callback function as the second parameter.
  200. */
  201. void rt_work_init(struct rt_work *work,
  202. void (*work_func)(struct rt_work *work, void *work_data),
  203. void *work_data)
  204. {
  205. RT_ASSERT(work != RT_NULL);
  206. RT_ASSERT(work_func != RT_NULL);
  207. rt_list_init(&(work->list));
  208. work->work_func = work_func;
  209. work->work_data = work_data;
  210. work->workqueue = RT_NULL;
  211. work->flags = 0;
  212. work->type = 0;
  213. }
  214. /**
  215. * @brief Create a work queue with a thread inside.
  216. *
  217. * @param name is a name of the work queue thread.
  218. *
  219. * @param stack_size is stack size of the work queue thread.
  220. *
  221. * @param priority is a priority of the work queue thread.
  222. *
  223. * @return Return a pointer to the workqueue object. It will return RT_NULL if failed.
  224. */
  225. struct rt_workqueue *rt_workqueue_create(const char *name, rt_uint16_t stack_size, rt_uint8_t priority)
  226. {
  227. struct rt_workqueue *queue = RT_NULL;
  228. queue = (struct rt_workqueue *)RT_KERNEL_MALLOC(sizeof(struct rt_workqueue));
  229. if (queue != RT_NULL)
  230. {
  231. /* initialize work list */
  232. rt_list_init(&(queue->work_list));
  233. rt_list_init(&(queue->delayed_list));
  234. queue->work_current = RT_NULL;
  235. rt_sem_init(&(queue->sem), "wqueue", 0, RT_IPC_FLAG_FIFO);
  236. /* create the work thread */
  237. queue->work_thread = rt_thread_create(name, _workqueue_thread_entry, queue, stack_size, priority, 10);
  238. if (queue->work_thread == RT_NULL)
  239. {
  240. RT_KERNEL_FREE(queue);
  241. return RT_NULL;
  242. }
  243. rt_thread_startup(queue->work_thread);
  244. }
  245. return queue;
  246. }
  247. /**
  248. * @brief Destroy a work queue.
  249. *
  250. * @param queue is a pointer to the workqueue object.
  251. *
  252. * @return RT_EOK Success.
  253. */
  254. rt_err_t rt_workqueue_destroy(struct rt_workqueue *queue)
  255. {
  256. RT_ASSERT(queue != RT_NULL);
  257. rt_workqueue_cancel_all_work(queue);
  258. rt_thread_delete(queue->work_thread);
  259. rt_sem_detach(&(queue->sem));
  260. RT_KERNEL_FREE(queue);
  261. return RT_EOK;
  262. }
  263. /**
  264. * @brief Submit a work item to the work queue without delay.
  265. *
  266. * @param queue is a pointer to the workqueue object.
  267. *
  268. * @param work is a pointer to the work item object.
  269. *
  270. * @return RT_EOK Success.
  271. * -RT_EBUSY This work item is executing.
  272. */
  273. rt_err_t rt_workqueue_dowork(struct rt_workqueue *queue, struct rt_work *work)
  274. {
  275. RT_ASSERT(queue != RT_NULL);
  276. RT_ASSERT(work != RT_NULL);
  277. return _workqueue_submit_work(queue, work, 0);
  278. }
  279. /**
  280. * @brief Submit a work item to the work queue with a delay.
  281. *
  282. * @param queue is a pointer to the workqueue object.
  283. *
  284. * @param work is a pointer to the work item object.
  285. *
  286. * @param ticks is the delay ticks for the work item to be submitted to the work queue.
  287. *
  288. * NOTE: The max timeout tick should be no more than (RT_TICK_MAX/2 - 1)
  289. *
  290. * @return RT_EOK Success.
  291. * -RT_EBUSY This work item is executing.
  292. * -RT_ERROR The ticks parameter is invalid.
  293. */
  294. rt_err_t rt_workqueue_submit_work(struct rt_workqueue *queue, struct rt_work *work, rt_tick_t ticks)
  295. {
  296. RT_ASSERT(queue != RT_NULL);
  297. RT_ASSERT(work != RT_NULL);
  298. RT_ASSERT(ticks < RT_TICK_MAX / 2);
  299. return _workqueue_submit_work(queue, work, ticks);
  300. }
  301. /**
  302. * @brief Submit a work item to the work queue without delay. This work item will be executed after the current work item.
  303. *
  304. * @param queue is a pointer to the workqueue object.
  305. *
  306. * @param work is a pointer to the work item object.
  307. *
  308. * @return RT_EOK Success.
  309. */
  310. rt_err_t rt_workqueue_urgent_work(struct rt_workqueue *queue, struct rt_work *work)
  311. {
  312. rt_base_t level;
  313. RT_ASSERT(queue != RT_NULL);
  314. RT_ASSERT(work != RT_NULL);
  315. level = rt_hw_interrupt_disable();
  316. /* NOTE: the work MUST be initialized firstly */
  317. rt_list_remove(&(work->list));
  318. rt_list_insert_after(&queue->work_list, &(work->list));
  319. /* whether the workqueue is doing work */
  320. if (queue->work_current == RT_NULL &&
  321. ((queue->work_thread->stat & RT_THREAD_STAT_MASK) == RT_THREAD_SUSPEND))
  322. {
  323. /* resume work thread */
  324. rt_thread_resume(queue->work_thread);
  325. rt_hw_interrupt_enable(level);
  326. rt_schedule();
  327. }
  328. else
  329. {
  330. rt_hw_interrupt_enable(level);
  331. }
  332. return RT_EOK;
  333. }
  334. /**
  335. * @brief Cancel a work item in the work queue.
  336. *
  337. * @param queue is a pointer to the workqueue object.
  338. *
  339. * @param work is a pointer to the work item object.
  340. *
  341. * @return RT_EOK Success.
  342. * -RT_EBUSY This work item is executing.
  343. */
  344. rt_err_t rt_workqueue_cancel_work(struct rt_workqueue *queue, struct rt_work *work)
  345. {
  346. RT_ASSERT(work != RT_NULL);
  347. RT_ASSERT(queue != RT_NULL);
  348. return _workqueue_cancel_work(queue, work);
  349. }
  350. /**
  351. * @brief Cancel a work item in the work queue. If the work item is executing, this function will block until it is done.
  352. *
  353. * @param queue is a pointer to the workqueue object.
  354. *
  355. * @param work is a pointer to the work item object.
  356. *
  357. * @return RT_EOK Success.
  358. */
  359. rt_err_t rt_workqueue_cancel_work_sync(struct rt_workqueue *queue, struct rt_work *work)
  360. {
  361. RT_ASSERT(queue != RT_NULL);
  362. RT_ASSERT(work != RT_NULL);
  363. if (queue->work_current == work) /* it's current work in the queue */
  364. {
  365. /* wait for work completion */
  366. rt_sem_take(&(queue->sem), RT_WAITING_FOREVER);
  367. }
  368. else
  369. {
  370. _workqueue_cancel_work(queue, work);
  371. }
  372. return RT_EOK;
  373. }
  374. /**
  375. * @brief This function will cancel all work items in work queue.
  376. *
  377. * @param queue is a pointer to the workqueue object.
  378. *
  379. * @return RT_EOK Success.
  380. */
  381. rt_err_t rt_workqueue_cancel_all_work(struct rt_workqueue *queue)
  382. {
  383. struct rt_work *work;
  384. RT_ASSERT(queue != RT_NULL);
  385. /* cancel work */
  386. rt_enter_critical();
  387. while (rt_list_isempty(&queue->work_list) == RT_FALSE)
  388. {
  389. work = rt_list_first_entry(&queue->work_list, struct rt_work, list);
  390. _workqueue_cancel_work(queue, work);
  391. }
  392. /* cancel delay work */
  393. while (rt_list_isempty(&queue->delayed_list) == RT_FALSE)
  394. {
  395. work = rt_list_first_entry(&queue->delayed_list, struct rt_work, list);
  396. _workqueue_cancel_work(queue, work);
  397. }
  398. rt_exit_critical();
  399. return RT_EOK;
  400. }
  401. #ifdef RT_USING_SYSTEM_WORKQUEUE
  402. static struct rt_workqueue *sys_workq; /* system work queue */
  403. /**
  404. * @brief Submit a work item to the system work queue with a delay.
  405. *
  406. * @param work is a pointer to the work item object.
  407. *
  408. * @param ticks is the delay OS ticks for the work item to be submitted to the work queue.
  409. *
  410. * NOTE: The max timeout tick should be no more than (RT_TICK_MAX/2 - 1)
  411. *
  412. * @return RT_EOK Success.
  413. * -RT_EBUSY This work item is executing.
  414. * -RT_ERROR The ticks parameter is invalid.
  415. */
  416. rt_err_t rt_work_submit(struct rt_work *work, rt_tick_t ticks)
  417. {
  418. return rt_workqueue_submit_work(sys_workq, work, ticks);
  419. }
  420. /**
  421. * @brief Submit a work item to the system work queue without delay. This work item will be executed after the current work item.
  422. *
  423. * @param work is a pointer to the work item object.
  424. *
  425. * @return RT_EOK Success.
  426. */
  427. rt_err_t rt_work_urgent(struct rt_work *work)
  428. {
  429. return rt_workqueue_urgent_work(sys_workq, work);
  430. }
  431. /**
  432. * @brief Cancel a work item in the system work queue.
  433. *
  434. * @param work is a pointer to the work item object.
  435. *
  436. * @return RT_EOK Success.
  437. * -RT_EBUSY This work item is executing.
  438. */
  439. rt_err_t rt_work_cancel(struct rt_work *work)
  440. {
  441. return rt_workqueue_cancel_work(sys_workq, work);
  442. }
  443. static int rt_work_sys_workqueue_init(void)
  444. {
  445. if (sys_workq != RT_NULL)
  446. return RT_EOK;
  447. sys_workq = rt_workqueue_create("sys workq", RT_SYSTEM_WORKQUEUE_STACKSIZE,
  448. RT_SYSTEM_WORKQUEUE_PRIORITY);
  449. RT_ASSERT(sys_workq != RT_NULL);
  450. return RT_EOK;
  451. }
  452. INIT_PREV_EXPORT(rt_work_sys_workqueue_init);
  453. #endif /* RT_USING_SYSTEM_WORKQUEUE */
  454. #endif /* RT_USING_HEAP */