annotate 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
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff changeset
1 from libc.errno cimport *
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff changeset
2 from libc.stdio cimport snprintf
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff changeset
3
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff changeset
4 cdef extern from "time.h":
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff changeset
5 ctypedef long time_t
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff changeset
6 cdef struct _timespec "timespec":
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff changeset
7 time_t tv_sec
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff changeset
8 long tv_nsec
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff changeset
9 cdef enum:
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff changeset
10 CLOCK_MONOTONIC
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff changeset
11 CLOCK_REALTIME
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff changeset
12 cdef enum:
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff changeset
13 TIMER_ABSTIME
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff changeset
14 int clock_gettime(int id, _timespec *tp) nogil
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff changeset
15 int clock_nanosleep(int id, int flags,
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff changeset
16 _timespec *request,
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff changeset
17 _timespec *remain) nogil
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff changeset
18
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff changeset
19
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff changeset
20 cdef class timespec:
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff changeset
21 cdef _timespec t
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff changeset
22
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff changeset
23 def __init__(self, long sec=0, long nsec=0):
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff changeset
24 self.t.tv_sec = sec
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff changeset
25 self.t.tv_nsec = nsec
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff changeset
26
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff changeset
27 property sec:
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff changeset
28 def __get__(self):
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff changeset
29 return self.t.tv_sec
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff changeset
30 def __set__(self, long v):
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff changeset
31 self.t.tv_sec = v
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff changeset
32
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff changeset
33 property nsec:
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff changeset
34 def __get__(self):
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff changeset
35 return self.t.tv_nsec
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff changeset
36 def __set__(self, long v):
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff changeset
37 self.t.tv_nsec = v
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff changeset
38 while self.t.tv_nsec >= 1000000000:
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff changeset
39 self.t.tv_sec += 1
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff changeset
40 self.t.tv_nsec -= 1000000000
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff changeset
41
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff changeset
42 def __add__(self, long v):
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff changeset
43 self.sec += v
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff changeset
44 return self
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff changeset
45
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff changeset
46 def __float__(self):
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff changeset
47 return self.t.tv_sec + self.t.tv_nsec * 1e-9
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff changeset
48
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff changeset
49 def __repr__(self):
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff changeset
50 cdef char[64] s
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff changeset
51 snprintf(s, 64, "{ sec=%ld nsec=%ld }", self.t.tv_sec, self.t.tv_nsec)
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff changeset
52 return s
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff changeset
53
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff changeset
54
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff changeset
55 def gettime(clockid=CLOCK_REALTIME):
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff changeset
56 t = timespec()
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff changeset
57 clock_gettime(clockid, &t.t)
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff changeset
58 return t
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff changeset
59
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff changeset
60
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff changeset
61 def nanosleep(timespec request, int clockid=CLOCK_REALTIME, bint absolute=False):
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff changeset
62 cdef _timespec remain
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff changeset
63 cdef int flags = 0
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff changeset
64 cdef int r
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff changeset
65 cdef _timespec *t = &request.t
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff changeset
66
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff changeset
67 if absolute:
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff changeset
68 flags = flags | TIMER_ABSTIME
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff changeset
69
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff changeset
70 with nogil:
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff changeset
71 while 1:
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff changeset
72 r = clock_nanosleep(clockid, flags, t, &remain)
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff changeset
73 if r == EINTR:
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff changeset
74 if not absolute:
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff changeset
75 t = &remain
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff changeset
76 continue
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff changeset
77 break
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff changeset
78
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff changeset
79 if r != 0:
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff changeset
80 raise ValueError