Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Stuks When a process ( Slave ) Tries to Recieve a Task...
[simgrid.git] / src / bindings / ruby / RubyProcess.rb
index ed92544..1272a0e 100644 (file)
+# 
+#  * $Id$
+#  *
+#  * Copyright 2010 Martin Quinson, Mehdi Fekari           
+#  * All right reserved. 
+#  *
+#  * This program is free software; you can redistribute 
+#  * it and/or modify it under the terms of the license 
+#  *(GNU LGPL) which comes with this package. 
+
 require 'msg'
 require 'Semaphore'
 include MSG
+$DEBUG = true  # This is a Global Variable Useful for Debugging
 
-$debug = true  # This is a Gloabl Variable Useful for Debbuging
-
-
-class RbProcess < Thread
-  
-  
-  
-  
+class RbProcess < Thread 
   @@nextProcessId = 0
 # Attributes
   attr_accessor :bind, :id, :proprieties, :name,
-      :pargs, :schedBegin, :schedEnd
-  
-  
-  
-# Initialize
-  
-  
-  # Used in ApplicationHandler to Initialize it
-  
+      :pargs, :schedBegin, :schedEnd, :mutex, :cv
   
+# Initialize : USED in ApplicationHandler to Initialize it
   def initialize(*args)
     
-   
     argc = args.size
-    
-    
-    
-#      Default Init
-    if argc == 0    #>>> new()
+# Default initializer
+    if argc == 0    #>>> new()  
     super() {
-    
     @id = 0
     @bind = 0
     @name = ""
     @pargs = Array.new()
-    
     init_var()
-
-         
-    
-     if $debug
-    puts "Init Default Initialzer..."
-     end
-    
-#     Thread.pass   #yield ?!!
-#     sleep   # Sleep Forever ... To Keep Thread Alive ?!!
-    
+    start()
+     if $DEBUG
+       puts "Init Default Initialzer...Nothing to do...Bye"
+     end  
     }
     end
-    
-    
-    # Initi with 2 arguments
-    
-    if argc == 2   # >>>(HostName,Name) Or (Host , Name)
+       
+    # Init with 2 arguments **********************>>>(HostName,Name) Or (Host , Name)
+    if argc == 2   
       super(){
-       
-      
       type = args[0].type()
-      
       if ( type.to_s == "String")
-          
-       host = Host.getByName(args[0])
-       
+        host = Host.getByName(args[0])
       end
-      
       if ( type.to_s == "MSG::Host")
-      host = args[0]  
+        host = args[0]  
       end
-      
-      
-      if $debug
-      puts host
+      if $DEBUG
+        puts host
       end
-      
-      
-      
-      
       raise "Process Name Cannot Be Null"   if args[1].empty?
-      
       @name = args[1] # First Arg
-      
-      if $debug
-      puts @name
+      if $DEBUG
+        puts @name
       end
-      
-      
       @pargs = Array.new()    # No Args[] Passed in Arguments
-      
-       @@nextProcessId += 1
-       @id = @@nextProcessId
-      
-       init_var()
-      
-       createProcess(self,host) #TODO >> MSG::Process
-      if $debug
-      puts "Initilize with 2 args"
-      end
-      
-#       sleep  # Keep The Thread Runin' 
+      @@nextProcessId += 1
+      @id = @@nextProcessId
+      init_var()
+      start()
+      createProcess(self,host)
+       if $DEBUG
+         puts "Initilize with 2 args"
+       end
       }
     
-    
     end
-    
        
-#     Init with 3 arguments
+    # Init with 3 arguments **********************(hostName,Name,args[]) or # (Host,Name,args[])
     
-    if argc == 3  #(hostName,Name,args[]) or # (Host,Name,args[])
+    if argc == 3  
       super(){
-       
        type = args[0].type()
-        
         if( type.to_s == "String")
-#      host = Host.getByName(args[0])
-      host ="Host.getByName(args[0])"
+         host =Host.getByName(args[0])
         end
-      
         if ( type.to_s == "MSG::Host" )
         host = args[0]
         end
-      
-        if $debug
+        if $DEBUG
         puts host
         end
       
-      
         raise "Process Name Cannot Be Null" if args[0].empty? 
-        
         @name = args[1]
-         
         type = args[2].type()
-       
         raise "Third Argument Should be an Array" if type != "Array"
-        
         @pargs = args[3]
-        
-        
         @@nextProcessId +=1
         @id = @@nextProcessId
-      
         init_var()
-             
-         createProcess(self,host)  #TODO  RubyMsg
+        createProcess(self,host)  
         
-      if $debug
+      if $DEBUG
        puts "Initilize with 3 args"
       end
       
 #       sleep #keep the thread running
        }
     end
-
     end
 
-    
-    # Init_var Called By Initialize  
-    
-    
-  def init_var()
-    
-    
+  # Init_var Called By Initialize  
+  def init_var()  
     @proprieties = Hash.new()
+    @mutex = Mutex.new
+    @cv = ConditionVariable.new
     # Process Synchronization Tools
-     @schedBegin = Semaphore.new(0)
-     @schedEnd = Semaphore.new(0)
-      
+    @schedBegin = Semaphore.new(0)
+    @schedEnd = Semaphore.new(0)    
   end
   
-  
-  
-  
+  #main
+  def msg_main(args)
+    # To Be Implemented within The Process...
+    # The Main Code of The Process to be Executed ...
+  end
+     
+  # Start : To keep the Process Alive and waitin' via semaphore
+  def start()
+    @schedBegin.acquire(@mutex,@cv)
+    #execute The Main Code of The Process ( Example Master ; Slave ...)     
+    msg_main(@pargs)
+    processExit(self) #Exite the Native Process
+    @schedEnd.release(@mutex,@cv)
+  end
+    
 #   NetxId
   def nextId ()
-    
     @@nextProcessId +=1
     return @@nextProcessId
-    
   end
-  
-  
-    
-  
-  
-  
-  if $debug
+
+  if $DEBUG
     #Process List
     def processList()
-      
       Thread.list.each {|t| p t}
-      
     end
   end
   
-  
   #Get Own ID
-  
   def getID()
-    
     return @id
-    
   end
   
   # set Id
-  
   def setID(id)
-    
     @id = id
-    
   end
   
   #Get a Process ID
-  
   def processID(process)
-    
     return process.id
-  
   end
   
-  
-  #get Own Name
-  
+  #Get Own Name
   def getName()
-   
     return @name
-  
   end
   
-  #get a Process Name
-  
+  #Get a Process Name
   def processName(process)
-  
     return process.name
-  
   end
   
-  #get Bind
+  #Get Bind
   def getBind()
-    
     return @bind
-    
   end
   
-  #set Binds
+  #Get Binds
   def setBind(bind)
-    
     @bind = bind
-    
   end
     
-  
-  
-  # Stop
-  
   def unschedule() 
     
-    Thread.pass
-  
+    @schedEnd.release(@mutex,@cv)
+#     info("@schedEnd.release(@mutex,@cv)")
+    @schedBegin.acquire(@mutex,@cv)
+#     info("@schedBegin.acquire(@mutex,@cv)")
+     
   end
   
+  def schedule()
+    @schedBegin.release(@mutex,@cv)
+    @schedEnd.acquire(@mutex,@cv)
+  end
   
-  #C Simualateur Process Equivalent  Management
+   #C Simualateur Process Equivalent  Management
   # After Binding Ruby Process to C Process
   
 #   pause
   def pause()
-  
     processSuspend(self)
-  
   end
   
 #   restart
   def restart()
-   
     processResume(self)
-  
   end
   
 #   isSuspended
   def isSuspended()
-  
     processIsSuspended(self)
   end
   
 #   getHost
   def getHost()
-  
     processGetHost(self)
-  
   end
   
-#    The Rest of Methods !!! To be Continued ...
-
+# The Rest of Methods !!! To be Continued ...
 end
\ No newline at end of file