Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
5d5d3767244fdc31e3b836ee9bb722769649a113
[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 <lua5.1/lauxlib.h>
9 #include <lua5.1/lualib.h>
10
11 /* lua can run with ultra tiny stacks since the user code lives in lua stacks, not the main one */
12 //#define CONTEXT_STACK_SIZE 4*1024
13 #include "smx_context_sysv_private.h"
14
15 XBT_LOG_EXTERNAL_DEFAULT_CATEGORY(lua);
16
17 lua_State *simgrid_lua_state;
18
19 static smx_context_t 
20 smx_ctx_lua_create_context(xbt_main_func_t code, int argc, char** argv, 
21     void_f_pvoid_t cleanup_func, void* cleanup_arg);
22
23 static int smx_ctx_lua_factory_finalize(smx_context_factory_t *factory);
24
25 static void smx_ctx_lua_free(smx_context_t context);
26 static void smx_ctx_lua_stop(smx_context_t context);
27 static void smx_ctx_lua_suspend(smx_context_t context);
28 static void smx_ctx_lua_resume(smx_context_t new_context);
29
30
31 void SIMIX_ctx_lua_factory_init(smx_context_factory_t *factory) {
32
33   smx_ctx_base_factory_init(factory);
34
35   (*factory)->create_context = smx_ctx_lua_create_context;
36   /* don't override (*factory)->finalize */;
37   (*factory)->free = smx_ctx_sysv_free;
38   (*factory)->stop = smx_ctx_sysv_stop;
39   (*factory)->suspend = smx_ctx_sysv_suspend;
40   (*factory)->resume = smx_ctx_sysv_resume;
41   (*factory)->name = "smx_lua_context_factory";
42
43   INFO0("Lua Factory created");
44 }
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   return smx_ctx_sysv_create_context_sized(sizeof(s_smx_ctx_sysv_t),
51       code,argc,argv,cleanup_func,cleanup_arg);
52 }
53 #if KILLME
54 static void smx_ctx_lua_free(smx_context_t context) {
55
56   if (context){
57     DEBUG1("smx_ctx_lua_free_context(%p)",context);
58
59   }
60
61   smx_ctx_sysv_free(context);
62 }
63
64 static void smx_ctx_lua_stop(smx_context_t pcontext) {
65   DEBUG1("Stopping '%s' (nothing to do)",pcontext->argv[0]);
66   smx_ctx_sysv_stop(pcontext);
67 }
68
69 static void smx_ctx_lua_suspend(smx_context_t pcontext) {
70   smx_ctx_lua_t context = (smx_ctx_lua_t)pcontext;
71   DEBUG1("Suspending '%s' (using sysv facilities)",context->super.super.argv[0]);
72   smx_ctx_sysv_suspend(pcontext);
73
74   DEBUG0("Back from call yielding");
75 }
76
77 static void 
78 smx_ctx_lua_resume(smx_context_t new_context) {
79   smx_ctx_lua_t context = (smx_ctx_lua_t)new_context;
80   DEBUG1("Resuming %s",context->super.super.argv[0]);
81   smx_ctx_sysv_resume(new_context);
82 }
83 #endif