commit-gnuradio
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[Commit-gnuradio] r3881 - in gnuradio/branches/developers/eb/binstats: g


From: eb
Subject: [Commit-gnuradio] r3881 - in gnuradio/branches/developers/eb/binstats: gnuradio-core/src/lib/general gnuradio-core/src/python/gnuradio/gr gr-error-correcting-codes/src/lib
Date: Fri, 27 Oct 2006 16:23:31 -0600 (MDT)

Author: eb
Date: 2006-10-27 16:23:31 -0600 (Fri, 27 Oct 2006)
New Revision: 3881

Modified:
   
gnuradio/branches/developers/eb/binstats/gnuradio-core/src/lib/general/gr_bin_statistics_f.cc
   
gnuradio/branches/developers/eb/binstats/gnuradio-core/src/lib/general/gr_feval.cc
   
gnuradio/branches/developers/eb/binstats/gnuradio-core/src/lib/general/gr_feval.h
   
gnuradio/branches/developers/eb/binstats/gnuradio-core/src/lib/general/gr_feval.i
   
gnuradio/branches/developers/eb/binstats/gnuradio-core/src/python/gnuradio/gr/qa_bin_statistics.py
   
gnuradio/branches/developers/eb/binstats/gr-error-correcting-codes/src/lib/ecc_syms_to_metrics.cc
Log:
Reworked gr_feval* so that it now properly reacquires the Python
Global Interpreter Lock when calling from C++ into Python.  This
required an API change on gr_feval*, and a bit of SWIG magic.

You still override "eval" to define your behavior, but when you want
to call "eval", you must now use the "calleval" method.  This change
was required in order to make room for the shim that manages the
Python GIL.



Modified: 
gnuradio/branches/developers/eb/binstats/gnuradio-core/src/lib/general/gr_bin_statistics_f.cc
===================================================================
--- 
gnuradio/branches/developers/eb/binstats/gnuradio-core/src/lib/general/gr_bin_statistics_f.cc
       2006-10-27 22:00:50 UTC (rev 3880)
+++ 
gnuradio/branches/developers/eb/binstats/gnuradio-core/src/lib/general/gr_bin_statistics_f.cc
       2006-10-27 22:23:31 UTC (rev 3881)
@@ -74,7 +74,7 @@
 {
   d_state = ST_TUNE_DELAY;
   d_delay = d_tune_delay;
-  d_center_freq = d_tune->eval(0);
+  d_center_freq = d_tune->calleval(0);
 }
 
 void

Modified: 
gnuradio/branches/developers/eb/binstats/gnuradio-core/src/lib/general/gr_feval.cc
===================================================================
--- 
gnuradio/branches/developers/eb/binstats/gnuradio-core/src/lib/general/gr_feval.cc
  2006-10-27 22:00:50 UTC (rev 3880)
+++ 
gnuradio/branches/developers/eb/binstats/gnuradio-core/src/lib/general/gr_feval.cc
  2006-10-27 22:23:31 UTC (rev 3881)
@@ -34,6 +34,14 @@
   return 0;
 }
 
+double
+gr_feval_dd::calleval(double x)
+{
+  return eval(x);
+}
+
+// ----------------------------------------------------------------
+
 gr_feval_cc::~gr_feval_cc(){}
 
 gr_complex
@@ -42,6 +50,14 @@
   return 0;
 }
 
+gr_complex
+gr_feval_cc::calleval(gr_complex x)
+{
+  return eval(x);
+}
+
+// ----------------------------------------------------------------
+
 gr_feval_ll::~gr_feval_ll(){}
 
 long
@@ -50,6 +66,14 @@
   return 0;
 }
 
+long
+gr_feval_ll::calleval(long x)
+{
+  return eval(x);
+}
+
+// ----------------------------------------------------------------
+
 gr_feval::~gr_feval(){}
 
 void
@@ -58,29 +82,35 @@
   // nop
 }
 
+void
+gr_feval::calleval(void)
+{
+  eval();
+}
+
 /*
  * Trivial examples showing C++ (transparently) calling Python
  */
 double
 gr_feval_dd_example(gr_feval_dd *f, double x)
 {
-  return f->eval(x);
+  return f->calleval(x);
 }
 
 gr_complex
 gr_feval_cc_example(gr_feval_cc *f, gr_complex x)
 {
-  return f->eval(x);
+  return f->calleval(x);
 }
 
 long
 gr_feval_ll_example(gr_feval_ll *f, long x)
 {
-  return f->eval(x);
+  return f->calleval(x);
 }
 
 void
 gr_feval_example(gr_feval *f)
 {
-  f->eval();
+  f->calleval();
 }

Modified: 
gnuradio/branches/developers/eb/binstats/gnuradio-core/src/lib/general/gr_feval.h
===================================================================
--- 
gnuradio/branches/developers/eb/binstats/gnuradio-core/src/lib/general/gr_feval.h
   2006-10-27 22:00:50 UTC (rev 3880)
+++ 
gnuradio/branches/developers/eb/binstats/gnuradio-core/src/lib/general/gr_feval.h
   2006-10-27 22:23:31 UTC (rev 3881)
@@ -31,17 +31,24 @@
  * and is callable from both places.  It uses SWIG's
  * "director" feature to implement the magic.
  * It's slow. Don't use it in a performance critical path.
+ *
+ * Override eval to define the behavior.
+ * Use calleval to invoke eval (this kludge is required to allow a
+ * python specific "shim" to be inserted.
  */
 class gr_feval_dd
 {
+protected:
+  /*!
+   * \brief override this to define the function
+   */
+  virtual double eval(double x);
+
 public:
   gr_feval_dd() {}
   virtual ~gr_feval_dd();
 
-  /*!
-   * \brief override this to define the function
-   */
-  virtual double eval(double x);
+  virtual double calleval(double x);   // invoke "eval"
 };
 
 /*!
@@ -51,17 +58,24 @@
  * and is callable from both places.  It uses SWIG's
  * "director" feature to implement the magic.
  * It's slow. Don't use it in a performance critical path.
+ *
+ * Override eval to define the behavior.
+ * Use calleval to invoke eval (this kludge is required to allow a
+ * python specific "shim" to be inserted.
  */
 class gr_feval_cc
 {
+protected:
+  /*!
+   * \brief override this to define the function
+   */
+  virtual gr_complex eval(gr_complex x);
+  
 public:
   gr_feval_cc() {}
   virtual ~gr_feval_cc();
 
-  /*!
-   * \brief override this to define the function
-   */
-  virtual gr_complex eval(gr_complex x);
+  virtual gr_complex calleval(gr_complex x);   // invoke "eval"
 };
 
 /*!
@@ -71,17 +85,24 @@
  * and is callable from both places.  It uses SWIG's
  * "director" feature to implement the magic.
  * It's slow. Don't use it in a performance critical path.
+ *
+ * Override eval to define the behavior.
+ * Use calleval to invoke eval (this kludge is required to allow a
+ * python specific "shim" to be inserted.
  */
 class gr_feval_ll
 {
+protected:
+  /*!
+   * \brief override this to define the function
+   */
+  virtual long eval(long x);
+
 public:
   gr_feval_ll() {}
   virtual ~gr_feval_ll();
 
-  /*!
-   * \brief override this to define the function
-   */
-  virtual long eval(long x);
+  virtual long calleval(long x);       // invoke "eval"
 };
 
 /*!
@@ -91,17 +112,24 @@
  * and is callable from both places.  It uses SWIG's
  * "director" feature to implement the magic.
  * It's slow. Don't use it in a performance critical path.
+ *
+ * Override eval to define the behavior.
+ * Use calleval to invoke eval (this kludge is required to allow a
+ * python specific "shim" to be inserted.
  */
 class gr_feval
 {
+protected:
+  /*!
+   * \brief override this to define the function
+   */
+  virtual void eval();
+
 public:
   gr_feval() {}
   virtual ~gr_feval();
 
-  /*!
-   * \brief override this to define the function
-   */
-  virtual void eval();
+  virtual void calleval();     // invoke "eval"
 };
 
 /*!

Modified: 
gnuradio/branches/developers/eb/binstats/gnuradio-core/src/lib/general/gr_feval.i
===================================================================
--- 
gnuradio/branches/developers/eb/binstats/gnuradio-core/src/lib/general/gr_feval.i
   2006-10-27 22:00:50 UTC (rev 3880)
+++ 
gnuradio/branches/developers/eb/binstats/gnuradio-core/src/lib/general/gr_feval.i
   2006-10-27 22:23:31 UTC (rev 3881)
@@ -20,54 +20,166 @@
  * Boston, MA 02110-1301, USA.
  */
 
+
+/*
+ * N.B., this is a _very_ non-standard SWIG .i file
+ *
+ * It contains a bunch of magic that is required to ensure that when
+ * these classes are used as base classes for python code,
+ * everything works when calling back from C++ into Python.
+ *
+ * The gist of the problem is that our C++ code is usually not holding
+ * the Python Global Interpreter Lock (GIL).  Thus if we invoke a
+ * "director" method from C++, we'll end up in Python not holding the
+ * GIL.  Disaster (SIGSEGV) will result.  To avoid this we insert a
+ * "shim" that grabs and releases the GIL.
+ *
+ * If you don't understand SWIG "directors" or the Python GIL,
+ * don't bother trying to understand what's going on in here.
+ *
+ * [We could eliminate a bunch of this hair by requiring SWIG 1.3.29
+ * or later and some additional magic declarations, but many systems
+ * aren't shipping that version yet.  Thus we kludge...]
+ */
+
+
 // Enable SWIG directors for these classes
-%feature("director") gr_feval_dd;
-%feature("director") gr_feval_cc;
-%feature("director") gr_feval_ll;
-%feature("director") gr_feval;
+%feature("director") gr_py_feval_dd;
+%feature("director") gr_py_feval_cc;
+%feature("director") gr_py_feval_ll;
+%feature("director") gr_py_feval;
 
+%feature("nodirector") gr_py_feval_dd::calleval;
+%feature("nodirector") gr_py_feval_cc::calleval;
+%feature("nodirector") gr_py_feval_ll::calleval;
+%feature("nodirector") gr_py_feval::calleval;
 
-%rename(feval_dd) gr_feval_dd;
+
+%rename(feval_dd) gr_py_feval_dd;
+%rename(feval_cc) gr_py_feval_cc;
+%rename(feval_ll) gr_py_feval_ll;
+%rename(feval)    gr_py_feval;
+
+%{
+
+// class that ensures we acquire and release the Python GIL
+
+class ensure_py_gil_state {
+  PyGILState_STATE     d_gstate;
+public:
+  ensure_py_gil_state()  { d_gstate = PyGILState_Ensure(); }
+  ~ensure_py_gil_state() { PyGILState_Release(d_gstate); }
+};
+
+%}
+
+/*
+ * These are the real C++ base classes, however we don't want these exposed.
+ */
+// %ignore gr_feval_dd;
 class gr_feval_dd
 {
+protected:
+  virtual double eval(double x);
+
 public:
   gr_feval_dd() {}
   virtual ~gr_feval_dd();
 
-  virtual double eval(double x);
+  virtual double calleval(double x);
 };
 
-%rename(feval_cc) gr_feval_cc;
+// %ignore gr_feval_cc;
 class gr_feval_cc
 {
+protected:
+  virtual gr_complex eval(gr_complex x);
+
 public:
   gr_feval_cc() {}
   virtual ~gr_feval_cc();
 
-  virtual gr_complex eval(gr_complex x);
+  virtual gr_complex calleval(gr_complex x);
 };
 
-%rename(feval_ll) gr_feval_ll;
+// %ignore gr_feval_ll;
 class gr_feval_ll
 {
+protected:
+  virtual long eval(long x);
+  
 public:
   gr_feval_ll() {}
   virtual ~gr_feval_ll();
 
-  virtual long eval(long x);
+  virtual long calleval(long x);
 };
 
-%rename(feval) gr_feval;
+// %ignore gr_feval;
 class gr_feval
 {
+protected:
+  virtual void eval();
+  
 public:
   gr_feval() {}
   virtual ~gr_feval();
 
-  virtual void eval();
+  virtual void calleval();
 };
 
+/*
+ * These are the ones to derive from in Python.  They have the magic shim
+ * that ensures that we're holding the Python GIL when we enter Python land...
+ * [ You really don't have to understand this.  Trust me, it works ;) ]
+ */
 
+%inline %{
+
+class gr_py_feval_dd : public gr_feval_dd
+{
+ public:
+  double calleval(double x)
+  {
+    ensure_py_gil_state _lock;
+    return eval(x);
+  }
+};
+
+class gr_py_feval_cc : public gr_feval_cc
+{
+ public:
+  gr_complex calleval(gr_complex x)
+  {
+    ensure_py_gil_state _lock;
+    return eval(x);
+  }
+};
+
+class gr_py_feval_ll : public gr_feval_ll
+{
+ public:
+  long calleval(long x)
+  {
+    ensure_py_gil_state _lock;
+    return eval(x);
+  }
+};
+
+class gr_py_feval : public gr_feval
+{
+ public:
+  void calleval()
+  {
+    ensure_py_gil_state _lock;
+    eval();
+  }
+};
+
+%}
+
+
+
 // examples / test cases
 
 %rename(feval_dd_example) gr_feval_dd_example;

Modified: 
gnuradio/branches/developers/eb/binstats/gnuradio-core/src/python/gnuradio/gr/qa_bin_statistics.py
===================================================================
--- 
gnuradio/branches/developers/eb/binstats/gnuradio-core/src/python/gnuradio/gr/qa_bin_statistics.py
  2006-10-27 22:00:50 UTC (rev 3880)
+++ 
gnuradio/branches/developers/eb/binstats/gnuradio-core/src/python/gnuradio/gr/qa_bin_statistics.py
  2006-10-27 22:23:31 UTC (rev 3881)
@@ -57,8 +57,8 @@
         vlen = 4
         tune = counter(1)
         #tune = gr.feval_dd()
-        tune_delay = 1
-        dwell_delay = 2
+        tune_delay = 0
+        dwell_delay = 1
         msgq = gr.msg_queue()
 
         src_data =         ( 1,  2,  3,  4,

Modified: 
gnuradio/branches/developers/eb/binstats/gr-error-correcting-codes/src/lib/ecc_syms_to_metrics.cc
===================================================================
--- 
gnuradio/branches/developers/eb/binstats/gr-error-correcting-codes/src/lib/ecc_syms_to_metrics.cc
   2006-10-27 22:00:50 UTC (rev 3880)
+++ 
gnuradio/branches/developers/eb/binstats/gr-error-correcting-codes/src/lib/ecc_syms_to_metrics.cc
   2006-10-27 22:23:31 UTC (rev 3881)
@@ -55,12 +55,12 @@
 
 static double pdf_fcn_0 (double x)
 {
-  return (l_pdf_fcn_0_bit->eval (x));
+  return (l_pdf_fcn_0_bit->calleval (x));
 }
 
 static double pdf_fcn_1 (double x)
 {
-  return (l_pdf_fcn_1_bit->eval (x));
+  return (l_pdf_fcn_1_bit->calleval (x));
 }
 
 ecc_syms_to_metrics::ecc_syms_to_metrics





reply via email to

[Prev in Thread] Current Thread [Next in Thread]