X-Git-Url: http://info.iut-bm.univ-fcomte.fr/pub/gitweb/simgrid.git/blobdiff_plain/d4c8ec334d02aea22b90114034f2b8b02e976c5d..05a2819a1d62a4ebe2af6d0a60654bf8e629d042:/src/bindings/ruby/Semaphore.rb diff --git a/src/bindings/ruby/Semaphore.rb b/src/bindings/ruby/Semaphore.rb index d8697c5a74..e087294f98 100644 --- a/src/bindings/ruby/Semaphore.rb +++ b/src/bindings/ruby/Semaphore.rb @@ -7,38 +7,44 @@ # * 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 'thread' class Semaphore - Thread.abort_on_exception = true - attr_accessor :permits, :mutex, :cv + Thread.abort_on_exception = true + attr_accessor :permits + def initialize ( permits ) - @permits = permits - @mutex = Mutex.new - @cv = ConditionVariable.new - + @permits = permits + end - def acquire() + def acquire(mutex,cv) raise "Interrupted Thread " if (!Thread.current.alive?) - @mutex.synchronize { - while @permits < 1 - @cv.wait(@mutex) + mutex.synchronize { + while @permits <= 0 + + cv.wait(mutex) + end + @permits = @permits - 1 + cv.signal + } + end - def release() - @mutex.synchronize{ + def release(mutex,cv) + mutex.synchronize{ @permits += 1 - @cv.signal - + cv.signal + } end