Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Prefer automatically updated date marker to manual one
[simgrid.git] / src / java / simgrid / msg / Sem.java
1 /*\r
2  * $Id$\r
3  *\r
4  * Copyright 2006,2007 Martin Quinson, Malek Cherier           \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 simgrid.msg;\r
13 \r
14 public class Sem { \r
15         \r
16         /******************************************************************/\r
17         /* Simple semaphore implementation, from Doug Lea (public domain) */\r
18         /******************************************************************/\r
19         private int permits_;\r
20         \r
21         public Sem(int i) {\r
22                 permits_ = i;\r
23         }\r
24         \r
25         public void acquire() throws InterruptedException {\r
26                 \r
27                 if (Thread.interrupted()) \r
28                         throw new InterruptedException();\r
29                 \r
30                 synchronized(this) {\r
31                 \r
32                         try {\r
33                                         while (permits_ <= 0) \r
34                                                 wait();\r
35                                         \r
36                                         --permits_;\r
37                         }\r
38                         catch (InterruptedException ex) {\r
39                                 notify();\r
40                                 throw ex;\r
41                         }\r
42                 }\r
43         }\r
44 \r
45         public synchronized void release() {\r
46                 ++(this.permits_);\r
47                 notify();\r
48         }\r
49 }\r