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

ruby-changes:19411

From: kosaki <ko1@a...>
Date: Sat, 7 May 2011 01:47:48 +0900 (JST)
Subject: [ruby-changes:19411] Ruby:r31451 (trunk): * thread_pthread.h (rb_thread_cond_t): add clockid field. it's

kosaki	2011-05-07 01:47:38 +0900 (Sat, 07 May 2011)

  New Revision: 31451

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

  Log:
    * thread_pthread.h (rb_thread_cond_t): add clockid field. it's
      no longer an alias of pthread_cond_t.
    * thread_pthread.c: adapt new rb_thread_cond_t type.
    * thread.c (mutex_alloc): ditto.
    * thread_win32.c (native_cond_initialize): ditto.
    * configure.in: add check for pthread_cond_attr_setclock() and
      clockid_t type.

  Modified files:
    trunk/ChangeLog
    trunk/configure.in
    trunk/thread.c
    trunk/thread_pthread.c
    trunk/thread_pthread.h
    trunk/thread_win32.c

Index: thread_win32.c
===================================================================
--- thread_win32.c	(revision 31450)
+++ thread_win32.c	(revision 31451)
@@ -490,7 +490,7 @@
 }
 
 static void
-native_cond_initialize(rb_thread_cond_t *cond)
+native_cond_initialize(rb_thread_cond_t *cond, int flags)
 {
     cond->next = 0;
     cond->last = 0;
Index: configure.in
===================================================================
--- configure.in	(revision 31450)
+++ configure.in	(revision 31451)
@@ -1162,6 +1162,13 @@
 @%:@ include <sys/time.h>
 @%:@endif])
 
+AC_CHECK_TYPES([clockid_t], [], [], [@%:@ifdef HAVE_TIME_H
+@%:@ include <time.h>
+@%:@endif
+@%:@ifdef HAVE_SYS_TIME_H
+@%:@ include <sys/time.h>
+@%:@endif])
+
 AC_CACHE_VAL([rb_cv_large_fd_select],
     [AC_CHECK_TYPE(fd_mask, [rb_cv_large_fd_select=yes], [rb_cv_large_fd_select=no])])
 if test "$rb_cv_large_fd_select" = yes; then
@@ -1734,7 +1741,8 @@
     AC_CHECK_FUNCS(sched_yield pthread_attr_setinheritsched \
 	pthread_getattr_np pthread_attr_get_np pthread_attr_getstack\
 	pthread_get_stackaddr_np pthread_get_stacksize_np \
-	thr_stksegment pthread_stackseg_np pthread_getthrds_np)
+	thr_stksegment pthread_stackseg_np pthread_getthrds_np \
+	pthread_condattr_setclock)
 fi
 if test x"$ac_cv_header_ucontext_h" = xyes; then
     if test x"$rb_with_pthread" = xyes; then
Index: ChangeLog
===================================================================
--- ChangeLog	(revision 31450)
+++ ChangeLog	(revision 31451)
@@ -1,3 +1,13 @@
+Sat May  7 01:43:37 2011  KOSAKI Motohiro  <kosaki.motohiro@g...>
+
+	* thread_pthread.h (rb_thread_cond_t): add clockid field. it's
+	  no longer an alias of pthread_cond_t.
+	* thread_pthread.c: adapt new rb_thread_cond_t type.
+	* thread.c (mutex_alloc): ditto.
+	* thread_win32.c (native_cond_initialize): ditto.
+	* configure.in: add check for pthread_cond_attr_setclock() and
+	  clockid_t type.
+
 Fri May  6 23:29:47 2011  KOSAKI Motohiro  <kosaki.motohiro@g...>
 
 	* thread.c (rb_wait_for_single_fd): use ppoll() instead of poll()
Index: thread_pthread.c
===================================================================
--- thread_pthread.c	(revision 31450)
+++ thread_pthread.c	(revision 31451)
@@ -22,12 +22,16 @@
 static int native_mutex_trylock(pthread_mutex_t *lock);
 static void native_mutex_initialize(pthread_mutex_t *lock);
 static void native_mutex_destroy(pthread_mutex_t *lock);
+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, pthread_mutex_t *mutex);
+static void native_cond_initialize(rb_thread_cond_t *cond, int flags);
+static void native_cond_destroy(rb_thread_cond_t *cond);
 
-static void native_cond_signal(pthread_cond_t *cond);
-static void native_cond_broadcast(pthread_cond_t *cond);
-static void native_cond_wait(pthread_cond_t *cond, pthread_mutex_t *mutex);
-static void native_cond_initialize(pthread_cond_t *cond);
-static void native_cond_destroy(pthread_cond_t *cond);
+#if defined(HAVE_PTHREAD_CONDATTR_SETCLOCK) && defined(CLOCK_MONOTONIC) && defined(HAVE_CLOCK_GETTIME)
+#define USE_MONOTONIC_COND 1
+#define RB_CONDATTR_CLOCK_MONOTONIC 1
+#endif
 
 #define GVL_SIMPLE_LOCK 0
 #define GVL_DEBUG 0
@@ -220,54 +224,71 @@
 }
 
 static void
-native_cond_initialize(pthread_cond_t *cond)
+native_cond_initialize(rb_thread_cond_t *cond, int flags)
 {
-    int r = pthread_cond_init(cond, 0);
+    int r;
+    pthread_condattr_t attr;
+
+    cond->clockid = CLOCK_REALTIME;
+    pthread_condattr_init(&attr);
+
+#if USE_MONOTONIC_COND
+    if (flags & RB_CONDATTR_CLOCK_MONOTONIC) {
+	r = pthread_condattr_setclock(&attr, CLOCK_MONOTONIC);
+	if (r == 0) {
+	    cond->clockid = CLOCK_MONOTONIC;
+	}
+    }
+#endif
+
+    r = pthread_cond_init(&cond->cond, &attr);
     if (r != 0) {
 	rb_bug_errno("pthread_cond_init", r);
     }
-}
 
+    return;
+ }
+
 static void
-native_cond_destroy(pthread_cond_t *cond)
+native_cond_destroy(rb_thread_cond_t *cond)
 {
-    int r = pthread_cond_destroy(cond);
+    int r = pthread_cond_destroy(&cond->cond);
     if (r != 0) {
 	rb_bug_errno("pthread_cond_destroy", r);
     }
 }
 
 static void
-native_cond_signal(pthread_cond_t *cond)
+native_cond_signal(rb_thread_cond_t *cond)
 {
-    int r = pthread_cond_signal(cond);
+    int r = pthread_cond_signal(&cond->cond);
     if (r != 0) {
 	rb_bug_errno("pthread_cond_signal", r);
     }
 }
 
 static void
-native_cond_broadcast(pthread_cond_t *cond)
+native_cond_broadcast(rb_thread_cond_t *cond)
 {
-    int r = pthread_cond_broadcast(cond);
+    int r = pthread_cond_broadcast(&cond->cond);
     if (r != 0) {
 	rb_bug_errno("native_cond_broadcast", r);
     }
 }
 
 static void
-native_cond_wait(pthread_cond_t *cond, pthread_mutex_t *mutex)
+native_cond_wait(rb_thread_cond_t *cond, pthread_mutex_t *mutex)
 {
-    int r = pthread_cond_wait(cond, mutex);
+    int r = pthread_cond_wait(&cond->cond, mutex);
     if (r != 0) {
 	rb_bug_errno("pthread_cond_wait", r);
     }
 }
 
 static int
-native_cond_timedwait(pthread_cond_t *cond, pthread_mutex_t *mutex, struct timespec *ts)
+native_cond_timedwait(rb_thread_cond_t *cond, pthread_mutex_t *mutex, struct timespec *ts)
 {
-    int r = pthread_cond_timedwait(cond, mutex, ts);
+    int r = pthread_cond_timedwait(&cond->cond, mutex, ts);
     if (r != 0 && r != ETIMEDOUT && r != EINTR /* Linux */) {
 	rb_bug_errno("pthread_cond_timedwait", r);
     }
@@ -326,16 +347,16 @@
 static void
 native_thread_init(rb_thread_t *th)
 {
-    native_cond_initialize(&th->native_thread_data.sleep_cond);
-    native_cond_initialize(&th->native_thread_data.gvl_cond);
+    native_cond_initialize(&th->native_thread_data.sleep_cond, 0);
+    native_cond_initialize(&th->native_thread_data.gvl_cond, 0);
     ruby_thread_set_native(th);
 }
 
 static void
 native_thread_destroy(rb_thread_t *th)
 {
-    pthread_cond_destroy(&th->native_thread_data.gvl_cond);
-    pthread_cond_destroy(&th->native_thread_data.sleep_cond);
+    native_cond_destroy(&th->native_thread_data.gvl_cond);
+    native_cond_destroy(&th->native_thread_data.sleep_cond);
 }
 
 #define USE_THREAD_CACHE 0
@@ -725,7 +746,7 @@
 {
     rb_thread_t *th = (rb_thread_t *)ptr;
     thread_debug("ubf_pthread_cond_signal (%p)\n", (void *)th);
-    pthread_cond_signal(&th->native_thread_data.sleep_cond);
+    native_cond_signal(&th->native_thread_data.sleep_cond);
 }
 
 #if !defined(__CYGWIN__) && !defined(__SYMBIAN32__)
@@ -780,19 +801,17 @@
 	}
 	else {
 	    if (tv == 0 || ts.tv_sec < tvn.tv_sec /* overflow */ ) {
-		int r;
 		thread_debug("native_sleep: pthread_cond_wait start\n");
-		r = pthread_cond_wait(&th->native_thread_data.sleep_cond,
-				      &th->interrupt_lock);
-                if (r) rb_bug_errno("pthread_cond_wait", r);
+		native_cond_wait(&th->native_thread_data.sleep_cond,
+				 &th->interrupt_lock);
 		thread_debug("native_sleep: pthread_cond_wait end\n");
 	    }
 	    else {
 		int r;
 		thread_debug("native_sleep: pthread_cond_timedwait start (%ld, %ld)\n",
 			     (unsigned long)ts.tv_sec, ts.tv_nsec);
-		r = pthread_cond_timedwait(&th->native_thread_data.sleep_cond,
-					   &th->interrupt_lock, &ts);
+		r = native_cond_timedwait(&th->native_thread_data.sleep_cond,
+					  &th->interrupt_lock, &ts);
 		if (r && r != ETIMEDOUT) rb_bug_errno("pthread_cond_timedwait", r);
 
 		thread_debug("native_sleep: pthread_cond_timedwait end (%d)\n", r);
@@ -895,7 +914,7 @@
 }
 
 static pthread_t timer_thread_id;
-static pthread_cond_t timer_thread_cond = PTHREAD_COND_INITIALIZER;
+static rb_thread_cond_t timer_thread_cond;
 static pthread_mutex_t timer_thread_lock = PTHREAD_MUTEX_INITIALIZER;
 
 static struct timespec *
@@ -956,6 +975,7 @@
 	int err;
 
 	pthread_attr_init(&attr);
+	native_cond_initialize(&timer_thread_cond, 0);
 #ifdef PTHREAD_STACK_MIN
 	pthread_attr_setstacksize(&attr,
 				  PTHREAD_STACK_MIN + (THREAD_DEBUG ? BUFSIZ : 0));
Index: thread_pthread.h
===================================================================
--- thread_pthread.h	(revision 31450)
+++ thread_pthread.h	(revision 31451)
@@ -17,12 +17,16 @@
 #endif
 typedef pthread_t rb_thread_id_t;
 typedef pthread_mutex_t rb_thread_lock_t;
-typedef pthread_cond_t rb_thread_cond_t;
 
+typedef struct rb_thread_cond_struct {
+    pthread_cond_t cond;
+    clockid_t clockid;
+} rb_thread_cond_t;
+
 typedef struct native_thread_data_struct {
     void *signal_thread_list;
-    pthread_cond_t sleep_cond;
-    pthread_cond_t gvl_cond;
+    rb_thread_cond_t sleep_cond;
+    rb_thread_cond_t gvl_cond;
     struct rb_thread_struct *gvl_next;
 } native_thread_data_t;
 
Index: thread.c
===================================================================
--- thread.c	(revision 31450)
+++ thread.c	(revision 31451)
@@ -3336,7 +3336,7 @@
 
     obj = TypedData_Make_Struct(klass, mutex_t, &mutex_data_type, mutex);
     native_mutex_initialize(&mutex->lock);
-    native_cond_initialize(&mutex->cond);
+    native_cond_initialize(&mutex->cond, 0);
     return obj;
 }
 

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

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