-
Notifications
You must be signed in to change notification settings - Fork 227
Expand file tree
/
Copy paththread.java
More file actions
106 lines (91 loc) · 3.79 KB
/
thread.java
File metadata and controls
106 lines (91 loc) · 3.79 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
// Copyright (c) Corporation for National Research Initiatives
package org.python.modules.thread;
import org.python.core.ClassDictInit;
import org.python.core.FunctionThread;
import org.python.core.Py;
import org.python.core.PyException;
import org.python.core.PyInteger;
import org.python.core.PyObject;
import org.python.core.PyString;
import org.python.core.PyType;
import org.python.core.PyTuple;
public class thread implements ClassDictInit {
private static volatile long stack_size = 0; // XXX - can we figure out the current stack size?
private static ThreadGroup group = new ThreadGroup("jython-threads");
public static PyString __doc__ = new PyString(
"This module provides primitive operations to write multi-threaded "+
"programs.\n" +
"The 'threading' module provides a more convenient interface."
);
public static void classDictInit(PyObject dict) {
dict.__setitem__("LockType", PyType.fromClass(PyLock.class));
dict.__setitem__("_local", PyLocal.TYPE);
dict.__setitem__("interruptAllThreads", null);
}
public static PyObject error = new PyString("thread.error");
public static void start_new_thread(PyObject func, PyTuple args) {
Thread pt = _newFunctionThread(func, 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.__nonzero__());
}
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.
*/
public static FunctionThread _newFunctionThread(PyObject func, PyTuple args) {
return new FunctionThread(func, args.getArray(), stack_size, group);
}
/**
* Interrupt all running threads spawned by the thread module. This works in conjunction with:
* {@link org.python.core.PyTableCode#call}, which checks for the interrupted status of the
* current thread, and {@link FunctionThread#run()}, which exits the current thread.
*/
public static void interruptAllThreads() {
group.interrupt();
}
public static PyLock allocate_lock() {
return new PyLock();
}
public static void exit() {
exit_thread();
}
public static void exit_thread() {
throw new PyException(Py.SystemExit, new PyInteger(0));
}
public static long get_ident() {
return Thread.currentThread().getId();
}
public static long stack_size(PyObject[] args) {
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)");
}
}
}