Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
laurent's eclipse was a bit too greedy
[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,2011 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 \r
12 package org.simgrid.msg;\r
13 \r
14 public class Sem {\r
15         /******************************************************************/ \r
16         /* Simple semaphore implementation, from Doug Lea (public domain) */ \r
17         /******************************************************************/ \r
18         private int permits_;\r
19 \r
20     /**\r
21      *\r
22      * @param i\r
23      */\r
24     public Sem(int i) {\r
25                 permits_ = i;\r
26         } \r
27 \r
28     /**\r
29      *\r
30      * @throws java.lang.InterruptedException\r
31      */\r
32     public void acquire() throws InterruptedException {\r
33                 if (Thread.interrupted())\r
34                         throw new InterruptedException();\r
35 \r
36                 synchronized(this) {\r
37                         try {\r
38                                 while (permits_ <= 0)\r
39                                         wait();\r
40                                 --permits_;\r
41                         }\r
42                         catch(InterruptedException ex) {\r
43                                 notify();\r
44                                 throw ex;\r
45                         }\r
46                 }\r
47         }\r
48 \r
49     /**\r
50      *\r
51      */\r
52     public synchronized void release() {\r
53                 ++(this.permits_);\r
54                 notify();\r
55         } \r
56\r