Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
lame attempt to fix it, but I guess I should remove it and use ctest now
[simgrid.git] / src / simix / smx_context_lua.c
1 /* context_lua - implementation of context switching with lua coroutines */
2
3 /* Copyright (c) 2010 the SimGrid team. All right reserved */
4
5 /* This program is free software; you can redistribute it and/or modify it
6  * under the terms of the license (GNU LGPL) which comes with this package. */
7
8 #include "smx_context_private.h"
9 #include <lua5.1/lauxlib.h>
10 #include <lua5.1/lualib.h>
11
12 /* lower this if you want to reduce the memory consumption  */
13 //#define STACK_SIZE 128*1024
14
15 XBT_LOG_EXTERNAL_DEFAULT_CATEGORY(lua);
16
17 typedef struct s_smx_ctx_lua {
18   s_smx_ctx_sysv_t super;  /* Fields of super implementation */
19
20   /* lua state info */
21   lua_State *state;
22   int ref; /* to prevent the lua GC from collecting my threads, I ref them explicitely */
23   int nargs; /* argument to lua_resume. First time: argc-1, afterward: 0 */
24 } s_smx_ctx_lua_t, *smx_ctx_lua_t;
25
26 static lua_State *lua_state;
27
28 static smx_context_t 
29 smx_ctx_lua_create_context(xbt_main_func_t code, int argc, char** argv, 
30     void_f_pvoid_t cleanup_func, void* cleanup_arg);
31
32 static int smx_ctx_lua_factory_finalize(smx_context_factory_t *factory);
33
34 static void smx_ctx_lua_free(smx_context_t context);
35 static void smx_ctx_lua_stop(smx_context_t context);
36 static void smx_ctx_lua_suspend(smx_context_t context);
37 static void smx_ctx_lua_resume(smx_context_t new_context);
38
39 static void smx_ctx_sysv_wrapper(void);
40
41 /* Actually, the parameter is a lua_State*, but it got anonymized because that function
42  * is defined in a global header where lua may not be defined */
43 void SIMIX_ctx_lua_factory_set_state(void* state) {
44   lua_state = state;
45 }
46 void SIMIX_ctx_lua_factory_init(smx_context_factory_t *factory) {
47
48   smx_ctx_base_factory_init(factory);
49
50   (*factory)->create_context = smx_ctx_lua_create_context;
51   (*factory)->finalize = smx_ctx_lua_factory_finalize;
52   (*factory)->free = smx_ctx_lua_free;
53   (*factory)->stop = smx_ctx_lua_stop;
54   (*factory)->suspend = smx_ctx_lua_suspend;
55   (*factory)->resume = smx_ctx_lua_resume;
56   (*factory)->name = "smx_lua_context_factory";
57
58   INFO0("Lua Factory created");
59 }
60
61 static int smx_ctx_lua_factory_finalize(smx_context_factory_t * factory) {
62   lua_close(lua_state);
63
64   return smx_ctx_base_factory_finalize(factory);
65 }
66
67 static smx_context_t 
68 smx_ctx_lua_create_context(xbt_main_func_t code, int argc, char** argv, 
69     void_f_pvoid_t cleanup_func, void* cleanup_arg) {
70
71   smx_ctx_lua_t context = (smx_ctx_lua_t)smx_ctx_sysv_create_context_sized
72       (sizeof(smx_ctx_lua_t), code,argc,argv,cleanup_func,cleanup_arg);
73
74
75   /* If the user provided a function for the process then use it
76      otherwise is the context for maestro */
77   if (code){
78     INFO1("Created context for function %s",argv[0]);
79
80     /* start the coroutine in charge of running that code */
81     context->state = lua_newthread(lua_state);
82     context->ref = luaL_ref(lua_state, LUA_REGISTRYINDEX); // protect the thread from being garbage collected
83
84     /* Start the co-routine */
85     lua_getglobal(context->state,context->super.super.argv[0]);
86     xbt_assert1(lua_isfunction(context->state,-1),
87         "The lua function %s does not seem to exist",context->super.super.argv[0]);
88
89     // push arguments onto the stack
90     int i;
91     for(i=1;i<context->super.super.argc;i++)
92       lua_pushstring(context->state,context->super.super.argv[i]);
93
94     // Call the function (in resume)
95     context->nargs = context->super.super.argc-1;
96
97   } else {
98     INFO0("Created context for maestro");
99   }
100
101   return (smx_context_t)context;
102 }
103
104 static void smx_ctx_lua_free(smx_context_t context) {
105
106   if (context){
107     DEBUG1("smx_ctx_lua_free_context(%p)",context);
108
109     /* let the lua garbage collector reclaim the thread used for the coroutine */
110     luaL_unref(lua_state,LUA_REGISTRYINDEX,((smx_ctx_lua_t)context)->ref );
111   }
112
113   smx_ctx_base_free(context);
114 }
115
116 static void smx_ctx_lua_stop(smx_context_t pcontext) {
117   smx_ctx_lua_t context = (smx_ctx_lua_t)pcontext;
118
119   INFO1("Stopping '%s' (nothing to do)",context->super.super.argv[0]);
120   if (context->super.super.cleanup_func)
121     (*context->super.super.cleanup_func) (context->super.super.cleanup_arg);
122
123 //  smx_ctx_lua_suspend(pcontext);
124 }
125
126 static void smx_ctx_lua_suspend(smx_context_t pcontext) {
127   smx_ctx_lua_t context = (smx_ctx_lua_t)pcontext;
128   DEBUG1("Suspending '%s' (calling lua_yield)",context->super.super.argv[0]);
129   //lua_yield(context->state,0);
130
131   lua_getglobal(context->state,"doyield");
132   xbt_assert0(lua_isfunction(context->state,-1),
133       "Cannot find the coroutine.yield function...");
134   INFO0("Call coroutine.yield");
135   lua_call(context->state,0,0);
136   INFO0("Back from call to coroutine.yield");
137 }
138
139 static void 
140 smx_ctx_lua_resume(smx_context_t new_context) {
141   smx_ctx_lua_t context = (smx_ctx_lua_t)new_context;
142   DEBUG1("Resuming %s",context->super.super.argv[0]);
143   int ret = lua_resume(context->state,context->nargs);
144   INFO3("Function %s yielded back with value %d %s",context->super.super.argv[0],ret,(ret==LUA_YIELD?"(ie, LUA_YIELD)":""));
145   if (lua_isstring(context->state,-1))
146     INFO2("Result of %s seem to be '%s'",context->super.super.argv[0],luaL_checkstring(context->state,-1));
147   context->nargs=0;
148 }