Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Merge context_start into context_new to simplify the soup
[simgrid.git] / src / simix / smx_context_ruby.c
1 /* context_Ruby - implementation of context switching with/for ruby         */
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 "private.h"
9 #include "xbt/function_types.h"
10 #include "xbt/sysdep.h"
11 #include "xbt/log.h"
12 #include "xbt/asserts.h"
13
14 #include "bindings/ruby_bindings.h"
15
16 XBT_LOG_EXTERNAL_DEFAULT_CATEGORY(ruby);
17
18 static smx_context_t
19 smx_ctx_ruby_create_context(xbt_main_func_t code,int argc,char** argv,
20     void_f_pvoid_t cleanup_func,void *cleanup_arg);
21
22 static int smx_ctx_ruby_factory_finalize(smx_context_factory_t *factory);
23 static void smx_ctx_ruby_free(smx_context_t context);
24 static void smx_ctx_ruby_stop(smx_context_t context);
25 static void smx_ctx_ruby_suspend(smx_context_t context);
26 static void 
27 smx_ctx_ruby_resume(smx_context_t old_context,smx_context_t new_context);
28 static void smx_ctx_ruby_wrapper(void); 
29
30
31 void SIMIX_ctx_ruby_factory_init(smx_context_factory_t *factory) {
32   *factory = xbt_new0(s_smx_context_factory_t,1);
33
34   (*factory)->create_context = smx_ctx_ruby_create_context;
35   (*factory)->finalize = smx_ctx_ruby_factory_finalize;
36   (*factory)->free = smx_ctx_ruby_free;
37   (*factory)->stop = smx_ctx_ruby_stop;
38   (*factory)->suspend = smx_ctx_ruby_suspend;
39   (*factory)->resume = smx_ctx_ruby_resume;
40   (*factory)->name = "smx_ruby_context_factory";
41   ruby_init();
42   ruby_init_loadpath();
43 }
44
45 static int smx_ctx_ruby_factory_finalize(smx_context_factory_t *factory) {
46   free(*factory);
47   *factory = NULL;
48   return 0;
49 }
50
51 static smx_context_t 
52 smx_ctx_ruby_create_context(xbt_main_func_t code,int argc,char** argv,
53     void_f_pvoid_t cleanup_func,void* cleanup_arg) {
54
55   smx_ctx_ruby_t context = xbt_new0(s_smx_ctx_ruby_t,1);
56
57   /* if the user provided a function for the process , then use it
58      Otherwise it's the context for maestro */
59   if (code) {
60     context->cleanup_func = cleanup_func;
61     context->cleanup_arg = cleanup_arg;
62     context->process = (VALUE)code;
63     context->argc=argc;
64     context->argv=argv;
65
66     DEBUG1("smx_ctx_ruby_create_context(%s)...Done",argv[0]);
67   }
68   return (smx_context_t) context;
69 }
70
71 // FIXME 
72 static void smx_ctx_ruby_free(smx_context_t context) {
73   int i;
74  if (context) {
75     DEBUG1("smx_ctx_ruby_free_context(%p)",context);
76     /* free argv */
77     if (context->argv) {
78       for (i = 0; i < context->argc; i++)
79         if (context->argv[i])
80           free(context->argv[i]);
81
82       free(context->argv);
83     }
84     free (context);
85     context = NULL;
86   }
87 }
88
89 static void smx_ctx_ruby_stop(smx_context_t context) {
90   DEBUG0("smx_ctx_ruby_stop()");
91   VALUE process = Qnil;
92   smx_ctx_ruby_t ctx_ruby,current;
93
94   if ( context->cleanup_func)
95     (*(context->cleanup_func)) (context->cleanup_arg);
96
97   ctx_ruby = (smx_ctx_ruby_t) context;
98   
99   if ( simix_global->current_process->iwannadie ) {
100     if( ctx_ruby->process ) {
101       
102       //if the Ruby Process still Alive ,let's Schedule it
103       if ( rb_process_isAlive( ctx_ruby->process ) ) {
104
105         current = (smx_ctx_ruby_t)simix_global->current_process->context;       
106         rb_process_schedule(current->process);
107         process = ctx_ruby->process;
108         // interupt/kill The Ruby Process
109         rb_process_kill_up(process);
110       }
111     }
112   } else {
113
114     if (ctx_ruby->process) 
115      ctx_ruby->process = Qnil;
116
117   }
118 }
119
120 static void smx_ctx_ruby_suspend(smx_context_t context) {
121
122     DEBUG1("smx_ctx_ruby_suspend(%s)",context->argv[0]);
123     smx_ctx_ruby_t ctx_ruby = (smx_ctx_ruby_t) context;
124     if (ctx_ruby->process)
125       rb_process_unschedule(ctx_ruby->process);
126 }
127
128 static void smx_ctx_ruby_resume(smx_context_t old_context,smx_context_t new_context) {
129   DEBUG2("smx_ctx_ruby_resume(%s,%s)",
130       (old_context->argc?old_context->argv[0]:"maestro"),
131       (new_context->argc?new_context->argv[0]:"maestro"));
132
133   smx_ctx_ruby_t ctx_ruby = (smx_ctx_ruby_t) new_context;
134   rb_process_schedule(ctx_ruby->process);
135
136 }