1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
|
/*
* Copyright (C) 2015 - 2018, IBEROXARXA SERVICIOS INTEGRALES, S.L.
* Copyright (C) 2015 - 2018, Jaume Olivé Petrus (jolive@whitecatboard.org)
*
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* * Neither the name of the <organization> nor the
* names of its contributors may be used to endorse or promote products
* derived from this software without specific prior written permission.
* * The WHITECAT logotype cannot be changed, you can remove it, but you
* cannot change it in any way. The WHITECAT logotype is:
*
* /\ /\
* / \_____/ \
* /_____________\
* W H I T E C A T
*
* * Redistributions in binary form must retain all copyright notices printed
* to any local or remote output device. This include any reference to
* Lua RTOS, whitecatboard.org, Lua, and other copyright notices that may
* appear in the future.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL <COPYRIGHT HOLDER> BE LIABLE FOR ANY
* DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
* Lua RTOS, FreeRTOS additions
*
*/
//#include "luartos.h"
//#include "esp_attr.h"
//#include "lua.h"
#include <FreeRTOS.h>
#include <task.h>
#include "adds.h"
#include <stdint.h>
#include <string.h>
#include <stdlib.h>
#include <malloc.h>
void panic(const char*);
#define THREAD_LOCAL_STORAGE_POINTER_ID 0
//// Reference to lua_thread, which is created in app_main
//extern pthread_t lua_thread;
//// Global state
//static lua_State *gL = NULL;
static int compare(const void *a, const void *b) {
if (((task_info_t *)a)->task_type < ((task_info_t *)b)->task_type) {
return 1;
} else if (((task_info_t*)a)->task_type > ((task_info_t *)b)->task_type) {
return -1;
} else {
if (((task_info_t *)a)->core < ((task_info_t *)b)->core) {
return -1;
} else if (((task_info_t *)a)->core > ((task_info_t *)b)->core) {
return 1;
} else {
return strcmp(((task_info_t *)a)->name, ((task_info_t *)b)->name);
}
}
}
void uxSetThreadId(UBaseType_t id) {
lua_rtos_tcb_t *lua_rtos_tcb;
// Get Lua RTOS specific TCB parts for current task
if ((lua_rtos_tcb = pvTaskGetThreadLocalStoragePointer(xTaskGetCurrentTaskHandle(), THREAD_LOCAL_STORAGE_POINTER_ID))) {
// Store thread id into Lua RTOS specific TCB parts
lua_rtos_tcb->threadid = id;
}
}
UBaseType_t uxGetThreadId() {
lua_rtos_tcb_t *lua_rtos_tcb;
int threadid = 0;
// Get Lua RTOS specific TCB parts for current task
if ((lua_rtos_tcb = pvTaskGetThreadLocalStoragePointer(xTaskGetCurrentTaskHandle(), THREAD_LOCAL_STORAGE_POINTER_ID))) {
// Get current thread od from Lua RTOS specific TCB parts
threadid = lua_rtos_tcb->threadid;
}
return threadid;
}
void uxSetThreadStatus(TaskHandle_t h, pthread_status_t status) {
lua_rtos_tcb_t *lua_rtos_tcb;
// Get Lua RTOS specific TCB parts for current task
if ((lua_rtos_tcb = pvTaskGetThreadLocalStoragePointer(xTaskGetCurrentTaskHandle(), THREAD_LOCAL_STORAGE_POINTER_ID))) {
// Store thread id into Lua RTOS specific TCB parts
lua_rtos_tcb->status = status;
}
}
void uxSetLThread(lthread_t *lthread) {
lua_rtos_tcb_t *lua_rtos_tcb;
// Get Lua RTOS specific TCB parts for current task
if ((lua_rtos_tcb = pvTaskGetThreadLocalStoragePointer(xTaskGetCurrentTaskHandle(), THREAD_LOCAL_STORAGE_POINTER_ID))) {
lua_rtos_tcb->lthread = lthread;
}
}
void uxSetLuaState(lua_State* L) {
lua_rtos_tcb_t *lua_rtos_tcb;
// Get Lua RTOS specific TCB parts for current task
if ((lua_rtos_tcb = pvTaskGetThreadLocalStoragePointer(xTaskGetCurrentTaskHandle(), THREAD_LOCAL_STORAGE_POINTER_ID))) {
// Store current lua state into Lua RTOS specific TCB parts
if (!lua_rtos_tcb->lthread) {
lua_rtos_tcb->lthread = calloc(1, sizeof(lthread_t));
if (lua_rtos_tcb->lthread == NULL) {
panic("No thread TLS!");
}
//assert(lua_rtos_tcb->lthread);
}
lua_rtos_tcb->lthread->L = L;
}
// // If this is the lua thread, store state in global state
// if ((uxGetThreadId() == lua_thread) && (gL == NULL)) {
// gL = L;
// }
}
lua_State* pvGetLuaState() {
lua_rtos_tcb_t *lua_rtos_tcb;
lua_State *L = NULL;
// Get Lua RTOS specific TCB parts for current task
if ((lua_rtos_tcb = pvTaskGetThreadLocalStoragePointer(NULL, THREAD_LOCAL_STORAGE_POINTER_ID))) {
// Get current lua state from Lua RTOS specific TCB parts
if (lua_rtos_tcb->lthread) {
L = lua_rtos_tcb->lthread->L;
}
}
// if (L == NULL) {
// L = gL;
// }
return L;
}
lthread_t *pvGetLThread() {
lua_rtos_tcb_t *lua_rtos_tcb;
lthread_t *lthread = NULL;
// Get Lua RTOS specific TCB parts for current task
if ((lua_rtos_tcb = pvTaskGetThreadLocalStoragePointer(NULL, THREAD_LOCAL_STORAGE_POINTER_ID))) {
lthread = lua_rtos_tcb->lthread;
}
return lthread;
}
uint32_t uxGetSignaled(TaskHandle_t h) {
lua_rtos_tcb_t *lua_rtos_tcb;
int signaled = 0;
// Get Lua RTOS specific TCB parts for current task
if ((lua_rtos_tcb = pvTaskGetThreadLocalStoragePointer(h, THREAD_LOCAL_STORAGE_POINTER_ID))) {
// Get current signeled mask from Lua RTOS specific TCB parts
signaled = lua_rtos_tcb->signaled;
}
return signaled;
}
void uxSetSignaled(TaskHandle_t h, int s) {
lua_rtos_tcb_t *lua_rtos_tcb;
// Get Lua RTOS specific TCB parts for current task
if ((lua_rtos_tcb = pvTaskGetThreadLocalStoragePointer(h, THREAD_LOCAL_STORAGE_POINTER_ID))) {
// Store current signaled mask into Lua RTOS specific TCB parts
lua_rtos_tcb->signaled = s;
}
}
uint8_t ucGetCoreID(TaskHandle_t h) {
tskTCB_t *task = (tskTCB_t *)h;
return task->xCoreID;
}
int uxGetStack(TaskHandle_t h) {
tskTCB_t *task = (tskTCB_t *)h;
return task->pxEndOfStack - task->pxStack + 4;
}
task_info_t *GetTaskInfo() {
tskTCB_t *ctask;
task_info_t *info;
uint8_t task_type;
lua_rtos_tcb_t *lua_rtos_tcb;
TaskStatus_t *status_array;
UBaseType_t task_num = 0;
UBaseType_t start_task_num = 0;
uint32_t total_runtime = 0;
//Allocate status_array
start_task_num = uxTaskGetNumberOfTasks();
status_array = (TaskStatus_t *)calloc(start_task_num, sizeof(TaskStatus_t));
if (!status_array) {
return NULL;
}
//#ifndef CONFIG_FREERTOS_USE_TRACE_FACILITY
//#warning Please enable CONFIG_FREERTOS_USE_TRACE_FACILITY to support thread.list
// free(status_array);
// return NULL;
#ifndef configUSE_TRACE_FACILITY
#warning Please enable configUSE_TRACE_FACILITY to support thread.list
free(status_array);
return NULL;
#else
task_num = uxTaskGetSystemState(status_array, (start_task_num), &total_runtime);
// For percentage calculations.
total_runtime /= 100UL;
info = (task_info_t *)calloc(task_num + 1, sizeof(task_info_t));
if (!info) {
free(status_array);
return NULL;
}
#endif
for(int i = 0; i <task_num; i++){
if (status_array[i].eCurrentState == eDeleted) {
continue;
}
// Get the task TCB
ctask = (tskTCB_t *)status_array[i].xHandle;
// Get the task type
// 0: freertos task
// 1: pthread task
// 2: lua thread task
task_type = 0;
info[i].thid = 0;
info[i].status = 0;
// Get Lua RTOS specific TCB parts for current task
if ((lua_rtos_tcb = pvTaskGetThreadLocalStoragePointer(status_array[i].xHandle, THREAD_LOCAL_STORAGE_POINTER_ID))) {
// Task has Lua RTOS specific TCB parts
if (lua_rtos_tcb->lthread) {
// Lua thread
task_type = 2;
} else {
// pthread
task_type = 1;
}
info[i].thid = lua_rtos_tcb->threadid;
info[i].lthread = lua_rtos_tcb->lthread;
info[i].status = lua_rtos_tcb->status;
}
// Populate info item
info[i].prio = status_array[i].uxCurrentPriority;
info[i].task_type = task_type;
info[i].core = ctask->xCoreID;
// Some system tasks shows 255!!
if (info[i].core > 1) {
info[i].core = 0;
}
info[i].free_stack = uxTaskGetStackHighWaterMark(status_array[i].xHandle);
info[i].stack_size = ctask->pxEndOfStack - ctask->pxStack + 4;
memcpy(info[i].name, status_array[i].pcTaskName, configMAX_TASK_NAME_LEN);
info[i].cpu_usage = 0;
#ifdef CONFIG_FREERTOS_GENERATE_RUN_TIME_STATS
if( total_runtime > 0 ) {
// only gives valid values if CONFIG_FREERTOS_GENERATE_RUN_TIME_STATS is defined
info[i].cpu_usage = status_array[i].ulRunTimeCounter / total_runtime;
}
#endif
}
free(status_array);
qsort (info, task_num, sizeof (task_info_t), compare);
return info;
}
|