1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158
|
/*
* SPDX-FileCopyrightText: All Contributors to the PyTango project
*
* SPDX-License-Identifier: LGPL-3.0-or-later
*/
#include "precompiled_header.hpp"
#include "defs.h"
#include "pytgutils.h"
#include "server/device_class.h"
namespace PyTango
{
class AutoTangoMonitor
{
Tango::AutoTangoMonitor *mon;
Tango::DeviceImpl *dev;
Tango::DeviceClass *klass;
public:
AutoTangoMonitor(Tango::DeviceImpl *dev_arg) :
mon(),
dev(),
klass()
{
dev = dev_arg;
}
AutoTangoMonitor(CppDeviceClass *klass_arg) :
mon(),
dev(),
klass()
{
klass = klass_arg;
}
void acquire()
{
if(mon != NULL)
{
return;
}
if(dev != NULL)
{
AutoPythonAllowThreads no_gil;
mon = new Tango::AutoTangoMonitor(dev);
}
else if(klass != NULL)
{
AutoPythonAllowThreads no_gil;
mon = new Tango::AutoTangoMonitor(klass);
}
}
void release()
{
if(mon != NULL)
{
delete mon;
mon = NULL;
}
}
~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{nullptr};
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<CppDeviceClass *>())
.def("_acquire", &PyTango::AutoTangoMonitor::acquire)
.def("_release", &PyTango::AutoTangoMonitor::release);
bopy::class_<PyTango::AutoTangoAllowThreads, boost::noncopyable>("AutoTangoAllowThreads",
bopy::init<Tango::DeviceImpl *>())
.def("_acquire", &PyTango::AutoTangoAllowThreads::acquire);
;
}
|