[runtime] Add a mono_cond_timedwait_ms () function which takes a timeout argument...
authorZoltan Varga <vargaz@gmail.com>
Fri, 8 May 2015 22:28:12 +0000 (18:28 -0400)
committerZoltan Varga <vargaz@gmail.com>
Fri, 8 May 2015 22:28:12 +0000 (18:28 -0400)
mono/utils/mono-mutex.c
mono/utils/mono-mutex.h

index 9f43b7cafe0392ee24034636efd5d898ce0522f8..51dee2339d12ba29999c1f8ff1499530c66eb2e6 100644 (file)
@@ -18,7 +18,7 @@
 #include <assert.h>
 #include <mono/utils/mono-memory-model.h>
 
-#ifndef HOST_WIN32
+#ifdef HAVE_SYS_TIME_H
 #include <sys/time.h>
 #endif
 
@@ -156,3 +156,31 @@ mono_mutex_init_suspend_safe (mono_mutex_t *mutex)
        return mono_mutex_init (mutex);
 #endif
 }
+
+#ifndef HOST_WIN32
+int
+mono_cond_timedwait_ms (mono_cond_t *cond, mono_mutex_t *mutex, int timeout_ms)
+{
+       struct timeval tv;
+       struct timespec ts;
+       gint64 usecs;
+       int res;
+
+       /* ms = 10^-3, us = 10^-6, ns = 10^-9 */
+
+       gettimeofday (&tv, NULL);
+       tv.tv_sec += timeout_ms / 1000;
+       usecs = tv.tv_usec + ((timeout_ms % 1000) * 1000);
+       if (usecs >= 1000000) {
+               usecs -= 1000000;
+               tv.tv_sec ++;
+       }
+       ts.tv_sec = tv.tv_sec;
+       ts.tv_nsec = usecs * 1000;
+
+       res = pthread_cond_timedwait (cond, mutex, &ts);
+       g_assert (res != EINVAL);
+       return res;
+}
+
+#endif
index 7628b1c8ef36c128d9236b26f501ac37bcc121c0..ecfe73adeb6cf381926b2e91aa9265880a523d31 100644 (file)
@@ -53,6 +53,11 @@ typedef pthread_cond_t mono_cond_t;
 #define mono_cond_broadcast(cond) pthread_cond_broadcast (cond)
 #define mono_cond_destroy(cond)
 
+/*
+ * This should be used instead of mono_cond_timedwait, since that function is not implemented on windows.
+ */
+int mono_cond_timedwait_ms (mono_cond_t *cond, mono_mutex_t *mutex, int timeout_ms);
+
 /* This is a function so it can be passed to pthread_cleanup_push -
  * that is a macro and giving it a macro as a parameter breaks.
  */
@@ -96,14 +101,11 @@ mono_cond_init (mono_cond_t *cond, int attr)
 }
 
 static inline int
-mono_cond_timedwait (mono_cond_t *cond, mono_mutex_t *mutex, DWORD timeout)
+mono_cond_wait (mono_cond_t *cond, mono_mutex_t *mutex)
 {
-       BOOL res;
-
-       // FIXME: This needs to take a struct timespec
-       g_assert_not_reached ();
+       int res;
 
-       res = SleepConditionVariableCS (cond, mutex, timeout);
+       res = SleepConditionVariableCS (cond, mutex, INFINITE);
        if (res)
                /* Success */
                return 0;
@@ -112,9 +114,11 @@ mono_cond_timedwait (mono_cond_t *cond, mono_mutex_t *mutex, DWORD timeout)
 }
 
 static inline int
-mono_cond_wait (mono_cond_t *cond, mono_mutex_t *mutex)
+mono_cond_timedwait (mono_cond_t *cond, mono_mutex_t *mutex, struct timespec *timeout)
 {
-       return mono_cond_timedwait (cond, mutex, INFINITE);
+       // FIXME:
+       g_assert_not_reached ();
+       return 0;
 }
 
 static inline int
@@ -134,6 +138,19 @@ mono_cond_destroy (mono_cond_t *cond)
 {
 }
 
+static inline int
+mono_cond_timedwait_ms (mono_cond_t *cond, mono_mutex_t *mutex, int timeout_ms)
+{
+       int res;
+
+       res = SleepConditionVariableCS (cond, mutex, timeout_ms);
+       if (res)
+               /* Success */
+               return 0;
+       else
+               return 1;
+}
+
 #endif
 
 int mono_mutex_init_suspend_safe (mono_mutex_t *mutex);