File: pytgutils.h

package info (click to toggle)
pytango 10.0.2-3
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 10,216 kB
  • sloc: python: 28,206; cpp: 16,380; sql: 255; sh: 82; makefile: 43
file content (70 lines) | stat: -rw-r--r-- 1,685 bytes parent folder | download | duplicates (3)
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
/*
 * SPDX-FileCopyrightText: All Contributors to the PyTango project
 *
 * SPDX-License-Identifier: LGPL-3.0-or-later
 */

#pragma once

#include <boost/python.hpp>
#include <tango/tango.h>

#include "defs.h"
#include "pyutils.h"
#include "from_py.h"
#include "to_py.h"
#include "tgutils.h"

/// Get the python Global Interpret Lock
class AutoPythonGIL
{
    PyGILState_STATE m_gstate;

    /**
     * Check python. Before acquiring python GIL check if python as not been
     * shutdown. If this is the case then the best we can do here is throw an
     * exception to try to prevent the PyTango from calling python code
     **/
    inline void check_python()
    {
        if(!Py_IsInitialized())
        {
            Tango::Except::throw_exception("AutoPythonGIL_PythonShutdown",
                                           "Trying to execute python code when python interpreter as shutdown.",
                                           "AutoPythonGIL::check_python");
        }
    }

  public:
    inline AutoPythonGIL(bool safe = true)
    {
        if(safe)
        {
            check_python();
        }
        m_gstate = PyGILState_Ensure();
    }

    inline ~AutoPythonGIL()
    {
        PyGILState_Release(m_gstate);
    }
};

/**
 * Translate a double into a timeval structure
 *
 * @param[out] tv timeval structure to be filled with the time
 * @param[in] t a double representing the time
 */
inline void double2timeval(struct timeval &tv, double t)
{
    double sec = floor(t);
#ifdef WIN32
    tv.tv_usec = (long) ((t - sec) * 1.0E6);
    tv.tv_sec = (long) (sec);
#else
    tv.tv_usec = (time_t) ((t - sec) * 1.0E6);
    tv.tv_sec = (suseconds_t) (sec);
#endif
}