Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Second try at lua. Still does not work: I get a 'attempt to yield across metamethod...
[simgrid.git] / src / simix / smx_context_lua.c
1 /* $Id$ */
2
3 /* context_lua - implementation of context switching with lua coroutines */
4
5 /* Copyright (c) 2004-2008 the SimGrid team. All right reserved */
6
7 /* This program is free software; you can redistribute it and/or modify it
8  * under the terms of the license (GNU LGPL) which comes with this package. */
9
10 #include "private.h"
11 //#include "context_sysv_config.h"        /* loads context system definitions */
12 //#include "portable.h"
13 //#include <ucontext.h>           /* context relative declarations */
14 #include <lua5.1/lauxlib.h>
15 #include <lua5.1/lualib.h>
16
17 /* lower this if you want to reduce the memory consumption  */
18 //#define STACK_SIZE 128*1024
19
20 //#ifdef HAVE_VALGRIND_VALGRIND_H
21 //#  include <valgrind/valgrind.h>
22 //#endif /* HAVE_VALGRIND_VALGRIND_H */
23
24 XBT_LOG_EXTERNAL_DEFAULT_CATEGORY(lua);
25
26 typedef struct s_smx_ctx_sysv {
27   SMX_CTX_BASE_T;
28 #ifdef KILLME
29   /* Ucontext info */
30   ucontext_t uc;                /* the thread that execute the code */
31   char stack[STACK_SIZE];       /* the thread stack size */
32   struct s_smx_ctx_sysv *prev;           /* the previous process */
33 #ifdef HAVE_VALGRIND_VALGRIND_H
34   unsigned int valgrind_stack_id;       /* the valgrind stack id */
35 #endif
36 #endif /* KILLME */
37
38   /* lua state info */
39   lua_State *state;
40   int ref; /* to prevent the lua GC from collecting my threads, I ref them explicitely */
41   int nargs; /* argument to lua_resume. First time: argc-1, afterward: 0 */
42 } s_smx_ctx_lua_t, *smx_ctx_lua_t;
43
44 static lua_State *lua_state;
45
46 static smx_context_t 
47 smx_ctx_lua_create_context(xbt_main_func_t code, int argc, char** argv, 
48     void_f_pvoid_t cleanup_func, void* cleanup_arg);
49
50 static int smx_ctx_lua_factory_finalize(smx_context_factory_t *factory);
51 static void smx_ctx_lua_free(smx_context_t context);
52 static void smx_ctx_lua_start(smx_context_t context);
53 static void smx_ctx_lua_stop(smx_context_t context);
54 static void smx_ctx_lua_suspend(smx_context_t context);
55
56 static void 
57 smx_ctx_lua_resume(smx_context_t old_context, smx_context_t new_context);
58
59 static void smx_ctx_sysv_wrapper(void);
60
61 /* Actually, the parameter is a lua_State*, but it got anonymized because that function
62  * is defined in a global header where lua may not be defined */
63 void SIMIX_ctx_lua_factory_set_state(void* state) {
64   lua_state = state;
65 }
66 void SIMIX_ctx_lua_factory_init(smx_context_factory_t *factory) {
67
68   *factory = xbt_new0(s_smx_context_factory_t, 1);
69
70   (*factory)->create_context = smx_ctx_lua_create_context;
71   (*factory)->finalize = smx_ctx_lua_factory_finalize;
72   (*factory)->free = smx_ctx_lua_free;
73   (*factory)->start = smx_ctx_lua_start;
74   (*factory)->stop = smx_ctx_lua_stop;
75   (*factory)->suspend = smx_ctx_lua_suspend;
76   (*factory)->resume = smx_ctx_lua_resume;
77   (*factory)->name = "smx_lua_context_factory";
78
79   INFO0("Lua Factory created");
80 }
81
82 static int smx_ctx_lua_factory_finalize(smx_context_factory_t * factory) {
83   lua_close(lua_state);
84
85   free(*factory);
86   *factory = NULL;
87   return 0;
88 }
89
90 static smx_context_t 
91 smx_ctx_lua_create_context(xbt_main_func_t code, int argc, char** argv, 
92     void_f_pvoid_t cleanup_func, void* cleanup_arg) {
93
94   smx_ctx_lua_t context = xbt_new0(s_smx_ctx_lua_t, 1);
95
96   /* If the user provided a function for the process then use it
97      otherwise is the context for maestro */
98   if (code){
99     context->code = code;
100
101     context->argc = argc;
102     context->argv = argv;
103     context->cleanup_func = cleanup_func;
104     context->cleanup_arg = cleanup_arg;
105     INFO1("Created context for function %s",argv[0]);
106
107     /* start the coroutine in charge of running that code */
108     context->state = lua_newthread(lua_state);
109     context->ref = luaL_ref(lua_state, LUA_REGISTRYINDEX); // protect the thread from being garbage collected
110     /* the actual co-routine starting is done in smx_ctx_lua_start */
111     context->nargs = argc-1;
112   } else {
113     INFO0("Created context for maestro");
114   }
115
116   return (smx_context_t)context;
117 }
118
119 static void smx_ctx_lua_free(smx_context_t pcontext)
120 {
121   int i;
122   smx_ctx_lua_t context = (smx_ctx_lua_t)pcontext;
123
124   if (context){
125     DEBUG1("smx_ctx_lua_free_context(%p)",context);
126
127     /* free argv */
128     if (context->argv) {
129       for (i = 0; i < context->argc; i++)
130         if (context->argv[i])
131           free(context->argv[i]);
132
133       free(context->argv);
134     }
135
136     /* let the lua garbage collector reclaim the thread used for the coroutine */
137     luaL_unref(lua_state,LUA_REGISTRYINDEX,context->ref );
138
139     free(context);
140     context = NULL;
141   }
142 }
143
144 static void smx_ctx_lua_start(smx_context_t pcontext) {
145   smx_ctx_lua_t context = (smx_ctx_lua_t)pcontext;
146
147   DEBUG1("Starting '%s'",context->argv[0]);
148
149   lua_getglobal(context->state,context->argv[0]);
150   xbt_assert1(lua_isfunction(context->state,-1),
151       "The lua function %s does not seem to exist",context->argv[0]);
152
153   // push arguments onto the stack
154   int i;
155   for(i=1;i<context->argc;i++)
156     lua_pushstring(context->state,context->argv[i]);
157
158   // Call the function
159   context->nargs = context->argc-1;
160 }
161
162 static void smx_ctx_lua_stop(smx_context_t pcontext) {
163   smx_ctx_lua_t context = (smx_ctx_lua_t)pcontext;
164
165   INFO1("Stopping '%s' (nothing to do)",context->argv[0]);
166   if (context->cleanup_func)
167     (*context->cleanup_func) (context->cleanup_arg);
168
169 //  smx_ctx_lua_suspend(pcontext);
170 }
171
172 static void smx_ctx_lua_suspend(smx_context_t pcontext) {
173   smx_ctx_lua_t context = (smx_ctx_lua_t)pcontext;
174   DEBUG1("Suspending '%s' (calling lua_yield)",context->argv[0]);
175   //lua_yield(context->state,0);
176
177   lua_getglobal(context->state,"doyield");
178   xbt_assert0(lua_isfunction(context->state,-1),
179       "Cannot find the coroutine.yield function...");
180   INFO0("Call coroutine.yield");
181   lua_call(context->state,0,0);
182   INFO0("Back from call to coroutine.yield");
183 }
184
185 static void 
186 smx_ctx_lua_resume(smx_context_t old_context, smx_context_t new_context) {
187   smx_ctx_lua_t context = (smx_ctx_lua_t)new_context;
188   DEBUG1("Resuming %s",context->argv[0]);
189   int ret = lua_resume(context->state,context->nargs);
190   INFO3("Function %s yielded back with value %d %s",context->argv[0],ret,(ret==LUA_YIELD?"(ie, LUA_YIELD)":""));
191   if (lua_isstring(context->state,-1))
192     INFO2("Result of %s seem to be '%s'",context->argv[0],luaL_checkstring(context->state,-1));
193   context->nargs=0;
194 }