File: pytgutils.h

package info (click to toggle)
pytango 9.2.0-2
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 22,296 kB
  • ctags: 4,072
  • sloc: python: 19,368; cpp: 14,365; sh: 258; sql: 245; makefile: 243
file content (71 lines) | stat: -rw-r--r-- 2,015 bytes parent folder | download | duplicates (4)
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
/******************************************************************************
  This file is part of PyTango (http://pytango.rtfd.io)

  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.
******************************************************************************/

#pragma once

#include <boost/python.hpp>
#include <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
}