pythonThread.h 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. // Filename: pythonThread.h
  2. // Created by: drose (13Apr07)
  3. //
  4. ////////////////////////////////////////////////////////////////////
  5. //
  6. // PANDA 3D SOFTWARE
  7. // Copyright (c) 2001 - 2004, Disney Enterprises, Inc. All rights reserved
  8. //
  9. // All use of this software is subject to the terms of the Panda 3d
  10. // Software license. You should have received a copy of this license
  11. // along with this source code; you will also find a current copy of
  12. // the license at http://etc.cmu.edu/panda3d/docs/license/ .
  13. //
  14. // To contact the maintainers of this program write to
  15. // [email protected] .
  16. //
  17. ////////////////////////////////////////////////////////////////////
  18. #ifndef PYTHONTHREAD_H
  19. #define PYTHONTHREAD_H
  20. #include "pandabase.h"
  21. #include "thread.h"
  22. #ifdef HAVE_PYTHON
  23. #undef HAVE_LONG_LONG // NSPR and Python both define this.
  24. #undef _POSIX_C_SOURCE
  25. #include <Python.h>
  26. #endif // HAVE_PYTHON
  27. #ifdef HAVE_PYTHON
  28. ////////////////////////////////////////////////////////////////////
  29. // Class : PythonThread
  30. // Description : This class is exposed to Python to allow creation of
  31. // a Panda thread from the Python level. It will spawn
  32. // a thread that executes an arbitrary Python functor.
  33. ////////////////////////////////////////////////////////////////////
  34. class EXPCL_PANDA PythonThread : public Thread {
  35. PUBLISHED:
  36. PythonThread(PyObject *function, PyObject *args,
  37. const string &name, const string &sync_name);
  38. virtual ~PythonThread();
  39. BLOCKING PyObject *join();
  40. protected:
  41. virtual void thread_main();
  42. private:
  43. void handle_python_exception();
  44. private:
  45. PyObject *_function;
  46. PyObject *_args;
  47. PyObject *_result;
  48. public:
  49. static TypeHandle get_class_type() {
  50. return _type_handle;
  51. }
  52. static void init_type() {
  53. Thread::init_type();
  54. register_type(_type_handle, "PythonThread",
  55. Thread::get_class_type());
  56. }
  57. virtual TypeHandle get_type() const {
  58. return get_class_type();
  59. }
  60. virtual TypeHandle force_init_type() {init_type(); return get_class_type();}
  61. private:
  62. static TypeHandle _type_handle;
  63. };
  64. #endif // HAVE_PYTHON
  65. #endif