-
Notifications
You must be signed in to change notification settings - Fork 227
Expand file tree
/
Copy pathCondition.java
More file actions
172 lines (145 loc) · 4.62 KB
/
Condition.java
File metadata and controls
172 lines (145 loc) · 4.62 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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
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.expose.ExposedType;
import org.python.expose.ExposedMethod;
import org.python.expose.ExposedNew;
import org.python.core.Traverseproc;
import org.python.core.Visitproc;
@ExposedType(name = "_threading.Condition")
public class Condition extends PyObject implements ContextManager, Traverseproc {
public static final PyType TYPE = PyType.fromClass(Condition.class);
private final ConditionSupportingLock _lock;
private final java.util.concurrent.locks.Condition _condition;
public Condition() {
this(new RLock());
}
public Condition(ConditionSupportingLock lock) {
_lock = lock;
_condition = lock.getLock().newCondition();
}
@ExposedNew
final static PyObject Condition___new__ (PyNewWrapper new_, boolean init,
PyType subtype, PyObject[] args, String[] keywords) {
final int nargs = args.length;
if (nargs == 1) {
return new Condition((ConditionSupportingLock)args[0]);
}
return new Condition();
}
public boolean acquire() {
return Condition_acquire(true);
}
public boolean acquire(boolean blocking) {
return Condition_acquire(blocking);
}
@ExposedMethod(defaults = "true")
final boolean Condition_acquire(boolean blocking) {
return _lock.acquire(blocking);
}
public PyObject __enter__(ThreadState ts) {
_lock.acquire();
return this;
}
@ExposedMethod
final PyObject Condition___enter__() {
Condition_acquire(true);
return this;
}
public void release() {
Condition_release();
}
@ExposedMethod
final void Condition_release() {
_lock.release();
}
public boolean __exit__(ThreadState ts, PyException exception) {
_lock.release();
return false;
}
@ExposedMethod
final boolean Condition___exit__(PyObject type, PyObject value, PyObject traceback) {
Condition_release();
return false;
}
public void wait$(PyObject timeout) throws InterruptedException {
Condition_wait(timeout);
}
@ExposedMethod(defaults = "Py.None")
final void Condition_wait(PyObject timeout) throws InterruptedException {
try {
if (timeout == Py.None) {
_condition.await();
} else {
long nanos = (long) (timeout.asDouble() * 1e9);
_condition.awaitNanos(nanos);
}
} catch (IllegalMonitorStateException ex) {
throw Py.RuntimeError("cannot wait on un-acquired lock");
}
}
public void notify$() {
Condition_notify(1);
}
@ExposedMethod(defaults = "1")
final void Condition_notify(int count) {
try {
for( int i = 0; i < count; i++) {
_condition.signal();
}
} catch (IllegalMonitorStateException ex) {
throw Py.RuntimeError("cannot notify on un-acquired lock");
}
}
public void notifyAll$() {
Condition_notifyAll();
}
@ExposedMethod
final void Condition_notifyAll() {
try {
_condition.signalAll();
} catch (IllegalMonitorStateException ex) {
throw Py.RuntimeError("cannot notify on un-acquired lock");
}
}
@ExposedMethod
final void Condition_notify_all() {
_condition.signalAll();
}
public boolean _is_owned() {
return Condition__is_owned();
}
@ExposedMethod
final boolean Condition__is_owned() {
return _lock._is_owned();
}
@Override
public String toString() {
int count = 0;
try {
count = _lock.getWaitQueueLength(_condition);
} catch (IllegalMonitorStateException ex) {
// ignore if lock is not held
}
return (Py.newString("<_threading.Condition(%s, %d)>").__mod__(
new PyTuple(
Py.newString(_lock.toString()),
Py.newInteger(count)))).toString();
}
/* Traverseproc implementation */
@Override
public int traverse(Visitproc visit, Object arg) {
return _lock != null ? visit.visit((PyObject)_lock, arg) : 0;
}
@Override
public boolean refersDirectlyTo(PyObject ob) {
return ob != null && _lock == ob;
}
}