Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
more debug
[simgrid.git] / src / bindings / ruby / simgrid.rb
1 # FIXME: add license like in C files
2
3 require 'simgrid_ruby'
4 require 'thread'
5
6 $DEBUG = false  # This is a Global Variable Useful for MSG::debugging
7
8 ###########################################################################
9 # Class Semaphore 
10 ###########################################################################
11 class MySemaphore
12    Thread.abort_on_exception = true
13     attr_accessor :permits
14
15    
16   def initialize (permits = 0)
17        @permits = permits
18   end
19   
20   def acquire(mutex,cv)
21
22     raise "Interrupted Thread " if (!Thread.current.alive?)
23     mutex.synchronize {
24     while @permits <= 0
25        
26        cv.wait(mutex)
27        
28     end
29     @permits = @permits - 1
30     cv.signal
31     }
32     
33   end
34     
35   def release(mutex,cv)
36     mutex.synchronize{
37       @permits += 1
38       cv.signal
39       }
40   end  
41 end
42 #######################################
43 # Another Semaphore
44 #######################################
45
46 class Semaphore
47   def initialize(initvalue = 0)
48     @counter = initvalue
49     @waiting_list = []
50   end
51
52   def acquire
53     MSG::debug(Thread.current.to_s+" acquires "+self.to_s)
54     Thread.critical = true
55     if (@counter -= 1) < 0
56       @waiting_list.push(Thread.current)
57       Thread.stop
58     end
59     self
60   ensure
61     Thread.critical = false
62   end
63
64   def release
65     MSG::debug(Thread.current.to_s+" releases "+self.to_s)
66     Thread.critical = true
67     begin
68       if (@counter += 1) <= 0
69         t = @waiting_list.shift
70         t.wakeup if t
71         MSG::debug("Wakeup "+t.to_s)
72       else 
73         MSG::debug("Nobody to wakeup")
74       end
75     rescue ThreadError
76       retry
77     end
78     self
79   ensure
80     Thread.critical = false
81   end
82 end
83
84 ########################################################################
85 # Class Process 
86 ########################################################################
87 class MSG::Process < Thread 
88   @@nextProcessId = 0
89
90 # Attributes
91   attr_reader :bind, :id, :name, :pargs ,:properties# Read only
92   
93     def initialize(*args)
94       super(){
95         
96      raise "Bad Number Of arguments to create a Ruby Process (name,args,prop) " if args.size < 3
97      
98 #     @cv = ConditionVariable.new
99 #     @mutex = Mutex.new
100     @schedBegin = Semaphore.new(0)
101     @schedEnd = Semaphore.new(0)    
102     @id = @@nextProcessId
103     @@nextProcessId +=1
104     @name = args[0]
105     @pargs = args[1]
106     @properties = args[2]
107       
108     start()
109       }
110     end
111     
112   # main
113   def main(args) 
114     # To be overriden by childs
115     raise("You must define a main() function in your process, containing the code of this process")
116   end
117      
118   # Start : To keep the process alive and waiting via semaphore
119   def start()
120     @schedBegin.acquire()
121     # execute the main code of the process     
122     MSG::debug("Begin execution")
123     main(@pargs)
124 #     processExit(self) # Exit the Native Process
125     @schedEnd.release()
126   end
127     
128
129   # FIXME: useless, there is an attribute for bind (or the attribute is useless)
130   # Get Bind
131   def getBind()
132     return @bind
133   end
134   
135   # Set Binds FIXME: same
136   def setBind(bind)
137     @bind = bind
138   end
139     
140   def unschedule()
141     @schedEnd.release()
142     @schedBegin.acquire()
143   end
144   
145   def schedule()
146     @schedBegin.release()
147     @schedEnd.acquire()
148   end
149   
150   def pause()
151     processSuspend(self)
152   end
153   
154   def restart()
155     processResume(self)
156   end
157   
158   def isSuspended()
159     processIsSuspended(self)
160   end
161   
162   def getHost()
163     processGetHost(self)
164   end
165   
166 # The Rest of Methods !!! To be Continued ... FIXME: what's missing?
167 end
168
169 ############################################
170 # Task Extend from the native Class RbTask
171 ############################################
172 class MSG::Task < MSG::RbTask
173
174   def initialize(*args)
175      super()
176   end
177   
178   def name
179      super(self)
180   end
181   
182   def compSize
183      super(self)
184   end
185   
186   def send(mailbox)
187     super(self,mailbox)
188   end
189   
190 #   FIXME : this methode should be associated to the class !! it reurn a task
191   def receive(mailbox)
192     super(self,mailbox)
193   end
194   
195   def source
196     super(self)
197   end
198   
199   def sender
200     super(self)
201   end
202   
203   def listen(t_alias)
204     super(t_alias)
205   end
206   
207   def execute
208     super(self)
209   end
210     
211   def listenFromHost(t_alias,host)
212     super(t_alias,host)
213   end
214     
215 end  
216
217 ############################################
218 # Host Extend from the native Class RbHost
219 ############################################
220 class MSG::Host < MSG::RbHost
221
222   def getByName(name)
223     super(name)
224   end
225   
226   def name
227     super(self)
228   end
229   
230   def speed
231     super(self)
232   end
233   
234   def getData
235     super(self)
236   end
237   
238   def setData(data)
239     super(self,data)
240   end
241   
242   def isAvail
243     super(self)
244   end
245   
246   def number
247     super()
248   end
249   
250 end
251
252 #########################
253 # Main chunck 
254 #########################
255 MSG.init(ARGV)