1 /*
2  * Copyright (C) 2015-2020 Alibaba Group Holding Limited
3  */
4 
5 #include "amp_task.h"
6 
7 #include "amp_list.h"
8 #include "amp_platform.h"
9 #include "aos_system.h"
10 
11 #define MOD_STR "PY_TASK"
12 #define AMP_MSGQ_WAITIME (2000)
13 #define AMP_MSGQ_MAX_NUM 10
14 
15 typedef struct {
16     dlist_t node;
17     void (*callback)(void);
18 } amp_source_node_t;
19 
20 static dlist_t g_sources_list = AMP_DLIST_HEAD_INIT(g_sources_list);
21 
22 static unsigned char g_init = 0;
23 static aos_queue_t amp_task_mq = { NULL }; /* JSEngine message queue */
24 static aos_mutex_t amp_task_mutex = { NULL }; /* JSEngine mutex */
25 
queue_deinit(aos_queue_t * queue)26 static void queue_deinit(aos_queue_t *queue)
27 {
28     aos_queue_free(queue);
29 }
30 
mutex_deinit(aos_mutex_t * mutex)31 static void mutex_deinit(aos_mutex_t *mutex)
32 {
33     aos_mutex_free(mutex);
34 }
35 
py_task_yield(uint32_t timeout)36 int32_t py_task_yield(uint32_t timeout)
37 {
38     int32_t ret = 0;
39     size_t revsize;
40 
41     amp_task_msg_t amp_msg;
42 
43     memset(&amp_msg, 0, sizeof(amp_msg));
44     amp_msg.type = AMP_TASK_MSG_TYPE_MAX;
45 
46     if ((ret = aos_queue_recv(&amp_task_mq, timeout, &amp_msg, &revsize)) != 0) {
47         return -1;
48     }
49 
50     if (amp_msg.type == AMP_TASK_MSG_CALLBACK) {
51         amp_msg.callback(amp_msg.param);
52     }
53 
54     else if (amp_msg.type == AMP_TASK_MSG_EXIT) {
55         return 1;
56     }
57 
58     return 0;
59 }
60 
py_task_schedule_call(amp_engine_call_t call,void * arg)61 int32_t py_task_schedule_call(amp_engine_call_t call, void *arg)
62 {
63     printf("************* py_task_schedule_call START  *******\r\n");
64 
65     amp_task_msg_t msg_buf;
66     amp_task_msg_t *p_param = &msg_buf;
67 
68     p_param->callback = call;
69     p_param->param = arg;
70     p_param->type = AMP_TASK_MSG_CALLBACK;
71 
72     if (!g_init) {
73         amp_warn(MOD_STR, "amp_task_mq has not been initlized");
74         return -1;
75     }
76 
77     aos_queue_send(&amp_task_mq, p_param, sizeof(amp_task_msg_t));
78     printf("************* py_task_schedule_call END *******\r\n");
79 
80     return 0;
81 }
82 
py_task_exit_call(amp_engine_call_t call,void * arg)83 int32_t py_task_exit_call(amp_engine_call_t call, void *arg)
84 {
85     amp_task_msg_t msg_buf;
86     amp_task_msg_t *p_param = &msg_buf;
87 
88     memset(p_param, 0, sizeof(amp_task_msg_t));
89     p_param->callback = call;
90     p_param->param = arg;
91     p_param->type = AMP_TASK_MSG_EXIT;
92 
93     if (!g_init) {
94         amp_warn(MOD_STR, "amp_task_mq has not been initlized");
95         return -1;
96     }
97 
98     aos_mutex_lock(&amp_task_mutex, AOS_WAIT_FOREVER);
99     aos_queue_send(&amp_task_mq, p_param, sizeof(amp_task_msg_t));
100     aos_mutex_unlock(&amp_task_mutex);
101     return 0;
102 }
103 
py_task_init()104 int32_t py_task_init()
105 {
106     if (g_init) {
107         return 0;
108     }
109 
110     if (aos_queue_new(&amp_task_mq, NULL, AMP_MSGQ_MAX_NUM * sizeof(amp_task_msg_t), sizeof(amp_task_msg_t)) != 0) {
111         amp_error(MOD_STR, "create messageQ error");
112         return -1;
113     }
114     if (aos_mutex_new(&amp_task_mutex) != 0) {
115         amp_error(MOD_STR, "create mutex error");
116         return -1;
117     }
118 
119     g_init = 1;
120     printf("************* python task init*******\r\n");
121 
122     amp_debug(MOD_STR, "pyengine task init");
123     return 0;
124 }
125 
py_task_deinit()126 int32_t py_task_deinit()
127 {
128     if (g_init) {
129         queue_deinit(&amp_task_mq);
130         mutex_deinit(&amp_task_mutex);
131     }
132 
133     g_init = 0;
134 
135     amp_debug(MOD_STR, "pyengine task free");
136     return 0;
137 }
138