Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
5c96cdbf708828a3ef6cde6853c78d813c4c23b9
[simgrid.git] / src / bindings / ruby / simgrid.rb
1 require 'simgrid_ruby'
2 require 'thread'
3
4 $DEBUG = false  # This is a Global Variable Useful for MSG::debugging
5
6 ###########################################################################
7 # Class Semaphore 
8 ###########################################################################
9
10 class Semaphore
11   def initialize(initvalue = 0)
12     @counter = initvalue
13     @waiting_list = []
14   end
15
16   def acquire
17     MSG::debug("Acquire "+self.to_s)
18     Thread.critical = true
19     if (@counter -= 1) < 0
20       @waiting_list.push(Thread.current)
21       Thread.stop
22     end
23     self
24   ensure
25     Thread.critical = false
26   end
27
28   def release
29     MSG::debug("Release "+self.to_s)
30     Thread.critical = true
31     begin
32       if (@counter += 1) <= 0
33         t = @waiting_list.shift
34         t.wakeup if t
35         MSG::debug("Wakeup "+t.to_s)
36       else 
37         MSG::debug("Nobody to wakeup")
38       end
39     rescue ThreadError
40       retry
41     end
42     self
43   ensure
44     Thread.critical = false
45   end
46 end
47
48 ########################################################################
49 # Class Process 
50 ########################################################################
51 class MSG::Process < Thread 
52   @@nextProcessId = 0
53
54 # Attributes
55   attr_reader :bind, :id ,:name           # Read only
56   attr_accessor :properties, :pargs  # R/W
57   
58
59     def initialize(*args)
60       super(){
61         
62      raise "Bad Number Of arguments to create a 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()
67     @id = @@nextProcessId
68     @@nextProcessId += 1
69     @name = args[0]
70     @pargs = args[1]
71     @properties = args[2]
72     
73      start()
74       
75       }
76       
77     end
78     
79     
80   # main
81   def main(args) 
82     # To be overriden by childs
83     raise("You must define a main() function in your process, containing the code of this process")
84   end
85      
86   # Start : To keep the process alive and waiting via semaphore
87   def start()
88     @schedBegin.acquire
89     # execute the main code of the process     
90     MSG::debug("Begin execution")
91     main(@pargs)
92     processExit(self) # Exit the Native Process
93     @schedEnd.release
94   end
95     
96
97   
98   #Get Bind ( Used > Native to Ruby)
99   def getBind()
100     return @bind
101   end
102   
103   #Get Binds (Used > Ruby to Native)
104   def setBind(bind)
105     @bind = bind
106   end
107     
108   def unschedule()
109     @schedEnd.release
110     @schedBegin.acquire
111   end
112   
113   def schedule()
114     @schedBegin.release
115     @schedEnd.acquire
116   end
117   
118    #C Simualator Process Equivalent  Management
119   # After Binding Ruby Process to C Process
120   
121 #   pause
122   def pause()
123     processSuspend(self)
124   end
125   
126 #   restart
127   def restart()
128     processResume(self)
129   end
130   
131 #   isSuspended
132   def isSuspended()
133     processIsSuspended(self)
134   end
135   
136 #   getHost
137   def getHost()
138     processGetHost(self)
139   end
140   
141 # The Rest of Methods !!! To be Continued ...
142 end
143
144 #########################
145 # Main chunck 
146 #########################
147 MSG.init(ARGV)