diff options
author | Ian Lance Taylor <ian@gcc.gnu.org> | 2011-11-29 21:58:48 +0000 |
---|---|---|
committer | Ian Lance Taylor <ian@gcc.gnu.org> | 2011-11-29 21:58:48 +0000 |
commit | 08ee945e0b945fd2797c8bb7524d79dc73a9b6bd (patch) | |
tree | e7665ea556f238ad5c34dff64c5b1839162dc2ae /libgo/runtime/thread-sema.c | |
parent | 85b8555ed3c45855b237b0f3a044eefb9382255c (diff) | |
download | gcc-08ee945e0b945fd2797c8bb7524d79dc73a9b6bd.zip gcc-08ee945e0b945fd2797c8bb7524d79dc73a9b6bd.tar.gz gcc-08ee945e0b945fd2797c8bb7524d79dc73a9b6bd.tar.bz2 |
runtime: If no sem_timedwait, use pthread_cond_timedwait.
From-SVN: r181821
Diffstat (limited to 'libgo/runtime/thread-sema.c')
-rw-r--r-- | libgo/runtime/thread-sema.c | 87 |
1 files changed, 77 insertions, 10 deletions
diff --git a/libgo/runtime/thread-sema.c b/libgo/runtime/thread-sema.c index b0a6dc3..71555d0 100644 --- a/libgo/runtime/thread-sema.c +++ b/libgo/runtime/thread-sema.c @@ -2,6 +2,7 @@ // Use of this source code is governed by a BSD-style // license that can be found in the LICENSE file. +#include "config.h" #include "runtime.h" #include <errno.h> @@ -9,19 +10,43 @@ #include <time.h> #include <semaphore.h> +/* If we don't have sem_timedwait, use pthread_cond_timedwait instead. + We don't always use condition variables because on some systems + pthread_mutex_lock and pthread_mutex_unlock must be called by the + same thread. That is never true of semaphores. */ + +struct go_sem +{ + sem_t sem; + +#ifndef HAVE_SEM_TIMEDWAIT + int timedwait; + pthread_mutex_t mutex; + pthread_cond_t cond; +#endif +}; + /* Create a semaphore. */ uintptr runtime_semacreate(void) { - sem_t *p; + struct go_sem *p; /* Call malloc rather than runtime_malloc. This will allocate space on the C heap. We can't call runtime_malloc here because it could cause a deadlock. */ - p = malloc (sizeof (sem_t)); - if (sem_init (p, 0, 0) != 0) + p = malloc (sizeof (struct go_sem)); + if (sem_init (&p->sem, 0, 0) != 0) runtime_throw ("sem_init"); + +#ifndef HAVE_SEM_TIMEDWAIT + if (pthread_mutex_init (&p->mutex, NULL) != 0) + runtime_throw ("pthread_mutex_init"); + if (pthread_cond_init (&p->cond, NULL) != 0) + runtime_throw ("pthread_cond_init"); +#endif + return (uintptr) p; } @@ -30,26 +55,56 @@ runtime_semacreate(void) int32 runtime_semasleep (int64 ns) { + M *m; + struct go_sem *sem; int r; + m = runtime_m (); + sem = (struct go_sem *) m->waitsema; if (ns >= 0) { + int64 abs; struct timespec ts; + int err; + + abs = ns + runtime_nanotime (); + ts.tv_sec = abs / 1000000000LL; + ts.tv_nsec = abs % 1000000000LL; + + err = 0; - ns += runtime_nanotime (); - ts.tv_sec = ns / 1000000000LL; - ts.tv_nsec = ns % 1000000000LL; - r = sem_timedwait ((sem_t *) m->waitsema, &ts); +#ifdef HAVE_SEM_TIMEDWAIT + r = sem_timedwait (&sem->sem, &ts); if (r != 0) + err = errno; +#else + if (pthread_mutex_lock (&sem->mutex) != 0) + runtime_throw ("pthread_mutex_lock"); + + while ((r = sem_trywait (&sem->sem)) != 0) { - if (errno == ETIMEDOUT || errno == EINTR) + r = pthread_cond_timedwait (&sem->cond, &sem->mutex, &ts); + if (r != 0) + { + err = r; + break; + } + } + + if (pthread_mutex_unlock (&sem->mutex) != 0) + runtime_throw ("pthread_mutex_unlock"); +#endif + + if (err != 0) + { + if (err == ETIMEDOUT || err == EAGAIN || err == EINTR) return -1; runtime_throw ("sema_timedwait"); } return 0; } - while (sem_wait ((sem_t *) m->waitsema) != 0) + while (sem_wait (&sem->sem) != 0) { if (errno == EINTR) continue; @@ -64,8 +119,20 @@ runtime_semasleep (int64 ns) void runtime_semawakeup (M *mp) { - if (sem_post ((sem_t *) mp->waitsema) != 0) + struct go_sem *sem; + + sem = (struct go_sem *) mp->waitsema; + if (sem_post (&sem->sem) != 0) runtime_throw ("sem_post"); + +#ifndef HAVE_SEM_TIMEDWAIT + if (pthread_mutex_lock (&sem->mutex) != 0) + runtime_throw ("pthread_mutex_lock"); + if (pthread_cond_broadcast (&sem->cond) != 0) + runtime_throw ("pthread_cond_broadcast"); + if (pthread_mutex_unlock (&sem->mutex) != 0) + runtime_throw ("pthread_mutex_unlock"); +#endif } void |