[pytango] 12/37: Expose AutoTangoMonitor and AutoTangoAllowThreads

Sandor Bodo-Merle sbodomerle-guest at moszumanska.debian.org
Thu Sep 28 19:16:55 UTC 2017


This is an automated email from the git hooks/post-receive script.

sbodomerle-guest pushed a commit to annotated tag v9.2.0b
in repository pytango.

commit 7ef97daf2bf6b131c1ab1a07f545898b4af7f0f0
Author: Jose Tiago Coutinho Macara <tiago.coutinho at esrf.fr>
Date:   Thu Mar 3 17:34:56 2016 +0100

    Expose AutoTangoMonitor and AutoTangoAllowThreads
---
 Makefile                              |   3 +-
 src/boost/cpp/pytango.cpp             |   2 +
 src/boost/cpp/server/auto_monitor.cpp | 121 ++++++++++++++++++++++++++++++++++
 src/boost/python/__init__.py          |   5 +-
 src/boost/python/auto_monitor.py      |  72 ++++++++++++++++++++
 src/boost/python/pytango_init.py      |   2 +
 6 files changed, 202 insertions(+), 3 deletions(-)

diff --git a/Makefile b/Makefile
index 9d6e276..7c00938 100644
--- a/Makefile
+++ b/Makefile
@@ -203,7 +203,8 @@ $(OBJS_DIR)/multi_class_attribute.o \
 $(OBJS_DIR)/subdev.o \
 $(OBJS_DIR)/tango_util.o \
 $(OBJS_DIR)/user_default_attr_prop.o \
-$(OBJS_DIR)/wattribute.o
+$(OBJS_DIR)/wattribute.o \
+$(OBJS_DIR)/auto_monitor.o
 
 INC := callback.h \
 defs.h \
diff --git a/src/boost/cpp/pytango.cpp b/src/boost/cpp/pytango.cpp
index 967b0cf..3963f02 100644
--- a/src/boost/cpp/pytango.cpp
+++ b/src/boost/cpp/pytango.cpp
@@ -52,6 +52,7 @@ void export_device_class();
 void export_device_impl();
 void export_group();
 void export_log4tango();
+void export_auto_tango_monitor();
 
 #ifdef DISABLE_PYTANGO_NUMPY
 void init_numpy(void) {}
@@ -111,4 +112,5 @@ BOOST_PYTHON_MODULE(_PyTango)
     export_dserver();
     export_group();
     export_log4tango();
+    export_auto_tango_monitor();
 }
diff --git a/src/boost/cpp/server/auto_monitor.cpp b/src/boost/cpp/server/auto_monitor.cpp
new file mode 100644
index 0000000..e1af8e3
--- /dev/null
+++ b/src/boost/cpp/server/auto_monitor.cpp
@@ -0,0 +1,121 @@
+/******************************************************************************
+  This file is part of PyTango (http://www.tinyurl.com/PyTango)
+
+  Copyright 2006-2012 CELLS / ALBA Synchrotron, Bellaterra, Spain
+  Copyright 2013-2014 European Synchrotron Radiation Facility, Grenoble, France
+
+  Distributed under the terms of the GNU Lesser General Public License,
+  either version 3 of the License, or (at your option) any later version.
+  See LICENSE.txt for more info.
+******************************************************************************/
+
+#include "precompiled_header.hpp"
+#include "defs.h"
+#include "pytgutils.h"
+
+namespace PyTango
+{
+
+class AutoTangoMonitor
+{
+  Tango::AutoTangoMonitor           *mon;  
+
+public:
+  AutoTangoMonitor(Tango::DeviceImpl *dev)
+  { mon = new Tango::AutoTangoMonitor(dev); }
+
+  AutoTangoMonitor(Tango::DeviceClass *klass)
+  { mon = new Tango::AutoTangoMonitor(klass); }
+
+  void release()
+  {
+    delete mon;
+  }
+
+  ~AutoTangoMonitor() { release(); }
+
+};
+
+class AutoTangoAllowThreads
+{
+public:
+  AutoTangoAllowThreads(Tango::DeviceImpl *dev): count(0)
+  {
+    Tango::Util* util = Tango::Util::instance();
+    Tango::SerialModel ser = util->get_serial_model();
+
+    switch(ser)
+    {
+      case Tango::BY_DEVICE:
+        mon = &(dev->get_dev_monitor());
+        break;
+      case Tango::BY_CLASS:
+        //mon = &(dev->device_class->ext->only_one);
+        break;
+      case Tango::BY_PROCESS:
+        //mon = &(util->ext->only_one);
+        break;
+      default:
+        mon = NULL;
+    }
+    release();
+  }
+
+  void acquire()
+  {
+    if (mon == NULL)
+      return;
+
+    AutoPythonAllowThreads no_gil;
+    for(int i=0; i < count; ++i) {
+      mon->get_monitor();
+    }
+  }
+
+protected:
+  void release()
+  {
+    if (mon == NULL)
+      return;
+
+    int cur_thread = omni_thread::self()->id();
+    int mon_thread = mon->get_locking_thread_id();
+    int lock_count = mon->get_locking_ctr();
+
+    // do something only if the monitor was taken by the current thread
+    if ((mon_thread == cur_thread) && lock_count) {
+      while (lock_count > 0) {
+	mon->rel_monitor();
+	lock_count = mon->get_locking_ctr();
+	count++;
+      }
+    }
+    else {
+      mon = NULL;
+    }
+  }
+
+private:
+  Tango::TangoMonitor           *mon;
+  int                          count;
+  omni_thread::ensure_self auto_self;
+};
+
+} // namespace PyTango
+
+
+void export_auto_tango_monitor()
+{
+  bopy::class_<PyTango::AutoTangoMonitor, boost::noncopyable>(
+    "AutoTangoMonitor", bopy::init<Tango::DeviceImpl*>())
+    .def(bopy::init<Tango::DeviceClass*>())
+    .def("_release", &PyTango::AutoTangoMonitor::release)
+  ;
+
+  bopy::class_<PyTango::AutoTangoAllowThreads, boost::noncopyable>(
+    "AutoTangoAllowThreads", bopy::init<Tango::DeviceImpl*>())
+    
+    .def("_acquire", &PyTango::AutoTangoAllowThreads::acquire);
+  ;
+}
+
diff --git a/src/boost/python/__init__.py b/src/boost/python/__init__.py
index 66a38d9..71a0342 100644
--- a/src/boost/python/__init__.py
+++ b/src/boost/python/__init__.py
@@ -70,7 +70,7 @@ __all__ = [ 'AccessControlType', 'ApiUtil', 'ArchiveEventInfo',
 'get_cpp_classes', 'is_array_type', 'is_float_type',
 'is_int_type', 'is_numerical_type', 'is_scalar_type', 'numpy_image',
 'numpy_spectrum', 'numpy_type', 'obj_2_str', 'raise_asynch_exception',
-'seqStr_2_obj']
+'seqStr_2_obj', 'AutoTangoMonitor', 'AutoTangoAllowThreads']
 
 __docformat__ = "restructuredtext"
 
@@ -155,7 +155,8 @@ from ._PyTango import (AccessControlType, ApiUtil, ArchiveEventInfo,
     StdNamedDevFailedVector, StdStringVector, SubDevDiag, TimeVal,
     UserDefaultAttrProp, WAttribute, WRITE, WrongData, WrongNameSyntax,
     alarm_flags, asyn_req_type, cb_sub_model, constants,
-    raise_asynch_exception, Interceptors)
+    raise_asynch_exception, Interceptors, 
+    AutoTangoMonitor, AutoTangoAllowThreads)
 
 ArgType = CmdArgType
 
diff --git a/src/boost/python/auto_monitor.py b/src/boost/python/auto_monitor.py
new file mode 100644
index 0000000..e4ad3f9
--- /dev/null
+++ b/src/boost/python/auto_monitor.py
@@ -0,0 +1,72 @@
+# ------------------------------------------------------------------------------
+# This file is part of PyTango (http://www.tinyurl.com/PyTango)
+#
+# Copyright 2006-2012 CELLS / ALBA Synchrotron, Bellaterra, Spain
+# Copyright 2013-2014 European Synchrotron Radiation Facility, Grenoble, France
+#
+# Distributed under the terms of the GNU Lesser General Public License,
+# either version 3 of the License, or (at your option) any later version.
+# See LICENSE.txt for more info.
+# ------------------------------------------------------------------------------
+
+"""
+This is an internal PyTango module.
+"""
+
+__all__ = ["auto_monitor_init"]
+
+__docformat__ = "restructuredtext"
+
+import copy
+
+from .utils import document_method as __document_method
+from ._PyTango import AutoTangoMonitor, AutoTangoAllowThreads
+
+def __AutoTangoMonitor__enter__(self):
+    return self
+
+def __AutoTangoMonitor__exit__(self, *args, **kwargs):
+    self._release()
+
+
+def __init_AutoTangoMonitor():
+    AutoTangoMonitor.__enter__ = __AutoTangoMonitor__enter__
+    AutoTangoMonitor.__exit__ = __AutoTangoMonitor__exit__
+
+def __doc_AutoTangoMonitor():
+    AutoTangoMonitor.__doc__ = """\
+    
+    In a tango server, guard the tango monitor within a python context::
+
+        with AutoTangoMonitor(dev):
+            # code here is protected by the tango device monitor
+            do something
+    """
+
+def __AutoTangoAllowThreads__enter__(self):
+    return self
+
+def __AutoTangoAllowThreads__exit__(self, *args, **kwargs):
+    self._acquire()
+
+
+def __init_AutoTangoAllowThreads():
+    AutoTangoAllowThreads.__enter__ = __AutoTangoAllowThreads__enter__
+    AutoTangoAllowThreads.__exit__ = __AutoTangoAllowThreads__exit__
+
+def __doc_AutoTangoAllowThreads():
+    AutoTangoAllowThreads.__doc__ = """\
+    
+    In a tango server, free the tango monitor within a context:
+
+        with AutoTangoAllowThreads(dev):
+            # code here is not under the tango device monitor
+            do something
+    """
+
+def auto_monitor_init(doc=True):
+    __init_AutoTangoMonitor()
+    __init_AutoTangoAllowThreads()
+    if doc:
+        __doc_AutoTangoMonitor()
+        __doc_AutoTangoAllowThreads()
diff --git a/src/boost/python/pytango_init.py b/src/boost/python/pytango_init.py
index 3512914..8d4fc56 100644
--- a/src/boost/python/pytango_init.py
+++ b/src/boost/python/pytango_init.py
@@ -36,6 +36,7 @@ from .group_reply_list import group_reply_list_init
 from .pytango_pprint import pytango_pprint_init
 from .pyutil import pyutil_init
 from .time_val import time_val_init
+from .auto_monitor import auto_monitor_init
 from ._PyTango import constants
 from ._PyTango import _get_tango_lib_release
 
@@ -111,6 +112,7 @@ def init():
     pytango_pprint_init(doc=doc)
     pyutil_init(doc=doc)
     time_val_init(doc=doc)
+    auto_monitor_init(doc=doc)
 
     # must come last: depends on device_proxy.init()
     attribute_proxy_init(doc=doc)

-- 
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/debian-science/packages/pytango.git



More information about the debian-science-commits mailing list