aboutsummaryrefslogtreecommitdiff
path: root/externals/grill/py/source
diff options
context:
space:
mode:
authorThomas Grill <xovo@users.sourceforge.net>2004-11-10 03:31:34 +0000
committerThomas Grill <xovo@users.sourceforge.net>2004-11-10 03:31:34 +0000
commit7ca685c4bc0b2881555f317db6408ae488fe05aa (patch)
treed8b1c4cf5368e40cad34da16c2eef979112994d5 /externals/grill/py/source
parent5901f4586235caba9fd264ff1e911708efe6cb17 (diff)
support for Python threads, at last
svn path=/trunk/; revision=2251
Diffstat (limited to 'externals/grill/py/source')
-rw-r--r--externals/grill/py/source/bound.cpp2
-rw-r--r--externals/grill/py/source/clmeth.cpp2
-rw-r--r--externals/grill/py/source/main.cpp48
-rw-r--r--externals/grill/py/source/main.h19
-rw-r--r--externals/grill/py/source/modmeth.cpp2
-rw-r--r--externals/grill/py/source/py.cpp2
-rw-r--r--externals/grill/py/source/pyargs.cpp2
-rw-r--r--externals/grill/py/source/pyext.cpp27
-rw-r--r--externals/grill/py/source/pyext.h2
-rw-r--r--externals/grill/py/source/register.cpp2
10 files changed, 63 insertions, 45 deletions
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 <map>
static PyMethodDef StdOut_Methods[] =
{
@@ -17,6 +17,44 @@ static PyMethodDef StdOut_Methods[] =
{ NULL, NULL, }
};
+
+#ifdef FLEXT_THREADS
+typedef std::map<flext::thrid_t,PyThreadState *> 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 <Python.h>
#endif
-#include <map>
#if FLEXT_OS == FLEXT_LINUX || FLEXT_OS == FLEXT_IRIX
#include <unistd.h>
@@ -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<flext::thrid_t,PyThreadState *> 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.