1 /* context_lua - implementation of context switching with lua coroutines */
3 /* Copyright (c) 2010 the SimGrid team. All right reserved */
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. */
8 #include "smx_context_private.h"
9 #include <lua5.1/lauxlib.h>
10 #include <lua5.1/lualib.h>
12 /* lower this if you want to reduce the memory consumption */
13 //#define STACK_SIZE 128*1024
15 XBT_LOG_EXTERNAL_DEFAULT_CATEGORY(lua);
17 typedef struct s_smx_ctx_lua {
18 s_smx_ctx_sysv_t super; /* Fields of super implementation */
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;
26 static lua_State *lua_state;
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);
32 static int smx_ctx_lua_factory_finalize(smx_context_factory_t *factory);
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);
39 static void smx_ctx_sysv_wrapper(void);
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) {
46 void SIMIX_ctx_lua_factory_init(smx_context_factory_t *factory) {
48 smx_ctx_base_factory_init(factory);
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";
58 INFO0("Lua Factory created");
61 static int smx_ctx_lua_factory_finalize(smx_context_factory_t * factory) {
64 return smx_ctx_base_factory_finalize(factory);
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) {
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);
75 /* If the user provided a function for the process then use it
76 otherwise is the context for maestro */
78 INFO1("Created context for function %s",argv[0]);
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
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]);
89 // push arguments onto the stack
91 for(i=1;i<context->super.super.argc;i++)
92 lua_pushstring(context->state,context->super.super.argv[i]);
94 // Call the function (in resume)
95 context->nargs = context->super.super.argc-1;
98 INFO0("Created context for maestro");
101 return (smx_context_t)context;
104 static void smx_ctx_lua_free(smx_context_t context) {
107 DEBUG1("smx_ctx_lua_free_context(%p)",context);
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 );
113 smx_ctx_base_free(context);
116 static void smx_ctx_lua_stop(smx_context_t pcontext) {
117 smx_ctx_lua_t context = (smx_ctx_lua_t)pcontext;
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);
123 // smx_ctx_lua_suspend(pcontext);
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);
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");
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));