-
Notifications
You must be signed in to change notification settings - Fork 234
/
Copy pathtask.c
96 lines (89 loc) · 2.52 KB
/
task.c
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
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <string.h>
#include <stdbool.h>
#include <rte_launch.h>
#include "task.h"
lua_State* launch_lua() {
lua_State* L = luaL_newstate();
luaL_openlibs(L);
(void) luaL_dostring(L, "package.path = package.path .. ';lua/include/?.lua;lua/include/?/init.lua;lua/include/lib/?/init.lua'");
(void) luaL_dostring(L, "package.path = package.path .. ';../lua/include/?.lua;../lua/include/?/init.lua;../lua/include/lib/?/init.lua'");
if (luaL_dostring(L, "require 'main'")) {
printf("Could not run main script: %s\n", lua_tostring(L, -1));
}
return L;
}
int lua_core_main(void* arg) {
int rc = -1;
struct lua_core_config* cfg = (struct lua_core_config*) arg;
lua_State* L = launch_lua();
if (!L) {
goto error;
}
lua_getglobal(L, "main");
lua_pushstring(L, "slave");
for (int i = 0; i < cfg->argc; i++) {
struct lua_core_arg* arg = cfg->argv[i];
switch (arg->arg_type) {
case ARG_TYPE_STRING:
lua_pushstring(L, arg->arg.str);
break;
case ARG_TYPE_NUMBER:
lua_pushnumber(L, arg->arg.number);
break;
case ARG_TYPE_BOOLEAN:
lua_pushboolean(L, arg->arg.boolean);
break;
case ARG_TYPE_POINTER:
lua_pushlightuserdata(L, arg->arg.ptr);
break;
case ARG_TYPE_NIL:
lua_pushnil(L);
break;
}
}
if (lua_pcall(L, cfg->argc + 1, 0, 0)) {
printf("Lua error: %s\n", lua_tostring(L, -1));
goto error;
}
rc = 0;
error:
for (int i = 0; i < cfg->argc; i++) {
struct lua_core_arg* arg = cfg->argv[i];
if (arg->arg_type == ARG_TYPE_STRING) {
free(arg->arg.str);
}
free(arg);
}
free(cfg);
return rc;
}
void launch_lua_core(int core, int argc, struct lua_core_arg* argv[]) {
struct lua_core_config* cfg = (struct lua_core_config*) malloc(sizeof(struct lua_core_config));
cfg->argc = argc;
cfg->argv = (struct lua_core_arg**) malloc(argc * sizeof(struct lua_core_arg*));
for (int i = 0; i < argc; i++) {
struct lua_core_arg* arg = cfg->argv[i] = (struct lua_core_arg*) malloc(sizeof(struct lua_core_arg));
arg->arg_type = argv[i]->arg_type;
switch (arg->arg_type) {
case ARG_TYPE_STRING:
arg->arg.str = malloc(strlen(argv[i]->arg.str) + 1);
strcpy(arg->arg.str, argv[i]->arg.str);
break;
case ARG_TYPE_NUMBER:
arg->arg.number = argv[i]->arg.number;
break;
case ARG_TYPE_BOOLEAN:
arg->arg.boolean = argv[i]->arg.boolean;
break;
case ARG_TYPE_POINTER:
arg->arg.ptr = argv[i]->arg.ptr;
break;
case ARG_TYPE_NIL:
break;
}
}
rte_eal_remote_launch(&lua_core_main, cfg, core);
}