From 738c88593ac343673ac69682fb1cc1dc465772e0 Mon Sep 17 00:00:00 2001 From: coldpeace Date: Fri, 12 Mar 2010 16:30:02 +0000 Subject: [PATCH] better synchronization , cleanin' up ruby process ,it works git-svn-id: svn+ssh://scm.gforge.inria.fr/svn/simgrid/simgrid/trunk@7231 48e7efb5-ca39-0410-a469-dd3cf9ba447f --- src/bindings/ruby/MasterSlave.rb | 22 ++++----- src/bindings/ruby/rb_msg_process.c | 1 - src/bindings/ruby/simgrid.rb | 72 ++++++------------------------ src/bindings/ruby/simgrid_ruby.c | 12 ++--- 4 files changed, 29 insertions(+), 78 deletions(-) diff --git a/src/bindings/ruby/MasterSlave.rb b/src/bindings/ruby/MasterSlave.rb index 1b0344fdb8..e90a1f4bb0 100644 --- a/src/bindings/ruby/MasterSlave.rb +++ b/src/bindings/ruby/MasterSlave.rb @@ -25,15 +25,14 @@ class Master < MSG::Process slaveCount = Integer(args[3]) # Creates and sends the tasks - for i in 0..numberOfTask-1 + for i in 0..numberOfTask-1 task = MSG::Task.new("Task_"+ i.to_s, taskComputeSize , taskCommunicationSize); - mailbox = "slave " + (i%slaveCount).to_s - MSG::info("Master Sending "+ task.name + " to " + mailbox + " with Comput Size " + - task.compSize.to_s) -# task.compSize.to_s) # FIXME: This version creates a deadlock. Interesting - task.send(mailbox) - MSG::info("Master Done Sending " + task.name + " to " + mailbox) - end + mailbox = "slave " + (i%slaveCount).to_s + MSG::info("Master Sending "+ task.name + " to " + mailbox + " with Comput Size " + + task.compSize.to_s) + task.send(mailbox) + MSG::info("Master Done Sending " + task.name + " to " + mailbox) + end # Sending Finalize MSG::Tasks MSG::info("Master: All tasks have been dispatched. Let's tell everybody the computation is over.") @@ -44,7 +43,6 @@ class Master < MSG::Process finalize_task.send(mailbox) end MSG::info("Master : Everything's Done") - Thread.list.each {|t| p t} end end @@ -85,11 +83,9 @@ if (ARGV.length == 2) else MSG.createEnvironment("platform.xml") MSG.deployApplication("deploy.xml") - #Thread.list.each {|t| p t} + end MSG.run -Thread.list.each {|t| p t} puts "Simulation time : " + MSG.getClock .to_s - -# exit() +MSG.exit diff --git a/src/bindings/ruby/rb_msg_process.c b/src/bindings/ruby/rb_msg_process.c index e6334cb198..dbf8c556fb 100644 --- a/src/bindings/ruby/rb_msg_process.c +++ b/src/bindings/ruby/rb_msg_process.c @@ -61,7 +61,6 @@ void rb_process_schedule( VALUE ruby_process ) { rb_funcall(ruby_process,rb_intern("schedule"),0); } - /*************************************************** Function for Native Process ( Bound ) Management diff --git a/src/bindings/ruby/simgrid.rb b/src/bindings/ruby/simgrid.rb index eea2fa9173..30dc1dfc6a 100644 --- a/src/bindings/ruby/simgrid.rb +++ b/src/bindings/ruby/simgrid.rb @@ -8,41 +8,8 @@ require 'simgrid_ruby' require 'thread' -########################################################## -# Class Semaphore -########################################################## -class MySemaphore - Thread.abort_on_exception = true - attr_accessor :permits - - def initialize (permits = 0) - @permits = permits - end - - def acquire(mutex,cv) - - raise "Interrupted Thread " if (!Thread.current.alive?) - mutex.synchronize { - while @permits <= 0 - - cv.wait(mutex) - - end - @permits = @permits - 1 - cv.signal - } - - end - - def release(mutex,cv) - mutex.synchronize{ - @permits += 1 - cv.signal - } - end -end ####################################### -# Another Semaphore +# Semaphore ####################################### class Semaphore @@ -91,17 +58,15 @@ class MSG::Process < Thread @@nextProcessId = 0 # Attributes - attr_reader :name, :pargs ,:properties, :cv, :mutex # Read only + attr_reader :name, :pargs ,:properties # Read only def initialize(*args) super(){ raise "Bad number of arguments to create a Ruby process. Expected (name,args,prop) " if args.size < 3 - @cv = ConditionVariable.new - @mutex = Mutex.new - @schedBegin = MySemaphore.new(0) - @schedEnd = MySemaphore.new(0) + @schedBegin = Semaphore.new(0) + @schedEnd = Semaphore.new(0) @id = @@nextProcessId @@nextProcessId +=1 @name = args[0] @@ -118,19 +83,13 @@ class MSG::Process < Thread end def start() - @schedBegin.acquire(@mutex,@cv) - + @schedBegin.acquire MSG::debug("Let's execute the main() of the Ruby process") main(@pargs) -<<<<<<< .mine -# processExit(self) # FIXME : Fix the simix_context_ruby_stop to stop context process before quitting - @schedEnd.release(@mutex,@cv) - -======= - @schedEnd.release() ->>>>>>> .r7205 + @schedEnd.release MSG::debug("Released my schedEnd, bailing out") processExit(self) # Exit the Native Process + end def getBind() @@ -142,13 +101,13 @@ class MSG::Process < Thread end def unschedule() - @schedEnd.release(@mutex,@cv) - @schedBegin.acquire(@mutex,@cv) + @schedEnd.release + @schedBegin.acquire end def schedule() - @schedBegin.release(@mutex,@cv) - @schedEnd.acquire(@mutex,@cv) + @schedBegin.release + @schedEnd.acquire end def pause() @@ -166,10 +125,9 @@ class MSG::Process < Thread def getHost() processGetHost(self) end - + # The Rest of Methods !!! To be Continued ... FIXME: what's missing? end - ############################################ # Task Extend from the native Class RbTask ############################################ @@ -191,7 +149,6 @@ class MSG::Task < MSG::RbTask super(self,mailbox) end - def source super(self) end @@ -212,12 +169,10 @@ class MSG::Task < MSG::RbTask super(t_alias,host) end end - #################################################### # Host Extend from the native Class RbHost #################################################### class MSG::Host < MSG::RbHost - def getByName(name) super(name) end @@ -246,8 +201,7 @@ class MSG::Host < MSG::RbHost super() end end - ######################### # Main chunck ######################### -MSG.init(ARGV) +MSG.init(ARGV) \ No newline at end of file diff --git a/src/bindings/ruby/simgrid_ruby.c b/src/bindings/ruby/simgrid_ruby.c index ca4bb743b4..c30de11a01 100644 --- a/src/bindings/ruby/simgrid_ruby.c +++ b/src/bindings/ruby/simgrid_ruby.c @@ -71,14 +71,15 @@ static void msg_run(VALUE class) { for (cpt=0;cptdata); } - - //FIXME Before Cleanin' up , we should stop process running to avoir a ThreadError - /* if (MSG_OK != MSG_clean()){ - rb_raise(rb_eRuntimeError,"MSG_clean() failed"); - }*/ return; } +static void msg_clean(VALUE class) +{ + if (MSG_OK != MSG_clean()) + rb_raise(rb_eRuntimeError,"MSG_clean() failed"); + +} static void msg_createEnvironment(VALUE class,VALUE plateformFile) { int type = TYPE(plateformFile); @@ -153,6 +154,7 @@ void Init_simgrid_ruby() { rb_define_module_function(rb_msg,"info",(rb_meth)msg_info,1); rb_define_module_function(rb_msg,"debug",(rb_meth)msg_debug,1); rb_define_module_function(rb_msg,"getClock",(rb_meth)msg_get_clock,0); + rb_define_module_function(rb_msg,"exit",(rb_meth)msg_clean,0); //Associated Process Methods rb_define_method(rb_msg,"processSuspend",(rb_meth)rb_process_suspend,1); -- 2.20.1