-
Notifications
You must be signed in to change notification settings - Fork 227
Expand file tree
/
Copy pathRLock.java
More file actions
131 lines (105 loc) · 3.33 KB
/
RLock.java
File metadata and controls
131 lines (105 loc) · 3.33 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
package org.python.modules._threading;
import org.python.core.ContextManager;
import org.python.core.Py;
import org.python.core.PyException;
import org.python.core.PyNewWrapper;
import org.python.core.PyObject;
import org.python.core.PyString;
import org.python.core.PyTuple;
import org.python.core.PyType;
import org.python.core.ThreadState;
import org.python.core.Untraversable;
import org.python.expose.ExposedMethod;
import org.python.expose.ExposedNew;
import org.python.expose.ExposedType;
@Untraversable
@ExposedType(name = "_threading.RLock")
public class RLock extends PyObject implements ContextManager, ConditionSupportingLock {
public static final PyType TYPE = PyType.fromClass(RLock.class);
private final RLockImplementation _lock = new RLockImplementation();
public RLock() {
}
public java.util.concurrent.locks.Lock getLock() {
return _lock;
}
@ExposedNew
final static PyObject RLock___new__(PyNewWrapper new_, boolean init,
PyType subtype, PyObject[] args, String[] keywords) {
final int nargs = args.length;
return new RLock();
}
@ExposedMethod(defaults = "true")
final boolean RLock_acquire(boolean blocking) {
if (blocking) {
_lock.lock();
return true;
} else {
return _lock.tryLock();
}
}
public boolean acquire() {
return RLock_acquire(true);
}
public boolean acquire(boolean blocking) {
return RLock_acquire(blocking);
}
@ExposedMethod
final PyObject RLock___enter__() {
_lock.lock();
return this;
}
public PyObject __enter__(ThreadState ts) {
_lock.lock();
return this;
}
@ExposedMethod
final void RLock_release() {
if (!_lock.isHeldByCurrentThread() || _lock.getHoldCount() <= 0) {
throw Py.RuntimeError("cannot release un-acquired lock");
}
_lock.unlock();
}
public void release() {
RLock_release();
}
@ExposedMethod
final boolean RLock___exit__(PyObject type, PyObject value, PyObject traceback) {
_lock.unlock();
return false;
}
public boolean __exit__(ThreadState ts, PyException exception) {
_lock.unlock();
return false;
}
@ExposedMethod
final boolean RLock_locked() {
return _lock.isLocked();
}
public boolean locked() {
return RLock_locked();
}
@ExposedMethod
final boolean RLock__is_owned() {
return _lock.isHeldByCurrentThread();
}
public boolean _is_owned() {
return RLock__is_owned();
}
public int getWaitQueueLength(java.util.concurrent.locks.Condition condition) {
return _lock.getWaitQueueLength(condition);
}
@ExposedMethod
public String toString() {
String owner = _lock.getOwnerName();
return Py.newString("<_threading.RLock owner=%r count=%d>").
__mod__(new PyTuple(
owner != null ? Py.newStringOrUnicode(owner) : Py.None,
Py.newInteger(_lock.getHoldCount()))).toString();
}
}
final class RLockImplementation extends java.util.concurrent.locks.ReentrantLock {
String getOwnerName() {
Thread owner = getOwner();
return owner != null ? owner.getName() : null;
}
}