01: // Copyright (c) Corporation for National Research Initiatives
02: package org.python.modules;
03:
04: import org.python.core.*;
05:
06: class FunctionThread extends Thread {
07: PyObject func;
08: PyObject[] args;
09: PySystemState systemState;
10:
11: public FunctionThread(PyObject func, PyObject[] args) {
12: super ();
13: this .func = func;
14: this .args = args;
15: this .systemState = Py.getSystemState();
16: }
17:
18: public void run() {
19: Py.setSystemState(systemState);
20: try {
21: func.__call__(args);
22: } catch (PyException exc) {
23: Py.printException(exc);
24: }
25: }
26: }
27:
28: public class thread implements ClassDictInit {
29: public static PyString __doc__ = new PyString(
30: "This module provides primitive operations to write multi-threaded "
31: + "programs.\n"
32: + "The 'threading' module provides a more convenient interface.");
33:
34: public static void classDictInit(PyObject dict) {
35: dict.__setitem__("LockType", PyType.fromClass(PyLock.class));
36: }
37:
38: public static PyObject error = new PyString("thread.error");
39:
40: public static void start_new_thread(PyObject func, PyTuple args) {
41: Thread pt = new FunctionThread(func, args.getArray());
42: PyObject currentThread = func.__findattr__("im_self");
43: if (currentThread != null) {
44: PyObject isDaemon = currentThread.__findattr__("isDaemon");
45: if (isDaemon != null && isDaemon.isCallable()) {
46: PyObject po = isDaemon.__call__();
47: pt.setDaemon(po.__nonzero__());
48: }
49: PyObject getName = currentThread.__findattr__("getName");
50: if (getName != null && getName.isCallable()) {
51: PyObject pname = getName.__call__();
52: pt.setName(String.valueOf(pname));
53: }
54: }
55: pt.start();
56: }
57:
58: public static PyLock allocate_lock() {
59: return new PyLock();
60: }
61:
62: public static void exit() {
63: exit_thread();
64: }
65:
66: public static void exit_thread() {
67: throw new PyException(Py.SystemExit, new PyInteger(0));
68: }
69:
70: public static long get_ident() {
71: return Py.java_obj_id(Thread.currentThread());
72: }
73: }
|