Mercurial > hg > python-clock
view clock.pyx @ 0:1c0396a8fff4 default tip
Import
author | Daniele Nicolodi <daniele.nicolodi@obspm.fr> |
---|---|
date | Fri, 03 Jul 2015 21:00:16 +0200 |
parents | |
children |
line wrap: on
line source
from libc.errno cimport * from libc.stdio cimport snprintf cdef extern from "time.h": ctypedef long time_t cdef struct _timespec "timespec": time_t tv_sec long tv_nsec cdef enum: CLOCK_MONOTONIC CLOCK_REALTIME cdef enum: TIMER_ABSTIME int clock_gettime(int id, _timespec *tp) nogil int clock_nanosleep(int id, int flags, _timespec *request, _timespec *remain) nogil cdef class timespec: cdef _timespec t def __init__(self, long sec=0, long nsec=0): self.t.tv_sec = sec self.t.tv_nsec = nsec property sec: def __get__(self): return self.t.tv_sec def __set__(self, long v): self.t.tv_sec = v property nsec: def __get__(self): return self.t.tv_nsec def __set__(self, long v): self.t.tv_nsec = v while self.t.tv_nsec >= 1000000000: self.t.tv_sec += 1 self.t.tv_nsec -= 1000000000 def __add__(self, long v): self.sec += v return self def __float__(self): return self.t.tv_sec + self.t.tv_nsec * 1e-9 def __repr__(self): cdef char[64] s snprintf(s, 64, "{ sec=%ld nsec=%ld }", self.t.tv_sec, self.t.tv_nsec) return s def gettime(clockid=CLOCK_REALTIME): t = timespec() clock_gettime(clockid, &t.t) return t def nanosleep(timespec request, int clockid=CLOCK_REALTIME, bint absolute=False): cdef _timespec remain cdef int flags = 0 cdef int r cdef _timespec *t = &request.t if absolute: flags = flags | TIMER_ABSTIME with nogil: while 1: r = clock_nanosleep(clockid, flags, t, &remain) if r == EINTR: if not absolute: t = &remain continue break if r != 0: raise ValueError