1 # Task-related bindings to ruby */
3 # Copyright 2010. The SimGrid Team. All right reserved. */
5 # This program is free software; you can redistribute it and/or modify it
6 # under the terms of the license (GNU LGPL) which comes with this package. */
7 # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
11 ##########################################################
13 ##########################################################
15 Thread.abort_on_exception = true
16 attr_accessor :permits
18 def initialize (permits = 0)
24 raise "Interrupted Thread " if (!Thread.current.alive?)
31 @permits = @permits - 1
44 #######################################
46 #######################################
49 def initialize(initvalue = 0)
55 Thread.critical = true
56 if (@counter -= 1) < 0
57 MSG::debug(Thread.current.to_s+" acquires "+self.to_s+". That's blocking.")
58 @waiting_list.push(Thread.current)
61 MSG::debug(Thread.current.to_s+" acquires "+self.to_s+". It was free.")
65 Thread.critical = false
69 Thread.critical = true
71 if (@counter += 1) <= 0
72 t = @waiting_list.shift
74 MSG::debug(Thread.current.to_s+" releases "+self.to_s+". Wakeup "+t.to_s)
76 MSG::debug(Thread.current.to_s+" releases "+self.to_s+". Nobody to wakeup")
83 Thread.critical = false
87 ########################################################################
89 ########################################################################
90 class MSG::Process < Thread
94 attr_reader :name, :pargs ,:properties, :cv, :mutex # Read only
99 raise "Bad number of arguments to create a Ruby process. Expected (name,args,prop) " if args.size < 3
101 @cv = ConditionVariable.new
103 @schedBegin = MySemaphore.new(0)
104 @schedEnd = MySemaphore.new(0)
105 @id = @@nextProcessId
109 @properties = args[2]
116 # To be overriden by childs
117 raise("You must define a main() function in your process, containing the code of this process")
121 @schedBegin.acquire(@mutex,@cv)
123 MSG::debug("Let's execute the main() of the Ruby process")
126 # processExit(self) # FIXME : Fix the simix_context_ruby_stop to stop context process before quitting
127 @schedEnd.release(@mutex,@cv)
132 MSG::debug("Released my schedEnd, bailing out")
133 processExit(self) # Exit the Native Process
145 @schedEnd.release(@mutex,@cv)
146 @schedBegin.acquire(@mutex,@cv)
150 @schedBegin.release(@mutex,@cv)
151 @schedEnd.acquire(@mutex,@cv)
163 processIsSuspended(self)
170 # The Rest of Methods !!! To be Continued ... FIXME: what's missing?
173 ############################################
174 # Task Extend from the native Class RbTask
175 ############################################
176 class MSG::Task < MSG::RbTask
178 def initialize(*args)
211 def listenFromHost(t_alias,host)
216 ####################################################
217 # Host Extend from the native Class RbHost
218 ####################################################
219 class MSG::Host < MSG::RbHost
250 #########################
252 #########################