+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);
+}
+
+