Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
further cleanups
[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 "xbt/function_types.h"
9 #include "xbt/sysdep.h"
10 #include "xbt/log.h"
11 #include "xbt/asserts.h"
12
13 #include "smx_context_private.h"
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 void smx_ctx_ruby_stop(smx_context_t context);
23 static void smx_ctx_ruby_suspend(smx_context_t context);
24 static void smx_ctx_ruby_resume(smx_context_t new_context);
25 static void smx_ctx_ruby_wrapper(void); 
26
27
28 void SIMIX_ctx_ruby_factory_init(smx_context_factory_t *factory) {
29   smx_ctx_base_factory_init(factory);
30
31   (*factory)->create_context = smx_ctx_ruby_create_context;
32   /* Do not overload that method (*factory)->finalize */
33   /* Do not overload that method (*factory)->free */
34   (*factory)->stop = smx_ctx_ruby_stop;
35   (*factory)->suspend = smx_ctx_ruby_suspend;
36   (*factory)->resume = smx_ctx_ruby_resume;
37   (*factory)->name = "smx_ruby_context_factory";
38   ruby_init();
39   ruby_init_loadpath();
40 }
41
42 static smx_context_t 
43 smx_ctx_ruby_create_context(xbt_main_func_t code,int argc,char** argv,
44     void_f_pvoid_t cleanup_func,void* cleanup_arg) {
45
46   smx_ctx_ruby_t context = (smx_ctx_ruby_t)smx_ctx_base_factory_create_context_sized
47       (sizeof(s_smx_ctx_ruby_t), code,argc,argv,cleanup_func,cleanup_arg);
48
49   /* if the user provided a function for the process , then use it
50      Otherwise it's the context for maestro */
51   if (code) {
52     context->process = (VALUE)code;
53
54     DEBUG1("smx_ctx_ruby_create_context(%s)...Done",argv[0]);
55   }
56   return (smx_context_t) context;
57 }
58
59 static void smx_ctx_ruby_stop(smx_context_t context) {
60   DEBUG0("smx_ctx_ruby_stop()");
61   VALUE process = Qnil;
62   smx_ctx_ruby_t ctx_ruby,current;
63
64   smx_ctx_base_stop(context);
65
66   ctx_ruby = (smx_ctx_ruby_t) context;
67   
68   if ( simix_global->current_process->iwannadie ) {
69     if( ctx_ruby->process ) {
70       
71       //if the Ruby Process still Alive ,let's Schedule it
72       if ( rb_process_isAlive( ctx_ruby->process ) ) {
73
74         current = (smx_ctx_ruby_t)simix_global->current_process->context;       
75         rb_process_schedule(current->process);
76         process = ctx_ruby->process;
77         // interupt/kill The Ruby Process
78         rb_process_kill_up(process);
79       }
80     }
81   } else {
82
83     if (ctx_ruby->process) 
84      ctx_ruby->process = Qnil;
85
86   }
87 }
88
89 static void smx_ctx_ruby_suspend(smx_context_t context) {
90
91     DEBUG1("smx_ctx_ruby_suspend(%s)",context->argv[0]);
92     smx_ctx_ruby_t ctx_ruby = (smx_ctx_ruby_t) context;
93     if (ctx_ruby->process)
94       rb_process_unschedule(ctx_ruby->process);
95 }
96
97 static void smx_ctx_ruby_resume(smx_context_t new_context) {
98   DEBUG1("smx_ctx_ruby_resume(%s)",
99       (new_context->argc?new_context->argv[0]:"maestro"));
100
101   smx_ctx_ruby_t ctx_ruby = (smx_ctx_ruby_t) new_context;
102   rb_process_schedule(ctx_ruby->process);
103
104 }