01: // Copyright (c) Corporation for National Research Initiatives
02: package org.python.modules;
03:
04: import org.python.core.*;
05:
06: public class PyLock extends PyObject {
07: private boolean locked = false;
08:
09: //private Object lock = new Object();
10:
11: public boolean acquire() {
12: return acquire(true);
13: }
14:
15: public synchronized boolean acquire(boolean waitflag) {
16: if (waitflag) {
17: while (locked) {
18: try {
19: wait();
20: } catch (InterruptedException e) {
21: System.err.println("Interrupted thread");
22: }
23: }
24: locked = true;
25: return true;
26: } else {
27: if (locked) {
28: return false;
29: } else {
30: locked = true;
31: return true;
32: }
33: }
34: }
35:
36: public synchronized void release() {
37: if (locked) {
38: locked = false;
39: notifyAll();
40: } else {
41: throw Py.ValueError("lock not acquired");
42: }
43: }
44:
45: public boolean locked() {
46: return locked;
47: }
48: }
|