workqueue.h 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. /*
  2. * Copyright (c) 2006-2021, RT-Thread Development Team
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. *
  6. * Change Logs:
  7. * Date Author Notes
  8. * 2021-08-01 Meco Man remove rt_delayed_work_init() and rt_delayed_work structure
  9. * 2021-08-14 Jackistang add commets for rt_work_init()
  10. */
  11. #ifndef WORKQUEUE_H__
  12. #define WORKQUEUE_H__
  13. #include <rtthread.h>
  14. enum
  15. {
  16. RT_WORK_STATE_PENDING = 0x0001, /* Work item pending state */
  17. RT_WORK_STATE_SUBMITTING = 0x0002, /* Work item submitting state */
  18. };
  19. /**
  20. * work type defitions
  21. */
  22. enum
  23. {
  24. RT_WORK_TYPE_DELAYED = 0x0001,
  25. };
  26. /* workqueue implementation */
  27. struct rt_workqueue
  28. {
  29. rt_list_t work_list;
  30. rt_list_t delayed_list;
  31. struct rt_work *work_current; /* current work */
  32. struct rt_semaphore sem;
  33. rt_thread_t work_thread;
  34. };
  35. struct rt_work
  36. {
  37. rt_list_t list;
  38. void (*work_func)(struct rt_work *work, void *work_data);
  39. void *work_data;
  40. rt_uint16_t flags;
  41. rt_uint16_t type;
  42. struct rt_timer timer;
  43. struct rt_workqueue *workqueue;
  44. };
  45. #ifdef RT_USING_HEAP
  46. /**
  47. * WorkQueue for DeviceDriver
  48. */
  49. struct rt_workqueue *rt_workqueue_create(const char *name, rt_uint16_t stack_size, rt_uint8_t priority);
  50. rt_err_t rt_workqueue_destroy(struct rt_workqueue *queue);
  51. rt_err_t rt_workqueue_dowork(struct rt_workqueue *queue, struct rt_work *work);
  52. rt_err_t rt_workqueue_submit_work(struct rt_workqueue *queue, struct rt_work *work, rt_tick_t time);
  53. rt_err_t rt_workqueue_cancel_work(struct rt_workqueue *queue, struct rt_work *work);
  54. rt_err_t rt_workqueue_cancel_work_sync(struct rt_workqueue *queue, struct rt_work *work);
  55. rt_err_t rt_workqueue_cancel_all_work(struct rt_workqueue *queue);
  56. rt_err_t rt_workqueue_critical_work(struct rt_workqueue *queue, struct rt_work *work);
  57. #ifdef RT_USING_SYSTEM_WORKQUEUE
  58. rt_err_t rt_work_submit(struct rt_work *work, rt_tick_t time);
  59. rt_err_t rt_work_cancel(struct rt_work *work);
  60. #endif /* RT_USING_SYSTEM_WORKQUEUE */
  61. /**
  62. * @brief Init a work item, and bind it with a callback function.
  63. *
  64. * @param work A pointer to work item object.
  65. * @param work_func A callback function will be called when this work item is being executed.
  66. * @param work_data A user data passed to the callback function as it's second parameter.
  67. */
  68. rt_inline void rt_work_init(struct rt_work *work, void (*work_func)(struct rt_work *work, void *work_data),
  69. void *work_data)
  70. {
  71. rt_list_init(&(work->list));
  72. work->work_func = work_func;
  73. work->work_data = work_data;
  74. work->workqueue = RT_NULL;
  75. work->flags = 0;
  76. work->type = 0;
  77. }
  78. #endif /* RT_USING_HEAP */
  79. #endif