5 $DEBUG = false # This is a Global Variable Useful for Debugging
7 ###########################################################################
9 ###########################################################################
12 def initialize(initvalue = 0)
18 Thread.critical = true
19 if (@counter -= 1) < 0
20 @waiting_list.push(Thread.current)
25 Thread.critical = false
29 Thread.critical = true
31 if (@counter += 1) <= 0
32 t = @waiting_list.shift
40 Thread.critical = false
44 ########################################################################
46 ########################################################################
47 class MsgProcess < Thread
51 attr_reader :bind, :id, :pargs # Read only
52 attr_accessor :name, :properties # R/W
54 # Initialize : Used from ApplicationHandler to fill it in
56 @schedBegin = Semaphore.new(0)
57 @schedEnd = Semaphore.new(0)
58 @properties = Hash.new()
59 @id = @@nextProcessId++
63 if argc == 0 # Default initializer
71 puts "Init Default Initializer...Nothing to do...Bye"
75 # 2 arguments: (HostName,Name) Or (Host , Name)
79 if ( type.to_s == "String")
80 host = Host.getByName(args[0])
81 elsif ( type.to_s == "MSG::Host")
84 raise "first argument of type "+args[0].type().to_s+", but expecting either String or MSG::Host"
89 raise "Process name cannot be null" if args[1].empty?
94 @pargs = Array.new() # No Args[] Passed in Arguments
96 createProcess(self,host)
98 puts "Initilize with 2 args"
102 # 3 arguments: (hostName,Name,args[]) or (Host,Name,args[])
105 type = args[0].type()
106 if ( type.to_s == "String")
107 host = Host.getByName(args[0])
108 elsif ( type.to_s == "MSG::Host")
111 raise "first argument of type "+args[0].type().to_s+", but expecting either String or MSG::Host"
117 raise "Process name cannot be null" if args[1].empty?
119 type = args[2].type()
120 raise "Third argument should be an Array" if type != "Array"
122 createProcess(self,host)
125 puts "Initilize with 3 args"
129 raise "Bad number of argument: Expecting either 1, 2 or 3, but got "+argc
135 # To be overriden by childs
136 raise("You must define a msg_main() function in your process, containing the code of this process")
139 # Start : To keep the process alive and waiting via semaphore
142 # execute the main code of the process
143 debug("Begin execution")
145 processExit(self) # Exit the Native Process
150 Thread.list.each {|t| p t}
159 def processID(process)
169 def processName(process)
193 #C Simualator Process Equivalent Management
194 # After Binding Ruby Process to C Process
208 processIsSuspended(self)
216 # The Rest of Methods !!! To be Continued ...
219 #########################################################################
220 # Class ApplicationHandler
221 #########################################################################
222 class ApplicationHandler
228 def onBeginProcess(hostName,function)
230 @properties = Hash.new
235 debug("onBeginProcess("+hostName+","+function+")")
238 def onProperty(id,value)
239 @properties[id] = value
242 def onProcessArg(arg)
247 process = rubyNewInstance(@function)
250 process.pargs.push(@args[i])
252 process.name = @function
253 host = Host.getByName(@hostName)
254 processCreate(process,host)
255 process.properties = @properties
259 #########################
261 #########################