]> git.decadent.org.uk Git - ion3.git/blobdiff - libmainloop/signal.c
[svn-upgrade] Integrating new upstream version, ion3 (20070708)
[ion3.git] / libmainloop / signal.c
index fea01ba6ccabebb43ff9ded99c57707db64bef84..37ee995f507da6818d8b0c622d9a9782ef12a0c4 100644 (file)
@@ -3,10 +3,7 @@
  *
  * Copyright (c) Tuomo Valkonen 1999-2007. 
  *
- * Ion is free software; you can redistribute it and/or modify it under
- * the terms of the GNU Lesser General Public License as published by
- * the Free Software Foundation; either version 2.1 of the License, or
- * (at your option) any later version.
+ * See the included file LICENSE for details.
  */
 
 #include <unistd.h>
@@ -17,6 +14,7 @@
 #include <signal.h>
 #include <string.h>
 #include <stdlib.h>
+#include <errno.h>
 
 #include <libtu/objp.h>
 #include <libtu/types.h>
@@ -45,6 +43,34 @@ WHook *mainloop_sigusr2_hook=NULL;
 static WTimer *queue=NULL;
 
 
+int mainloop_gettime(struct timeval *val)
+{
+#if defined(_POSIX_MONOTONIC_CLOCK) && (_POSIX_MONOTONIC_CLOCK>=0)
+    struct timespec spec;
+    int ret;
+    static int checked=0;
+    
+    if(checked>=0){
+        ret=clock_gettime(CLOCK_MONOTONIC, &spec);
+    
+        if(ret==-1 && errno==EINVAL && checked==0){
+            checked=-1;
+        }else{
+            checked=1;
+            
+            val->tv_sec=spec.tv_sec;
+            val->tv_usec=spec.tv_nsec/1000;
+        
+            return ret;
+        }
+    }
+#else
+    #warning "Monotonic clock unavailable; please fix your operating system."
+#endif
+    return gettimeofday(val, NULL);
+}
+
+
 #define TIMEVAL_LATER(a, b) \
     ((a.tv_sec > b.tv_sec) || \
     ((a.tv_sec == b.tv_sec) && \
@@ -63,7 +89,7 @@ static void do_timer_set()
     }
 
     /* Subtract queue time from current time, don't go below zero */
-    gettimeofday(&(val.it_value), NULL);
+    mainloop_gettime(&(val.it_value));
     if(TIMEVAL_LATER((queue)->when, val.it_value)){
         if(queue->when.tv_usec<val.it_value.tv_usec){
             queue->when.tv_usec+=USECS_IN_SEC;
@@ -193,7 +219,7 @@ bool mainloop_check_signals()
     /* Check for timer events in the queue */
     while(had_tmr && queue!=NULL){
         had_tmr=FALSE;
-        gettimeofday(&current_time, NULL);
+        mainloop_gettime(&current_time);
         while(queue!=NULL){
             if(TIMEVAL_LATER(current_time, queue->when)){
                 q=queue;
@@ -228,7 +254,7 @@ static void add_to_current_time(struct timeval *when, uint msecs)
 {
     long tmp_usec;
 
-    gettimeofday(when, NULL);
+    mainloop_gettime(when);
     tmp_usec=when->tv_usec + (msecs * 1000);
     when->tv_usec=tmp_usec % 1000000;
     when->tv_sec+=tmp_usec / 1000000;