summaryrefslogtreecommitdiff
path: root/thread_pthread.c
diff options
context:
space:
mode:
authornobu <nobu@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2008-11-06 19:02:35 +0000
committernobu <nobu@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2008-11-06 19:02:35 +0000
commit4f3abb93f6ec6fe127da0adfadb1485d1b9ee9c4 (patch)
treea662aa7d1d239bfab87123210034574b7a3eff55 /thread_pthread.c
parenta324863b99e90303746742157e06c8d183db8257 (diff)
* thread.c (thread_timer): uses timedwait on cygwin.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@20123 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
Diffstat (limited to 'thread_pthread.c')
-rw-r--r--thread_pthread.c46
1 files changed, 42 insertions, 4 deletions
diff --git a/thread_pthread.c b/thread_pthread.c
index 08bacde8e9..601a060fc8 100644
--- a/thread_pthread.c
+++ b/thread_pthread.c
@@ -650,21 +650,51 @@ remove_signal_thread_list(rb_thread_t *th)
static pthread_t timer_thread_id;
+#ifdef __CYGWIN__
+#define TIMER_USE_TIMEDWAIT 1
+#endif
+#ifndef TIMER_USE_TIMEDWAIT
+#define TIMER_USE_TIMEDWAIT 0
+#endif
+#if TIMER_USE_TIMEDWAIT
+static pthread_cond_t timer_thread_cond = PTHREAD_COND_INITIALIZER;
+
+static struct timespec *
+get_ts(struct timespec *ts, unsigned long nsec)
+{
+ struct timeval tv;
+ gettimeofday(&tv, 0);
+ ts->tv_sec = tv.tv_sec;
+ ts->tv_nsec = tv.tv_usec * 1000 + nsec;
+ if (ts->tv_nsec >= PER_NANO) {
+ ts->tv_sec++;
+ ts->tv_nsec -= PER_NANO;
+ }
+ return ts;
+}
+#endif
+
static void *
thread_timer(void *dummy)
{
-#ifdef HAVE_NANOSLEEP
+#if TIMER_USE_TIMEDWAIT
+ struct timespec ts;
+ static pthread_mutex_t lock = PTHREAD_MUTEX_INITIALIZER;
+
+ pthread_mutex_lock(&lock);
+#define WAIT_FOR_10MS() (pthread_cond_timedwait(&timer_thread_cond, &lock, get_ts(&ts, PER_NANO/100)) == 0)
+#elif defined HAVE_NANOSLEEP
struct timespec req, rem;
req.tv_sec = 0;
req.tv_nsec = 10 * 1000 * 1000; /* 10 ms */
-#define WAIT_FOR_10MS() nanosleep(&req, &rem)
+#define WAIT_FOR_10MS() (nanosleep(&req, &rem) != -1)
#else
struct timeval tv;
tv.tv_sec = 0;
tv.tv_usec = 10000; /* 10 ms */
-#define WAIT_FOR_10MS() select(0, NULL, NULL, NULL, &tv)
+#define WAIT_FOR_10MS() (select(0, NULL, NULL, NULL, &tv) != -1)
#endif
- while (WAIT_FOR_10MS() != -1) {
+ while (WAIT_FOR_10MS()) {
#ifndef __CYGWIN__
if (signal_thread_list_anchor.next) {
FGLOCK(&signal_thread_list_lock, {
@@ -679,6 +709,10 @@ thread_timer(void *dummy)
#endif
timer_thread_function(dummy);
}
+#if TIMER_USE_TIMEDWAIT
+ pthread_mutex_unlock(&lock);
+ pthread_mutex_destroy(&lock);
+#endif
return NULL;
}
@@ -704,6 +738,10 @@ rb_thread_create_timer_thread(void)
rb_disable_interrupt(); /* only timer thread recieve signal */
}
+#if TIMER_USE_TIMEDWAIT
+#define native_stop_timer_thread() pthread_cond_signal(&timer_thread_cond)
+#else
#define native_stop_timer_thread() pthread_kill(timer_thread_id, SIGTERM)
+#endif
#endif /* THREAD_SYSTEM_DEPENDENT_IMPLEMENTATION */