Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
3c5eedea18849278a5e19da9ca3b1ecefffcaf94
[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
12 class Semaphore
13   def initialize(initvalue = 0)
14     @counter = initvalue
15     @waiting_list = []
16   end
17
18   def acquire
19     MSG::debug("Acquire "+self.to_s)
20     Thread.critical = true
21     if (@counter -= 1) < 0
22       @waiting_list.push(Thread.current)
23       Thread.stop
24     end
25     self
26   ensure
27     Thread.critical = false
28   end
29
30   def release
31     MSG::debug("Release "+self.to_s)
32     Thread.critical = true
33     begin
34       if (@counter += 1) <= 0
35         t = @waiting_list.shift
36         t.wakeup if t
37         MSG::debug("Wakeup "+t.to_s)
38       else 
39         MSG::debug("Nobody to wakeup")
40       end
41     rescue ThreadError
42       retry
43     end
44     self
45   ensure
46     Thread.critical = false
47   end
48 end
49
50 ########################################################################
51 # Class Process 
52 ########################################################################
53 class MSG::Process < Thread 
54   @@nextProcessId = 0
55
56 # Attributes
57   attr_reader :bind, :id, :name, :pargs ,:properties# Read only
58   
59     def initialize(*args)
60       super(){
61         
62      raise "Bad Number Of arguments to create a Ruby Process (name,args,prop) " if args.size < 3
63      
64     @schedBegin = Semaphore.new(0)
65     @schedEnd = Semaphore.new(0)    
66     #@properties = Hash.new() FIXME: get this from the C (yep that makes 4 args to this function)
67     @id = @@nextProcessId++
68     @name = args[0]
69     @pargs = args[1]
70     @properties = args[2]
71     start()
72       }
73     end
74     
75   # main
76   def main(args) 
77     # To be overriden by childs
78     raise("You must define a main() function in your process, containing the code of this process")
79   end
80      
81   # Start : To keep the process alive and waiting via semaphore
82   def start()
83     @schedBegin.acquire
84     # execute the main code of the process     
85     MSG::debug("Begin execution")
86     main(@pargs)
87 #     processExit(self) # Exit the Native Process
88     @schedEnd.release
89   end
90     
91
92   
93   # Get Bind
94   def getBind()
95     return @bind
96   end
97   
98   # Set Binds
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 ############################################
133 # Task Extend from the native Class RbTask
134 ############################################
135 class MSG::Task < MSG::RbTask
136
137   def initialize(*args)
138      super()
139   end
140   
141   def name
142      super(self)
143   end
144   
145   def compSize
146      super(self)
147   end
148   
149   def send(mailbox)
150     super(mailbox)
151   end
152   
153   def receive(mailbox)
154     super(mailbox)
155   end
156   
157   def source
158     super(self)
159   end
160   
161   def sender
162     super(self)
163   end
164   
165   def listen(t_alias)
166     super(t_alias)
167   end
168   
169   def execute
170     super(self)
171   end
172     
173   def listenFromHost(t_alias,host)
174     super(t_alias,host)
175   end
176     
177 end  
178
179 ############################################
180 # Host Extend from the native Class RbHost
181 ############################################
182 class MSG::Host < MSG::RbHost
183
184   def getByName(name)
185     super(name)
186   end
187   
188   def name
189     super(self)
190   end
191   
192   def speed
193     super(self)
194   end
195   
196   def getData
197     super(self)
198   end
199   
200   def setData(data)
201     super(self,data)
202   end
203   
204   def isAvail
205     super(self)
206   end
207   
208   def number
209     super()
210   end
211   
212 end
213
214 #########################
215 # Main chunck 
216 #########################
217 MSG.init(ARGV)