File: auto_monitor.cpp

package info (click to toggle)
pytango 10.1.4-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 8,304 kB
  • sloc: python: 27,795; cpp: 16,150; sql: 252; sh: 152; makefile: 43
file content (144 lines) | stat: -rw-r--r-- 3,750 bytes parent folder | download
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
/*
 * SPDX-FileCopyrightText: All Contributors to the PyTango project
 *
 * SPDX-License-Identifier: LGPL-3.0-or-later
 */

#include "common_header.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(Tango::DeviceClass *klass_arg) :
        mon(),
        dev(),
        klass() {
        klass = klass_arg;
    }

    void acquire() {
        if(mon != nullptr) {
            return;
        }
        if(dev != nullptr) {
            py::gil_scoped_release no_gil;
            mon = new Tango::AutoTangoMonitor(dev);
        } else if(klass != nullptr) {
            py::gil_scoped_release no_gil;
            mon = new Tango::AutoTangoMonitor(klass);
        }
    }

    void release() {
        if(mon != nullptr) {
            delete mon;
            mon = nullptr;
        }
    }

    ~AutoTangoMonitor() {
        release();
    }
};

class AutoTangoAllowThreads {
  public:
    AutoTangoAllowThreads(Tango::DeviceImpl *dev) {
        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 = nullptr;
        }
        release();
    }

    void acquire() {
        if(mon == nullptr) {
            return;
        }

        py::gil_scoped_release no_gil;
        for(int i = 0; i < count; ++i) {
            mon->get_monitor();
        }
    }

  protected:
    void release() {
        if(mon == nullptr) {
            return;
        }

        int cur_thread = omni_thread::self()->id();
        int mon_thread = mon->get_locking_thread_id();

        // do something only if the monitor was taken by the current thread
        if(mon_thread == cur_thread) {
            do {
                mon->rel_monitor();
                mon_thread = mon->get_locking_thread_id();
                count++;
            } while(mon_thread == cur_thread);
        } else {
            mon = nullptr;
        }
    }

  private:
    Tango::TangoMonitor *mon{nullptr};
    int count{0};
    omni_thread::ensure_self auto_self;
};

} // namespace PyTango

void export_auto_tango_monitor(py::module &m) {
    py::class_<PyTango::AutoTangoMonitor>(m,
                                          "AutoTangoMonitor",
                                          R"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
)doc")
        .def(py::init<Tango::DeviceImpl *>())
        .def(py::init<Tango::DeviceClass *>())
        .def("_acquire", &PyTango::AutoTangoMonitor::acquire)
        .def("_release", &PyTango::AutoTangoMonitor::release);

    py::class_<PyTango::AutoTangoAllowThreads>(m,
                                               "AutoTangoAllowThreads",
                                               R"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
)doc")
        .def(py::init<Tango::DeviceImpl *>(), py::arg("device"))
        .def("_acquire", &PyTango::AutoTangoAllowThreads::acquire);
}