-
Notifications
You must be signed in to change notification settings - Fork 60
Expand file tree
/
Copy path_thread.java
More file actions
136 lines (120 loc) · 4.59 KB
/
Copy path_thread.java
File metadata and controls
136 lines (120 loc) · 4.59 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
// Copyright (c) Corporation for National Research Initiatives
package org.python.modules.thread;
import org.python.core.FunctionThread;
import org.python.core.Py;
import org.python.core.PyException;
import org.python.core.PyInteger;
import org.python.core.PyLong;
import org.python.core.PyObject;
import org.python.core.PyTuple;
import org.python.expose.ExposedConst;
import org.python.expose.ExposedFunction;
import org.python.expose.ExposedModule;
import org.python.expose.ModuleInit;
import java.util.concurrent.atomic.AtomicInteger;
@ExposedModule(name = "_thread")
public class _thread {
private static volatile long stack_size = 0; // XXX - can we figure out the current stack size?
private static ThreadGroup group = new ThreadGroup("jython-threads");
private static final AtomicInteger _count = new AtomicInteger(0);
@ExposedConst
public static final double TIMEOUT_MAX = 9223372036.0;
@ModuleInit
public static void classDictInit(PyObject dict) {
dict.__setitem__("LockType", PyLock.TYPE);
dict.__setitem__("_local", PyLocal.TYPE);
dict.__setitem__("error", Py.RuntimeError);
}
@ExposedFunction
public static PyLong _count() {
return new PyLong(_count.get());
}
@ExposedFunction
public static void start_new_thread(PyObject func, PyObject args) {
Thread pt = newFunctionThread(func, (PyTuple) args);
PyObject currentThread = func.__findattr__("__self__");
if (currentThread != null) {
PyObject isDaemon = currentThread.__findattr__("isDaemon");
if (isDaemon != null && isDaemon.isCallable()) {
PyObject po = isDaemon.__call__();
pt.setDaemon(po.__bool__());
}
PyObject getName = currentThread.__findattr__("getName");
if (getName != null && getName.isCallable()) {
PyObject pname = getName.__call__();
pt.setName(String.valueOf(pname));
}
}
pt.start();
}
/**
* Initializes a {@link FunctionThread}, using the configured stack_size and
* registering the thread in the @link {@link #group} of threads spawned by
* the thread module.
*
* Also used from the threading.py module.
*/
@ExposedFunction
public static PyObject _newFunctionThread(PyObject func, PyObject args) {
return Py.java2py(newFunctionThread(func, (PyTuple) args));
}
public static FunctionThread newFunctionThread(PyObject func, PyTuple args) {
return new FunctionThread(func, args.getArray(), stack_size, group);
}
/**
* Interrupts all running threads spawned by the thread module.
*
* This works in conjunction with:<ul> <li>
* {@link org.python.core.PyTableCode#call}: checks for the interrupted
* status of the current thread and raise a SystemRestart exception if a
* interruption is detected.</li>
* <li>{@link FunctionThread#run()}: exits the current thread when a
* SystemRestart exception is not caught.</li>
*
* Thus, it is possible that this doesn't make all running threads to stop,
* if SystemRestart exception is caught.
*/
@ExposedFunction
public static void interruptAllThreads() {
group.interrupt();
}
@ExposedFunction
public static PyLock allocate_lock() {
return new PyLock();
}
@ExposedFunction
public static void exit() {
exit_thread();
}
@ExposedFunction
public static void exit_thread() {
throw new PyException(Py.SystemExit, new PyInteger(0));
}
@ExposedFunction
public static PyObject _set_sentinel() {
return new PyLock();
}
@ExposedFunction
public static long get_ident() {
return Thread.currentThread().getId();
}
@ExposedFunction
public static long stack_size(PyObject[] args, String[] keywords) {
switch (args.length) {
case 0:
return stack_size;
case 1:
long old_stack_size = stack_size;
int proposed_stack_size = args[0].asInt();
if (proposed_stack_size != 0 && proposed_stack_size < 32768) {
// as specified by Python, Java quietly ignores what
// it considers are too small
throw Py.ValueError("size not valid: " + proposed_stack_size + " bytes");
}
stack_size = proposed_stack_size;
return old_stack_size;
default:
throw Py.TypeError("stack_size() takes at most 1 argument (" + args.length + "given)");
}
}
}