Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
c618f33a6193823e8da64096a05c4240ef1103a8
[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_ruby'
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()    
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 end  
188 ####################################################
189 # Host Extend from the native Class RbHost
190 ####################################################
191 class MSG::Host < MSG::RbHost
192   def getByName(name)
193     super(name)
194   end
195   
196   def name
197     super(self)
198   end
199   
200   def speed
201     super(self)
202   end
203   
204   def getData
205     super(self)
206   end
207   
208   def setData(data)
209     super(self,data)
210   end
211   
212   def isAvail
213     super(self)
214   end
215   
216   def number
217     super()
218   end
219   
220   def getHostProcess(process)
221     super(process)
222   end
223     
224 end
225 #########################
226 # Main chunck 
227 #########################
228 MSG.init(ARGV)