1 /* context_Ruby - implementation of context switching with/for ruby */
3 /* Copyright (c) 2010. The SimGrid Team.
4 * All rights reserved. */
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. */
9 #include "xbt/function_types.h"
10 #include "xbt/sysdep.h"
12 #include "xbt/asserts.h"
14 #include "smx_context_private.h"
15 #include "bindings/ruby_bindings.h"
17 XBT_LOG_EXTERNAL_DEFAULT_CATEGORY(ruby);
20 smx_ctx_ruby_create_context(xbt_main_func_t code,int argc,char** argv,
21 void_f_pvoid_t cleanup_func,void *cleanup_arg);
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_wrapper(void);
29 void SIMIX_ctx_ruby_factory_init(smx_context_factory_t *factory) {
30 smx_ctx_base_factory_init(factory);
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)->resume = smx_ctx_ruby_resume;
38 (*factory)->name = "smx_ruby_context_factory";
44 smx_ctx_ruby_create_context(xbt_main_func_t code,int argc,char** argv,
45 void_f_pvoid_t cleanup_func,void* cleanup_arg) {
47 smx_ctx_ruby_t context = (smx_ctx_ruby_t)smx_ctx_base_factory_create_context_sized
48 (sizeof(s_smx_ctx_ruby_t), code,argc,argv,cleanup_func,cleanup_arg);
50 /* if the user provided a function for the process , then use it
51 Otherwise it's the context for maestro */
53 context->process = (VALUE)code;
55 DEBUG1("smx_ctx_ruby_create_context(%s)...Done",argv[0]);
57 return (smx_context_t) context;
60 static void smx_ctx_ruby_stop(smx_context_t context) {
61 DEBUG0("smx_ctx_ruby_stop()");
63 smx_ctx_ruby_t ctx_ruby,current;
65 smx_ctx_base_stop(context);
67 ctx_ruby = (smx_ctx_ruby_t) context;
69 if ( simix_global->current_process->iwannadie ) {
70 if( ctx_ruby->process ) {
72 //if the Ruby Process still Alive ,let's Schedule it
73 if ( rb_process_isAlive( ctx_ruby->process ) ) {
75 current = (smx_ctx_ruby_t)simix_global->current_process->context;
76 rb_process_schedule(current->process);
77 process = ctx_ruby->process;
78 // interupt/kill The Ruby Process
79 rb_process_kill_up(process);
84 if (ctx_ruby->process)
85 ctx_ruby->process = Qnil;
90 static void smx_ctx_ruby_suspend(smx_context_t context) {
92 DEBUG1("smx_ctx_ruby_suspend(%s)",context->argv[0]);
93 smx_ctx_ruby_t ctx_ruby = (smx_ctx_ruby_t) context;
94 if (ctx_ruby->process)
95 rb_process_unschedule(ctx_ruby->process);
98 static void smx_ctx_ruby_resume(smx_context_t new_context) {
99 DEBUG1("smx_ctx_ruby_resume(%s)",
100 (new_context->argc?new_context->argv[0]:"maestro"));
102 smx_ctx_ruby_t ctx_ruby = (smx_ctx_ruby_t) new_context;
103 rb_process_schedule(ctx_ruby->process);