CodeBrick/framework/async_work.c
2021-05-11 15:01:31 +08:00

101 lines
2.9 KiB
C
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

/******************************************************************************
* @brief <20><EFBFBD><ECB2BD>ҵ
*
* Copyright (c) 2020, <morro_luo@163.com>
*
* SPDX-License-Identifier: Apache-2.0
*
* Change Logs:
* Date Author Notes
* 2020-09-22 Morro Initial version.
******************************************************************************/
#include "async_work.h"
#include "comdef.h"
#include <stdbool.h>
/*******************************************************************************
* @brief <20><>ҵ<EFBFBD><D2B5><EFBFBD><EFBFBD>
* @param[in] qlink - <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
* @param[in] new_item - <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
* @return none
******************************************************************************/
static inline void workqueue_put(struct qlink *q, work_node_t *n)
{
qlink_put(q, &n->node); /*<2A><><EFBFBD><EFBFBD><EBB5BD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>*/
}
/*******************************************************************************
* @brief <20><>ҵ<EFBFBD><D2B5><EFBFBD><EFBFBD>
* @param[in] qlink - <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
* @return <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
******************************************************************************/
static inline work_node_t *workqueue_get(struct qlink *q)
{
struct qlink_node *n = qlink_get(q);
return n ? container_of(n, work_node_t, node) : NULL;
}
/*******************************************************************************
* @brief <20><>ҵԤ<D2B5><D4A4><EFBFBD><EFBFBD>
* @param[in] qlink - <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
* @return <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
******************************************************************************/
static inline work_node_t * workqueue_peek(struct qlink *q)
{
struct qlink_node *n = qlink_peek(q);
return n ? container_of(n, work_node_t, node) : NULL;
}
/*
* @brief <20><EFBFBD><ECB3A3>ҵ<EFBFBD><D2B5>ʼ<EFBFBD><CABC>
* @param[in] w - <20><>ҵ<EFBFBD><D2B5><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
* @param[in] node_tbl - <20><>ҵ<EFBFBD>ڵ<EFBFBD><DAB5><EFBFBD>
* @param[in] count - node_tbl<62><6C><EFBFBD><EFBFBD>
*/
void async_work_init(async_work_t *w, work_node_t *node_tbl, int count)
{
qlink_init(&w->idle);
qlink_init(&w->ready);
while (count--) {
qlink_put(&w->idle, &node_tbl->node);
node_tbl++;
}
}
/*
* @brief <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ҵ<EFBFBD><D2B5><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
* @param[in] w - <20><>ҵ<EFBFBD><D2B5><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
* @param[in] params - <20><>ҵ<EFBFBD><D2B5><EFBFBD><EFBFBD>
* @param[in] work - <20><>ҵ<EFBFBD><D2B5><EFBFBD><EFBFBD>
*/
bool async_work_add(async_work_t *w, void *object, void *params,
async_work_func_t work)
{
work_node_t *n = workqueue_get(&w->idle);
if (n == NULL)
return false;
n->object = object;
n->params = params;
n->work = work;
workqueue_put(&w->ready, n); /*<2A><><EFBFBD><EFBFBD><EBB5BD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>*/
return true;
}
/*
* @brief <20><EFBFBD><ECB2BD>ҵ<EFBFBD><D2B5><EFBFBD><EFBFBD>
* @param[in] w - <20><>ҵ<EFBFBD><D2B5><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
*/
void async_work_process(async_work_t *w)
{
work_node_t *n;
if ((n = workqueue_get(&w->ready)) == NULL)
return;
n->work(w, n->object, n->params);
workqueue_put(&w->idle, n);
}