Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Fix compile: ruby's context factoy should respect the factory interface
[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.
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 "xbt/function_types.h"
10 #include "xbt/sysdep.h"
11 #include "xbt/log.h"
12 #include "xbt/asserts.h"
13
14 #include "bindings/smx_context.h"
15 #include "bindings/ruby_bindings.h"
16
17 XBT_LOG_EXTERNAL_DEFAULT_CATEGORY(ruby);
18
19 static smx_context_t
20 smx_ctx_ruby_create_context(xbt_main_func_t code, int argc, char **argv,
21     void_pfn_smxprocess_t cleanup_func,
22     void *data);
23
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 smx_ctx_ruby_resume(smx_context_t new_context);
27 static void smx_ctx_ruby_runall(xbt_swag_t processes);
28
29 void SIMIX_ctx_ruby_factory_init(smx_context_factory_t * factory)
30 {
31   smx_ctx_base_factory_init(factory);
32
33   (*factory)->create_context = smx_ctx_ruby_create_context;
34   /* Do not overload that method (*factory)->finalize */
35   /* Do not overload that method (*factory)->free */
36   (*factory)->stop = smx_ctx_ruby_stop;
37   (*factory)->suspend = smx_ctx_ruby_suspend;
38   (*factory)->name = "smx_ruby_context_factory";
39   (*factory)->runall = smx_ctx_ruby_runall;
40   ruby_init();
41   ruby_init_loadpath();
42 }
43
44 static smx_context_t
45 smx_ctx_ruby_create_context(xbt_main_func_t code, int argc, char **argv,
46     void_pfn_smxprocess_t cleanup_func, void *data)
47 {
48
49   smx_ctx_ruby_t context = (smx_ctx_ruby_t)
50       smx_ctx_base_factory_create_context_sized(sizeof(s_smx_ctx_ruby_t),
51                                                 code, argc, argv,
52                                                 cleanup_func, data);
53
54   /* if the user provided a function for the process , then use it
55      Otherwise it's the context for maestro */
56   if (code) {
57     context->process = (VALUE) code;
58
59     DEBUG1("smx_ctx_ruby_create_context(%s)...Done", argv[0]);
60   }
61   
62   return (smx_context_t) context;
63 }
64
65 static void smx_ctx_ruby_stop(smx_context_t context)
66 {
67   DEBUG0("smx_ctx_ruby_stop()");
68   VALUE process = Qnil;
69   smx_ctx_ruby_t ctx_ruby, current;
70
71   smx_ctx_base_stop(context);
72
73   ctx_ruby = (smx_ctx_ruby_t) context;
74
75   if (((smx_process_t)smx_current_context->data)->iwannadie) {
76     if (ctx_ruby->process) {
77
78       //if the Ruby Process still Alive ,let's Schedule it
79       if (rb_process_isAlive(ctx_ruby->process)) {
80
81         current = (smx_ctx_ruby_t) smx_current_context;
82         rb_process_schedule(current->process);
83         process = ctx_ruby->process;
84         // interupt/kill The Ruby Process
85         rb_process_kill_up(process);
86       }
87     }
88   } else {
89
90     if (ctx_ruby->process)
91       ctx_ruby->process = Qnil;
92
93   }
94 }
95
96 static void smx_ctx_ruby_suspend(smx_context_t context)
97 {
98
99   DEBUG1("smx_ctx_ruby_suspend(%s)", context->argv[0]);
100   smx_ctx_ruby_t ctx_ruby = (smx_ctx_ruby_t) context;
101   if (ctx_ruby->process)
102     rb_process_unschedule(ctx_ruby->process);
103 }
104
105 static void smx_ctx_ruby_resume(smx_context_t new_context)
106 {
107   DEBUG1("smx_ctx_ruby_resume(%s)",
108          (new_context->argc ? new_context->argv[0] : "maestro"));
109
110   smx_ctx_ruby_t ctx_ruby = (smx_ctx_ruby_t) new_context;
111   rb_process_schedule(ctx_ruby->process);
112 }
113
114 static void smx_ctx_ruby_runall(xbt_swag_t processes)
115 {
116   smx_process_t process;
117   smx_context_t old_context;
118   while ((process = xbt_swag_extract(processes))) {
119     old_context = smx_current_context;
120     smx_current_context = process->context;
121     smx_ctx_ruby_resume(smx_current_context);
122     smx_current_context = old_context;
123   } 
124 }