Logo AND Algorithmique Numérique Distribuée

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