Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
6e448708138fee0e1979554a1934fd950ad4b414
[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   \rpackage simgrid.msg;
12 \r\rpublic class Sem {
13   \r\r
14         /******************************************************************/ \r
15     /* Simple semaphore implementation, from Doug Lea (public domain) */ \r
16         /******************************************************************/ \r
17   private int permits_;
18   \r\rpublic Sem(int i) {
19     \rpermits_ = i;
20   \r\r\rpublic void acquire() throws InterruptedException {
21     \r\rif (Thread.interrupted())
22       \rthrow new InterruptedException();
23     \r\rsynchronized(this) {
24       \r\rtry {
25         \rwhile (permits_ <= 0)
26           \rwait();
27         \r\r--permits_;
28       \r}
29        \rcatch(InterruptedException ex) {
30         \rnotify();
31         \rthrow ex;
32       \r}
33     \r}
34   \r}
35   \r\rpublic synchronized void release() {
36     \r++(this.permits_);
37     \rnotify();
38 \r\r\r