Panda3D
 All Classes Functions Variables Enumerations
pythonThread.cxx
1 // Filename: pythonThread.cxx
2 // Created by: drose (13Apr07)
3 //
4 ////////////////////////////////////////////////////////////////////
5 //
6 // PANDA 3D SOFTWARE
7 // Copyright (c) Carnegie Mellon University. All rights reserved.
8 //
9 // All use of this software is subject to the terms of the revised BSD
10 // license. You should have received a copy of this license along
11 // with this source code in a file named "LICENSE."
12 //
13 ////////////////////////////////////////////////////////////////////
14 
15 #include "pythonThread.h"
16 #include "pnotify.h"
17 
18 #ifdef HAVE_PYTHON
19 
20 TypeHandle PythonThread::_type_handle;
21 
22 ////////////////////////////////////////////////////////////////////
23 // Function: PythonThread::Constructor
24 // Access: Published
25 // Description:
26 ////////////////////////////////////////////////////////////////////
27 PythonThread::
28 PythonThread(PyObject *function, PyObject *args,
29  const string &name, const string &sync_name) :
30  Thread(name, sync_name)
31 {
32  _function = function;
33  Py_INCREF(_function);
34  _args = NULL;
35  _result = NULL;
36 
37  if (!PyCallable_Check(_function)) {
38  nassert_raise("Invalid function passed to PythonThread constructor");
39  }
40 
41  if (args == Py_None) {
42  // None means no arguments; create an empty tuple.
43  _args = PyTuple_New(0);
44  } else {
45  _args = NULL;
46  if (PySequence_Check(args)) {
47  _args = PySequence_Tuple(args);
48  }
49  if (_args == NULL) {
50  nassert_raise("Invalid args passed to PythonThread constructor");
51  }
52  }
53 }
54 
55 ////////////////////////////////////////////////////////////////////
56 // Function: PythonThread::Destructor
57 // Access: Published, Virtual
58 // Description:
59 ////////////////////////////////////////////////////////////////////
60 PythonThread::
61 ~PythonThread() {
62  Py_DECREF(_function);
63  Py_XDECREF(_args);
64  Py_XDECREF(_result);
65 }
66 
67 ////////////////////////////////////////////////////////////////////
68 // Function: PythonThread::join
69 // Access: Published
70 // Description: Blocks the calling process until the thread
71 // terminates. If the thread has already terminated,
72 // this returns immediately.
73 //
74 // The PythonThread flavor of this function returns the
75 // same value returned by the thread function.
76 ////////////////////////////////////////////////////////////////////
77 PyObject *PythonThread::
78 join() {
79  Thread::join();
80 
81  if (_result == NULL) {
82  // No result; return None.
83  return Py_BuildValue("");
84  }
85 
86  Py_INCREF(_result);
87  return _result;
88 }
89 
90 ////////////////////////////////////////////////////////////////////
91 // Function: PythonThread::thread_main
92 // Access: Protected, Virtual
93 // Description:
94 ////////////////////////////////////////////////////////////////////
95 void PythonThread::
96 thread_main() {
97  _result = call_python_func(_function, _args);
98 }
99 
100 #endif // HAVE_PYTHON
void join()
Blocks the calling process until the thread terminates.
Definition: thread.I:298
A thread; that is, a lightweight process.
Definition: thread.h:51
TypeHandle is the identifier used to differentiate C++ class types.
Definition: typeHandle.h:85