rel_1.6.0 init

This commit is contained in:
guocheng.kgc 2020-06-18 20:06:52 +08:00 committed by shengdong.dsd
commit 27b3e2883d
19359 changed files with 8093121 additions and 0 deletions

View file

@ -0,0 +1,289 @@
/*
* Copyright (C) 2015-2017 Alibaba Group Holding Limited
*/
#include <stdbool.h>
#include <stdlib.h>
#include <errno.h>
#include <aos/aos.h>
#include "event_device.h"
#include "yloop.h"
#include "k_config.h"
typedef struct {
dlist_t node;
aos_event_cb cb;
void *priv;
uint16_t type_filter;
} event_list_node_t;
static struct {
void *handle;
int fd;
} local_event = {
.fd = -1,
};
static dlist_t g_local_event_list = AOS_DLIST_INIT(g_local_event_list);
static void handle_events(input_event_t *event);
static int input_add_event(int fd, input_event_t *event);
static void event_read_cb(int fd, void *param);
extern yloop_ctx_t *g_main_ctx;
/* Handle events
* just dispatch
*/
void handle_events(input_event_t *event)
{
if (event->type == EV_RPC) {
aos_call_t handler = (aos_call_t)event->value;
void *arg = (void *)event->extra;
handler(arg);
return;
}
event_list_node_t *event_node = NULL;
dlist_for_each_entry(&g_local_event_list, event_node, event_list_node_t, node) {
if (event_node->type_filter != EV_ALL
&& event_node->type_filter != event->type) {
continue;
}
(event_node->cb)(event, event_node->priv);
}
}
static int input_add_event(int fd, input_event_t *event)
{
bool urgent = event->type & EV_FLAG_URGENT;
event->type &= ~EV_FLAG_URGENT;
int cmd;
if (urgent) {
cmd = MK_CMD(IOCTL_WRITE_URGENT, sizeof(*event));
} else {
cmd = MK_CMD(IOCTL_WRITE_NORMAL, sizeof(*event));
}
return aos_ioctl(fd, cmd, (unsigned long)event);
}
void event_read_cb(int fd, void *param)
{
input_event_t event;
int ret = aos_read(fd, &event, sizeof(event));
if (ret == sizeof(event)) {
handle_events(&event);
}
}
int aos_event_service_init(void)
{
int fd = aos_open("/dev/event", 0);
if (local_event.fd < 0) {
local_event.fd = fd;
}
aos_poll_read_fd(fd, event_read_cb, NULL);
aos_loop_set_eventfd(fd);
return 0;
}
void aos_event_service_deinit(int fd)
{
aos_cancel_poll_read_fd(fd, event_read_cb, NULL);
aos_close(fd);
}
int aos_post_event(uint16_t type, uint16_t code, unsigned long value)
{
input_event_t event = {
.type = type,
.code = code,
.value = value,
};
return input_add_event(local_event.fd, &event);
}
int aos_register_event_filter(uint16_t type, aos_event_cb cb, void *priv)
{
if (cb == NULL) {
return -EINVAL;
}
event_list_node_t *event_node = aos_malloc(sizeof(event_list_node_t));
if (NULL == event_node) {
return -ENOMEM;
}
event_node->cb = cb;
event_node->type_filter = type;
event_node->priv = priv;
dlist_add_tail(&event_node->node, &g_local_event_list);
return 0;
}
int aos_unregister_event_filter(uint16_t type, aos_event_cb cb, void *priv)
{
event_list_node_t *event_node = NULL;
dlist_for_each_entry(&g_local_event_list, event_node, event_list_node_t, node) {
if (event_node->type_filter != type) {
continue;
}
if (event_node->cb != cb) {
continue;
}
if (event_node->priv != priv) {
continue;
}
dlist_del(&event_node->node);
aos_free(event_node);
return 0;
}
return -EINVAL;
}
/*
* schedule a callback in aos loop main thread
*/
static int _schedule_call(aos_loop_t *loop, aos_call_t fun, void *arg,
bool urgent)
{
if (fun == NULL || g_main_ctx == NULL) {
return -EINVAL;
}
input_event_t event = {
.type = EV_RPC,
.value = (unsigned long)fun,
.extra = (unsigned long)arg,
};
int fd = aos_loop_get_eventfd(loop);
if (fd < 0) {
fd = local_event.fd;
}
if (urgent) {
event.type |= EV_FLAG_URGENT;
}
return input_add_event(fd, &event);
}
int aos_loop_schedule_urgent_call(aos_loop_t *loop, aos_call_t fun, void *arg)
{
return _schedule_call(loop, fun, arg, true);
}
int aos_loop_schedule_call(aos_loop_t *loop, aos_call_t fun, void *arg)
{
return _schedule_call(loop, fun, arg, false);
}
int aos_schedule_call(aos_call_t fun, void *arg)
{
return _schedule_call(NULL, fun, arg, false);
}
#if (RHINO_CONFIG_WORKQUEUE>0)
typedef struct work_para {
aos_work_t *work;
aos_loop_t loop;
aos_call_t action;
void *arg1;
aos_call_t fini_cb;
void *arg2;
} work_par_t;
static void free_wpar(work_par_t *wpar)
{
aos_work_destroy(wpar->work);
aos_free(wpar->work);
aos_free(wpar);
}
static void run_my_work(void *arg)
{
work_par_t *wpar = arg;
wpar->action(wpar->arg1);
if (wpar->fini_cb) {
aos_loop_schedule_call(wpar->loop, wpar->fini_cb, wpar->arg2);
}
free_wpar(wpar);
}
void aos_cancel_work(void *w, aos_call_t action, void *arg1)
{
work_par_t *wpar = w;
if (wpar == NULL) {
return;
}
if (wpar->action != action) {
return;
}
if (wpar->arg1 != arg1) {
return;
}
int ret = aos_work_cancel(wpar->work);
if (ret != 0) {
return;
}
free_wpar(wpar);
}
void *aos_loop_schedule_work(int ms, aos_call_t action, void *arg1,
aos_call_t fini_cb, void *arg2)
{
int ret;
if (action == NULL) {
return NULL;
}
aos_work_t *work = aos_malloc(sizeof(*work));
work_par_t *wpar = aos_malloc(sizeof(*wpar));
if (!work || !wpar) {
goto err_out;
}
wpar->work = work;
wpar->loop = aos_current_loop();
wpar->action = action;
wpar->arg1 = arg1;
wpar->fini_cb = fini_cb;
wpar->arg2 = arg2;
ret = aos_work_init(work, run_my_work, wpar, ms);
if (ret != 0) {
goto err_out;
}
ret = aos_work_sched(work);
if (ret != 0) {
goto err_out;
}
return wpar;
err_out:
aos_free(work);
aos_free(wpar);
return NULL;
}
#endif

View file

@ -0,0 +1,10 @@
src = Split('''
yloop.c
local_event.c
''')
component = aos_component('yloop', src)
component.add_comp_deps('utility/log', 'kernel/vfs')
component.add_global_macros('AOS_LOOP')
if aos_global_config.compiler == 'armcc':
component.add_prebuilt_objs('local_event.o')

View file

@ -0,0 +1,336 @@
/*
* Copyright (C) 2015-2017 Alibaba Group Holding Limited
*/
#include <sys/time.h>
#include <string.h>
#include <stdbool.h>
#include <stdlib.h>
#include <errno.h>
#include <aos/aos.h>
#include <aos/network.h>
#include "yloop.h"
#define TAG "yloop"
typedef struct yloop_timeout_s {
dlist_t next;
long long timeout_ms;
void *private_data;
aos_call_t cb;
int ms;
} yloop_timeout_t;
yloop_ctx_t *g_main_ctx = NULL;
static aos_task_key_t g_loop_key;
static inline void _set_context(yloop_ctx_t *ctx)
{
aos_task_setspecific(g_loop_key, ctx);
}
static inline yloop_ctx_t *_get_context(void)
{
return aos_task_getspecific(g_loop_key);
}
static inline yloop_ctx_t *get_context(void)
{
yloop_ctx_t *ctx = _get_context();
if (!ctx) {
_set_context(g_main_ctx);
return g_main_ctx;
}
return ctx;
}
void aos_loop_set_eventfd(int fd)
{
yloop_ctx_t *ctx = get_context();
ctx->eventfd = fd;
}
int aos_loop_get_eventfd(void *loop)
{
yloop_ctx_t *ctx = loop ? loop : get_context();
return ctx->eventfd;
}
aos_loop_t aos_current_loop(void)
{
return get_context();
}
aos_loop_t aos_loop_init(void)
{
yloop_ctx_t *ctx = _get_context();
if (!g_main_ctx) {
aos_task_key_create(&g_loop_key);
} else if (ctx) {
LOGE(TAG, "yloop already inited");
return ctx;
}
ctx = aos_zalloc(sizeof(*g_main_ctx));
if (!g_main_ctx) {
g_main_ctx = ctx;
}
dlist_init(&ctx->timeouts);
ctx->eventfd = -1;
_set_context(ctx);
aos_event_service_init();
return ctx;
}
int aos_poll_read_fd(int sock, aos_poll_call_t cb, void *private_data)
{
yloop_ctx_t *ctx = get_context();
if (sock < 0) {
return -EINVAL;
}
yloop_sock_t *new_sock;
struct pollfd *new_loop_pollfds;
int cnt = ctx->reader_count + 1;
new_sock = aos_malloc(cnt * sizeof(yloop_sock_t));
new_loop_pollfds = aos_malloc(cnt * sizeof(struct pollfd));
if (new_sock == NULL || new_loop_pollfds == NULL) {
LOGE(TAG, "out of memory");
aos_free(new_sock);
aos_free(new_loop_pollfds);
return -ENOMEM;
}
int status = aos_fcntl(sock, F_GETFL, 0);
aos_fcntl(sock, F_SETFL, status | O_NONBLOCK);
ctx->reader_count++;
memcpy(new_sock, ctx->readers, (cnt - 1) * sizeof(yloop_sock_t));
aos_free(ctx->readers);
ctx->readers = new_sock;
memcpy(new_loop_pollfds, ctx->pollfds, (cnt - 1) * sizeof(struct pollfd));
aos_free(ctx->pollfds);
ctx->pollfds = new_loop_pollfds;
new_sock += cnt - 1;
new_sock->sock = sock;
new_sock->private_data = private_data;
new_sock->cb = cb;
if (sock > ctx->max_sock) {
ctx->max_sock = sock;
}
return 0;
}
void aos_cancel_poll_read_fd(int sock, aos_poll_call_t action, void *param)
{
yloop_ctx_t *ctx = get_context();
if (ctx->readers == NULL || ctx->reader_count == 0) {
return;
}
int i;
for (i = 0; i < ctx->reader_count; i++) {
if (ctx->readers[i].sock == sock) {
break;
}
}
if (i == ctx->reader_count) {
return;
}
if (i != ctx->reader_count - 1) {
memmove(&ctx->readers[i], &ctx->readers[i + 1],
(ctx->reader_count - i - 1) *
sizeof(yloop_sock_t));
}
ctx->reader_count--;
}
int aos_post_delayed_action(int ms, aos_call_t action, void *param)
{
if (action == NULL) {
return -EINVAL;
}
yloop_ctx_t *ctx = get_context();
yloop_timeout_t *timeout = aos_malloc(sizeof(*timeout));
if (timeout == NULL) {
return -ENOMEM;
}
timeout->timeout_ms = aos_now_ms() + ms;
timeout->private_data = param;
timeout->cb = action;
timeout->ms = ms;
yloop_timeout_t *tmp;
dlist_for_each_entry(&ctx->timeouts, tmp, yloop_timeout_t, next) {
if (timeout->timeout_ms < tmp->timeout_ms) {
break;
}
}
dlist_add_tail(&timeout->next, &tmp->next);
return 0;
}
void aos_cancel_delayed_action(int ms, aos_call_t cb, void *private_data)
{
yloop_ctx_t *ctx = get_context();
yloop_timeout_t *tmp;
dlist_for_each_entry(&ctx->timeouts, tmp, yloop_timeout_t, next) {
if (ms != -1 && tmp->ms != ms) {
continue;
}
if (tmp->cb != cb) {
continue;
}
if (tmp->private_data != private_data) {
continue;
}
dlist_del(&tmp->next);
aos_free(tmp);
return;
}
}
void aos_cancel_delayed_action_loose(int ms, aos_call_t cb)
{
yloop_ctx_t *ctx = get_context();
yloop_timeout_t *tmp;
dlist_for_each_entry(&ctx->timeouts, tmp, yloop_timeout_t, next) {
if (ms != -1 && tmp->ms != ms) {
continue;
}
if (tmp->cb != cb) {
continue;
}
dlist_del(&tmp->next);
aos_free(tmp);
return;
}
}
void aos_loop_run(void)
{
yloop_ctx_t *ctx = get_context();
while (!ctx->terminate &&
(!dlist_empty(&ctx->timeouts) || ctx->reader_count > 0)) {
int delayed_ms = -1;
int readers = ctx->reader_count;
int i;
#ifdef AOS_DISABLE_ALL_LOGS
log_check_uart_input(10);
#endif
if (!dlist_empty(&ctx->timeouts)) {
yloop_timeout_t *tmo = dlist_first_entry(&ctx->timeouts, yloop_timeout_t, next);
long long now = aos_now_ms();
if (now < tmo->timeout_ms) {
delayed_ms = tmo->timeout_ms - now;
} else {
delayed_ms = 0;
}
}
for (i = 0; i < readers; i++) {
ctx->pollfds[i].fd = ctx->readers[i].sock;
ctx->pollfds[i].events = POLLIN;
}
int res = aos_poll(ctx->pollfds, readers, delayed_ms);
if (res < 0 && errno != EINTR) {
LOGE(TAG, "aos_poll");
return;
}
/* check if some registered timeouts have occurred */
if (!dlist_empty(&ctx->timeouts)) {
yloop_timeout_t *tmo = dlist_first_entry(&ctx->timeouts, yloop_timeout_t, next);
long long now = aos_now_ms();
if (now >= tmo->timeout_ms) {
dlist_del(&tmo->next);
tmo->cb(tmo->private_data);
aos_free(tmo);
}
}
if (res <= 0) {
continue;
}
for (i = 0; i < readers; i++) {
if (ctx->pollfds[i].revents & POLLIN) {
ctx->readers[i].cb(
ctx->readers[i].sock,
ctx->readers[i].private_data);
}
}
}
ctx->terminate = 0;
}
void aos_loop_exit(void)
{
yloop_ctx_t *ctx = get_context();
ctx->terminate = 1;
}
void aos_loop_destroy(void)
{
yloop_ctx_t *ctx = _get_context();
if (ctx == NULL) {
return;
}
aos_event_service_deinit(ctx->eventfd);
while (!dlist_empty(&ctx->timeouts)) {
yloop_timeout_t *timeout = dlist_first_entry(&ctx->timeouts, yloop_timeout_t,
next);
dlist_del(&timeout->next);
aos_free(timeout);
}
aos_free(ctx->readers);
aos_free(ctx->pollfds);
_set_context(NULL);
if (ctx == g_main_ctx) {
g_main_ctx = NULL;
}
aos_free(ctx);
}

View file

@ -0,0 +1,37 @@
/*
* Copyright (C) 2015-2017 Alibaba Group Holding Limited
*/
#ifndef YLOOP_H
#define YLOOP_H
typedef struct {
int sock;
void *private_data;
aos_poll_call_t cb;
} yloop_sock_t;
typedef struct {
dlist_t timeouts;
struct pollfd *pollfds;
yloop_sock_t *readers;
int eventfd;
uint8_t max_sock;
uint8_t reader_count;
bool pending_terminate;
bool terminate;
} yloop_ctx_t;
/* set loop's event fd */
void aos_loop_set_eventfd(int fd);
/* get loop's event fd */
int aos_loop_get_eventfd(void *loop);
/* init per-loop event service */
int aos_event_service_init(void);
/* deinit per-loop event service */
void aos_event_service_deinit(int fd);
#endif /* YLOOP_H */

View file

@ -0,0 +1,22 @@
NAME := yloop
$(NAME)_COMPONENTS := log vfs
$(NAME)_TYPE := kernel
$(NAME)_MBINS_TYPE := kernel
$(NAME)_SOURCES := yloop.c
$(NAME)_SOURCES += local_event.c
#default gcc
ifeq ($(COMPILER),)
$(NAME)_CFLAGS += -Wall -Werror
else ifeq ($(COMPILER),gcc)
$(NAME)_CFLAGS += -Wall -Werror
endif
GLOBAL_DEFINES += AOS_LOOP
ifeq ($(COMPILER),armcc)
$(NAME)_LINK_FILES := local_event.o
endif