Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Modified tesh files.
[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 'simgrid'
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 # The Rest of Methods !!! To be Continued ... FIXME: what's missing?
129 end
130 ############################################
131 # Task Extend from the native Class RbTask
132 ############################################
133 class MSG::Task < MSG::RbTask
134
135   def initialize(*args)
136    super() #no effect
137   end
138   
139   def setData(value)
140     super(self,value)
141   end
142   
143   def data()
144     super(self)
145   end
146   
147   def name
148       super(self)
149   end
150   
151   def compSize
152      super(self)
153   end
154   
155   def send(mailbox)
156     super(self,mailbox)
157   end
158   
159   def source
160     super(self)
161   end
162   
163   def sender
164     super(self)
165   end
166   
167   def listen(t_alias)
168     super(t_alias)
169   end
170   
171   def execute
172     super(self)
173   end
174     
175   def listenFromHost(t_alias,host)
176     super(t_alias,host)
177   end
178   
179   def setPriority(priority)
180     super(self,priority)
181   end
182   
183   def cancel()
184     super(self)
185   end
186   
187   def hasData()
188     super(self)
189   end
190    
191 end  
192 ####################################################
193 # Host Extend from the native Class RbHost
194 ####################################################
195 class MSG::Host < MSG::RbHost
196   
197   attr_reader :data
198   def initialize(*ars)
199     @data = 1
200     p "Host Initializer"
201   end
202   
203   def data()
204     return @data
205   end
206   
207   def setData(value)
208     @data = value
209   end
210   
211   def getByName(name)
212     super(name)
213   end
214   
215   def name
216     super(self)
217   end
218   
219   def speed
220     super(self)
221   end
222   
223   def getData
224     super(self)
225   end
226   
227   
228   def isAvail
229     super(self)
230   end
231   
232   def number
233     super()
234   end
235   
236   def getHostProcess(process)
237     super(process)
238   end
239     
240 end
241 #########################
242 # Main chunck 
243 #########################
244 MSG.init(ARGV)