From 7ca685c4bc0b2881555f317db6408ae488fe05aa Mon Sep 17 00:00:00 2001 From: Thomas Grill Date: Wed, 10 Nov 2004 03:31:34 +0000 Subject: support for Python threads, at last svn path=/trunk/; revision=2251 --- externals/grill/py/source/bound.cpp | 2 +- externals/grill/py/source/clmeth.cpp | 2 +- externals/grill/py/source/main.cpp | 48 ++++++++++++++++++++++++++++------ externals/grill/py/source/main.h | 19 ++++---------- externals/grill/py/source/modmeth.cpp | 2 +- externals/grill/py/source/py.cpp | 2 +- externals/grill/py/source/pyargs.cpp | 2 +- externals/grill/py/source/pyext.cpp | 27 ++++++++----------- externals/grill/py/source/pyext.h | 2 +- externals/grill/py/source/register.cpp | 2 +- 10 files changed, 63 insertions(+), 45 deletions(-) (limited to 'externals/grill/py/source') diff --git a/externals/grill/py/source/bound.cpp b/externals/grill/py/source/bound.cpp index 6fe0cd2b..2504dde0 100644 --- a/externals/grill/py/source/bound.cpp +++ b/externals/grill/py/source/bound.cpp @@ -2,7 +2,7 @@ py/pyext - python external object for PD and MaxMSP -Copyright (c) 2002-2004 Thomas Grill (xovo@gmx.net) +Copyright (c) 2002-2004 Thomas Grill (gr@grrrr.org) For information on usage and redistribution, and for a DISCLAIMER OF ALL WARRANTIES, see the file, "license.txt," in this distribution. diff --git a/externals/grill/py/source/clmeth.cpp b/externals/grill/py/source/clmeth.cpp index a9770834..b1d1253a 100644 --- a/externals/grill/py/source/clmeth.cpp +++ b/externals/grill/py/source/clmeth.cpp @@ -2,7 +2,7 @@ py/pyext - python external object for PD and Max/MSP -Copyright (c) 2002-2004 Thomas Grill (xovo@gmx.net) +Copyright (c) 2002-2004 Thomas Grill (gr@grrrr.org) For information on usage and redistribution, and for a DISCLAIMER OF ALL WARRANTIES, see the file, "license.txt," in this distribution. diff --git a/externals/grill/py/source/main.cpp b/externals/grill/py/source/main.cpp index 8c46b037..657349e7 100644 --- a/externals/grill/py/source/main.cpp +++ b/externals/grill/py/source/main.cpp @@ -2,14 +2,14 @@ py/pyext - python external object for PD and MaxMSP -Copyright (c)2002-2004 Thomas Grill (xovo@gmx.net) +Copyright (c)2002-2004 Thomas Grill (gr@grrrr.org) For information on usage and redistribution, and for a DISCLAIMER OF ALL WARRANTIES, see the file, "license.txt," in this distribution. */ #include "main.h" - +#include static PyMethodDef StdOut_Methods[] = { @@ -17,6 +17,44 @@ static PyMethodDef StdOut_Methods[] = { NULL, NULL, } }; + +#ifdef FLEXT_THREADS +typedef std::map PyThrMap; + +static PyInterpreterState *pystate = NULL; +static PyThreadState *pythrmain = NULL; +static PyThrMap pythrmap; + +PyThreadState *FindThreadState() +{ + flext::thrid_t id = flext::GetThreadId(); + PyThrMap::iterator it = pythrmap.find(id); + if(it == pythrmap.end()) { + // Make new thread state + PyThreadState *st = PyThreadState_New(pystate); + pythrmap[id] = st; + return st; + } + else + return it->second; +} + +void FreeThreadState() +{ + flext::thrid_t id = flext::GetThreadId(); + PyThrMap::iterator it = pythrmap.find(id); + if(it != pythrmap.end()) { + // clear out any cruft from thread state object + PyThreadState_Clear(it->second); + // delete my thread state object + PyThreadState_Delete(it->second); + // delete from map + pythrmap.erase(it); + } +} +#endif + + V py::lib_setup() { post(""); @@ -72,12 +110,6 @@ V py::lib_setup() FLEXT_LIB_SETUP(py,py::lib_setup) -#ifdef FLEXT_THREADS -PyInterpreterState *py::pystate = NULL; -PyThreadState *py::pythrmain = NULL; -PyThrMap py::pythrmap; -#endif - PyObject *py::module_obj = NULL; PyObject *py::module_dict = NULL; diff --git a/externals/grill/py/source/main.h b/externals/grill/py/source/main.h index 9178a4d7..3aed0f10 100644 --- a/externals/grill/py/source/main.h +++ b/externals/grill/py/source/main.h @@ -2,7 +2,7 @@ py/pyext - python script object for PD and MaxMSP -Copyright (c)2002-2004 Thomas Grill (xovo@gmx.net) +Copyright (c)2002-2004 Thomas Grill (gr@grrrr.org) For information on usage and redistribution, and for a DISCLAIMER OF ALL WARRANTIES, see the file, "license.txt," in this distribution. @@ -19,7 +19,6 @@ WARRANTIES, see the file, "license.txt," in this distribution. #else #include #endif -#include #if FLEXT_OS == FLEXT_LINUX || FLEXT_OS == FLEXT_IRIX #include @@ -29,7 +28,7 @@ WARRANTIES, see the file, "license.txt," in this distribution. #error You need at least flext version 0.4.6 #endif -#define PY__VERSION "0.1.4" +#define PY__VERSION "0.2.0pre" #define PYEXT_MODULE "pyext" // name for module @@ -50,10 +49,6 @@ WARRANTIES, see the file, "license.txt," in this distribution. #include "main.h" -#ifdef FLEXT_THREADS -typedef std::map PyThrMap; -#endif - class py: public flext_base { @@ -134,9 +129,6 @@ protected: public: #ifdef FLEXT_THREADS - static PyInterpreterState *pystate; - static PyThreadState *pythrmain; - static PyThrMap pythrmap; ThrMutex mutex; inline V Lock() { mutex.Unlock(); } inline V Unlock() { mutex.Unlock(); } @@ -161,14 +153,13 @@ protected: #ifdef FLEXT_THREADS -// if thread is not found in the thread map, the state of the system thread is used -// we have yet to see if this has bad side-effects +PyThreadState *FindThreadState(); +void FreeThreadState(); #define PY_LOCK \ { \ PyEval_AcquireLock(); \ - PyThrMap::iterator it = pythrmap.find(GetThreadId()); \ - PyThreadState *__st = it != pythrmap.end()?it->second:pythrmain; \ + PyThreadState *__st = FindThreadState(); \ PyThreadState *__oldst = PyThreadState_Swap(__st); #define PY_UNLOCK \ diff --git a/externals/grill/py/source/modmeth.cpp b/externals/grill/py/source/modmeth.cpp index ac95e01e..5a3efd10 100644 --- a/externals/grill/py/source/modmeth.cpp +++ b/externals/grill/py/source/modmeth.cpp @@ -2,7 +2,7 @@ py/pyext - python external object for PD and Max/MSP -Copyright (c) 2002-2004 Thomas Grill (xovo@gmx.net) +Copyright (c) 2002-2004 Thomas Grill (gr@grrrr.org) For information on usage and redistribution, and for a DISCLAIMER OF ALL WARRANTIES, see the file, "license.txt," in this distribution. diff --git a/externals/grill/py/source/py.cpp b/externals/grill/py/source/py.cpp index 6883bfad..021e710f 100644 --- a/externals/grill/py/source/py.cpp +++ b/externals/grill/py/source/py.cpp @@ -2,7 +2,7 @@ py/pyext - python script object for PD and Max/MSP -Copyright (c)2002-2004 Thomas Grill (xovo@gmx.net) +Copyright (c)2002-2004 Thomas Grill (gr@grrrr.org) For information on usage and redistribution, and for a DISCLAIMER OF ALL WARRANTIES, see the file, "license.txt," in this distribution. diff --git a/externals/grill/py/source/pyargs.cpp b/externals/grill/py/source/pyargs.cpp index 64bcf275..3c2d3c35 100644 --- a/externals/grill/py/source/pyargs.cpp +++ b/externals/grill/py/source/pyargs.cpp @@ -2,7 +2,7 @@ py/pyext - python external object for PD and MaxMSP -Copyright (c)2002-2004 Thomas Grill (xovo@gmx.net) +Copyright (c)2002-2004 Thomas Grill (gr@grrrr.org) For information on usage and redistribution, and for a DISCLAIMER OF ALL WARRANTIES, see the file, "license.txt," in this distribution. diff --git a/externals/grill/py/source/pyext.cpp b/externals/grill/py/source/pyext.cpp index 27ad942b..9e1ff30b 100644 --- a/externals/grill/py/source/pyext.cpp +++ b/externals/grill/py/source/pyext.cpp @@ -2,7 +2,7 @@ py/pyext - python script object for PD and Max/MSP -Copyright (c)2002-2004 Thomas Grill (xovo@gmx.net) +Copyright (c)2002-2004 Thomas Grill (gr@grrrr.org) For information on usage and redistribution, and for a DISCLAIMER OF ALL WARRANTIES, see the file, "license.txt," in this distribution. @@ -256,10 +256,14 @@ pyext::~pyext() PY_LOCK ClearBinding(); - Unregister("_pyext"); - UnimportModule(); + + if(pyobj) { + if(pyobj->ob_refcnt > 1) post("%s - Python object is still referenced",thisName()); + Py_DECREF(pyobj); // opposite of SetClssMeth + } - Py_XDECREF(pyobj); // opposite of SetClssMeth + Unregister("_pyext"); + UnimportModule(); PY_UNLOCK } @@ -495,13 +499,9 @@ V pyext::work_wrapper(V *data) // get the global lock PyEval_AcquireLock(); // create a thread state object for this thread - PyThreadState *newthr = PyThreadState_New(pystate); + PyThreadState *newthr = FindThreadState(); // free the lock PyEval_ReleaseLock(); - // ----------------------------- - - // store new thread state - pythrmap[GetThreadId()] = newthr; #endif { // call worker @@ -511,18 +511,13 @@ V pyext::work_wrapper(V *data) } #ifdef FLEXT_THREADS - // delete mapped thread state - pythrmap.erase(GetThreadId()); - // --- delete Python thread --- // grab the lock PyEval_AcquireLock(); // swap my thread state out of the interpreter PyThreadState_Swap(NULL); - // clear out any cruft from thread state object - PyThreadState_Clear(newthr); - // delete my thread state object - PyThreadState_Delete(newthr); + // delete mapped thread state + FreeThreadState(); // release the lock PyEval_ReleaseLock(); // ----------------------------- diff --git a/externals/grill/py/source/pyext.h b/externals/grill/py/source/pyext.h index 4b3e1b17..9fd4c157 100644 --- a/externals/grill/py/source/pyext.h +++ b/externals/grill/py/source/pyext.h @@ -2,7 +2,7 @@ py/pyext - python external object for PD and MaxMSP -Copyright (c)2002-2004 Thomas Grill (xovo@gmx.net) +Copyright (c)2002-2004 Thomas Grill (gr@grrrr.org) For information on usage and redistribution, and for a DISCLAIMER OF ALL WARRANTIES, see the file, "license.txt," in this distribution. diff --git a/externals/grill/py/source/register.cpp b/externals/grill/py/source/register.cpp index 16e1df65..c0ca4675 100644 --- a/externals/grill/py/source/register.cpp +++ b/externals/grill/py/source/register.cpp @@ -2,7 +2,7 @@ py/pyext - python external object for PD and MaxMSP -Copyright (c) 2002-2003 Thomas Grill (xovo@gmx.net) +Copyright (c) 2002-2004 Thomas Grill (gr@grrrr.org) For information on usage and redistribution, and for a DISCLAIMER OF ALL WARRANTIES, see the file, "license.txt," in this distribution. -- cgit v1.2.1