From 7ca685c4bc0b2881555f317db6408ae488fe05aa Mon Sep 17 00:00:00 2001
From: Thomas Grill <xovo@users.sourceforge.net>
Date: Wed, 10 Nov 2004 03:31:34 +0000
Subject: support for Python threads, at last

svn path=/trunk/; revision=2251
---
 externals/grill/py/readme.txt          | 12 +++++----
 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 +-
 11 files changed, 70 insertions(+), 50 deletions(-)

(limited to 'externals/grill/py')

diff --git a/externals/grill/py/readme.txt b/externals/grill/py/readme.txt
index e4916be4..637cd176 100644
--- a/externals/grill/py/readme.txt
+++ b/externals/grill/py/readme.txt
@@ -1,6 +1,6 @@
 py/pyext - python script objects for PD (and MaxMSP... once, under MacOSX and Windows)
 
-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.  
 
@@ -45,8 +45,8 @@ You can send messages to named objects or receive (with pyext) with Python metho
 
 Known bugs:
 - The TCL/TK help patch is not usable under OSX.
-- With the standard PD distribution, threaded py scripts will cause "Stack overflows" under some circumstances
-	(the devel_0_37 cvs branch of PD contains the relevant fixes to avoid that)
+- With standard PD 0.37, threaded py scripts will cause "Stack overflows" under some circumstances
+  -> use PD 0.38 or the devel_0_37 cvs branch instead
 
 ----------------------------------------------------------------------------
 
@@ -61,7 +61,7 @@ PD @ Windows:
 -------------
 o Borland C++ 5.5 (free): edit "config-pd-bcc.txt" & run "build-pd-bcc.bat" 
 
-o Microsoft Visual C++ 6: usr "py.dsp" or edit "config-pd-msvc.txt" & run "build-pd-msvc.bat" 
+o Microsoft Visual C++ 6/7: edit "config-pd-msvc.txt" & run "build-pd-msvc.bat"
 
 
 PD @ linux:
@@ -82,6 +82,9 @@ o GCC: edit "config-pd-darwin.txt" & run "sh build-pd-darwin.sh"
 
 Version history:
 
+0.1.5:
+- ADD: handling of Python threads
+
 0.1.4:
 - ADD: better (and independent) handling of inlet and outlet count (as class variables or dynamically initialized in __init__)
 - FIX: many memory leaks associated to ***GetItem stuff (big thanks to sven!)
@@ -158,5 +161,4 @@ tests:
 
 bugs:
 - named (keyword) arguments are not supported
-- currently no support for Python threads
 
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.  
 
-- 
cgit v1.2.1