###########################################################################
# Class Semaphore
###########################################################################
+class MySemaphore
+ Thread.abort_on_exception = true
+ attr_accessor :permits
+
+
+ def initialize (permits = 0)
+ @permits = permits
+ end
+
+ def acquire(mutex,cv)
+
+ raise "Interrupted Thread " if (!Thread.current.alive?)
+ mutex.synchronize {
+ while @permits <= 0
+
+ cv.wait(mutex)
+
+ end
+ @permits = @permits - 1
+ cv.signal
+ }
+
+ end
+
+ def release(mutex,cv)
+ mutex.synchronize{
+ @permits += 1
+ cv.signal
+ }
+ end
+end
+#######################################
+# Another Semaphore
+#######################################
class Semaphore
def initialize(initvalue = 0)
end
def acquire
- MSG::debug("Acquire "+self.to_s)
+ MSG::debug(Thread.current.to_s+" acquires "+self.to_s)
Thread.critical = true
if (@counter -= 1) < 0
@waiting_list.push(Thread.current)
end
def release
- MSG::debug("Release "+self.to_s)
+ MSG::debug(Thread.current.to_s+" releases "+self.to_s)
Thread.critical = true
begin
if (@counter += 1) <= 0
raise "Bad Number Of arguments to create a Ruby Process (name,args,prop) " if args.size < 3
+# @cv = ConditionVariable.new
+# @mutex = Mutex.new
@schedBegin = Semaphore.new(0)
@schedEnd = Semaphore.new(0)
- #@properties = Hash.new() FIXME: get this from the C (yep that makes 4 args to this function)
- @id = @@nextProcessId++
+ @id = @@nextProcessId
+ @@nextProcessId +=1
@name = args[0]
@pargs = args[1]
@properties = args[2]
+
start()
}
end
# Start : To keep the process alive and waiting via semaphore
def start()
- @schedBegin.acquire
+ @schedBegin.acquire()
# execute the main code of the process
MSG::debug("Begin execution")
main(@pargs)
# processExit(self) # Exit the Native Process
- @schedEnd.release
+ @schedEnd.release()
end
-
+ # FIXME: useless, there is an attribute for bind (or the attribute is useless)
# Get Bind
def getBind()
return @bind
end
- # Set Binds
+ # Set Binds FIXME: same
def setBind(bind)
@bind = bind
end
def unschedule()
- @schedEnd.release
- @schedBegin.acquire
+ @schedEnd.release()
+ @schedBegin.acquire()
end
def schedule()
- @schedBegin.release
- @schedEnd.acquire
+ @schedBegin.release()
+ @schedEnd.acquire()
end
def pause()
end
def send(mailbox)
- super(mailbox)
+ super(self,mailbox)
end
+# FIXME : this methode should be associated to the class !! it reurn a task
def receive(mailbox)
- super(mailbox)
+ super(self,mailbox)
end
def source