Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
30dc1dfc6a0990bc2901c7a694310f7d6ca46e24
[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       
76     start()
77       }
78     end
79     
80   def main(args) 
81     # To be overriden by childs
82     raise("You must define a main() function in your process, containing the code of this process")
83   end
84      
85   def start()
86      @schedBegin.acquire
87     MSG::debug("Let's execute the main() of the Ruby process")
88     main(@pargs)
89     @schedEnd.release
90     MSG::debug("Released my schedEnd, bailing out")
91     processExit(self) # Exit the Native Process
92     
93   end
94     
95   def getBind()
96     return @bind
97   end
98    
99   def setBind(bind)
100     @bind = bind
101   end
102     
103   def unschedule()
104     @schedEnd.release
105     @schedBegin.acquire
106   end
107   
108   def schedule()   
109     @schedBegin.release
110     @schedEnd.acquire
111   end
112   
113   def pause()
114     processSuspend(self)
115   end
116   
117   def restart()
118     processResume(self)
119   end
120   
121   def isSuspended()
122     processIsSuspended(self)
123   end
124   
125   def getHost()
126     processGetHost(self)
127   end
128
129 # The Rest of Methods !!! To be Continued ... FIXME: what's missing?
130 end
131 ############################################
132 # Task Extend from the native Class RbTask
133 ############################################
134 class MSG::Task < MSG::RbTask
135
136   def initialize(*args)
137      super()
138   end
139   
140   def name
141      super(self)
142   end
143   
144   def compSize
145      super(self)
146   end
147   
148   def send(mailbox)
149     super(self,mailbox)
150   end
151   
152   def source
153     super(self)
154   end
155   
156   def sender
157     super(self)
158   end
159   
160   def listen(t_alias)
161     super(t_alias)
162   end
163   
164   def execute
165     super(self)
166   end
167     
168   def listenFromHost(t_alias,host)
169     super(t_alias,host)
170   end
171 end  
172 ####################################################
173 # Host Extend from the native Class RbHost
174 ####################################################
175 class MSG::Host < MSG::RbHost
176   def getByName(name)
177     super(name)
178   end
179   
180   def name
181     super(self)
182   end
183   
184   def speed
185     super(self)
186   end
187   
188   def getData
189     super(self)
190   end
191   
192   def setData(data)
193     super(self,data)
194   end
195   
196   def isAvail
197     super(self)
198   end
199   
200   def number
201     super()
202   end
203 end
204 #########################
205 # Main chunck 
206 #########################
207 MSG.init(ARGV)