runtime/monotonic_time.py
author Edouard Tisserant <edouard.tisserant@gmail.com>
Tue, 15 Nov 2022 20:43:39 +0100
branchwxPython4
changeset 3677 6d9040e07c32
parent 3420 da8cceaa247d
child 3750 f62625418bff
permissions -rw-r--r--
OPC-UA: only support the encryption policy selected in config.

By default open62541 client accepts all supported policies, but in makes problem
when negociating with some servers while most clients seems to only support
one policy at a time.
3420
da8cceaa247d Runtime : force using monotonic time in Timer (threading). Prevent watchdog to trigger when changing system time.
Edouard Tisserant
parents:
diff changeset
     1
# vi: set ft=python sw=4 ts=4 et:
da8cceaa247d Runtime : force using monotonic time in Timer (threading). Prevent watchdog to trigger when changing system time.
Edouard Tisserant
parents:
diff changeset
     2
da8cceaa247d Runtime : force using monotonic time in Timer (threading). Prevent watchdog to trigger when changing system time.
Edouard Tisserant
parents:
diff changeset
     3
'''monotonic time for Python 2 and 3, on Linux, FreeBSD, Mac OS X, and Windows.
da8cceaa247d Runtime : force using monotonic time in Timer (threading). Prevent watchdog to trigger when changing system time.
Edouard Tisserant
parents:
diff changeset
     4
da8cceaa247d Runtime : force using monotonic time in Timer (threading). Prevent watchdog to trigger when changing system time.
Edouard Tisserant
parents:
diff changeset
     5
Copyright 2010, 2011, 2017 Gavin Beatty <gavinbeatty@gmail.com>
da8cceaa247d Runtime : force using monotonic time in Timer (threading). Prevent watchdog to trigger when changing system time.
Edouard Tisserant
parents:
diff changeset
     6
da8cceaa247d Runtime : force using monotonic time in Timer (threading). Prevent watchdog to trigger when changing system time.
Edouard Tisserant
parents:
diff changeset
     7
Permission is hereby granted, free of charge, to any person obtaining a copy of
da8cceaa247d Runtime : force using monotonic time in Timer (threading). Prevent watchdog to trigger when changing system time.
Edouard Tisserant
parents:
diff changeset
     8
this software and associated documentation files (the "Software"), to deal in
da8cceaa247d Runtime : force using monotonic time in Timer (threading). Prevent watchdog to trigger when changing system time.
Edouard Tisserant
parents:
diff changeset
     9
the Software without restriction, including without limitation the rights to
da8cceaa247d Runtime : force using monotonic time in Timer (threading). Prevent watchdog to trigger when changing system time.
Edouard Tisserant
parents:
diff changeset
    10
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
da8cceaa247d Runtime : force using monotonic time in Timer (threading). Prevent watchdog to trigger when changing system time.
Edouard Tisserant
parents:
diff changeset
    11
of the Software, and to permit persons to whom the Software is furnished to do
da8cceaa247d Runtime : force using monotonic time in Timer (threading). Prevent watchdog to trigger when changing system time.
Edouard Tisserant
parents:
diff changeset
    12
so, subject to the following conditions:
da8cceaa247d Runtime : force using monotonic time in Timer (threading). Prevent watchdog to trigger when changing system time.
Edouard Tisserant
parents:
diff changeset
    13
da8cceaa247d Runtime : force using monotonic time in Timer (threading). Prevent watchdog to trigger when changing system time.
Edouard Tisserant
parents:
diff changeset
    14
The above copyright notice and this permission notice shall be included in all
da8cceaa247d Runtime : force using monotonic time in Timer (threading). Prevent watchdog to trigger when changing system time.
Edouard Tisserant
parents:
diff changeset
    15
copies or substantial portions of the Software.
da8cceaa247d Runtime : force using monotonic time in Timer (threading). Prevent watchdog to trigger when changing system time.
Edouard Tisserant
parents:
diff changeset
    16
da8cceaa247d Runtime : force using monotonic time in Timer (threading). Prevent watchdog to trigger when changing system time.
Edouard Tisserant
parents:
diff changeset
    17
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
da8cceaa247d Runtime : force using monotonic time in Timer (threading). Prevent watchdog to trigger when changing system time.
Edouard Tisserant
parents:
diff changeset
    18
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
da8cceaa247d Runtime : force using monotonic time in Timer (threading). Prevent watchdog to trigger when changing system time.
Edouard Tisserant
parents:
diff changeset
    19
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
da8cceaa247d Runtime : force using monotonic time in Timer (threading). Prevent watchdog to trigger when changing system time.
Edouard Tisserant
parents:
diff changeset
    20
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
da8cceaa247d Runtime : force using monotonic time in Timer (threading). Prevent watchdog to trigger when changing system time.
Edouard Tisserant
parents:
diff changeset
    21
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
da8cceaa247d Runtime : force using monotonic time in Timer (threading). Prevent watchdog to trigger when changing system time.
Edouard Tisserant
parents:
diff changeset
    22
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
da8cceaa247d Runtime : force using monotonic time in Timer (threading). Prevent watchdog to trigger when changing system time.
Edouard Tisserant
parents:
diff changeset
    23
SOFTWARE.
da8cceaa247d Runtime : force using monotonic time in Timer (threading). Prevent watchdog to trigger when changing system time.
Edouard Tisserant
parents:
diff changeset
    24
'''
da8cceaa247d Runtime : force using monotonic time in Timer (threading). Prevent watchdog to trigger when changing system time.
Edouard Tisserant
parents:
diff changeset
    25
da8cceaa247d Runtime : force using monotonic time in Timer (threading). Prevent watchdog to trigger when changing system time.
Edouard Tisserant
parents:
diff changeset
    26
from __future__ import print_function
da8cceaa247d Runtime : force using monotonic time in Timer (threading). Prevent watchdog to trigger when changing system time.
Edouard Tisserant
parents:
diff changeset
    27
from __future__ import unicode_literals
da8cceaa247d Runtime : force using monotonic time in Timer (threading). Prevent watchdog to trigger when changing system time.
Edouard Tisserant
parents:
diff changeset
    28
da8cceaa247d Runtime : force using monotonic time in Timer (threading). Prevent watchdog to trigger when changing system time.
Edouard Tisserant
parents:
diff changeset
    29
__author__ = 'Gavin Beatty <gavinbeatty@gmail.com>'
da8cceaa247d Runtime : force using monotonic time in Timer (threading). Prevent watchdog to trigger when changing system time.
Edouard Tisserant
parents:
diff changeset
    30
__version__ = '2.1.0.dev0'
da8cceaa247d Runtime : force using monotonic time in Timer (threading). Prevent watchdog to trigger when changing system time.
Edouard Tisserant
parents:
diff changeset
    31
__date__ = '2017-05-28'
da8cceaa247d Runtime : force using monotonic time in Timer (threading). Prevent watchdog to trigger when changing system time.
Edouard Tisserant
parents:
diff changeset
    32
__all__ = ['monotonic']
da8cceaa247d Runtime : force using monotonic time in Timer (threading). Prevent watchdog to trigger when changing system time.
Edouard Tisserant
parents:
diff changeset
    33
da8cceaa247d Runtime : force using monotonic time in Timer (threading). Prevent watchdog to trigger when changing system time.
Edouard Tisserant
parents:
diff changeset
    34
import contextlib
da8cceaa247d Runtime : force using monotonic time in Timer (threading). Prevent watchdog to trigger when changing system time.
Edouard Tisserant
parents:
diff changeset
    35
import ctypes
da8cceaa247d Runtime : force using monotonic time in Timer (threading). Prevent watchdog to trigger when changing system time.
Edouard Tisserant
parents:
diff changeset
    36
import errno
da8cceaa247d Runtime : force using monotonic time in Timer (threading). Prevent watchdog to trigger when changing system time.
Edouard Tisserant
parents:
diff changeset
    37
import os
da8cceaa247d Runtime : force using monotonic time in Timer (threading). Prevent watchdog to trigger when changing system time.
Edouard Tisserant
parents:
diff changeset
    38
import platform
da8cceaa247d Runtime : force using monotonic time in Timer (threading). Prevent watchdog to trigger when changing system time.
Edouard Tisserant
parents:
diff changeset
    39
import sys
da8cceaa247d Runtime : force using monotonic time in Timer (threading). Prevent watchdog to trigger when changing system time.
Edouard Tisserant
parents:
diff changeset
    40
import time
da8cceaa247d Runtime : force using monotonic time in Timer (threading). Prevent watchdog to trigger when changing system time.
Edouard Tisserant
parents:
diff changeset
    41
da8cceaa247d Runtime : force using monotonic time in Timer (threading). Prevent watchdog to trigger when changing system time.
Edouard Tisserant
parents:
diff changeset
    42
_machine64 = (platform.machine(), sys.maxsize > 2**32)
da8cceaa247d Runtime : force using monotonic time in Timer (threading). Prevent watchdog to trigger when changing system time.
Edouard Tisserant
parents:
diff changeset
    43
da8cceaa247d Runtime : force using monotonic time in Timer (threading). Prevent watchdog to trigger when changing system time.
Edouard Tisserant
parents:
diff changeset
    44
da8cceaa247d Runtime : force using monotonic time in Timer (threading). Prevent watchdog to trigger when changing system time.
Edouard Tisserant
parents:
diff changeset
    45
class _NS():
da8cceaa247d Runtime : force using monotonic time in Timer (threading). Prevent watchdog to trigger when changing system time.
Edouard Tisserant
parents:
diff changeset
    46
    pass
da8cceaa247d Runtime : force using monotonic time in Timer (threading). Prevent watchdog to trigger when changing system time.
Edouard Tisserant
parents:
diff changeset
    47
da8cceaa247d Runtime : force using monotonic time in Timer (threading). Prevent watchdog to trigger when changing system time.
Edouard Tisserant
parents:
diff changeset
    48
da8cceaa247d Runtime : force using monotonic time in Timer (threading). Prevent watchdog to trigger when changing system time.
Edouard Tisserant
parents:
diff changeset
    49
class _mach_timespec(ctypes.Structure):
da8cceaa247d Runtime : force using monotonic time in Timer (threading). Prevent watchdog to trigger when changing system time.
Edouard Tisserant
parents:
diff changeset
    50
    _fields_ = [('tv_sec', ctypes.c_uint), ('tv_nsec', ctypes.c_int)]
da8cceaa247d Runtime : force using monotonic time in Timer (threading). Prevent watchdog to trigger when changing system time.
Edouard Tisserant
parents:
diff changeset
    51
da8cceaa247d Runtime : force using monotonic time in Timer (threading). Prevent watchdog to trigger when changing system time.
Edouard Tisserant
parents:
diff changeset
    52
da8cceaa247d Runtime : force using monotonic time in Timer (threading). Prevent watchdog to trigger when changing system time.
Edouard Tisserant
parents:
diff changeset
    53
class _posix_timespec(ctypes.Structure):
da8cceaa247d Runtime : force using monotonic time in Timer (threading). Prevent watchdog to trigger when changing system time.
Edouard Tisserant
parents:
diff changeset
    54
    _fields_ = [('tv_sec', ctypes.c_long), ('tv_nsec', ctypes.c_long)]
da8cceaa247d Runtime : force using monotonic time in Timer (threading). Prevent watchdog to trigger when changing system time.
Edouard Tisserant
parents:
diff changeset
    55
da8cceaa247d Runtime : force using monotonic time in Timer (threading). Prevent watchdog to trigger when changing system time.
Edouard Tisserant
parents:
diff changeset
    56
da8cceaa247d Runtime : force using monotonic time in Timer (threading). Prevent watchdog to trigger when changing system time.
Edouard Tisserant
parents:
diff changeset
    57
def _timespec_to_seconds(ts):
da8cceaa247d Runtime : force using monotonic time in Timer (threading). Prevent watchdog to trigger when changing system time.
Edouard Tisserant
parents:
diff changeset
    58
    return float(ts.tv_sec) + float(ts.tv_nsec) * 1e-9
da8cceaa247d Runtime : force using monotonic time in Timer (threading). Prevent watchdog to trigger when changing system time.
Edouard Tisserant
parents:
diff changeset
    59
da8cceaa247d Runtime : force using monotonic time in Timer (threading). Prevent watchdog to trigger when changing system time.
Edouard Tisserant
parents:
diff changeset
    60
da8cceaa247d Runtime : force using monotonic time in Timer (threading). Prevent watchdog to trigger when changing system time.
Edouard Tisserant
parents:
diff changeset
    61
def _get_ctypes_libmacho_macho_functions():
da8cceaa247d Runtime : force using monotonic time in Timer (threading). Prevent watchdog to trigger when changing system time.
Edouard Tisserant
parents:
diff changeset
    62
    libmacho = ctypes.CDLL('/usr/lib/system/libmacho.dylib', use_errno=True)
da8cceaa247d Runtime : force using monotonic time in Timer (threading). Prevent watchdog to trigger when changing system time.
Edouard Tisserant
parents:
diff changeset
    63
    macho = _NS()
da8cceaa247d Runtime : force using monotonic time in Timer (threading). Prevent watchdog to trigger when changing system time.
Edouard Tisserant
parents:
diff changeset
    64
    macho.get_host = libmacho.mach_host_self
da8cceaa247d Runtime : force using monotonic time in Timer (threading). Prevent watchdog to trigger when changing system time.
Edouard Tisserant
parents:
diff changeset
    65
    macho.get_host.argtypes = []
da8cceaa247d Runtime : force using monotonic time in Timer (threading). Prevent watchdog to trigger when changing system time.
Edouard Tisserant
parents:
diff changeset
    66
    macho.get_host.restype = ctypes.c_uint
da8cceaa247d Runtime : force using monotonic time in Timer (threading). Prevent watchdog to trigger when changing system time.
Edouard Tisserant
parents:
diff changeset
    67
    macho.get_clock = libmacho.host_get_clock_service
da8cceaa247d Runtime : force using monotonic time in Timer (threading). Prevent watchdog to trigger when changing system time.
Edouard Tisserant
parents:
diff changeset
    68
    macho.get_clock.argtypes = [ctypes.c_uint,
da8cceaa247d Runtime : force using monotonic time in Timer (threading). Prevent watchdog to trigger when changing system time.
Edouard Tisserant
parents:
diff changeset
    69
                                ctypes.c_int,
da8cceaa247d Runtime : force using monotonic time in Timer (threading). Prevent watchdog to trigger when changing system time.
Edouard Tisserant
parents:
diff changeset
    70
                                ctypes.POINTER(ctypes.c_uint)
da8cceaa247d Runtime : force using monotonic time in Timer (threading). Prevent watchdog to trigger when changing system time.
Edouard Tisserant
parents:
diff changeset
    71
                                ]
da8cceaa247d Runtime : force using monotonic time in Timer (threading). Prevent watchdog to trigger when changing system time.
Edouard Tisserant
parents:
diff changeset
    72
    macho.get_time = libmacho.clock_get_time
da8cceaa247d Runtime : force using monotonic time in Timer (threading). Prevent watchdog to trigger when changing system time.
Edouard Tisserant
parents:
diff changeset
    73
    macho.get_time.argtypes = [ctypes.c_uint, ctypes.POINTER(_mach_timespec)]
da8cceaa247d Runtime : force using monotonic time in Timer (threading). Prevent watchdog to trigger when changing system time.
Edouard Tisserant
parents:
diff changeset
    74
    macho.deallocate = libmacho.mach_port_deallocate
da8cceaa247d Runtime : force using monotonic time in Timer (threading). Prevent watchdog to trigger when changing system time.
Edouard Tisserant
parents:
diff changeset
    75
    macho.deallocate.argtypes = [ctypes.c_uint, ctypes.c_uint]
da8cceaa247d Runtime : force using monotonic time in Timer (threading). Prevent watchdog to trigger when changing system time.
Edouard Tisserant
parents:
diff changeset
    76
    return libmacho, macho
da8cceaa247d Runtime : force using monotonic time in Timer (threading). Prevent watchdog to trigger when changing system time.
Edouard Tisserant
parents:
diff changeset
    77
da8cceaa247d Runtime : force using monotonic time in Timer (threading). Prevent watchdog to trigger when changing system time.
Edouard Tisserant
parents:
diff changeset
    78
da8cceaa247d Runtime : force using monotonic time in Timer (threading). Prevent watchdog to trigger when changing system time.
Edouard Tisserant
parents:
diff changeset
    79
def _get_ctypes_clock_gettime(library):
da8cceaa247d Runtime : force using monotonic time in Timer (threading). Prevent watchdog to trigger when changing system time.
Edouard Tisserant
parents:
diff changeset
    80
    clock_gettime = ctypes.CDLL(library, use_errno=True).clock_gettime
da8cceaa247d Runtime : force using monotonic time in Timer (threading). Prevent watchdog to trigger when changing system time.
Edouard Tisserant
parents:
diff changeset
    81
    clock_gettime.argtypes = [ctypes.c_int, ctypes.POINTER(_posix_timespec)]
da8cceaa247d Runtime : force using monotonic time in Timer (threading). Prevent watchdog to trigger when changing system time.
Edouard Tisserant
parents:
diff changeset
    82
    return clock_gettime
da8cceaa247d Runtime : force using monotonic time in Timer (threading). Prevent watchdog to trigger when changing system time.
Edouard Tisserant
parents:
diff changeset
    83
da8cceaa247d Runtime : force using monotonic time in Timer (threading). Prevent watchdog to trigger when changing system time.
Edouard Tisserant
parents:
diff changeset
    84
da8cceaa247d Runtime : force using monotonic time in Timer (threading). Prevent watchdog to trigger when changing system time.
Edouard Tisserant
parents:
diff changeset
    85
def _call_ctypes_clock_gettime(clock_gettime, clockid):
da8cceaa247d Runtime : force using monotonic time in Timer (threading). Prevent watchdog to trigger when changing system time.
Edouard Tisserant
parents:
diff changeset
    86
    timespec = _posix_timespec()
da8cceaa247d Runtime : force using monotonic time in Timer (threading). Prevent watchdog to trigger when changing system time.
Edouard Tisserant
parents:
diff changeset
    87
    ret = clock_gettime(clockid, ctypes.pointer(timespec))
da8cceaa247d Runtime : force using monotonic time in Timer (threading). Prevent watchdog to trigger when changing system time.
Edouard Tisserant
parents:
diff changeset
    88
    if int(ret) != 0:
da8cceaa247d Runtime : force using monotonic time in Timer (threading). Prevent watchdog to trigger when changing system time.
Edouard Tisserant
parents:
diff changeset
    89
        errno_ = ctypes.get_errno()
da8cceaa247d Runtime : force using monotonic time in Timer (threading). Prevent watchdog to trigger when changing system time.
Edouard Tisserant
parents:
diff changeset
    90
        raise OSError(errno_, os.strerror(errno_))
da8cceaa247d Runtime : force using monotonic time in Timer (threading). Prevent watchdog to trigger when changing system time.
Edouard Tisserant
parents:
diff changeset
    91
    return timespec
da8cceaa247d Runtime : force using monotonic time in Timer (threading). Prevent watchdog to trigger when changing system time.
Edouard Tisserant
parents:
diff changeset
    92
da8cceaa247d Runtime : force using monotonic time in Timer (threading). Prevent watchdog to trigger when changing system time.
Edouard Tisserant
parents:
diff changeset
    93
da8cceaa247d Runtime : force using monotonic time in Timer (threading). Prevent watchdog to trigger when changing system time.
Edouard Tisserant
parents:
diff changeset
    94
_py_monotonic = getattr(time, 'monotonic', None)
da8cceaa247d Runtime : force using monotonic time in Timer (threading). Prevent watchdog to trigger when changing system time.
Edouard Tisserant
parents:
diff changeset
    95
if _py_monotonic is not None:
da8cceaa247d Runtime : force using monotonic time in Timer (threading). Prevent watchdog to trigger when changing system time.
Edouard Tisserant
parents:
diff changeset
    96
    monotonic = _py_monotonic
da8cceaa247d Runtime : force using monotonic time in Timer (threading). Prevent watchdog to trigger when changing system time.
Edouard Tisserant
parents:
diff changeset
    97
elif sys.platform.startswith('linux'):
da8cceaa247d Runtime : force using monotonic time in Timer (threading). Prevent watchdog to trigger when changing system time.
Edouard Tisserant
parents:
diff changeset
    98
    _clock_gettime = _get_ctypes_clock_gettime('librt.so.1')
da8cceaa247d Runtime : force using monotonic time in Timer (threading). Prevent watchdog to trigger when changing system time.
Edouard Tisserant
parents:
diff changeset
    99
da8cceaa247d Runtime : force using monotonic time in Timer (threading). Prevent watchdog to trigger when changing system time.
Edouard Tisserant
parents:
diff changeset
   100
    def monotonic():
da8cceaa247d Runtime : force using monotonic time in Timer (threading). Prevent watchdog to trigger when changing system time.
Edouard Tisserant
parents:
diff changeset
   101
        clockid = ctypes.c_int(1)
da8cceaa247d Runtime : force using monotonic time in Timer (threading). Prevent watchdog to trigger when changing system time.
Edouard Tisserant
parents:
diff changeset
   102
        timespec = _call_ctypes_clock_gettime(_clock_gettime, clockid)
da8cceaa247d Runtime : force using monotonic time in Timer (threading). Prevent watchdog to trigger when changing system time.
Edouard Tisserant
parents:
diff changeset
   103
        return _timespec_to_seconds(timespec)
da8cceaa247d Runtime : force using monotonic time in Timer (threading). Prevent watchdog to trigger when changing system time.
Edouard Tisserant
parents:
diff changeset
   104
elif sys.platform.startswith('freebsd'):
da8cceaa247d Runtime : force using monotonic time in Timer (threading). Prevent watchdog to trigger when changing system time.
Edouard Tisserant
parents:
diff changeset
   105
    _clock_gettime = _get_ctypes_clock_gettime('libc.so')
da8cceaa247d Runtime : force using monotonic time in Timer (threading). Prevent watchdog to trigger when changing system time.
Edouard Tisserant
parents:
diff changeset
   106
da8cceaa247d Runtime : force using monotonic time in Timer (threading). Prevent watchdog to trigger when changing system time.
Edouard Tisserant
parents:
diff changeset
   107
    def monotonic():
da8cceaa247d Runtime : force using monotonic time in Timer (threading). Prevent watchdog to trigger when changing system time.
Edouard Tisserant
parents:
diff changeset
   108
        clockid = ctypes.c_int(4)
da8cceaa247d Runtime : force using monotonic time in Timer (threading). Prevent watchdog to trigger when changing system time.
Edouard Tisserant
parents:
diff changeset
   109
        timespec = _call_ctypes_clock_gettime(_clock_gettime, clockid)
da8cceaa247d Runtime : force using monotonic time in Timer (threading). Prevent watchdog to trigger when changing system time.
Edouard Tisserant
parents:
diff changeset
   110
        return _timespec_to_seconds(timespec)
da8cceaa247d Runtime : force using monotonic time in Timer (threading). Prevent watchdog to trigger when changing system time.
Edouard Tisserant
parents:
diff changeset
   111
elif sys.platform.startswith('darwin') and _machine64 == ('x86_64', True):
da8cceaa247d Runtime : force using monotonic time in Timer (threading). Prevent watchdog to trigger when changing system time.
Edouard Tisserant
parents:
diff changeset
   112
    _libmacho, _macho = _get_ctypes_libmacho_macho_functions()
da8cceaa247d Runtime : force using monotonic time in Timer (threading). Prevent watchdog to trigger when changing system time.
Edouard Tisserant
parents:
diff changeset
   113
da8cceaa247d Runtime : force using monotonic time in Timer (threading). Prevent watchdog to trigger when changing system time.
Edouard Tisserant
parents:
diff changeset
   114
    @contextlib.contextmanager
da8cceaa247d Runtime : force using monotonic time in Timer (threading). Prevent watchdog to trigger when changing system time.
Edouard Tisserant
parents:
diff changeset
   115
    def _deallocate(task, port):
da8cceaa247d Runtime : force using monotonic time in Timer (threading). Prevent watchdog to trigger when changing system time.
Edouard Tisserant
parents:
diff changeset
   116
        try:
da8cceaa247d Runtime : force using monotonic time in Timer (threading). Prevent watchdog to trigger when changing system time.
Edouard Tisserant
parents:
diff changeset
   117
            yield
da8cceaa247d Runtime : force using monotonic time in Timer (threading). Prevent watchdog to trigger when changing system time.
Edouard Tisserant
parents:
diff changeset
   118
        finally:
da8cceaa247d Runtime : force using monotonic time in Timer (threading). Prevent watchdog to trigger when changing system time.
Edouard Tisserant
parents:
diff changeset
   119
            if int(_macho.deallocate(task, port)) == 0:
da8cceaa247d Runtime : force using monotonic time in Timer (threading). Prevent watchdog to trigger when changing system time.
Edouard Tisserant
parents:
diff changeset
   120
                return
da8cceaa247d Runtime : force using monotonic time in Timer (threading). Prevent watchdog to trigger when changing system time.
Edouard Tisserant
parents:
diff changeset
   121
            errno_ = ctypes.get_errno()
da8cceaa247d Runtime : force using monotonic time in Timer (threading). Prevent watchdog to trigger when changing system time.
Edouard Tisserant
parents:
diff changeset
   122
            raise OSError(errno_, os.strerror(errno_))
da8cceaa247d Runtime : force using monotonic time in Timer (threading). Prevent watchdog to trigger when changing system time.
Edouard Tisserant
parents:
diff changeset
   123
da8cceaa247d Runtime : force using monotonic time in Timer (threading). Prevent watchdog to trigger when changing system time.
Edouard Tisserant
parents:
diff changeset
   124
    def monotonic():
da8cceaa247d Runtime : force using monotonic time in Timer (threading). Prevent watchdog to trigger when changing system time.
Edouard Tisserant
parents:
diff changeset
   125
        task = ctypes.c_uint.in_dll(_libmacho, 'mach_task_self_')
da8cceaa247d Runtime : force using monotonic time in Timer (threading). Prevent watchdog to trigger when changing system time.
Edouard Tisserant
parents:
diff changeset
   126
        host = _macho.get_host()
da8cceaa247d Runtime : force using monotonic time in Timer (threading). Prevent watchdog to trigger when changing system time.
Edouard Tisserant
parents:
diff changeset
   127
        with _deallocate(task, host):
da8cceaa247d Runtime : force using monotonic time in Timer (threading). Prevent watchdog to trigger when changing system time.
Edouard Tisserant
parents:
diff changeset
   128
            clock = ctypes.c_uint(0)
da8cceaa247d Runtime : force using monotonic time in Timer (threading). Prevent watchdog to trigger when changing system time.
Edouard Tisserant
parents:
diff changeset
   129
            clockid = ctypes.c_int(0)
da8cceaa247d Runtime : force using monotonic time in Timer (threading). Prevent watchdog to trigger when changing system time.
Edouard Tisserant
parents:
diff changeset
   130
            ret = _macho.get_clock(host, clockid, ctypes.pointer(clock))
da8cceaa247d Runtime : force using monotonic time in Timer (threading). Prevent watchdog to trigger when changing system time.
Edouard Tisserant
parents:
diff changeset
   131
            if int(ret) != 0:
da8cceaa247d Runtime : force using monotonic time in Timer (threading). Prevent watchdog to trigger when changing system time.
Edouard Tisserant
parents:
diff changeset
   132
                errno_ = ctypes.get_errno()
da8cceaa247d Runtime : force using monotonic time in Timer (threading). Prevent watchdog to trigger when changing system time.
Edouard Tisserant
parents:
diff changeset
   133
                raise OSError(errno_, os.strerror(errno_))
da8cceaa247d Runtime : force using monotonic time in Timer (threading). Prevent watchdog to trigger when changing system time.
Edouard Tisserant
parents:
diff changeset
   134
            with _deallocate(task, clock):
da8cceaa247d Runtime : force using monotonic time in Timer (threading). Prevent watchdog to trigger when changing system time.
Edouard Tisserant
parents:
diff changeset
   135
                timespec = _mach_timespec()
da8cceaa247d Runtime : force using monotonic time in Timer (threading). Prevent watchdog to trigger when changing system time.
Edouard Tisserant
parents:
diff changeset
   136
                ret = _macho.get_time(clock, ctypes.pointer(timespec))
da8cceaa247d Runtime : force using monotonic time in Timer (threading). Prevent watchdog to trigger when changing system time.
Edouard Tisserant
parents:
diff changeset
   137
                if int(ret) != 0:
da8cceaa247d Runtime : force using monotonic time in Timer (threading). Prevent watchdog to trigger when changing system time.
Edouard Tisserant
parents:
diff changeset
   138
                    errno_ = ctypes.get_errno()
da8cceaa247d Runtime : force using monotonic time in Timer (threading). Prevent watchdog to trigger when changing system time.
Edouard Tisserant
parents:
diff changeset
   139
                    raise OSError(errno_, os.strerror(errno_))
da8cceaa247d Runtime : force using monotonic time in Timer (threading). Prevent watchdog to trigger when changing system time.
Edouard Tisserant
parents:
diff changeset
   140
                return _timespec_to_seconds(timespec)
da8cceaa247d Runtime : force using monotonic time in Timer (threading). Prevent watchdog to trigger when changing system time.
Edouard Tisserant
parents:
diff changeset
   141
elif sys.platform.startswith('win32'):
da8cceaa247d Runtime : force using monotonic time in Timer (threading). Prevent watchdog to trigger when changing system time.
Edouard Tisserant
parents:
diff changeset
   142
    _GetTickCount = getattr(ctypes.windll.kernel32, 'GetTickCount64', None)
da8cceaa247d Runtime : force using monotonic time in Timer (threading). Prevent watchdog to trigger when changing system time.
Edouard Tisserant
parents:
diff changeset
   143
da8cceaa247d Runtime : force using monotonic time in Timer (threading). Prevent watchdog to trigger when changing system time.
Edouard Tisserant
parents:
diff changeset
   144
    if _GetTickCount is not None:
da8cceaa247d Runtime : force using monotonic time in Timer (threading). Prevent watchdog to trigger when changing system time.
Edouard Tisserant
parents:
diff changeset
   145
        _GetTickCount.restype = ctypes.c_uint64
da8cceaa247d Runtime : force using monotonic time in Timer (threading). Prevent watchdog to trigger when changing system time.
Edouard Tisserant
parents:
diff changeset
   146
    else:
da8cceaa247d Runtime : force using monotonic time in Timer (threading). Prevent watchdog to trigger when changing system time.
Edouard Tisserant
parents:
diff changeset
   147
        _GetTickCount = ctypes.windll.kernel32.GetTickCount
da8cceaa247d Runtime : force using monotonic time in Timer (threading). Prevent watchdog to trigger when changing system time.
Edouard Tisserant
parents:
diff changeset
   148
        _GetTickCount.restype = ctypes.c_uint32
da8cceaa247d Runtime : force using monotonic time in Timer (threading). Prevent watchdog to trigger when changing system time.
Edouard Tisserant
parents:
diff changeset
   149
da8cceaa247d Runtime : force using monotonic time in Timer (threading). Prevent watchdog to trigger when changing system time.
Edouard Tisserant
parents:
diff changeset
   150
    def monotonic():
da8cceaa247d Runtime : force using monotonic time in Timer (threading). Prevent watchdog to trigger when changing system time.
Edouard Tisserant
parents:
diff changeset
   151
        return float(_GetTickCount()) * 1e-3
da8cceaa247d Runtime : force using monotonic time in Timer (threading). Prevent watchdog to trigger when changing system time.
Edouard Tisserant
parents:
diff changeset
   152
else:
da8cceaa247d Runtime : force using monotonic time in Timer (threading). Prevent watchdog to trigger when changing system time.
Edouard Tisserant
parents:
diff changeset
   153
    def monotonic():
da8cceaa247d Runtime : force using monotonic time in Timer (threading). Prevent watchdog to trigger when changing system time.
Edouard Tisserant
parents:
diff changeset
   154
        msg = 'monotonic not supported on your platform'
da8cceaa247d Runtime : force using monotonic time in Timer (threading). Prevent watchdog to trigger when changing system time.
Edouard Tisserant
parents:
diff changeset
   155
        raise OSError(errno.ENOSYS, msg)
da8cceaa247d Runtime : force using monotonic time in Timer (threading). Prevent watchdog to trigger when changing system time.
Edouard Tisserant
parents:
diff changeset
   156
da8cceaa247d Runtime : force using monotonic time in Timer (threading). Prevent watchdog to trigger when changing system time.
Edouard Tisserant
parents:
diff changeset
   157
da8cceaa247d Runtime : force using monotonic time in Timer (threading). Prevent watchdog to trigger when changing system time.
Edouard Tisserant
parents:
diff changeset
   158
if __name__ == '__main__':
da8cceaa247d Runtime : force using monotonic time in Timer (threading). Prevent watchdog to trigger when changing system time.
Edouard Tisserant
parents:
diff changeset
   159
    print(monotonic())