src.org.python.modules.thread.PyLock Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of jython-standalone Show documentation
Show all versions of jython-standalone Show documentation
Jython is an implementation of the high-level, dynamic, object-oriented
language Python written in 100% Pure Java, and seamlessly integrated with
the Java platform. It thus allows you to run Python on any Java platform.
// Copyright (c) Corporation for National Research Initiatives
package org.python.modules.thread;
import org.python.core.PyObject;
import org.python.core.ContextManager;
import org.python.core.Py;
import org.python.core.ThreadState;
import org.python.core.PyException;
import org.python.core.Untraversable;
@Untraversable
public class PyLock extends PyObject implements ContextManager {
private boolean locked = false;
public boolean acquire() {
return acquire(true);
}
public synchronized boolean acquire(boolean waitflag) {
if (waitflag) {
while (locked) {
try {
wait();
} catch (InterruptedException e) {
System.err.println("Interrupted thread");
}
}
locked = true;
return true;
} else {
if (locked) {
return false;
} else {
locked = true;
return true;
}
}
}
public synchronized void release() {
if (locked) {
locked = false;
notifyAll();
} else {
throw Py.ValueError("lock not acquired");
}
}
public boolean locked() {
return locked;
}
@Override
public PyObject __enter__(ThreadState ts) {
acquire();
return this;
}
@Override
public boolean __exit__(ThreadState ts, PyException exception) {
release();
return false;
}
}