2017-02-27 00:58:11 +08:00
|
|
|
/*
|
2018-10-14 19:37:18 +08:00
|
|
|
* Copyright (c) 2006-2018, 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
|
|
|
|
* 2017-02-27 bernard fix the re-work issue.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#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)
|
|
|
|
{
|
|
|
|
if (rt_list_isempty(&(queue->work_list)))
|
|
|
|
{
|
|
|
|
/* no software timer exist, suspend self. */
|
|
|
|
rt_thread_suspend(rt_thread_self());
|
|
|
|
rt_schedule();
|
|
|
|
}
|
|
|
|
|
|
|
|
/* we have work to do with. */
|
|
|
|
level = rt_hw_interrupt_disable();
|
|
|
|
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);
|
|
|
|
level = rt_hw_interrupt_disable();
|
|
|
|
/* clean current work */
|
|
|
|
queue->work_current = RT_NULL;
|
|
|
|
rt_hw_interrupt_enable(level);
|
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;
|
|
|
|
|
|
|
|
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;
|
|
|
|
/* */
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* whether the workqueue is doing work */
|
|
|
|
if (queue->work_current == RT_NULL &&
|
|
|
|
((queue->work_thread->stat & RT_THREAD_STAT_MASK) == RT_THREAD_SUSPEND))
|
|
|
|
{
|
|
|
|
rt_hw_interrupt_enable(level);
|
|
|
|
/* resume work thread */
|
|
|
|
rt_thread_resume(queue->work_thread);
|
|
|
|
rt_schedule();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
rt_hw_interrupt_enable(level);
|
|
|
|
}
|
|
|
|
return 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
|
|
|
{
|
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))
|
|
|
|
{
|
|
|
|
rt_hw_interrupt_enable(level);
|
|
|
|
/* resume work thread */
|
|
|
|
rt_thread_resume(queue->work_thread);
|
|
|
|
rt_schedule();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
rt_hw_interrupt_enable(level);
|
|
|
|
}
|
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
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
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
|
|
|
|
2019-03-17 16:01:12 +08:00
|
|
|
rt_err_t rt_workqueue_submit_work(struct rt_workqueue *queue, struct rt_work *work, rt_tick_t time)
|
|
|
|
{
|
|
|
|
RT_ASSERT(queue != RT_NULL);
|
|
|
|
RT_ASSERT(work != RT_NULL);
|
2017-02-27 00:58:11 +08:00
|
|
|
|
2021-02-01 14:43:12 +08:00
|
|
|
return _workqueue_submit_work(queue, work, time);
|
2014-07-13 07:27:57 +08:00
|
|
|
}
|
|
|
|
|
2019-03-17 16:01:12 +08:00
|
|
|
rt_err_t rt_workqueue_critical_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;
|
|
|
|
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
|
|
|
{
|
|
|
|
rt_hw_interrupt_enable(level);
|
|
|
|
/* resume work thread */
|
|
|
|
rt_thread_resume(queue->work_thread);
|
|
|
|
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
|
|
|
}
|
|
|
|
|
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);
|
|
|
|
return _workqueue_cancel_work(queue, work);
|
2014-07-13 07:27:57 +08:00
|
|
|
}
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
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-06 20:08:31 +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
|
|
|
}
|
|
|
|
// 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
|
|
|
void rt_delayed_work_init(struct rt_delayed_work *work, void (*work_func)(struct rt_work *work,
|
|
|
|
void *work_data), void *work_data)
|
|
|
|
{
|
2019-08-05 14:18:15 +08:00
|
|
|
rt_work_init(&work->work, work_func, work_data);
|
2019-03-17 16:01:12 +08:00
|
|
|
}
|
2014-07-13 07:27:57 +08:00
|
|
|
|
2019-03-17 16:01:12 +08:00
|
|
|
#ifdef RT_USING_SYSTEM_WORKQUEUE
|
|
|
|
static struct rt_workqueue *sys_workq;
|
|
|
|
|
|
|
|
rt_err_t rt_work_submit(struct rt_work *work, rt_tick_t time)
|
|
|
|
{
|
|
|
|
return rt_workqueue_submit_work(sys_workq, work, time);
|
|
|
|
}
|
|
|
|
|
|
|
|
rt_err_t rt_work_cancel(struct rt_work *work)
|
|
|
|
{
|
|
|
|
return rt_workqueue_cancel_work(sys_workq, work);
|
|
|
|
}
|
|
|
|
|
2019-08-05 14:18:15 +08:00
|
|
|
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
|
|
|
|
2019-04-03 17:50:16 +08:00
|
|
|
sys_workq = rt_workqueue_create("sys_work", 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-01 14:44:49 +08:00
|
|
|
INIT_DEVICE_EXPORT(rt_work_sys_workqueue_init);
|
2019-03-17 16:01:12 +08:00
|
|
|
#endif
|
|
|
|
#endif
|