[PATCH] use CLOCK_MONOTONIC_COARSE posix timer instead of CLOCK_MONOTONIC in Xorg

Daniel Stone daniel at fooishbar.org
Mon Aug 23 19:12:40 PDT 2010


Hi,

On Tue, Aug 24, 2010 at 08:55:22AM +0800, ykzhao wrote:
> What Mark mentioned is that the CLOCK_MONOTONIC_COARSE posix timer is
> not supported while the corresponding ID is used for other posix timer.
> Right? 

6 has no meaning to clock_gettime().  CLOCK_MONOTONIC_COARSE is the only
thing that has any meaning: if it's not defined, you can't just invent a
definition and hope that it works.  That's why the spec says
CLOCK_MONOTONIC_COARSE and not 6.

> If so, is there an approach that helps us to detect whether the
> CLOCK_MONOTONIC_COARSE posix timer is supported on one OS?

Try the following completely untested patch (hey, it compiles).  It's
not perfect though: if CLOCK_MONOTONIC or CLOCK_MONOTONIC_COARSE were
ever 0, we'd make two or three syscalls for GetTimeInMillis() instead of
one, and if either of them were ~0L, we'd never use them.

Cheers,
Daniel

diff --git a/os/utils.c b/os/utils.c
index 51455cc..a1659ec 100644
--- a/os/utils.c
+++ b/os/utils.c
@@ -427,7 +427,20 @@ GetTimeInMillis(void)
 
 #ifdef MONOTONIC_CLOCK
     struct timespec tp;
-    if (clock_gettime(CLOCK_MONOTONIC, &tp) == 0)
+    static clockid_t clockid;
+    if (!clockid) {
+#ifdef CLOCK_MONOTONIC_COARSE
+        if (clock_getres(CLOCK_MONOTONIC_COARSE, &tp) == 0 &&
+            (tp.tv_nsec / 1000) <= 1000)
+            clockid = CLOCK_MONOTONIC_COARSE;
+        else
+#endif
+        if (clock_gettime(CLOCK_MONOTONIC, &tp) == 0)
+            clockid = CLOCK_MONOTONIC;
+        else
+            clockid = ~0L;
+    }
+    if (clockid != ~0L && clock_gettime(clockid, &tp) == 0)
         return (tp.tv_sec * 1000) + (tp.tv_nsec / 1000000L);
 #endif
 
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 198 bytes
Desc: Digital signature
URL: <http://lists.x.org/archives/xorg-devel/attachments/20100824/714088d0/attachment.pgp>


More information about the xorg-devel mailing list