/*\r
- * $Id$\r
+ * Simple semaphore implementation, from Doug Lea (public domain)\r
*\r
- * Copyright 2006,2007 Martin Quinson, Malek Cherier \r
+ * Copyright 2006,2007,2010 The SimGrid Team \r
* All right reserved. \r
*\r
* This program is free software; you can redistribute \r
* it and/or modify it under the terms of the license \r
*(GNU LGPL) which comes with this package. \r
*/ \r
- \rpackage simgrid.msg;
-\r\rpublic class Sem {
- \r\r
- /******************************************************************/ \r
- /* Simple semaphore implementation, from Doug Lea (public domain) */ \r
- /******************************************************************/ \r
- private int permits_;
- \r\rpublic Sem(int i) {
- \rpermits_ = i;
- \r} \r\rpublic void acquire() throws InterruptedException {
- \r\rif (Thread.interrupted())
- \rthrow new InterruptedException();
- \r\rsynchronized(this) {
- \r\rtry {
- \rwhile (permits_ <= 0)
- \rwait();
- \r\r--permits_;
- \r}
- \rcatch(InterruptedException ex) {
- \rnotify();
- \rthrow ex;
- \r}
- \r}
- \r}
- \r\rpublic synchronized void release() {
- \r++(this.permits_);
- \rnotify();
-\r} \r} \r
+\rpackage simgrid.msg;
+\r\rpublic class Sem {\r
+ /******************************************************************/ \r
+ /* Simple semaphore implementation, from Doug Lea (public domain) */ \r
+ /******************************************************************/ \r
+ private int permits_;
+\r public Sem(int i) {\r permits_ = i;\r } \r\r public void acquire() throws InterruptedException {
+ if (Thread.interrupted())
+ throw new InterruptedException();
+\r synchronized(this) {
+ try {
+ while (permits_ <= 0)
+ wait();
+ --permits_;
+ }
+ catch(InterruptedException ex) {
+ notify();
+ throw ex;
+ }
+ }
+ }
+\r public synchronized void release() {
+ ++(this.permits_);
+ notify();
+ } \r} \r