chore: migrate project into clean repository

This commit is contained in:
yuuux
2026-08-13 16:50:52 +08:00
commit d1d25a09e7
27405 changed files with 9422808 additions and 0 deletions

View File

@@ -0,0 +1,15 @@
# ARCS Work Queue
#
# Copyright (c) 2025, LISTENAI
# SPDX-License-Identifier: Apache-2.0
if(CONFIG_WORK_QUEUE)
listenai_library_named(work_queue)
listenai_include_directories(.)
listenai_library_sources(
workqueue.c
)
endif() # CONFIG_WORK_QUEUE

View File

@@ -0,0 +1,9 @@
menu "work_queue"
config WORK_QUEUE
bool "Enable work queue component"
default n
help
Enable the work queue component for asynchronous task execution.
endmenu

View File

@@ -0,0 +1,100 @@
# Work Queue 组件
Work Queue 组件提供了一种轻量级的异步任务执行机制。它允许用户创建一个专用的工作线程Work Queue并将任务函数及其参数提交到该线程中串行执行。这对于将耗时操作从中断服务程序ISR或其他高优先级任务中卸载非常有用。
## 功能特性
- **异步执行**: 任务提交后立即返回,实际执行在专用线程中进行。
- **线程安全**: 基于 FreeRTOS Queue 实现,支持多线程并发提交。
- **灵活配置**: 支持自定义线程优先级、栈大小和队列深度。
## 配置选项
本组件默认不启用,需在 `prj.conf``Kconfig` 中进行配置:
```kconfig
CONFIG_WORK_QUEUE=y
```
## API 接口
### 数据结构
```c
typedef struct {
void (*function)(void *param); // 工作函数指针
void *parameter; // 工作函数参数
} workqueue_item_t;
typedef struct {
QueueHandle_t queue; // 内部消息队列
TaskHandle_t taskHandle; // 工作线程句柄
// ...
} workqueue_t;
```
### 创建工作队列
```c
workqueue_t *workqueue_create(const char *name, int priority, uint16_t queueLength, uint16_t stackSize);
```
* **name**: 工作线程名称。
* **priority**: 工作线程优先级。
* **queueLength**: 队列最大长度(可缓存的任务数量)。
* **stackSize**: 工作线程栈大小。
* **返回**: 成功返回 `workqueue_t` 指针,失败返回 `NULL`
### 提交任务
```c
int workqueue_submit(workqueue_t *workQueue, void (*function)(void *), void *parameter);
```
* **workQueue**: 工作队列句柄。
* **function**: 要执行的任务函数。
* **parameter**: 传递给任务函数的参数。
* **返回**: `pdPASS` 表示提交成功,其他值表示失败(如队列已满)。
* **说明**: 该函数为非阻塞调用WaitTime=0。如果队列已满将直接返回失败。
## 使用示例
```c
#include "workqueue.h"
#include "lisa_log.h"
// 1. 定义工作任务函数
static void my_heavy_work(void *param)
{
int value = (int)param;
// Log message in English as per rule
LISA_LOGI("Processing work item with value: %d", value);
// 模拟耗时操作
// vTaskDelay(100);
}
void app_main(void)
{
// 2. 创建工作队列
// 名称: "my_worker", 优先级: 5, 队列深度: 10, 栈大小: 2048
workqueue_t *my_wq = workqueue_create("my_worker", 5, 10, 2048);
if (my_wq == NULL) {
LISA_LOGE("Failed to create work queue");
return;
}
// 3. 提交任务
for (int i = 0; i < 5; i++) {
// 将整数 i 作为参数传递
int ret = workqueue_submit(my_wq, my_heavy_work, (void *)i);
if (ret != pdPASS) {
LISA_LOGE("Failed to submit work item %d, queue might be full", i);
} else {
LISA_LOGI("Submitted work item %d", i);
}
}
}
```

View File

@@ -0,0 +1,86 @@
#include "workqueue.h"
#define TAG "workqueue"
#include "lisa_log.h"
// 工作队列任务函数
static void workqueue_task(void *pvParameters)
{
workqueue_t *workQueue = (workqueue_t *)pvParameters;
workqueue_item_t workItem;
for (;;) {
// 等待队列中的工作项
if (xQueueReceive(workQueue->queue, &workItem, portMAX_DELAY) == pdPASS) {
// 执行工作项
if (workItem.function != NULL) {
workItem.function(workItem.parameter);
}
}
}
}
// 创建工作队列
workqueue_t *workqueue_create(const char *name, int priority, uint16_t queueLength, uint16_t stackSize)
{
workqueue_t *workQueue = (workqueue_t *)pvPortMalloc(sizeof(workqueue_t));
if (workQueue == NULL) {
return NULL;
}
// 创建队列
workQueue->queue = xQueueCreate(queueLength, sizeof(workqueue_item_t));
if (workQueue->queue == NULL) {
vPortFree(workQueue);
return NULL;
}
// 创建任务
if (xTaskCreate(workqueue_task, name, stackSize, workQueue, priority, &workQueue->taskHandle) != pdPASS) {
vQueueDelete(workQueue->queue);
vPortFree(workQueue);
return NULL;
}
return workQueue;
}
// 创建工作队列
workqueue_t *workqueue_create_static(const char *name, int priority, uint16_t queueLength, void* stack,uint16_t stackSize)
{
workqueue_t *workQueue = (workqueue_t *)pvPortMalloc(sizeof(workqueue_t));
if (workQueue == NULL) {
return NULL;
}
// 创建队列
workQueue->queue = xQueueCreate(queueLength, sizeof(workqueue_item_t));
if (workQueue->queue == NULL) {
vPortFree(workQueue);
return NULL;
}
// 创建任务
workQueue->taskHandle = xTaskCreateStatic(workqueue_task, name, stackSize, workQueue, priority,stack, &workQueue->statictask);
if (workQueue->taskHandle == NULL) {
vQueueDelete(workQueue->queue);
vPortFree(workQueue);
return NULL;
}
return workQueue;
}
// 提交工作到工作队列
int workqueue_submit(workqueue_t *workQueue, void (*function)(void *), void *parameter)
{
if (workQueue == NULL || function == NULL) {
return -1;
}
workqueue_item_t workItem;
workItem.function = function;
workItem.parameter = parameter;
return xQueueSend(workQueue->queue, &workItem, 0);
}

View File

@@ -0,0 +1,31 @@
#ifndef _WORKQUEUE_H_
#define _WORKQUEUE_H_
#include "FreeRTOS.h"
#include "queue.h"
#ifdef __cplusplus
extern "C" {
#endif
// 工作项结构体
typedef struct {
void (*function)(void *param); // 工作函数指针
void *parameter; // 工作函数参数
} workqueue_item_t;
// 工作队列句柄
typedef struct {
QueueHandle_t queue; // 存储工作项的队列
TaskHandle_t taskHandle; // 工作队列任务句柄
StaticTask_t statictask;
} workqueue_t;
workqueue_t *workqueue_create(const char *name, int priority, uint16_t queueLength, uint16_t stackSize);
int workqueue_submit(workqueue_t *workQueue, void (*function)(void *), void *parameter);
#ifdef __cplusplus
}
#endif
#endif