changeset 0:1c0396a8fff4 default tip

Import
author Daniele Nicolodi <daniele.nicolodi@obspm.fr>
date Fri, 03 Jul 2015 21:00:16 +0200
parents
children
files .hgignore Makefile clock.pyx setup.py test.py
diffstat 5 files changed, 120 insertions(+), 0 deletions(-) [+]
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/.hgignore	Fri Jul 03 21:00:16 2015 +0200
@@ -0,0 +1,7 @@
+syntax: glob
+clock.c
+build/
+*.pyc
+*.so
+*~
+
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/Makefile	Fri Jul 03 21:00:16 2015 +0200
@@ -0,0 +1,4 @@
+PYTHON = python
+
+all:
+	$(PYTHON) setup.py build_ext --inplace
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/clock.pyx	Fri Jul 03 21:00:16 2015 +0200
@@ -0,0 +1,80 @@
+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
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/setup.py	Fri Jul 03 21:00:16 2015 +0200
@@ -0,0 +1,7 @@
+from distutils.core import setup
+from distutils.extension import Extension
+from Cython.Build import cythonize
+
+setup(
+    ext_modules = cythonize([Extension("clock", ["clock.pyx"])])
+)
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/test.py	Fri Jul 03 21:00:16 2015 +0200
@@ -0,0 +1,22 @@
+import clock
+
+# print clock.gettime()
+# clock.nanosleep(clock.timespec(1))
+# print clock.gettime()
+
+# t = clock.gettime()
+# print t
+# t.sec += 1
+# print t
+# clock.nanosleep(t, absolute=True)
+# print clock.gettime()
+
+# t += 1
+# print t
+
+t = clock.gettime()
+print t
+for x in range(10):
+    t.nsec += 10
+    print t
+    clock.nanosleep(t, absolute=True)