Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
New version of the java bindings by Malek, passing all [existing] tests
[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 package simgrid.msg;\r
12 \r
13 public class Sem { \r
14         private int permits_;\r
15         \r
16         public Sem(int i) {\r
17                 permits_ = i;\r
18         }\r
19         \r
20         public void acquire() throws InterruptedException {\r
21                 \r
22                 if (Thread.interrupted()) \r
23                         throw new InterruptedException();\r
24                 \r
25                 synchronized(this) {\r
26                 \r
27                         try {\r
28                                         while (permits_ <= 0) \r
29                                                 wait();\r
30                                         \r
31                                         --permits_;\r
32                         }\r
33                         catch (InterruptedException ex) {\r
34                                 notify();\r
35                                 throw ex;\r
36                         }\r
37                 }\r
38         }\r
39 \r
40         public synchronized void release() {\r
41                 ++permits_;\r
42                 notify();\r
43         }\r
44 }\r