Skip to content

Commit a8bb673

Browse files
committed
test_thread add append lock (to be removed when the append method (and simmilar) are thread safe)
- merge _thread module builtins with the new builtin LazyPythonClass mechanism
1 parent 1e69638 commit a8bb673

File tree

7 files changed

+26
-17
lines changed

7 files changed

+26
-17
lines changed

graalpython/com.oracle.graal.python.test/src/tests/test_thread.py

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -121,16 +121,19 @@ def test_stack_size(self):
121121
def test__count(self):
122122
# Test the _count() function.
123123
orig = thread._count()
124+
_append_lock = thread.allocate_lock()
124125
mut = thread.allocate_lock()
125126
mut.acquire()
126127
started = []
127128
done = []
128129

129130
def task():
130-
started.append(None)
131+
with _append_lock:
132+
started.append(None)
131133
mut.acquire()
132134
mut.release()
133-
done.append(None)
135+
with _append_lock:
136+
done.append(None)
134137

135138
thread.start_new_thread(task, ())
136139
while not started:
@@ -249,14 +252,19 @@ def __init__(self, f, n, wait_before_exit=False):
249252
self.started = []
250253
self.finished = []
251254
self._can_exit = not wait_before_exit
255+
self._append_lock = thread.allocate_lock()
252256

253257
def task():
254258
tid = threading.get_ident()
255-
self.started.append(tid)
259+
# TODO: remove the append lock once append like ops are thread safe
260+
with self._append_lock:
261+
self.started.append(tid)
256262
try:
257263
f()
258264
finally:
259-
self.finished.append(tid)
265+
# TODO: remove the append lock once append like ops are thread safe
266+
with self._append_lock:
267+
self.finished.append(tid)
260268
while not self._can_exit:
261269
_wait()
262270
try:

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/PythonBuiltinClassType.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -86,9 +86,9 @@ public enum PythonBuiltinClassType implements LazyPythonClass {
8686
PCode("code"),
8787
PZip("zip", "builtins"),
8888
PBuffer("buffer", "builtins"),
89-
PThread("thread", "_thread"),
90-
PLock("lock", "_thread"),
91-
PRLock("rlock", "_thread"),
89+
PThread("start_new_thread", "_thread"),
90+
PLock("LockType", "_thread"),
91+
PRLock("RLock", "_thread"),
9292
PSocket("socket", "_socket"),
9393

9494
// Errors and exceptions:

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/objects/thread/AbstractPythonLock.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,15 +41,15 @@
4141
package com.oracle.graal.python.builtins.objects.thread;
4242

4343
import com.oracle.graal.python.builtins.objects.object.PythonBuiltinObject;
44-
import com.oracle.graal.python.builtins.objects.type.PythonClass;
44+
import com.oracle.graal.python.builtins.objects.type.LazyPythonClass;
4545

4646
public abstract class AbstractPythonLock extends PythonBuiltinObject {
4747

4848
public static double TIMEOUT_MAX = 2 ^ 31;
4949
public static boolean DEFAULT_BLOCKING = true;
5050
public static double DEFAULT_TIMEOUT = -1.0;
5151

52-
AbstractPythonLock(PythonClass cls) {
52+
AbstractPythonLock(LazyPythonClass cls) {
5353
super(cls);
5454
}
5555

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/objects/thread/PLock.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,13 +43,14 @@
4343
import java.util.concurrent.Semaphore;
4444
import java.util.concurrent.TimeUnit;
4545

46+
import com.oracle.graal.python.builtins.objects.type.LazyPythonClass;
4647
import com.oracle.graal.python.builtins.objects.type.PythonClass;
4748
import com.oracle.truffle.api.CompilerDirectives.TruffleBoundary;
4849

4950
public final class PLock extends AbstractPythonLock {
5051
private final Semaphore semaphore;
5152

52-
public PLock(PythonClass cls) {
53+
public PLock(LazyPythonClass cls) {
5354
super(cls);
5455
semaphore = new Semaphore(1);
5556
}

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/objects/thread/PRLock.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@
4343
import java.util.concurrent.TimeUnit;
4444
import java.util.concurrent.locks.ReentrantLock;
4545

46-
import com.oracle.graal.python.builtins.objects.type.PythonClass;
46+
import com.oracle.graal.python.builtins.objects.type.LazyPythonClass;
4747
import com.oracle.truffle.api.CompilerDirectives.TruffleBoundary;
4848

4949
public final class PRLock extends AbstractPythonLock {
@@ -61,7 +61,7 @@ long getOwnerId() {
6161

6262
private final InternalReentrantLock lock;
6363

64-
public PRLock(PythonClass cls) {
64+
public PRLock(LazyPythonClass cls) {
6565
super(cls);
6666
this.lock = new InternalReentrantLock();
6767
}

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/objects/thread/PThread.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -41,18 +41,18 @@
4141
package com.oracle.graal.python.builtins.objects.thread;
4242

4343
import com.oracle.graal.python.builtins.objects.object.PythonBuiltinObject;
44-
import com.oracle.graal.python.builtins.objects.type.PythonClass;
44+
import com.oracle.graal.python.builtins.objects.type.LazyPythonClass;
4545
import com.oracle.truffle.api.CompilerDirectives.TruffleBoundary;
4646

4747
public class PThread extends PythonBuiltinObject {
4848
public final static String GRAALPYTHON_THREADS = "GRAALPYTHON_THREADS";
4949
private final Thread thread;
5050

51-
public PThread(PythonClass cls, ThreadGroup group, Runnable runnable) {
51+
public PThread(LazyPythonClass cls, ThreadGroup group, Runnable runnable) {
5252
this(cls, group, 0, runnable);
5353
}
5454

55-
public PThread(PythonClass cls, ThreadGroup group, long stackSize, Runnable runnable) {
55+
public PThread(LazyPythonClass cls, ThreadGroup group, long stackSize, Runnable runnable) {
5656
super(cls);
5757
this.thread = new Thread(group, runnable, "graalpython-thread-" + group.activeCount(), stackSize);
5858
}

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/runtime/object/PythonObjectFactory.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -753,15 +753,15 @@ public PSocket createSocket(PythonClass cls, int family, int type, int proto) {
753753
*/
754754

755755
public PLock createLock() {
756-
return createLock(PythonBuiltinClassType.PLock);
756+
return trace(new PLock(PythonBuiltinClassType.PLock));
757757
}
758758

759759
public PLock createLock(PythonClass cls) {
760760
return trace(new PLock(cls));
761761
}
762762

763763
public PRLock createRLock() {
764-
return trace(new PRLock(lookupClass(PythonBuiltinClassType.PRLock)));
764+
return trace(new PRLock(PythonBuiltinClassType.PRLock));
765765
}
766766

767767
public PRLock createRLock(PythonClass cls) {

0 commit comments

Comments
 (0)