CodeBrick/framework/async_work.c

101 lines
2.9 KiB
C
Raw Normal View History

2020-09-22 21:49:25 +08:00
/******************************************************************************
* @brief <EFBFBD><EFBFBD><EFBFBD>ҵ
*
* Copyright (c) 2020, <master_roger@sina.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 <EFBFBD><EFBFBD>ҵ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>
* @param[in] qlink - <EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
* @param[in] new_item - <EFBFBD><EFBFBD><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 <EFBFBD><EFBFBD>ҵ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>
* @param[in] qlink - <EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
* @return <EFBFBD><EFBFBD><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 <EFBFBD><EFBFBD>ҵԤ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>
* @param[in] qlink - <EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
* @return <EFBFBD><EFBFBD><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 <EFBFBD><EFBFBD><EFBFBD>ҵ<EFBFBD><EFBFBD>ʼ<EFBFBD><EFBFBD>
* @param[in] w - <EFBFBD><EFBFBD>ҵ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
* @param[in] node_tbl - <EFBFBD><EFBFBD>ҵ<EFBFBD>ڵ<EFBFBD><EFBFBD><EFBFBD>
* @param[in] count - node_tbl<EFBFBD><EFBFBD><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 <EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ҵ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
* @param[in] w - <EFBFBD><EFBFBD>ҵ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
* @param[in] params - <EFBFBD><EFBFBD>ҵ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>
* @param[in] work - <EFBFBD><EFBFBD>ҵ<EFBFBD><EFBFBD><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 <EFBFBD><EFBFBD><EFBFBD>ҵ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>
* @param[in] w - <EFBFBD><EFBFBD>ҵ<EFBFBD><EFBFBD><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);
}