Logo AND Algorithmique Numérique Distribuée

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