4 $DEBUG = false # This is a Global Variable Useful for MSG::debugging
6 ###########################################################################
8 ###########################################################################
11 def initialize(initvalue = 0)
17 MSG::debug("Acquire "+self.to_s)
18 Thread.critical = true
19 if (@counter -= 1) < 0
20 @waiting_list.push(Thread.current)
25 Thread.critical = false
29 MSG::debug("Release "+self.to_s)
30 Thread.critical = true
32 if (@counter += 1) <= 0
33 t = @waiting_list.shift
35 MSG::debug("Wakeup "+t.to_s)
37 MSG::debug("Nobody to wakeup")
44 Thread.critical = false
48 ########################################################################
50 ########################################################################
51 class MSG::Process < Thread
55 attr_reader :bind, :id # Read only
56 attr_accessor :name, :properties, :pargs # R/W
58 # Initialize : Used from ApplicationHandler to fill it in
60 # FIXME: use only one variante (the one with 3 args) and kill the others
61 @schedBegin = Semaphore.new(0)
62 @schedEnd = Semaphore.new(0)
63 @properties = Hash.new()
64 @id = @@nextProcessId++
68 if argc == 0 # Default initializer
75 MSG::debug "Initializer without any argument"
78 # 2 arguments: (HostName,Name) Or (Host , Name)
81 MSG::debug "Initilize with 2 args"
83 if ( type.to_s == "String")
84 host = Host.getByName(args[0])
85 elsif ( type.to_s == "MSG::Host")
88 raise "first argument of type "+args[0].type().to_s+", but expecting either String or MSG::Host"
93 raise "Process name cannot be null" if args[1].empty?
98 @pargs = Array.new() # No Args[] Passed in Arguments
100 createProcess(self,host)
103 # 3 arguments: (hostName,Name,args[]) or (Host,Name,args[])
106 MSG::debug "Initilize with 3 args"
107 type = args[0].type()
108 if ( type.to_s == "String")
109 host = Host.getByName(args[0])
110 elsif ( type.to_s == "MSG::Host")
113 raise "first argument of type "+args[0].type().to_s+", but expecting either String or MSG::Host"
119 raise "Process name cannot be null" if args[1].empty?
121 type = args[2].type()
122 raise "Third argument should be an Array" if type != "Array"
124 createProcess(self,host)
128 raise "Bad number of argument: Expecting either 1, 2 or 3, but got "+argc.to_s
134 # To be overriden by childs
135 raise("You must define a main() function in your process, containing the code of this process")
138 # Start : To keep the process alive and waiting via semaphore
141 # execute the main code of the process
142 MSG::debug("Begin execution")
144 processExit(self) # Exit the Native Process
148 def processList() (KILLME?)
149 Thread.list.each {|t| p t}
152 #Get Own ID (KILLME?)
157 #Get a Process ID (KILLME?)
158 def processID(process)
162 #Get Own Name (KILLME?)
187 #C Simualator Process Equivalent Management
188 # After Binding Ruby Process to C Process
202 processIsSuspended(self)
210 # The Rest of Methods !!! To be Continued ...
213 #########################################################################
214 # Class ApplicationHandler
215 #########################################################################
216 class ApplicationHandler
222 def onBeginProcess(hostName,function)
224 @properties = Hash.new
229 MSG::debug("onBeginProcess("+hostName+","+function+")")
232 def onProperty(id,value)
233 @properties[id] = value
236 def onProcessArg(arg)
241 # must be in C, called from a callback to the FlexML parser
242 # newInstance must take args and hostname as argument to initialize everything, *and* bind it to C element
243 # Allows to mark all attributes of process (but properties) to read-only
244 process = MSG::rubyNewInstance(@function)
245 process.pargs = @args
246 process.name = @function
247 host = MSG::Host.getByName(@hostName)
248 MSG::processCreate(process,host)
249 process.properties = @properties
253 #########################
255 #########################