Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Cleanup log mess in ruby bindings, various cosmetics, restaure a FIXME which slept...
[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 # Read only
58   attr_accessor :properties             # R/W 
59   
60
61     def initialize(*args)
62       super(){
63         
64      raise "Bad Number Of arguments to create a a Ruby Process (name,args,prop) " if args.size < 3
65      
66     @schedBegin = Semaphore.new(0)
67     @schedEnd = Semaphore.new(0)    
68     #@properties = Hash.new() FIXME: get this from the C (yep that makes 4 args to this function)
69     @id = @@nextProcessId++
70     @name = args[0]
71     @pargs = args[1]
72     @properties = args[2]
73     
74      start()
75       
76       }
77       
78     end
79     
80     
81   # main
82   def main(args) 
83     # To be overriden by childs
84     raise("You must define a main() function in your process, containing the code of this process")
85   end
86      
87   # Start : To keep the process alive and waiting via semaphore
88   def start()
89     @schedBegin.acquire
90     # execute the main code of the process     
91     MSG::debug("Begin execution")
92     main(@pargs)
93     processExit(self) # Exit the Native Process
94     @schedEnd.release
95   end
96     
97
98   
99   # Get Bind
100   def getBind()
101     return @bind
102   end
103   
104   # Set Binds
105   def setBind(bind)
106     @bind = bind
107   end
108     
109   def unschedule()
110     @schedEnd.release
111     @schedBegin.acquire
112   end
113   
114   def schedule()
115     @schedBegin.release
116     @schedEnd.acquire
117   end
118   
119   def pause()
120     processSuspend(self)
121   end
122   
123   def restart()
124     processResume(self)
125   end
126   
127   def isSuspended()
128     processIsSuspended(self)
129   end
130   
131   def getHost()
132     processGetHost(self)
133   end
134   
135 # The Rest of Methods !!! To be Continued ... FIXME: what's missing?
136 end
137
138 #########################
139 # Main chunck 
140 #########################
141 MSG.init(ARGV)