Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
git-svn-id: svn+ssh://scm.gforge.inria.fr/svn/simgrid/simgrid/trunk@7117 48e7efb5...
[simgrid.git] / src / bindings / ruby / Semaphore.rb
1
2 #  * $Id$
3 #  *
4 #  * Copyright 2010 Martin Quinson, Mehdi Fekari           
5 #  * All right reserved. 
6 #  *
7 #  * This program is free software; you can redistribute 
8 #  * it and/or modify it under the terms of the license 
9 #  *(GNU LGPL) which comes with this package. 
10 require 'msg'
11 require 'thread'
12 class Semaphore
13   
14    Thread.abort_on_exception = true
15     attr_accessor :permits
16
17    
18   def initialize ( permits )
19     
20        @permits = permits
21
22   end
23    
24
25   def acquire(mutex,cv)
26
27     raise "Interrupted Thread " if (!Thread.current.alive?)
28     mutex.synchronize {
29     while @permits <= 0
30        
31        cv.wait(mutex)
32        
33     end
34     
35     @permits = @permits - 1
36     cv.signal
37     
38     }
39     
40   end
41     
42   def release(mutex,cv)
43     mutex.synchronize{
44       
45       @permits += 1
46       cv.signal
47            
48       }
49   end
50   
51 end