From: mquinson Date: Mon, 1 Mar 2010 15:10:48 +0000 (+0000) Subject: Ongoing cleanup in ruby bindings. Does not compile yet, but commiting anyway because... X-Git-Tag: SVN~589 X-Git-Url: http://info.iut-bm.univ-fcomte.fr/pub/gitweb/simgrid.git/commitdiff_plain/9e3250f2231eb0fdff16629d7b344070d7c3bace?ds=sidebyside Ongoing cleanup in ruby bindings. Does not compile yet, but commiting anyway because I reindented some sources git-svn-id: svn+ssh://scm.gforge.inria.fr/svn/simgrid/simgrid/trunk@7149 48e7efb5-ca39-0410-a469-dd3cf9ba447f --- diff --git a/configure.ac b/configure.ac index 98b5b531a2..8c6496cc80 100644 --- a/configure.ac +++ b/configure.ac @@ -466,10 +466,12 @@ else #undef PACKAGE_STRING #undef PACKAGE_BUGREPORT #include - ], use_ruby=yes,use_ruby=no) + ], use_ruby=yes,use_ruby="no (Package ruby1.8-dev missing)") AC_MSG_RESULT($use_ruby) if test "x$use_ruby" = "xyes" ; then AC_DEFINE(HAVE_RUBY,1, [defines whether Ruby bindings must be compiled or not]) + else + AC_MSG_RESULT(Please install the ruby1.8-dev package (on debian and similar) to get ruby bindings) fi fi AC_MSG_RESULT(decide whether to compile ruby bindings... $use_ruby) diff --git a/src/Makefile.am b/src/Makefile.am index ce1e167dda..303d760870 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -383,6 +383,13 @@ if HAVE_LUA else EXTRA_DIST += $(LUA_SRC) endif + +RUBY_SRC= simix/smx_context_ruby.c bindings/ruby/rb_msg_process.c +if HAVE_RUBY + simgrid_sources += $(RUBY_SRC) +else + EXTRA_DIST += $(RUBY_SRC) +endif ## ## Compile the libs CLEANFILES=supernovae_sg.c supernovae_gras.c supernovae_smpi.c diff --git a/src/bindings/ruby/rb_msg_process.c b/src/bindings/ruby/rb_msg_process.c index d82d07b441..e6df224d29 100644 --- a/src/bindings/ruby/rb_msg_process.c +++ b/src/bindings/ruby/rb_msg_process.c @@ -6,37 +6,28 @@ *(GNU LGPL) which comes with this package. */ -#include "rb_msg_process.h" +#include "bindings/ruby_bindings.h" #define DEBUG // Init Ruby -static void initRuby() -{ - +void initRuby(void) { ruby_init(); ruby_init_loadpath(); rb_require("RubyProcess.rb"); - } -/*********************************************** - -Functions for Ruby Process Management ( Up Call) - -Idependant Methods - -************************************************/ +/* + * Functions for Ruby Process Management (Up Calls) + */ // get Ruby Process Name -static VALUE process_getName( VALUE ruby_process ) -{ - - initRuby(); +VALUE rb_process_getName(VALUE ruby_process) { + initRuby(); // instance = rb_funcall3(rb_const_get(rb_cObject, rb_intern("RbProcess")), rb_intern("new"), 0, 0); - return rb_funcall(ruby_process,rb_intern("getName"),0); - + return rb_funcall(ruby_process,rb_intern("getName"),0); + } // Get Process ID @@ -45,16 +36,16 @@ static VALUE process_getID(VALUE ruby_process) initRuby(); return rb_funcall(ruby_process,rb_intern("getID"),0); - + } // Get Bind static VALUE process_getBind(VALUE ruby_process) { - + initRuby(); return rb_funcall(ruby_process,rb_intern("getBind"),0); - + } @@ -65,52 +56,52 @@ static void process_setBind(VALUE ruby_process,long bind) initRuby(); VALUE r_bind = LONG2FIX(bind); rb_funcall(ruby_process,rb_intern("setBind"),1,r_bind); - + } // isAlive static VALUE process_isAlive(VALUE ruby_process) { - - initRuby(); - return rb_funcall(ruby_process,rb_intern("alive?"),0); - + + initRuby(); + return rb_funcall(ruby_process,rb_intern("alive?"),0); + } // Kill Process static void process_kill(VALUE ruby_process) { - + initRuby(); rb_funcall(ruby_process,rb_intern("kill"),0); - + } // join Process static void process_join( VALUE ruby_process ) { - - initRuby(); - rb_funcall(ruby_process,rb_intern("join"),0); - + + initRuby(); + rb_funcall(ruby_process,rb_intern("join"),0); + } // unschedule Process static void process_unschedule( VALUE ruby_process ) { - + initRuby(); rb_funcall(ruby_process,rb_intern("unschedule"),0); - + } // schedule Process static void process_schedule( VALUE ruby_process ) { - - initRuby(); - rb_funcall(ruby_process,rb_intern("schedule"),0); - + + initRuby(); + rb_funcall(ruby_process,rb_intern("schedule"),0); + } /*************************************************** @@ -119,32 +110,32 @@ Function for Native Process ( Bound ) Management Methods Belong to MSG Module -****************************************************/ + ****************************************************/ // Process To Native static m_process_t process_to_native(VALUE ruby_process) { - + VALUE id = process_getBind(ruby_process); if (!id) { - rb_raise(rb_eRuntimeError,"Process Not Bound >>> id_Bind Null"); - return NULL; + rb_raise(rb_eRuntimeError,"Process Not Bound >>> id_Bind Null"); + return NULL; } long l_id= FIX2LONG(id); return (m_process_t)l_id; - + } // Bind Process static void processBind(VALUE ruby_process,m_process_t process) { - + long bind = (long)(process); process_setBind(ruby_process,bind); - + } @@ -152,68 +143,67 @@ static void processBind(VALUE ruby_process,m_process_t process) static void processCreate(VALUE class,VALUE ruby_process,VALUE host) { - - VALUE rbName; // Name of Java Process instance - m_process_t process; // Native Process to Create - const char * name ; // Name of C Native Process - char alias[MAX_ALIAS_NAME + 1 ] = {0}; - msg_mailbox_t mailbox; - rbName = process_getName(ruby_process); - - if(!rbName) - { - rb_raise(rb_eRuntimeError,"Internal error : Process Name Cannot be NULL"); - return; - } - // Allocate the data for the simulation - process = xbt_new0(s_m_process_t,1); - process->simdata = xbt_new0(s_simdata_process_t,1); - // Do we Really Need to Create Ruby Process Instance , >> process is already a Ruby Process !! So..Keep on ;) - // Bind The Ruby Process instance to The Native Process - processBind(ruby_process,process); - name = RSTRING(rbName)->ptr; - process->name = xbt_strdup(name); - Data_Get_Struct(host,m_host_t,process->simdata->m_host); - - if(!(process->simdata->m_host)) // Not Binded - { - free(process->simdata); - free(process->data); - free(process); - rb_raise(rb_eRuntimeError,"Host not bound...while creating native process"); - return; - } - process->simdata->PID = msg_global->PID++; // msg_global ?? - - #ifdef DEBUG - printf("fill in process %s/%s (pid=%d) %p (sd%=%p , host=%p, host->sd=%p)\n", - process->name , process->simdata->m_host->name,process->simdata->PID, - process,process->simdata, process->simdata->m_host, - process->simdata->m_host->simdata); - #endif + + VALUE rbName; // Name of Java Process instance + m_process_t process; // Native Process to Create + const char * name ; // Name of C Native Process + char alias[MAX_ALIAS_NAME + 1 ] = {0}; + msg_mailbox_t mailbox; + rbName = process_getName(ruby_process); + + if(!rbName) { + rb_raise(rb_eRuntimeError,"Internal error : Process Name Cannot be NULL"); + return; + } + // Allocate the data for the simulation + process = xbt_new0(s_m_process_t,1); + process->simdata = xbt_new0(s_simdata_process_t,1); + // Do we Really Need to Create Ruby Process Instance , >> process is already a Ruby Process !! So..Keep on ;) + // Bind The Ruby Process instance to The Native Process + processBind(ruby_process,process); + name = RSTRING(rbName)->ptr; + process->name = xbt_strdup(name); + Data_Get_Struct(host,m_host_t,process->simdata->m_host); + + if(!(process->simdata->m_host)) // Not Binded + { + free(process->simdata); + free(process->data); + free(process); + rb_raise(rb_eRuntimeError,"Host not bound...while creating native process"); + return; + } + process->simdata->PID = msg_global->PID++; // msg_global ?? + +#ifdef DEBUG + printf("fill in process %s/%s (pid=%d) %p (sd%=%p , host=%p, host->sd=%p)\n", + process->name , process->simdata->m_host->name,process->simdata->PID, + process,process->simdata, process->simdata->m_host, + process->simdata->m_host->simdata); +#endif process->simdata->s_process = - SIMIX_process_create(process->name, - (xbt_main_func_t)ruby_process, - (void *) process, - process->simdata->m_host->simdata->smx_host->name, - 0,NULL,NULL); - - #ifdef DEBUG - printf("context created (s_process=%p)\n",process->simdata->s_process); - #endif - if (SIMIX_process_self()) { // SomeOne Created Me !! - process->simdata->PPID = MSG_process_get_PID(SIMIX_process_self()->data); - } - else - { - process->simdata->PPID = -1; - } + SIMIX_process_create(process->name, + (xbt_main_func_t)ruby_process, + (void *) process, + process->simdata->m_host->simdata->smx_host->name, + 0,NULL,NULL); + +#ifdef DEBUG + printf("context created (s_process=%p)\n",process->simdata->s_process); +#endif + if (SIMIX_process_self()) { // SomeOne Created Me !! + process->simdata->PPID = MSG_process_get_PID(SIMIX_process_self()->data); + } + else + { + process->simdata->PPID = -1; + } process->simdata->last_errno = MSG_OK; // let's Add the Process to the list of the Simulation's Processes xbt_fifo_unshift(msg_global->process_list,process); sprintf(alias,"%s:%s",(process->simdata->m_host->simdata->smx_host)->name, - process->name); - + process->name); + mailbox = MSG_mailbox_new(alias); } @@ -223,21 +213,21 @@ static void processCreate(VALUE class,VALUE ruby_process,VALUE host) static void processSuspend(VALUE class,VALUE ruby_process) { - + m_process_t process = process_to_native(ruby_process); - + if (!process) { rb_raise(rb_eRuntimeError,"Process Not Bound...while suspending process"); return; } - + // Trying to suspend The Process - + if ( MSG_OK != MSG_process_suspend(process)) - rb_raise(rb_eRuntimeError,"MSG_process_suspend() failed"); - - + rb_raise(rb_eRuntimeError,"MSG_process_suspend() failed"); + + } static void processResume(VALUE class,VALUE ruby_process) @@ -251,76 +241,76 @@ static void processResume(VALUE class,VALUE ruby_process) // Trying to resume the process if ( MSG_OK != MSG_process_resume(process)) rb_raise(rb_eRuntimeError,"MSG_process_resume() failed"); - + } static VALUE processIsSuspend(VALUE class,VALUE ruby_process) { - + m_process_t process = process_to_native(ruby_process); if (!process) { rb_raise (rb_eRuntimeError,"Process not Bound...while testing if suspended"); return; } - + // 1 is The Process is Suspended , 0 Otherwise if(MSG_process_is_suspended(process)) return Qtrue; - + return Qfalse; - + } static void processKill(VALUE class,VALUE ruby_process) { - m_process_t process = process_to_native(ruby_process); - - if(!process) - { - rb_raise (rb_eRuntimeError,"Process Not Bound...while killing process"); - return ; - } + m_process_t process = process_to_native(ruby_process); + + if(!process) + { + rb_raise (rb_eRuntimeError,"Process Not Bound...while killing process"); + return ; + } // Delete The Global Reference / Ruby Process process_kill(ruby_process); // Delete the Native Process MSG_process_kill(process); - + } static VALUE processGetHost(VALUE class,VALUE ruby_process) { - + m_process_t process = process_to_native(ruby_process); m_host_t host; - + if (!process) { - rb_raise(rb_eRuntimeError,"Process Not Bound...while getting Host"); - return Qnil; // NULL + rb_raise(rb_eRuntimeError,"Process Not Bound...while getting Host"); + return Qnil; // NULL } - + host = MSG_process_get_host(process); - + if(!host->data) { - rb_raise (rb_eRuntimeError,"MSG_process_get_host() failed"); - return Qnil; + rb_raise (rb_eRuntimeError,"MSG_process_get_host() failed"); + return Qnil; } - - return Data_Wrap_Struct(class, 0, host_free, host); - + + return Data_Wrap_Struct(class, 0, host_free, host); + } static void processExit(VALUE class,VALUE ruby_process) { - + m_process_t process = process_to_native(ruby_process); if(!process) { - rb_raise(rb_eRuntimeError,"Process Not Bound...while exiting process"); - return; + rb_raise(rb_eRuntimeError,"Process Not Bound...while exiting process"); + return; } SIMIX_context_stop(SIMIX_process_self()->context); - + } diff --git a/src/bindings/ruby/rb_msg_process.h b/src/bindings/ruby/rb_msg_process.h deleted file mode 100644 index 860f9a2de4..0000000000 --- a/src/bindings/ruby/rb_msg_process.h +++ /dev/null @@ -1,105 +0,0 @@ -#ifndef RB_MSG_PROCESS -#define RB_MSG_PROCESS - -#include -#include -#include "msg/msg.h" -#include "msg/datatypes.h" - -#include "msg/private.h" -#include "msg/mailbox.h" -#include "surf/surfxml_parse.h" -#include "simix/simix.h" -#include "simix/private.h" -#include "xbt/sysdep.h" -#include "xbt/log.h" -#include "xbt/asserts.h" -#include "rb_msg_host.h" - - -/************************************************************************** -There are 2 section in This File: - -1 - Functions to Manage The Ruby Process >> Up Call -2 - Functions to Manage The Native Process Simulation Bound >> Down Call - -***************************************************************************/ -// Init Ruby : To Call Ruby Methods From C - -static void initRuby(); - -/*********************************************** - -Functions for Ruby Process Management ( Up Call ) - -Independant Methods - -************************************************/ - -// Get Name -static VALUE process_getName( VALUE ruby_process ); - -// Get Process ID -static VALUE process_getID(VALUE ruby_process); - -// Get Bind : return the ID of Bind member -static VALUE process_getBind(VALUE ruby_class); - -// Set Bind -static void process_setBind(VALUE ruby_class,long bind); - -// isAlive -static VALUE process_isAlive(VALUE ruby_process); - -// Kill Process -static void process_kill(VALUE ruby_process); - -// join Process -static void process_join( VALUE ruby_process ); - -// unschedule Process -static void process_unschedule( VALUE ruby_process ); - -// schedule Process -static void process_schedule( VALUE ruby_process ); - - - - -/*************************************************** - -Function for Native Process ( Bound ) Management - -Methods Belong to The MSG Module -****************************************************/ - -// ProcessBind Method ; Process Ruby >> Process C - -//friend Method // Not belong to the Class but Called within !! -static m_process_t process_to_native(VALUE ruby_process); - -// Binding Process >> Friend Method -static void processBind(VALUE ruby_class,m_process_t process); - -// CreateProcess Method -static void processCreate(VALUE Class,VALUE rb_process,VALUE host); - -// ProcessSuspend -static void processSuspend(VALUE Class,VALUE ruby_process); - -// ProcessResume -static void processResume(VALUE Class,VALUE ruby_process); - -//ProcessIsSuspend return Boolean ( Qtrue / Qfalse ) -static VALUE processIsSuspend(VALUE Class,VALUE ruby_process); - -//Processkill -static void processKill(VALUE Class,VALUE ruby_process); - -//ProcessGetHost -static VALUE processGetHost(VALUE Class,VALUE ruby_process); - -//ProcessExit -static void processExit(VALUE Class,VALUE ruby_process); - -#endif \ No newline at end of file diff --git a/src/bindings/ruby_bindings.h b/src/bindings/ruby_bindings.h new file mode 100644 index 0000000000..e62264e41d --- /dev/null +++ b/src/bindings/ruby_bindings.h @@ -0,0 +1,110 @@ +/* SimGrid -- Ruby bindings */ + +/* Copyright (c) 2010, the SimGrid team. All right reserved */ + +/* This program is free software; you can redistribute it and/or modify it + * under the terms of the license (GNU LGPL) which comes with this package. */ + + +#ifndef RB_SG_BINDINGS +#define RB_SG_BINDINGS +/* + * There is 4 sections in this file: + * - Header loading (ruby makes it quite difficult, damn it) + * - definitions of ruby contextes for use in simix + * - Functions to Manage The Ruby Process (named Up Calls) + * - Functions to Manage The Native Process Simulation Bound (named Down Calls) + */ + +#include "msg/msg.h" +#include "msg/datatypes.h" + +//#include "msg/private.h" +//#include "msg/mailbox.h" +#include "surf/surfxml_parse.h" +#include "simix/simix.h" +#include "simix/private.h" +#include "xbt/sysdep.h" +#include "xbt/log.h" +#include "xbt/asserts.h" +//#include "rb_msg_host.h" + +/* Damn Ruby. They load their full config.h, which breaks since we also load ours. + * So, we undef the offending defines + */ +#undef PACKAGE_VERSION +#undef PACKAGE_NAME +#undef PACKAGE_TARNAME +#undef PACKAGE_STRING +#undef PACKAGE_BUGREPORT +#undef _GNU_SOURCE +#include + + + +/* ********************* * + * Context related stuff * + * ********************* */ +typedef struct s_smx_ctx_ruby { + SMX_CTX_BASE_T; + VALUE process; // The Ruby Process Instance + //... +}s_smx_ctx_ruby_t,*smx_ctx_ruby_t; +void SIMIX_ctx_ruby_factory_init(smx_context_factory_t *factory); + + +void initRuby(void); // Mandatory to call Ruby methods from C + +/* *********************************************** * + * Functions for Ruby Process Management (Up Call) * + * * + * Independent Methods * + * *********************************************** */ + +VALUE rb_process_getName( VALUE ruby_process ); +VALUE rb_process_getID(VALUE ruby_process); +VALUE rb_process_getBind(VALUE ruby_class); +void rb_process_setBind(VALUE ruby_class,long bind); +VALUE rb_process_isAlive(VALUE ruby_process); +void rb_process_kill(VALUE ruby_process); +void rb_process_join( VALUE ruby_process ); +void rb_process_unschedule( VALUE ruby_process ); +void rb_process_schedule( VALUE ruby_process ); + + +/* ********************************************** * + * Function for Native Process (Bound) Management * + * * + * Methods Belonging to The MSG Module * + * ********************************************** */ + +// ProcessBind Method ; Process Ruby >> Process C + +//friend Method // Not belong to the Class but Called within !! +m_process_t rb_process_to_native(VALUE ruby_process); + +// Binding Process >> Friend Method +void rb_processBind(VALUE ruby_class,m_process_t process); + +// CreateProcess Method +void rb_processCreate(VALUE Class,VALUE rb_process,VALUE host); + +// ProcessSuspend +void rb_processSuspend(VALUE Class,VALUE ruby_process); + +// ProcessResume +void rb_processResume(VALUE Class,VALUE ruby_process); + +//ProcessIsSuspend return Boolean ( Qtrue / Qfalse ) +VALUE rb_processIsSuspend(VALUE Class,VALUE ruby_process); + +//Processkill +void rb_processKill(VALUE Class,VALUE ruby_process); + +//ProcessGetHost +VALUE rb_processGetHost(VALUE Class,VALUE ruby_process); + +//ProcessExit +void rb_processExit(VALUE Class,VALUE ruby_process); + +#endif /* RB_SG_BINDINGS */ diff --git a/src/simix/smx_context.c b/src/simix/smx_context.c index 70a41babaf..71c2b0edeb 100644 --- a/src/simix/smx_context.c +++ b/src/simix/smx_context.c @@ -10,22 +10,13 @@ #include "xbt/log.h" #include "xbt/swag.h" #include "private.h" -#include +#ifdef HAVE_LUA +#include +#endif #ifdef HAVE_RUBY -/* Damn Ruby. They load their full config.h, which breaks since we also load ours. - * So, we undef the offending defines - */ -#undef PACKAGE_VERSION -#undef PACKAGE_NAME -#undef PACKAGE_TARNAME -#undef PACKAGE_STRING -#undef PACKAGE_BUGREPORT -#undef _GNU_SOURCE -#include void SIMIX_ctx_ruby_factory_init(smx_context_factory_t *factory); -#include "smx_context_ruby.c" #endif XBT_LOG_NEW_DEFAULT_SUBCATEGORY(simix_context, simix, "Context switching mecanism"); diff --git a/src/simix/smx_context_ruby.c b/src/simix/smx_context_ruby.c index 54a9fdf69d..8c453177cc 100644 --- a/src/simix/smx_context_ruby.c +++ b/src/simix/smx_context_ruby.c @@ -6,23 +6,14 @@ /* This program is free software; you can redistribute it and/or modify it * under the terms of the license (GNU LGPL) which comes with this package. */ -#include + #include "private.h" #include "xbt/function_types.h" #include "xbt/sysdep.h" #include "xbt/log.h" #include "xbt/asserts.h" -#include "context_sysv_config.h" -#include "bindings/ruby/rb_msg_process.c" - -typedef struct s_smx_ctx_ruby - -{ - SMX_CTX_BASE_T; - VALUE process; // The Ruby Process Instance - //... -}s_smx_ctx_ruby_t,*smx_ctx_ruby_t; +#include "bindings/ruby_bindings.h" static smx_context_t smx_ctx_ruby_create_context(xbt_main_func_t code,int argc,char** argv, @@ -153,13 +144,13 @@ static void smx_ctx_ruby_stop(smx_context_t context) if( ctx_ruby->process ) { //if the Ruby Process still Alive ,let's Schedule it - if ( process_isAlive( ctx_ruby->process ) ) + if ( rb_process_isAlive( ctx_ruby->process ) ) { current = (smx_ctx_ruby_t)simix_global->current_process->context; - process_schedule(current->process); + rb_process_schedule(current->process); process = ctx_ruby->process; // interupt/kill The Ruby Process - process_kill(process); + rb_process_kill(process); } } }else { @@ -180,7 +171,7 @@ if (context) { smx_ctx_ruby_t ctx_ruby = (smx_ctx_ruby_t) context; if (ctx_ruby->process) - process_unschedule( ctx_ruby->process ) ; + rb_process_unschedule( ctx_ruby->process ) ; #ifdef MY_DEBUG printf("smx_ctx_ruby_unschedule...Done\n"); #endif @@ -195,7 +186,7 @@ static void smx_ctx_ruby_resume(smx_context_t old_context,smx_context_t new_cont { smx_ctx_ruby_t ctx_ruby = (smx_ctx_ruby_t) new_context; - process_schedule(ctx_ruby->process); + rb_process_schedule(ctx_ruby->process); #ifdef MY_DEBUG printf("smx_ctx_ruby_schedule...Done\n");