2017-02-27 00:58:11 +08:00
|
|
|
/*
|
2022-01-17 04:05:50 +08:00
|
|
|
* Copyright (c) 2006-2022, RT-Thread Development Team
|
2017-02-27 00:58:11 +08:00
|
|
|
*
|
2018-10-14 19:37:18 +08:00
|
|
|
* SPDX-License-Identifier: Apache-2.0
|
2017-02-27 00:58:11 +08:00
|
|
|
*
|
|
|
|
* Change Logs:
|
|
|
|
* Date Author Notes
|
2021-08-01 16:53:26 +08:00
|
|
|
* 2017-02-27 Bernard fix the re-work issue.
|
|
|
|
* 2021-08-01 Meco Man remove rt_delayed_work_init()
|
2022-01-17 05:00:55 +08:00
|
|
|
* 2021-08-14 Jackistang add comments for function interface
|
|
|
|
* 2022-01-16 Meco Man add rt_work_urgent()
|
2017-02-27 00:58:11 +08:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include <rthw.h>
|
2014-07-13 07:27:57 +08:00
|
|
|
#include <rtthread.h>
|
|
|
|
#include <rtdevice.h>
|
|
|
|
|
|
|
|
#ifdef RT_USING_HEAP
|
2019-03-17 16:01:12 +08:00
|
|
|
|
2019-04-22 11:41:17 +08:00
|
|
|
static void _delayed_work_timeout_handler(void *parameter);
|
|
|
|
|
2017-10-17 17:53:01 +08:00
|
|
|
rt_inline rt_err_t _workqueue_work_completion(struct rt_workqueue *queue)
|
|
|
|
{
|
|
|
|
rt_err_t result;
|
2019-03-17 16:01:12 +08:00
|
|
|
|
|
|
|
rt_enter_critical();
|
2017-10-17 17:53:01 +08:00
|
|
|
while (1)
|
|
|
|
{
|
|
|
|
/* try to take condition semaphore */
|
|
|
|
result = rt_sem_trytake(&(queue->sem));
|
|
|
|
if (result == -RT_ETIMEOUT)
|
|
|
|
{
|
|
|
|
/* it's timeout, release this semaphore */
|
|
|
|
rt_sem_release(&(queue->sem));
|
|
|
|
}
|
|
|
|
else if (result == RT_EOK)
|
|
|
|
{
|
|
|
|
/* keep the sem value = 0 */
|
|
|
|
result = RT_EOK;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
result = -RT_ERROR;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
rt_exit_critical();
|
2019-03-17 16:01:12 +08:00
|
|
|
|
2017-10-17 17:53:01 +08:00
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2019-03-17 16:01:12 +08:00
|
|
|
static void _workqueue_thread_entry(void *parameter)
|
2014-07-13 07:27:57 +08:00
|
|
|
{
|
2017-02-27 00:58:11 +08:00
|
|
|
rt_base_t level;
|
2019-03-17 16:01:12 +08:00
|
|
|
struct rt_work *work;
|
|
|
|
struct rt_workqueue *queue;
|
2017-02-27 00:58:11 +08:00
|
|
|
|
2019-03-17 16:01:12 +08:00
|
|
|
queue = (struct rt_workqueue *) parameter;
|
2017-02-27 00:58:11 +08:00
|
|
|
RT_ASSERT(queue != RT_NULL);
|
|
|
|
|
|
|
|
while (1)
|
|
|
|
{
|
2021-02-06 21:54:25 +08:00
|
|
|
level = rt_hw_interrupt_disable();
|
2017-02-27 00:58:11 +08:00
|
|
|
if (rt_list_isempty(&(queue->work_list)))
|
|
|
|
{
|
|
|
|
/* no software timer exist, suspend self. */
|
|
|
|
rt_thread_suspend(rt_thread_self());
|
2021-02-22 17:10:33 +08:00
|
|
|
rt_hw_interrupt_enable(level);
|
2017-02-27 00:58:11 +08:00
|
|
|
rt_schedule();
|
2021-02-06 21:54:25 +08:00
|
|
|
continue;
|
2017-02-27 00:58:11 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/* we have work to do with. */
|
|
|
|
work = rt_list_entry(queue->work_list.next, struct rt_work, list);
|
|
|
|
rt_list_remove(&(work->list));
|
|
|
|
queue->work_current = work;
|
2019-03-17 16:01:12 +08:00
|
|
|
work->flags &= ~RT_WORK_STATE_PENDING;
|
2019-08-05 14:18:15 +08:00
|
|
|
work->workqueue = RT_NULL;
|
2017-02-27 00:58:11 +08:00
|
|
|
rt_hw_interrupt_enable(level);
|
|
|
|
|
|
|
|
/* do work */
|
|
|
|
work->work_func(work, work->work_data);
|
|
|
|
/* clean current work */
|
|
|
|
queue->work_current = RT_NULL;
|
2017-10-17 17:53:01 +08:00
|
|
|
|
|
|
|
/* ack work completion */
|
|
|
|
_workqueue_work_completion(queue);
|
2017-02-27 00:58:11 +08:00
|
|
|
}
|
2014-07-13 07:27:57 +08:00
|
|
|
}
|
|
|
|
|
2021-01-30 18:21:33 +08:00
|
|
|
static rt_err_t _workqueue_submit_work(struct rt_workqueue *queue,
|
|
|
|
struct rt_work *work, rt_tick_t ticks)
|
2019-03-17 16:01:12 +08:00
|
|
|
{
|
|
|
|
rt_base_t level;
|
2021-02-07 10:30:25 +08:00
|
|
|
rt_err_t err;
|
2019-03-17 16:01:12 +08:00
|
|
|
|
|
|
|
level = rt_hw_interrupt_disable();
|
2021-01-30 18:21:33 +08:00
|
|
|
/* remove list */
|
2019-03-17 16:01:12 +08:00
|
|
|
rt_list_remove(&(work->list));
|
2021-01-30 18:21:33 +08:00
|
|
|
work->flags &= ~RT_WORK_STATE_PENDING;
|
2022-01-17 03:33:41 +08:00
|
|
|
|
2021-01-30 18:21:33 +08:00
|
|
|
if (ticks == 0)
|
2019-03-17 16:01:12 +08:00
|
|
|
{
|
2021-01-30 18:21:33 +08:00
|
|
|
if (queue->work_current != work)
|
|
|
|
{
|
|
|
|
rt_list_insert_after(queue->work_list.prev, &(work->list));
|
|
|
|
work->flags |= RT_WORK_STATE_PENDING;
|
|
|
|
work->workqueue = queue;
|
2021-02-07 10:30:25 +08:00
|
|
|
err = RT_EOK;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
err = -RT_EBUSY;
|
2021-01-30 18:21:33 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/* whether the workqueue is doing work */
|
|
|
|
if (queue->work_current == RT_NULL &&
|
|
|
|
((queue->work_thread->stat & RT_THREAD_STAT_MASK) == RT_THREAD_SUSPEND))
|
|
|
|
{
|
|
|
|
/* resume work thread */
|
|
|
|
rt_thread_resume(queue->work_thread);
|
2021-02-22 17:10:33 +08:00
|
|
|
rt_hw_interrupt_enable(level);
|
2021-01-30 18:21:33 +08:00
|
|
|
rt_schedule();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
rt_hw_interrupt_enable(level);
|
|
|
|
}
|
2021-02-07 10:30:25 +08:00
|
|
|
return err;
|
2019-03-17 16:01:12 +08:00
|
|
|
}
|
2021-01-30 18:21:33 +08:00
|
|
|
else if (ticks < RT_TICK_MAX / 2)
|
2019-03-17 16:01:12 +08:00
|
|
|
{
|
2021-01-30 18:21:33 +08:00
|
|
|
/* Timer started */
|
|
|
|
if (work->flags & RT_WORK_STATE_SUBMITTING)
|
|
|
|
{
|
|
|
|
rt_timer_stop(&work->timer);
|
|
|
|
rt_timer_control(&work->timer, RT_TIMER_CTRL_SET_TIME, &ticks);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
rt_timer_init(&(work->timer), "work", _delayed_work_timeout_handler,
|
|
|
|
work, ticks, RT_TIMER_FLAG_ONE_SHOT | RT_TIMER_FLAG_SOFT_TIMER);
|
|
|
|
work->flags |= RT_WORK_STATE_SUBMITTING;
|
|
|
|
}
|
|
|
|
work->workqueue = queue;
|
2021-02-06 20:08:31 +08:00
|
|
|
/* insert delay work list */
|
|
|
|
rt_list_insert_after(queue->delayed_list.prev, &(work->list));
|
2019-03-17 16:01:12 +08:00
|
|
|
rt_hw_interrupt_enable(level);
|
2021-01-30 18:21:33 +08:00
|
|
|
rt_timer_start(&(work->timer));
|
|
|
|
return RT_EOK;
|
2019-03-17 16:01:12 +08:00
|
|
|
}
|
2021-01-30 18:21:33 +08:00
|
|
|
rt_hw_interrupt_enable(level);
|
|
|
|
return -RT_ERROR;
|
2019-03-17 16:01:12 +08:00
|
|
|
}
|
|
|
|
|
2021-02-06 20:39:52 +08:00
|
|
|
static rt_err_t _workqueue_cancel_work(struct rt_workqueue *queue, struct rt_work *work)
|
2019-03-17 16:01:12 +08:00
|
|
|
{
|
|
|
|
rt_base_t level;
|
2021-02-06 20:39:52 +08:00
|
|
|
rt_err_t err;
|
2019-03-17 16:01:12 +08:00
|
|
|
|
|
|
|
level = rt_hw_interrupt_disable();
|
|
|
|
rt_list_remove(&(work->list));
|
|
|
|
work->flags &= ~RT_WORK_STATE_PENDING;
|
2021-01-30 18:29:17 +08:00
|
|
|
/* Timer started */
|
|
|
|
if (work->flags & RT_WORK_STATE_SUBMITTING)
|
2019-03-17 16:01:12 +08:00
|
|
|
{
|
2021-01-30 18:29:17 +08:00
|
|
|
rt_timer_stop(&(work->timer));
|
|
|
|
rt_timer_detach(&(work->timer));
|
|
|
|
work->flags &= ~RT_WORK_STATE_SUBMITTING;
|
2019-03-17 16:01:12 +08:00
|
|
|
}
|
2021-02-06 20:39:52 +08:00
|
|
|
err = queue->work_current != work ? RT_EOK : -RT_EBUSY;
|
2019-03-17 16:01:12 +08:00
|
|
|
work->workqueue = RT_NULL;
|
|
|
|
rt_hw_interrupt_enable(level);
|
2021-02-06 20:39:52 +08:00
|
|
|
return err;
|
2019-03-17 16:01:12 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static void _delayed_work_timeout_handler(void *parameter)
|
|
|
|
{
|
2021-02-01 14:26:47 +08:00
|
|
|
struct rt_work *work;
|
|
|
|
struct rt_workqueue *queue;
|
2019-04-22 11:41:17 +08:00
|
|
|
rt_base_t level;
|
2019-03-17 16:01:12 +08:00
|
|
|
|
2021-02-01 14:26:47 +08:00
|
|
|
work = (struct rt_work *)parameter;
|
|
|
|
queue = work->workqueue;
|
|
|
|
RT_ASSERT(queue != RT_NULL);
|
|
|
|
|
2019-04-22 11:41:17 +08:00
|
|
|
level = rt_hw_interrupt_disable();
|
2021-02-01 14:26:47 +08:00
|
|
|
rt_timer_detach(&(work->timer));
|
|
|
|
work->flags &= ~RT_WORK_STATE_SUBMITTING;
|
2021-02-06 20:08:31 +08:00
|
|
|
/* remove delay list */
|
|
|
|
rt_list_remove(&(work->list));
|
2021-02-01 14:26:47 +08:00
|
|
|
/* insert work queue */
|
|
|
|
if (queue->work_current != work)
|
|
|
|
{
|
|
|
|
rt_list_insert_after(queue->work_list.prev, &(work->list));
|
|
|
|
work->flags |= RT_WORK_STATE_PENDING;
|
|
|
|
}
|
|
|
|
/* whether the workqueue is doing work */
|
|
|
|
if (queue->work_current == RT_NULL &&
|
|
|
|
((queue->work_thread->stat & RT_THREAD_STAT_MASK) == RT_THREAD_SUSPEND))
|
|
|
|
{
|
|
|
|
/* resume work thread */
|
|
|
|
rt_thread_resume(queue->work_thread);
|
2021-02-22 17:10:33 +08:00
|
|
|
rt_hw_interrupt_enable(level);
|
2021-02-01 14:26:47 +08:00
|
|
|
rt_schedule();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
rt_hw_interrupt_enable(level);
|
|
|
|
}
|
2019-03-17 16:01:12 +08:00
|
|
|
}
|
|
|
|
|
2022-01-17 04:15:08 +08:00
|
|
|
/**
|
|
|
|
* @brief Initialize a work item, binding with a callback function.
|
|
|
|
*
|
|
|
|
* @param work is a pointer to the work item object.
|
|
|
|
*
|
|
|
|
* @param work_func is a callback function that will be called when this work item is executed.
|
|
|
|
*
|
|
|
|
* @param work_data is a user data passed to the callback function as the second parameter.
|
|
|
|
*/
|
|
|
|
void rt_work_init(struct rt_work *work,
|
|
|
|
void (*work_func)(struct rt_work *work, void *work_data),
|
|
|
|
void *work_data)
|
|
|
|
{
|
|
|
|
RT_ASSERT(work != RT_NULL);
|
|
|
|
RT_ASSERT(work_func != RT_NULL);
|
|
|
|
|
|
|
|
rt_list_init(&(work->list));
|
|
|
|
work->work_func = work_func;
|
|
|
|
work->work_data = work_data;
|
|
|
|
work->workqueue = RT_NULL;
|
|
|
|
work->flags = 0;
|
|
|
|
work->type = 0;
|
|
|
|
}
|
|
|
|
|
2021-08-14 11:10:43 +08:00
|
|
|
/**
|
2021-08-18 09:44:52 +08:00
|
|
|
* @brief Create a work queue with a thread inside.
|
2021-08-14 14:32:58 +08:00
|
|
|
*
|
2022-01-17 04:05:50 +08:00
|
|
|
* @param name is a name of the work queue thread.
|
2021-08-14 14:32:58 +08:00
|
|
|
*
|
2022-01-17 04:05:50 +08:00
|
|
|
* @param stack_size is stack size of the work queue thread.
|
|
|
|
*
|
|
|
|
* @param priority is a priority of the work queue thread.
|
|
|
|
*
|
|
|
|
* @return Return a pointer to the workqueue object. It will return RT_NULL if failed.
|
2021-08-14 11:10:43 +08:00
|
|
|
*/
|
2019-03-17 16:01:12 +08:00
|
|
|
struct rt_workqueue *rt_workqueue_create(const char *name, rt_uint16_t stack_size, rt_uint8_t priority)
|
2014-07-13 07:27:57 +08:00
|
|
|
{
|
2017-02-27 00:58:11 +08:00
|
|
|
struct rt_workqueue *queue = RT_NULL;
|
2014-08-04 16:40:40 +08:00
|
|
|
|
2019-03-17 16:01:12 +08:00
|
|
|
queue = (struct rt_workqueue *)RT_KERNEL_MALLOC(sizeof(struct rt_workqueue));
|
2017-02-27 00:58:11 +08:00
|
|
|
if (queue != RT_NULL)
|
|
|
|
{
|
2014-07-13 07:27:57 +08:00
|
|
|
/* initialize work list */
|
|
|
|
rt_list_init(&(queue->work_list));
|
2021-02-06 20:08:31 +08:00
|
|
|
rt_list_init(&(queue->delayed_list));
|
2017-02-27 00:58:11 +08:00
|
|
|
queue->work_current = RT_NULL;
|
2017-10-17 17:53:01 +08:00
|
|
|
rt_sem_init(&(queue->sem), "wqueue", 0, RT_IPC_FLAG_FIFO);
|
2017-02-27 00:58:11 +08:00
|
|
|
|
|
|
|
/* create the work thread */
|
|
|
|
queue->work_thread = rt_thread_create(name, _workqueue_thread_entry, queue, stack_size, priority, 10);
|
|
|
|
if (queue->work_thread == RT_NULL)
|
|
|
|
{
|
|
|
|
RT_KERNEL_FREE(queue);
|
|
|
|
return RT_NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
rt_thread_startup(queue->work_thread);
|
|
|
|
}
|
|
|
|
|
|
|
|
return queue;
|
2014-07-13 07:27:57 +08:00
|
|
|
}
|
|
|
|
|
2021-08-14 11:10:43 +08:00
|
|
|
/**
|
|
|
|
* @brief Destroy a work queue.
|
2021-08-14 14:32:58 +08:00
|
|
|
*
|
2022-01-17 04:05:50 +08:00
|
|
|
* @param queue is a pointer to the workqueue object.
|
2021-08-14 14:32:58 +08:00
|
|
|
*
|
2022-01-17 04:05:50 +08:00
|
|
|
* @return RT_EOK Success.
|
2021-08-14 11:10:43 +08:00
|
|
|
*/
|
2019-03-17 16:01:12 +08:00
|
|
|
rt_err_t rt_workqueue_destroy(struct rt_workqueue *queue)
|
2014-07-13 07:27:57 +08:00
|
|
|
{
|
2017-02-27 00:58:11 +08:00
|
|
|
RT_ASSERT(queue != RT_NULL);
|
2014-07-13 07:27:57 +08:00
|
|
|
|
2021-02-06 20:39:52 +08:00
|
|
|
rt_workqueue_cancel_all_work(queue);
|
2017-02-27 00:58:11 +08:00
|
|
|
rt_thread_delete(queue->work_thread);
|
2021-02-06 20:15:50 +08:00
|
|
|
rt_sem_detach(&(queue->sem));
|
2017-02-27 00:58:11 +08:00
|
|
|
RT_KERNEL_FREE(queue);
|
2014-07-13 07:27:57 +08:00
|
|
|
|
2017-02-27 00:58:11 +08:00
|
|
|
return RT_EOK;
|
2014-07-13 07:27:57 +08:00
|
|
|
}
|
|
|
|
|
2021-08-14 11:10:43 +08:00
|
|
|
/**
|
2021-08-18 09:44:52 +08:00
|
|
|
* @brief Submit a work item to the work queue without delay.
|
2021-08-14 14:32:58 +08:00
|
|
|
*
|
2022-01-17 04:05:50 +08:00
|
|
|
* @param queue is a pointer to the workqueue object.
|
|
|
|
*
|
|
|
|
* @param work is a pointer to the work item object.
|
2021-08-14 14:32:58 +08:00
|
|
|
*
|
2021-08-14 11:10:43 +08:00
|
|
|
* @return RT_EOK Success.
|
2022-01-17 04:05:50 +08:00
|
|
|
* -RT_EBUSY This work item is executing.
|
2021-08-14 11:10:43 +08:00
|
|
|
*/
|
2019-03-17 16:01:12 +08:00
|
|
|
rt_err_t rt_workqueue_dowork(struct rt_workqueue *queue, struct rt_work *work)
|
2014-07-13 07:27:57 +08:00
|
|
|
{
|
2017-02-27 00:58:11 +08:00
|
|
|
RT_ASSERT(queue != RT_NULL);
|
|
|
|
RT_ASSERT(work != RT_NULL);
|
|
|
|
|
2021-01-30 18:21:33 +08:00
|
|
|
return _workqueue_submit_work(queue, work, 0);
|
2019-03-17 16:01:12 +08:00
|
|
|
}
|
2017-02-27 00:58:11 +08:00
|
|
|
|
2021-08-14 11:10:43 +08:00
|
|
|
/**
|
2021-08-18 09:44:52 +08:00
|
|
|
* @brief Submit a work item to the work queue with a delay.
|
2021-08-14 14:32:58 +08:00
|
|
|
*
|
2022-01-17 04:05:50 +08:00
|
|
|
* @param queue is a pointer to the workqueue object.
|
|
|
|
*
|
|
|
|
* @param work is a pointer to the work item object.
|
|
|
|
*
|
2022-01-17 04:15:08 +08:00
|
|
|
* @param ticks is the delay ticks for the work item to be submitted to the work queue.
|
2022-01-17 04:05:50 +08:00
|
|
|
*
|
|
|
|
* NOTE: The max timeout tick should be no more than (RT_TICK_MAX/2 - 1)
|
2021-08-14 14:32:58 +08:00
|
|
|
*
|
2021-08-14 11:10:43 +08:00
|
|
|
* @return RT_EOK Success.
|
2022-01-17 04:05:50 +08:00
|
|
|
* -RT_EBUSY This work item is executing.
|
2022-01-17 04:15:08 +08:00
|
|
|
* -RT_ERROR The ticks parameter is invalid.
|
2021-08-14 11:10:43 +08:00
|
|
|
*/
|
2022-01-17 04:15:08 +08:00
|
|
|
rt_err_t rt_workqueue_submit_work(struct rt_workqueue *queue, struct rt_work *work, rt_tick_t ticks)
|
2019-03-17 16:01:12 +08:00
|
|
|
{
|
|
|
|
RT_ASSERT(queue != RT_NULL);
|
|
|
|
RT_ASSERT(work != RT_NULL);
|
2022-01-17 04:15:08 +08:00
|
|
|
RT_ASSERT(ticks < RT_TICK_MAX / 2);
|
2017-02-27 00:58:11 +08:00
|
|
|
|
2022-01-17 04:15:08 +08:00
|
|
|
return _workqueue_submit_work(queue, work, ticks);
|
2014-07-13 07:27:57 +08:00
|
|
|
}
|
|
|
|
|
2021-08-14 11:10:43 +08:00
|
|
|
/**
|
2021-08-18 22:51:00 +08:00
|
|
|
* @brief Submit a work item to the work queue without delay. This work item will be executed after the current work item.
|
2021-08-14 14:32:58 +08:00
|
|
|
*
|
2022-01-17 04:05:50 +08:00
|
|
|
* @param queue is a pointer to the workqueue object.
|
|
|
|
*
|
|
|
|
* @param work is a pointer to the work item object.
|
2021-08-14 14:32:58 +08:00
|
|
|
*
|
|
|
|
* @return RT_EOK Success.
|
2021-08-14 11:10:43 +08:00
|
|
|
*/
|
2021-10-21 03:48:07 +08:00
|
|
|
rt_err_t rt_workqueue_urgent_work(struct rt_workqueue *queue, struct rt_work *work)
|
2017-01-31 13:17:04 +08:00
|
|
|
{
|
2017-02-27 00:58:11 +08:00
|
|
|
rt_base_t level;
|
2022-01-17 04:05:50 +08:00
|
|
|
|
2017-02-27 00:58:11 +08:00
|
|
|
RT_ASSERT(queue != RT_NULL);
|
|
|
|
RT_ASSERT(work != RT_NULL);
|
|
|
|
|
|
|
|
level = rt_hw_interrupt_disable();
|
|
|
|
/* NOTE: the work MUST be initialized firstly */
|
|
|
|
rt_list_remove(&(work->list));
|
2021-02-01 14:36:18 +08:00
|
|
|
rt_list_insert_after(&queue->work_list, &(work->list));
|
|
|
|
/* whether the workqueue is doing work */
|
|
|
|
if (queue->work_current == RT_NULL &&
|
|
|
|
((queue->work_thread->stat & RT_THREAD_STAT_MASK) == RT_THREAD_SUSPEND))
|
2017-02-27 00:58:11 +08:00
|
|
|
{
|
|
|
|
/* resume work thread */
|
|
|
|
rt_thread_resume(queue->work_thread);
|
2021-02-22 17:10:33 +08:00
|
|
|
rt_hw_interrupt_enable(level);
|
2017-02-27 00:58:11 +08:00
|
|
|
rt_schedule();
|
|
|
|
}
|
2021-02-01 14:36:18 +08:00
|
|
|
else
|
|
|
|
{
|
|
|
|
rt_hw_interrupt_enable(level);
|
|
|
|
}
|
2017-02-27 00:58:11 +08:00
|
|
|
|
|
|
|
return RT_EOK;
|
2017-01-31 13:17:04 +08:00
|
|
|
}
|
|
|
|
|
2021-08-14 11:10:43 +08:00
|
|
|
/**
|
|
|
|
* @brief Cancel a work item in the work queue.
|
2021-08-14 14:32:58 +08:00
|
|
|
*
|
2022-01-17 04:05:50 +08:00
|
|
|
* @param queue is a pointer to the workqueue object.
|
|
|
|
*
|
|
|
|
* @param work is a pointer to the work item object.
|
2021-08-14 14:32:58 +08:00
|
|
|
*
|
2021-08-14 11:10:43 +08:00
|
|
|
* @return RT_EOK Success.
|
2022-01-17 04:05:50 +08:00
|
|
|
* -RT_EBUSY This work item is executing.
|
2021-08-14 11:10:43 +08:00
|
|
|
*/
|
2019-03-17 16:01:12 +08:00
|
|
|
rt_err_t rt_workqueue_cancel_work(struct rt_workqueue *queue, struct rt_work *work)
|
2014-07-13 07:27:57 +08:00
|
|
|
{
|
2017-02-27 00:58:11 +08:00
|
|
|
RT_ASSERT(work != RT_NULL);
|
2021-02-06 20:39:52 +08:00
|
|
|
RT_ASSERT(queue != RT_NULL);
|
2022-01-17 04:05:50 +08:00
|
|
|
|
2021-02-06 20:39:52 +08:00
|
|
|
return _workqueue_cancel_work(queue, work);
|
2014-07-13 07:27:57 +08:00
|
|
|
}
|
|
|
|
|
2021-08-14 11:10:43 +08:00
|
|
|
/**
|
2021-08-18 09:44:52 +08:00
|
|
|
* @brief Cancel a work item in the work queue. If the work item is executing, this function will block until it is done.
|
2021-08-14 14:32:58 +08:00
|
|
|
*
|
2022-01-17 04:05:50 +08:00
|
|
|
* @param queue is a pointer to the workqueue object.
|
|
|
|
*
|
|
|
|
* @param work is a pointer to the work item object.
|
2021-08-14 14:32:58 +08:00
|
|
|
*
|
2021-08-14 11:10:43 +08:00
|
|
|
* @return RT_EOK Success.
|
|
|
|
*/
|
2019-03-17 16:01:12 +08:00
|
|
|
rt_err_t rt_workqueue_cancel_work_sync(struct rt_workqueue *queue, struct rt_work *work)
|
2017-10-17 17:53:01 +08:00
|
|
|
{
|
|
|
|
RT_ASSERT(queue != RT_NULL);
|
|
|
|
RT_ASSERT(work != RT_NULL);
|
|
|
|
|
|
|
|
if (queue->work_current == work) /* it's current work in the queue */
|
|
|
|
{
|
|
|
|
/* wait for work completion */
|
|
|
|
rt_sem_take(&(queue->sem), RT_WAITING_FOREVER);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2021-02-06 20:39:52 +08:00
|
|
|
_workqueue_cancel_work(queue, work);
|
2017-10-17 17:53:01 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
return RT_EOK;
|
|
|
|
}
|
|
|
|
|
2021-08-14 11:10:43 +08:00
|
|
|
/**
|
2021-08-18 09:44:52 +08:00
|
|
|
* @brief This function will cancel all work items in work queue.
|
2021-08-14 14:32:58 +08:00
|
|
|
*
|
2022-01-17 04:05:50 +08:00
|
|
|
* @param queue is a pointer to the workqueue object.
|
2021-08-14 14:32:58 +08:00
|
|
|
*
|
2021-08-14 11:10:43 +08:00
|
|
|
* @return RT_EOK Success.
|
|
|
|
*/
|
2019-03-17 16:01:12 +08:00
|
|
|
rt_err_t rt_workqueue_cancel_all_work(struct rt_workqueue *queue)
|
2017-01-31 13:17:04 +08:00
|
|
|
{
|
2021-02-06 20:08:31 +08:00
|
|
|
struct rt_work *work;
|
|
|
|
|
2017-02-27 00:58:11 +08:00
|
|
|
RT_ASSERT(queue != RT_NULL);
|
|
|
|
|
2021-02-22 17:10:33 +08:00
|
|
|
/* cancel work */
|
2017-02-27 00:58:11 +08:00
|
|
|
rt_enter_critical();
|
2021-02-06 20:39:52 +08:00
|
|
|
while (rt_list_isempty(&queue->work_list) == RT_FALSE)
|
2017-02-27 00:58:11 +08:00
|
|
|
{
|
2021-02-06 20:08:31 +08:00
|
|
|
work = rt_list_first_entry(&queue->work_list, struct rt_work, list);
|
2021-02-06 20:39:52 +08:00
|
|
|
_workqueue_cancel_work(queue, work);
|
2021-02-06 20:08:31 +08:00
|
|
|
}
|
2021-02-22 17:10:33 +08:00
|
|
|
/* cancel delay work */
|
2021-02-06 20:39:52 +08:00
|
|
|
while (rt_list_isempty(&queue->delayed_list) == RT_FALSE)
|
2021-02-06 20:08:31 +08:00
|
|
|
{
|
|
|
|
work = rt_list_first_entry(&queue->delayed_list, struct rt_work, list);
|
2021-02-06 20:39:52 +08:00
|
|
|
_workqueue_cancel_work(queue, work);
|
2017-02-27 00:58:11 +08:00
|
|
|
}
|
|
|
|
rt_exit_critical();
|
|
|
|
|
|
|
|
return RT_EOK;
|
2017-01-31 13:17:04 +08:00
|
|
|
}
|
|
|
|
|
2019-03-17 16:01:12 +08:00
|
|
|
#ifdef RT_USING_SYSTEM_WORKQUEUE
|
2022-01-17 04:15:08 +08:00
|
|
|
|
|
|
|
static struct rt_workqueue *sys_workq; /* system work queue */
|
2019-03-17 16:01:12 +08:00
|
|
|
|
2021-08-14 11:10:43 +08:00
|
|
|
/**
|
2021-08-18 09:44:52 +08:00
|
|
|
* @brief Submit a work item to the system work queue with a delay.
|
2021-08-14 14:32:58 +08:00
|
|
|
*
|
2022-01-17 04:05:50 +08:00
|
|
|
* @param work is a pointer to the work item object.
|
|
|
|
*
|
2022-01-17 04:15:08 +08:00
|
|
|
* @param ticks is the delay OS ticks for the work item to be submitted to the work queue.
|
2022-01-17 04:05:50 +08:00
|
|
|
*
|
|
|
|
* NOTE: The max timeout tick should be no more than (RT_TICK_MAX/2 - 1)
|
2021-08-14 14:32:58 +08:00
|
|
|
*
|
2021-08-14 11:10:43 +08:00
|
|
|
* @return RT_EOK Success.
|
2022-01-17 04:05:50 +08:00
|
|
|
* -RT_EBUSY This work item is executing.
|
2022-01-17 04:15:08 +08:00
|
|
|
* -RT_ERROR The ticks parameter is invalid.
|
2021-08-14 11:10:43 +08:00
|
|
|
*/
|
2022-01-17 04:15:08 +08:00
|
|
|
rt_err_t rt_work_submit(struct rt_work *work, rt_tick_t ticks)
|
2019-03-17 16:01:12 +08:00
|
|
|
{
|
2022-01-17 04:15:08 +08:00
|
|
|
return rt_workqueue_submit_work(sys_workq, work, ticks);
|
2019-03-17 16:01:12 +08:00
|
|
|
}
|
|
|
|
|
2022-01-17 05:00:55 +08:00
|
|
|
/**
|
|
|
|
* @brief Submit a work item to the system work queue without delay. This work item will be executed after the current work item.
|
|
|
|
*
|
|
|
|
* @param work is a pointer to the work item object.
|
|
|
|
*
|
|
|
|
* @return RT_EOK Success.
|
|
|
|
*/
|
|
|
|
rt_err_t rt_work_urgent(struct rt_work *work)
|
|
|
|
{
|
|
|
|
return rt_workqueue_urgent_work(sys_workq, work);
|
|
|
|
}
|
|
|
|
|
2021-08-14 11:10:43 +08:00
|
|
|
/**
|
2021-08-18 09:44:52 +08:00
|
|
|
* @brief Cancel a work item in the system work queue.
|
2021-08-14 14:32:58 +08:00
|
|
|
*
|
2022-01-17 04:05:50 +08:00
|
|
|
* @param work is a pointer to the work item object.
|
2021-08-14 14:32:58 +08:00
|
|
|
*
|
2021-08-14 11:10:43 +08:00
|
|
|
* @return RT_EOK Success.
|
2022-01-17 04:05:50 +08:00
|
|
|
* -RT_EBUSY This work item is executing.
|
2021-08-14 11:10:43 +08:00
|
|
|
*/
|
2019-03-17 16:01:12 +08:00
|
|
|
rt_err_t rt_work_cancel(struct rt_work *work)
|
|
|
|
{
|
|
|
|
return rt_workqueue_cancel_work(sys_workq, work);
|
|
|
|
}
|
|
|
|
|
2021-07-22 17:00:21 +08:00
|
|
|
static int rt_work_sys_workqueue_init(void)
|
2019-03-17 16:01:12 +08:00
|
|
|
{
|
2019-08-05 14:18:15 +08:00
|
|
|
if (sys_workq != RT_NULL)
|
2021-02-01 14:44:49 +08:00
|
|
|
return RT_EOK;
|
2019-08-05 14:18:15 +08:00
|
|
|
|
2022-01-17 05:00:55 +08:00
|
|
|
sys_workq = rt_workqueue_create("sys workq", RT_SYSTEM_WORKQUEUE_STACKSIZE,
|
2019-03-17 16:01:12 +08:00
|
|
|
RT_SYSTEM_WORKQUEUE_PRIORITY);
|
2021-02-01 14:44:49 +08:00
|
|
|
RT_ASSERT(sys_workq != RT_NULL);
|
2019-03-17 16:01:12 +08:00
|
|
|
|
|
|
|
return RT_EOK;
|
|
|
|
}
|
2021-02-22 11:23:20 +08:00
|
|
|
INIT_PREV_EXPORT(rt_work_sys_workqueue_init);
|
2021-07-22 17:00:21 +08:00
|
|
|
#endif /* RT_USING_SYSTEM_WORKQUEUE */
|
|
|
|
#endif /* RT_USING_HEAP */
|