Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
props dict does not always exists anymore. Check if not NULL before
[simgrid.git] / src / bindings / ruby / simgrid.rb
1 #  Task-related bindings to ruby  */
2
3 #  Copyright 2010. The SimGrid Team. All right reserved. */
4
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 # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # 
8 require 'libsimgrid.so'
9 require 'thread'
10
11 #######################################
12 #  Semaphore 
13 #######################################
14
15 class Semaphore
16   def initialize(initvalue = 0)
17     @counter = initvalue
18     @waiting_list = []
19   end
20
21   def acquire
22     Thread.critical = true
23     if (@counter -= 1) < 0
24       MSG::debug(Thread.current.to_s+" acquires "+self.to_s+". That's blocking.")
25       @waiting_list.push(Thread.current)
26       Thread.stop
27     else
28       MSG::debug(Thread.current.to_s+" acquires "+self.to_s+". It was free.")      
29     end
30     self
31   ensure
32     Thread.critical = false
33   end
34
35   def release
36     Thread.critical = true
37     begin
38       if (@counter += 1) <= 0
39         t = @waiting_list.shift
40         t.wakeup if t
41         MSG::debug(Thread.current.to_s+" releases "+self.to_s+". Wakeup "+t.to_s)
42       else 
43         MSG::debug(Thread.current.to_s+" releases "+self.to_s+". Nobody to wakeup")
44       end
45     rescue ThreadError
46       retry
47     end
48     self
49   ensure
50     Thread.critical = false
51   end
52 end
53
54 ########################################################################
55 # Class Process 
56 ########################################################################
57 class MSG::Process < Thread 
58   @@nextProcessId = 0
59
60 # Attributes
61   attr_reader :name, :pargs ,:properties        # Read only
62   
63     def initialize(*args)
64       super(){
65         
66      raise "Bad number of arguments to create a Ruby process. Expected (name,args,prop) " if args.size < 3
67      
68     @schedBegin = Semaphore.new(0)
69     @schedEnd = Semaphore.new(0)    
70     @id = @@nextProcessId
71     @@nextProcessId +=1
72     @name = args[0]
73     @pargs = args[1]
74     @properties = args[2]
75     start()
76       }
77     end
78     
79   def main(args) 
80     # To be overriden by childs
81     raise("You must define a main() function in your process, containing the code of this process")
82   end
83      
84   def start()
85     @schedBegin.acquire
86     MSG::debug("Let's execute the main() of the Ruby process")
87     main(@pargs)
88     @schedEnd.release
89     MSG::debug("Released my schedEnd, bailing out")
90     processExit(self) # Exit the Native Process
91     
92   end
93     
94   def getBind()
95     return @bind
96   end
97    
98   def setBind(bind)
99     @bind = bind
100   end
101     
102   def unschedule()
103     @schedEnd.release
104     @schedBegin.acquire
105   end
106   
107   def schedule()   
108     @schedBegin.release
109     @schedEnd.acquire
110   end
111   
112   def pause()
113     processSuspend(self)
114   end
115   
116   def restart()
117     processResume(self)
118   end
119   
120   def isSuspended()
121     processIsSuspended(self)
122   end
123   
124   def getHost()
125     processGetHost(self)
126   end
127
128 end
129 ############################################
130 # Task Extend from the native Class RbTask
131 ############################################
132 class MSG::Task < MSG::RbTask
133   
134   def initialize(*args)
135     #Nothing todo   
136   end
137   
138   def join(value) 
139     super(self,value)
140   end
141   
142   def data()
143     super(self)
144   end
145   
146   def name
147    super(self)
148   end
149   
150   def compSize
151     super(self)
152   end
153   
154   def send(mailbox)
155    super(self,mailbox)
156   end
157   
158   def receive(mailbox)
159     super(self)
160   end
161   
162   def source
163     super(self)
164   end
165   
166   def sender
167     super(self)
168   end
169   
170   def listen(t_alias)
171     super(self)
172   end
173   
174   def execute
175     super(self)
176   end
177     
178   def listenFromHost(t_alias,host)
179     super(self)
180   end
181   
182   def setPriority(priority)
183     super(self,priority)
184   end
185   
186   def cancel
187     super(self)
188   end
189   
190   def hasData
191     super(self)
192   end
193    
194 end  
195
196 ####################################################
197 # Host Extend from the native Class RbHost
198 ####################################################
199 class MSG::Host < MSG::RbHost
200   
201   attr_reader :data
202   def initialize(*ars)
203     @data = 1
204     p "Host Initializer"
205   end
206   
207   def data()
208     return @data
209   end
210   
211   def setData(value)
212     @data = value
213   end
214   
215   def getByName(name)
216     super(name)
217   end
218   
219   def name
220     super(self)
221   end
222   
223   def speed
224     super(self)
225   end
226   
227   def getData
228     super(self)
229   end
230   
231   def isAvail
232     super(self)
233   end
234   
235   def number
236     super()
237   end
238   
239   def getHostProcess(process)
240     super(process)
241   end
242     
243 end
244 #########################
245 # Main chunck 
246 #########################
247 MSG.init(ARGV)