Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
initial (almost working) release of a separate package for the Java bindings of SimGrid
[simgrid.git] / org / simgrid / msg / Sem.java
1 /*\r
2  * Simple semaphore implementation, from Doug Lea (public domain)\r
3  *\r
4  * Copyright 2006,2007,2010 The SimGrid Team           \r
5  * All right reserved. \r
6  *\r
7  * This program is free software; you can redistribute \r
8  * it and/or modify it under the terms of the license \r
9  *(GNU LGPL) which comes with this package. \r
10  */  \r
11 \rpackage simgrid.msg;
12 \r\rpublic class Sem {\r
13         /******************************************************************/ \r
14         /* Simple semaphore implementation, from Doug Lea (public domain) */ \r
15         /******************************************************************/ \r
16         private int permits_;
17 \r       public Sem(int i) {\r            permits_ = i;\r  } \r\r    public void acquire() throws InterruptedException {
18                 if (Thread.interrupted())
19                         throw new InterruptedException();
20 \r               synchronized(this) {
21                         try {
22                                 while (permits_ <= 0)
23                                         wait();
24                                 --permits_;
25                         }
26                         catch(InterruptedException ex) {
27                                 notify();
28                                 throw ex;
29                         }
30                 }
31         }
32 \r       public synchronized void release() {
33                 ++(this.permits_);
34                 notify();
35         } \r\r