Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
bummer. Let's add all the needed files to the svn (sorry)
[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   s_smx_ctx_base_t super;  /* Fields of super implementation */
28
29 #ifdef KILLME
30   /* Ucontext info */
31   ucontext_t uc;                /* the thread that execute the code */
32   char stack[STACK_SIZE];       /* the thread stack size */
33   struct s_smx_ctx_sysv *prev;           /* the previous process */
34 #ifdef HAVE_VALGRIND_VALGRIND_H
35   unsigned int valgrind_stack_id;       /* the valgrind stack id */
36 #endif
37 #endif /* KILLME */
38
39   /* lua state info */
40   lua_State *state;
41   int ref; /* to prevent the lua GC from collecting my threads, I ref them explicitely */
42   int nargs; /* argument to lua_resume. First time: argc-1, afterward: 0 */
43 } s_smx_ctx_lua_t, *smx_ctx_lua_t;
44
45 static lua_State *lua_state;
46
47 static smx_context_t 
48 smx_ctx_lua_create_context(xbt_main_func_t code, int argc, char** argv, 
49     void_f_pvoid_t cleanup_func, void* cleanup_arg);
50
51 static int smx_ctx_lua_factory_finalize(smx_context_factory_t *factory);
52
53 static void smx_ctx_lua_free(smx_context_t context);
54 static void smx_ctx_lua_stop(smx_context_t context);
55 static void smx_ctx_lua_suspend(smx_context_t context);
56 static void smx_ctx_lua_resume(smx_context_t new_context);
57
58 static void smx_ctx_sysv_wrapper(void);
59
60 /* Actually, the parameter is a lua_State*, but it got anonymized because that function
61  * is defined in a global header where lua may not be defined */
62 void SIMIX_ctx_lua_factory_set_state(void* state) {
63   lua_state = state;
64 }
65 void SIMIX_ctx_lua_factory_init(smx_context_factory_t *factory) {
66
67   smx_ctx_base_factory_init(factory);
68
69   (*factory)->create_context = smx_ctx_lua_create_context;
70   (*factory)->finalize = smx_ctx_lua_factory_finalize;
71   (*factory)->free = smx_ctx_lua_free;
72   (*factory)->stop = smx_ctx_lua_stop;
73   (*factory)->suspend = smx_ctx_lua_suspend;
74   (*factory)->resume = smx_ctx_lua_resume;
75   (*factory)->name = "smx_lua_context_factory";
76
77   INFO0("Lua Factory created");
78 }
79
80 static int smx_ctx_lua_factory_finalize(smx_context_factory_t * factory) {
81   lua_close(lua_state);
82
83   return smx_ctx_base_factory_finalize(factory);
84 }
85
86 static smx_context_t 
87 smx_ctx_lua_create_context(xbt_main_func_t code, int argc, char** argv, 
88     void_f_pvoid_t cleanup_func, void* cleanup_arg) {
89
90   smx_ctx_lua_t context = xbt_new0(s_smx_ctx_lua_t, 1);
91
92   /* If the user provided a function for the process then use it
93      otherwise is the context for maestro */
94   if (code){
95     context->super.code = code;
96
97     context->super.argc = argc;
98     context->super.argv = argv;
99     context->super.cleanup_func = cleanup_func;
100     context->super.cleanup_arg = cleanup_arg;
101     INFO1("Created context for function %s",argv[0]);
102
103     /* start the coroutine in charge of running that code */
104     context->state = lua_newthread(lua_state);
105     context->ref = luaL_ref(lua_state, LUA_REGISTRYINDEX); // protect the thread from being garbage collected
106
107     /* Start the co-routine */
108     lua_getglobal(context->state,context->super.argv[0]);
109     xbt_assert1(lua_isfunction(context->state,-1),
110         "The lua function %s does not seem to exist",context->super.argv[0]);
111
112     // push arguments onto the stack
113     int i;
114     for(i=1;i<context->super.argc;i++)
115       lua_pushstring(context->state,context->super.argv[i]);
116
117     // Call the function (in resume)
118     context->nargs = context->super.argc-1;
119
120   } else {
121     INFO0("Created context for maestro");
122   }
123
124   return (smx_context_t)context;
125 }
126
127 static void smx_ctx_lua_free(smx_context_t context) {
128
129   if (context){
130     DEBUG1("smx_ctx_lua_free_context(%p)",context);
131
132     /* let the lua garbage collector reclaim the thread used for the coroutine */
133     luaL_unref(lua_state,LUA_REGISTRYINDEX,((smx_ctx_lua_t)context)->ref );
134   }
135
136   smx_ctx_base_free(context);
137 }
138
139 static void smx_ctx_lua_stop(smx_context_t pcontext) {
140   smx_ctx_lua_t context = (smx_ctx_lua_t)pcontext;
141
142   INFO1("Stopping '%s' (nothing to do)",context->super.argv[0]);
143   if (context->super.cleanup_func)
144     (*context->super.cleanup_func) (context->super.cleanup_arg);
145
146 //  smx_ctx_lua_suspend(pcontext);
147 }
148
149 static void smx_ctx_lua_suspend(smx_context_t pcontext) {
150   smx_ctx_lua_t context = (smx_ctx_lua_t)pcontext;
151   DEBUG1("Suspending '%s' (calling lua_yield)",context->super.argv[0]);
152   //lua_yield(context->state,0);
153
154   lua_getglobal(context->state,"doyield");
155   xbt_assert0(lua_isfunction(context->state,-1),
156       "Cannot find the coroutine.yield function...");
157   INFO0("Call coroutine.yield");
158   lua_call(context->state,0,0);
159   INFO0("Back from call to coroutine.yield");
160 }
161
162 static void 
163 smx_ctx_lua_resume(smx_context_t new_context) {
164   smx_ctx_lua_t context = (smx_ctx_lua_t)new_context;
165   DEBUG1("Resuming %s",context->super.argv[0]);
166   int ret = lua_resume(context->state,context->nargs);
167   INFO3("Function %s yielded back with value %d %s",context->super.argv[0],ret,(ret==LUA_YIELD?"(ie, LUA_YIELD)":""));
168   if (lua_isstring(context->state,-1))
169     INFO2("Result of %s seem to be '%s'",context->super.argv[0],luaL_checkstring(context->state,-1));
170   context->nargs=0;
171 }