[前][次][番号順一覧][スレッド一覧]

ruby-changes:19342

From: kosaki <ko1@a...>
Date: Fri, 29 Apr 2011 13:18:36 +0900 (JST)
Subject: [ruby-changes:19342] Ruby:r31382 (trunk): * thread_win32.c (native_cond_timedwait): New. r31373 caused

kosaki	2011-04-29 13:18:29 +0900 (Fri, 29 Apr 2011)

  New Revision: 31382

  http://svn.ruby-lang.org/cgi-bin/viewvc.cgi?view=rev&revision=31382

  Log:
    * thread_win32.c (native_cond_timedwait): New. r31373 caused
      win32 build failure.
    
    * thread_win32.c (__cond_timedwait, abs_timespec_to_timeout_ms):
      New helper functions.
    
    * win32/win32.c (rb_w32_time_subtract): rename from subtract and
      remove static.

  Modified files:
    trunk/ChangeLog
    trunk/include/ruby/win32.h
    trunk/thread_win32.c
    trunk/win32/win32.c

Index: thread_win32.c
===================================================================
--- thread_win32.c	(revision 31381)
+++ thread_win32.c	(revision 31382)
@@ -21,19 +21,6 @@
 
 static volatile DWORD ruby_native_thread_key = TLS_OUT_OF_INDEXES;
 
-static int native_mutex_lock(rb_thread_lock_t *);
-static int native_mutex_unlock(rb_thread_lock_t *);
-static int native_mutex_trylock(rb_thread_lock_t *);
-static void native_mutex_initialize(rb_thread_lock_t *);
-static void native_mutex_destroy(rb_thread_lock_t *);
-
-static void native_cond_signal(rb_thread_cond_t *cond);
-static void native_cond_broadcast(rb_thread_cond_t *cond);
-static void native_cond_wait(rb_thread_cond_t *cond, rb_thread_lock_t *mutex);
-static void native_cond_initialize(rb_thread_cond_t *cond);
-static void native_cond_destroy(rb_thread_cond_t *cond);
-static int w32_wait_events(HANDLE *events, int count, DWORD timeout, rb_thread_t *th);
-
 static void
 w32_error(const char *func)
 {
@@ -433,8 +420,9 @@
     }
 }
 
-static void
-native_cond_wait(rb_thread_cond_t *cond, rb_thread_lock_t *mutex)
+
+static int
+__cond_timedwait(rb_thread_cond_t *cond, rb_thread_lock_t *mutex, unsigned long msec)
 {
     DWORD r;
     struct cond_event_entry entry;
@@ -454,16 +442,51 @@
 
     native_mutex_unlock(mutex);
     {
-	r = WaitForSingleObject(entry.event, INFINITE);
-	if (r != WAIT_OBJECT_0) {
+	r = WaitForSingleObject(entry.event, msec);
+	if ((r != WAIT_OBJECT_0) && (r != WAIT_TIMEOUT)) {
 	    rb_bug("native_cond_wait: WaitForSingleObject returns %lu", r);
 	}
     }
     native_mutex_lock(mutex);
 
     w32_close_handle(entry.event);
+    return (r == WAIT_OBJECT_0) ? 0 : ETIMEDOUT;
 }
 
+static int
+native_cond_wait(rb_thread_cond_t *cond, rb_thread_lock_t *mutex)
+{
+    return __cond_timedwait(cond, mutex, INFINITE);
+}
+
+static unsigned long
+abs_timespec_to_timeout_ms(struct timespec *ts)
+{
+    struct timeval tv;
+    struct timeval now;
+
+    gettimeofday(&now, NULL);
+    tv.tv_sec = ts->tv_sec;
+    tv.tv_usec = ts->tv_nsec;
+
+    if (!rb_w32_time_subtract(&tv, &now))
+	return 0;
+
+    return (tv.tv_sec * 1000) + (tv.tv_usec / 1000);
+}
+
+static int
+native_cond_timedwait(rb_thread_cond_t *cond, rb_thread_lock_t *mutex, struct timespec *ts)
+{
+    unsigned long timeout_ms;
+
+    timeout_ms = abs_timespec_to_timeout_ms(ts);
+    if (!timeout_ms)
+	return ETIMEDOUT;
+
+    return __cond_timedwait(cond, mutex, timeout_ms);
+}
+
 static void
 native_cond_initialize(rb_thread_cond_t *cond)
 {
Index: include/ruby/win32.h
===================================================================
--- include/ruby/win32.h	(revision 31381)
+++ include/ruby/win32.h	(revision 31382)
@@ -303,6 +303,7 @@
 extern int rb_w32_ustati64(const char *, struct stati64 *);
 extern int rb_w32_access(const char *, int);
 extern int rb_w32_uaccess(const char *, int);
+extern int rb_w32_subtract(struct timeval *rest, const struct timeval *wait);
 
 #ifdef __BORLANDC__
 extern int rb_w32_fstati64(int, struct stati64 *);
Index: ChangeLog
===================================================================
--- ChangeLog	(revision 31381)
+++ ChangeLog	(revision 31382)
@@ -1,3 +1,15 @@
+Fri Apr 29 13:15:15 2011  KOSAKI Motohiro  <kosaki.motohiro@g...>
+
+	* thread_win32.c (native_cond_timedwait): New. r31373 caused
+	  win32 build failure.
+
+	* thread_win32.c (__cond_timedwait, abs_timespec_to_timeout_ms):
+	  New helper functions.
+
+	* win32/win32.c (rb_w32_time_subtract): rename from subtract and
+	  remove static.
+
+
 Fri Apr 29 10:43:09 2011  KOSAKI Motohiro  <kosaki.motohiro@g...>
 
 	* benchmark/bm_vm4_pipe.rb: Add two new benchmark for GVL
Index: win32/win32.c
===================================================================
--- win32/win32.c	(revision 31381)
+++ win32/win32.c	(revision 31382)
@@ -2528,8 +2528,12 @@
     return r;
 }
 
-static inline int
-subtract(struct timeval *rest, const struct timeval *wait)
+/*
+ * rest -= wait
+ * return 0 if rest is smaller than wait.
+ */
+int
+rb_w32_time_subtract(struct timeval *rest, const struct timeval *wait)
 {
     if (rest->tv_sec < wait->tv_sec) {
 	return 0;
@@ -2668,7 +2672,7 @@
 		    struct timeval now;
 		    gettimeofday(&now, NULL);
 		    rest = limit;
-		    if (!subtract(&rest, &now)) break;
+		    if (!rb_w32_time_subtract(&rest, &now)) break;
 		    if (compare(&rest, &wait) < 0) dowait = &rest;
 		}
 		Sleep(dowait->tv_sec * 1000 + dowait->tv_usec / 1000);

--
ML: ruby-changes@q...
Info: http://www.atdot.net/~ko1/quickml/

[前][次][番号順一覧][スレッド一覧]