2017-02-27 00:58:11 +08:00
|
|
|
/*
|
2024-12-21 02:31:32 +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-16 16:00:55 -05:00
|
|
|
* 2021-08-14 Jackistang add comments for function interface
|
|
|
|
* 2022-01-16 Meco Man add rt_work_urgent()
|
2023-10-25 20:31:25 +08:00
|
|
|
* 2023-09-15 xqyjlj perf rt_hw_interrupt_disable/enable
|
2024-12-21 02:31:32 +08:00
|
|
|
* 2024-12-21 yuqingli delete timer, using list
|
2017-02-27 00:58:11 +08:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include <rthw.h>
|
2014-07-13 07:27:57 +08:00
|
|
|
#include <rtdevice.h>
|
|
|
|
|
|
|
|
#ifdef RT_USING_HEAP
|
2019-03-17 16:01:12 +08:00
|
|
|
|
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
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
}
|
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
|
|
|
{
|
2024-12-21 02:31:32 +08:00
|
|
|
rt_base_t level;
|
|
|
|
struct rt_work *work;
|
2019-03-17 16:01:12 +08:00
|
|
|
struct rt_workqueue *queue;
|
2024-12-21 02:31:32 +08:00
|
|
|
rt_tick_t current_tick;
|
|
|
|
rt_int32_t delay_tick;
|
|
|
|
void (*work_func)(struct rt_work *work, void *work_data);
|
|
|
|
void *work_data;
|
2017-02-27 00:58:11 +08:00
|
|
|
|
2024-12-21 02:31:32 +08:00
|
|
|
queue = (struct rt_workqueue *)parameter;
|
2017-02-27 00:58:11 +08:00
|
|
|
RT_ASSERT(queue != RT_NULL);
|
|
|
|
|
|
|
|
while (1)
|
|
|
|
{
|
2023-10-25 20:31:25 +08:00
|
|
|
level = rt_spin_lock_irqsave(&(queue->spinlock));
|
2024-12-21 02:31:32 +08:00
|
|
|
|
|
|
|
/* timer check */
|
|
|
|
current_tick = rt_tick_get();
|
|
|
|
delay_tick = RT_WAITING_FOREVER;
|
|
|
|
while (!rt_list_isempty(&(queue->delayed_list)))
|
2017-02-27 00:58:11 +08:00
|
|
|
{
|
2024-12-21 02:31:32 +08:00
|
|
|
work = rt_list_entry(queue->delayed_list.next, struct rt_work, list);
|
|
|
|
if ((current_tick - work->timeout_tick) < RT_TICK_MAX / 2)
|
|
|
|
{
|
|
|
|
rt_list_remove(&(work->list));
|
|
|
|
rt_list_insert_after(queue->work_list.prev, &(work->list));
|
|
|
|
work->flags &= ~RT_WORK_STATE_SUBMITTING;
|
|
|
|
work->flags |= RT_WORK_STATE_PENDING;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
delay_tick = work->timeout_tick - current_tick;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2024-02-23 17:49:15 +08:00
|
|
|
|
2024-12-21 02:31:32 +08:00
|
|
|
if (rt_list_isempty(&(queue->work_list)))
|
|
|
|
{
|
2023-10-25 20:31:25 +08:00
|
|
|
rt_spin_unlock_irqrestore(&(queue->spinlock), level);
|
2024-12-21 02:31:32 +08:00
|
|
|
/* wait for work completion */
|
|
|
|
rt_completion_wait(&(queue->wakeup_completion), delay_tick);
|
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));
|
2024-12-21 02:31:32 +08:00
|
|
|
queue->work_current = work;
|
|
|
|
work->flags &= ~RT_WORK_STATE_PENDING;
|
|
|
|
work->workqueue = RT_NULL;
|
|
|
|
work_func = work->work_func;
|
|
|
|
work_data = work->work_data;
|
2023-10-25 20:31:25 +08:00
|
|
|
rt_spin_unlock_irqrestore(&(queue->spinlock), level);
|
2017-02-27 00:58:11 +08:00
|
|
|
|
|
|
|
/* do work */
|
2024-12-21 02:31:32 +08:00
|
|
|
work_func(work, work_data);
|
2017-02-27 00:58:11 +08:00
|
|
|
/* 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,
|
2022-07-25 10:21:18 +08:00
|
|
|
struct rt_work *work, rt_tick_t ticks)
|
2019-03-17 16:01:12 +08:00
|
|
|
{
|
2024-12-21 02:31:32 +08:00
|
|
|
rt_base_t level;
|
|
|
|
rt_err_t err = RT_EOK;
|
|
|
|
struct rt_work *work_tmp;
|
|
|
|
rt_list_t *list_tmp;
|
2019-03-17 16:01:12 +08:00
|
|
|
|
2023-10-25 20:31:25 +08:00
|
|
|
level = rt_spin_lock_irqsave(&(queue->spinlock));
|
2021-01-30 18:21:33 +08:00
|
|
|
/* remove list */
|
2019-03-17 16:01:12 +08:00
|
|
|
rt_list_remove(&(work->list));
|
2024-12-21 02:31:32 +08:00
|
|
|
work->flags = 0;
|
2022-01-16 14:33:41 -05:00
|
|
|
|
2021-01-30 18:21:33 +08:00
|
|
|
if (ticks == 0)
|
2019-03-17 16:01:12 +08:00
|
|
|
{
|
2023-02-16 20:19:26 +08:00
|
|
|
rt_list_insert_after(queue->work_list.prev, &(work->list));
|
2024-12-21 02:31:32 +08:00
|
|
|
work->flags |= RT_WORK_STATE_PENDING;
|
|
|
|
work->workqueue = queue;
|
2021-01-30 18:21:33 +08:00
|
|
|
|
2024-12-21 02:31:32 +08:00
|
|
|
rt_completion_done(&(queue->wakeup_completion));
|
|
|
|
err = RT_EOK;
|
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
|
|
|
{
|
2024-12-21 02:31:32 +08:00
|
|
|
/* insert delay work list */
|
|
|
|
work->flags |= RT_WORK_STATE_SUBMITTING;
|
|
|
|
work->workqueue = queue;
|
|
|
|
work->timeout_tick = rt_tick_get() + ticks;
|
|
|
|
|
|
|
|
list_tmp = &(queue->delayed_list);
|
|
|
|
rt_list_for_each_entry(work_tmp, &(queue->delayed_list), list)
|
2021-01-30 18:21:33 +08:00
|
|
|
{
|
2024-12-21 16:43:16 +08:00
|
|
|
if ((work_tmp->timeout_tick - work->timeout_tick) < RT_TICK_MAX / 2)
|
2024-12-21 02:31:32 +08:00
|
|
|
{
|
|
|
|
list_tmp = &(work_tmp->list);
|
|
|
|
break;
|
|
|
|
}
|
2021-01-30 18:21:33 +08:00
|
|
|
}
|
2024-12-21 02:31:32 +08:00
|
|
|
rt_list_insert_before(list_tmp, &(work->list));
|
2024-04-16 11:15:37 +08:00
|
|
|
|
2024-12-21 02:31:32 +08:00
|
|
|
rt_completion_done(&(queue->wakeup_completion));
|
|
|
|
err = RT_EOK;
|
2019-03-17 16:01:12 +08:00
|
|
|
}
|
2024-06-20 04:20:38 +08:00
|
|
|
else
|
|
|
|
{
|
2024-12-21 02:31:32 +08:00
|
|
|
err = -RT_ERROR;
|
2024-06-20 04:20:38 +08:00
|
|
|
}
|
2023-10-25 20:31:25 +08:00
|
|
|
rt_spin_unlock_irqrestore(&(queue->spinlock), level);
|
2024-06-20 04:20:38 +08:00
|
|
|
return err;
|
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;
|
2024-12-21 02:31:32 +08:00
|
|
|
rt_err_t err;
|
2019-03-17 16:01:12 +08:00
|
|
|
|
2023-10-25 20:31:25 +08:00
|
|
|
level = rt_spin_lock_irqsave(&(queue->spinlock));
|
2019-03-17 16:01:12 +08:00
|
|
|
rt_list_remove(&(work->list));
|
2024-12-21 02:31:32 +08:00
|
|
|
work->flags = 0;
|
|
|
|
err = queue->work_current != work ? RT_EOK : -RT_EBUSY;
|
2019-03-17 16:01:12 +08:00
|
|
|
work->workqueue = RT_NULL;
|
2023-10-25 20:31:25 +08:00
|
|
|
rt_spin_unlock_irqrestore(&(queue->spinlock), level);
|
2021-02-06 20:39:52 +08:00
|
|
|
return err;
|
2019-03-17 16:01:12 +08:00
|
|
|
}
|
|
|
|
|
2022-01-16 15:15:08 -05: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;
|
2024-12-21 02:31:32 +08:00
|
|
|
work->flags = 0;
|
|
|
|
work->type = 0;
|
2022-01-16 15:15:08 -05:00
|
|
|
}
|
|
|
|
|
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-16 15:05:50 -05:00
|
|
|
* @param name is a name of the work queue thread.
|
2021-08-14 14:32:58 +08:00
|
|
|
*
|
2022-01-16 15:05:50 -05: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);
|
2024-12-21 02:31:32 +08:00
|
|
|
rt_completion_init(&(queue->wakeup_completion));
|
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)
|
|
|
|
{
|
2024-03-20 08:01:49 +08:00
|
|
|
rt_sem_detach(&(queue->sem));
|
2017-02-27 00:58:11 +08:00
|
|
|
RT_KERNEL_FREE(queue);
|
|
|
|
return RT_NULL;
|
|
|
|
}
|
|
|
|
|
2023-10-25 20:31:25 +08:00
|
|
|
rt_spin_lock_init(&(queue->spinlock));
|
2017-02-27 00:58:11 +08:00
|
|
|
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-16 15:05:50 -05:00
|
|
|
* @param queue is a pointer to the workqueue object.
|
2021-08-14 14:32:58 +08:00
|
|
|
*
|
2022-01-16 15:05:50 -05: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-16 15:05:50 -05: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-16 15:05:50 -05: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-16 15:05:50 -05:00
|
|
|
* @param queue is a pointer to the workqueue object.
|
|
|
|
*
|
|
|
|
* @param work is a pointer to the work item object.
|
|
|
|
*
|
2022-01-16 15:15:08 -05:00
|
|
|
* @param ticks is the delay ticks for the work item to be submitted to the work queue.
|
2022-01-16 15:05:50 -05: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-16 15:05:50 -05:00
|
|
|
* -RT_EBUSY This work item is executing.
|
2022-01-16 15:15:08 -05:00
|
|
|
* -RT_ERROR The ticks parameter is invalid.
|
2021-08-14 11:10:43 +08:00
|
|
|
*/
|
2022-01-16 15:15:08 -05: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-16 15:15:08 -05:00
|
|
|
RT_ASSERT(ticks < RT_TICK_MAX / 2);
|
2017-02-27 00:58:11 +08:00
|
|
|
|
2022-01-16 15:15:08 -05: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-16 15:05:50 -05: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-20 15:48:07 -04: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-16 15:05:50 -05:00
|
|
|
|
2017-02-27 00:58:11 +08:00
|
|
|
RT_ASSERT(queue != RT_NULL);
|
|
|
|
RT_ASSERT(work != RT_NULL);
|
|
|
|
|
2023-10-25 20:31:25 +08:00
|
|
|
level = rt_spin_lock_irqsave(&(queue->spinlock));
|
2017-02-27 00:58:11 +08:00
|
|
|
/* 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));
|
2017-02-27 00:58:11 +08:00
|
|
|
|
2024-12-21 02:31:32 +08:00
|
|
|
rt_completion_done(&(queue->wakeup_completion));
|
2024-06-20 04:20:38 +08:00
|
|
|
rt_spin_unlock_irqrestore(&(queue->spinlock), level);
|
2024-12-21 02:31:32 +08:00
|
|
|
|
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-16 15:05:50 -05: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-16 15:05:50 -05: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-16 15:05:50 -05: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-16 15:05:50 -05: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);
|
2024-06-12 19:50:37 +08:00
|
|
|
/* Note that because work items are automatically deleted after execution, they do not need to be deleted again */
|
2017-10-17 17:53:01 +08:00
|
|
|
}
|
|
|
|
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-16 15:05:50 -05: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-16 15:15:08 -05: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-16 15:05:50 -05:00
|
|
|
* @param work is a pointer to the work item object.
|
|
|
|
*
|
2022-01-16 15:15:08 -05:00
|
|
|
* @param ticks is the delay OS ticks for the work item to be submitted to the work queue.
|
2022-01-16 15:05:50 -05: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-16 15:05:50 -05:00
|
|
|
* -RT_EBUSY This work item is executing.
|
2022-01-16 15:15:08 -05:00
|
|
|
* -RT_ERROR The ticks parameter is invalid.
|
2021-08-14 11:10:43 +08:00
|
|
|
*/
|
2022-01-16 15:15:08 -05: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-16 15:15:08 -05:00
|
|
|
return rt_workqueue_submit_work(sys_workq, work, ticks);
|
2019-03-17 16:01:12 +08:00
|
|
|
}
|
|
|
|
|
2022-01-16 16:00:55 -05: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-16 15:05:50 -05: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-16 15:05:50 -05: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-16 16:00:55 -05: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 */
|