Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
cosmetics
[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.
4  * All rights reserved.                                                     */
5
6 /* This program is free software; you can redistribute it and/or modify it
7  * under the terms of the license (GNU LGPL) which comes with this package. */
8
9 #include <lauxlib.h>
10 #include <lualib.h>
11
12 #include "smx_context_private.h"
13
14 /* We don't use that factory at all for now. This may change at some point,
15  * to reduce the amount of memory per user thread in sysv
16  */
17
18 lua_State *simgrid_lua_state;
19 void SIMIX_ctx_lua_factory_init(smx_context_factory_t * factory)
20 {
21 }
22
23 #ifdef KILLME
24 /* lua can run with ultra tiny stacks since the user code lives in lua stacks, not the main one */
25 //#define CONTEXT_STACK_SIZE 4*1024
26
27 XBT_LOG_EXTERNAL_DEFAULT_CATEGORY(lua);
28
29
30 static smx_context_t
31 smx_ctx_lua_create_context(xbt_main_func_t code, int argc, char **argv,
32                            void_f_pvoid_t cleanup_func, void *cleanup_arg);
33
34 static int smx_ctx_lua_factory_finalize(smx_context_factory_t * factory);
35
36 static void smx_ctx_lua_free(smx_context_t context);
37 static void smx_ctx_lua_stop(smx_context_t context);
38 static void smx_ctx_lua_suspend(smx_context_t context);
39 static void smx_ctx_lua_resume(smx_context_t new_context);
40
41
42 void SIMIX_ctx_lua_factory_init(smx_context_factory_t * factory)
43 {
44
45   smx_ctx_base_factory_init(factory);
46
47   (*factory)->create_context = smx_ctx_lua_create_context;
48   /* don't override (*factory)->finalize */ ;
49   (*factory)->free = smx_ctx_sysv_free;
50   (*factory)->stop = smx_ctx_sysv_stop;
51   (*factory)->suspend = smx_ctx_sysv_suspend;
52   (*factory)->resume = smx_ctx_sysv_resume;
53   (*factory)->name = "smx_lua_context_factory";
54
55   INFO0("Lua Factory created");
56 }
57
58 static smx_context_t
59 smx_ctx_lua_create_context(xbt_main_func_t code, int argc, char **argv,
60                            void_f_pvoid_t cleanup_func, void *cleanup_arg)
61 {
62
63   return smx_ctx_sysv_create_context_sized(sizeof(s_smx_ctx_sysv_t),
64                                            code, argc, argv, cleanup_func,
65                                            cleanup_arg);
66 }
67
68 #if KILLME
69 static void smx_ctx_lua_free(smx_context_t context)
70 {
71
72   if (context) {
73     DEBUG1("smx_ctx_lua_free_context(%p)", context);
74
75   }
76
77   smx_ctx_sysv_free(context);
78 }
79
80 static void smx_ctx_lua_stop(smx_context_t pcontext)
81 {
82   DEBUG1("Stopping '%s' (nothing to do)", pcontext->argv[0]);
83   smx_ctx_sysv_stop(pcontext);
84 }
85
86 static void smx_ctx_lua_suspend(smx_context_t pcontext)
87 {
88   smx_ctx_lua_t context = (smx_ctx_lua_t) pcontext;
89   DEBUG1("Suspending '%s' (using sysv facilities)",
90          context->super.super.argv[0]);
91   smx_ctx_sysv_suspend(pcontext);
92
93   DEBUG0("Back from call yielding");
94 }
95
96 static void smx_ctx_lua_resume(smx_context_t new_context)
97 {
98   smx_ctx_lua_t context = (smx_ctx_lua_t) new_context;
99   DEBUG1("Resuming %s", context->super.super.argv[0]);
100   smx_ctx_sysv_resume(new_context);
101 }
102 #endif
103
104 #endif