thread_pthread: prefer rb_nativethread* types/functions

This will make it easier for us to try alternative mutex/condvar
implementations while still using pthreads for thread management.
[Feature #10134]

* thread_pthread.h: define RB_NATIVETHREAD_LOCK_INIT and
  RB_NATIVETHREAD_COND_INIT macros

* thread_pthread.c (native_mutex_lock, native_mutex_unlock,
  native_mutex_trylock, native_mutex_initialize,
  native_mutex_destroy, native_cond_wait):
  use rb_nativethread_lock_t instead of pthread_mutex_t

* thread_pthread.c (native_mutex_debug): make argument type-agnostic
  to avoid later cast.

* thread_pthread.c (register_cached_thread_and_wait):
  replace PTHREAD_COND_INITIALIZER with RB_NATIVETHREAD_COND_INIT,
  use native_mutex_{lock,unlock}

* thread_pthread.c (use_cached_thread):
  use native_mutex_{lock,unlock}

* thread_pthread.c (native_sleep):
  use rb_nativethread_lock_t to match th->interrupt_lock,
  use native_mutex_{lock,unlock}

* thread_pthread.c (timer_thread_lock): use rb_nativethread_lock_t type

git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@47185 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
This commit is contained in:
normal 2014-08-15 00:17:53 +00:00
Родитель bef5ddbb71
Коммит fdb2fc0b04
3 изменённых файлов: 48 добавлений и 18 удалений

Просмотреть файл

@ -1,3 +1,30 @@
Fri Aug 15 09:06:31 2014 Eric Wong <e@80x24.org>
* thread_pthread.h: define RB_NATIVETHREAD_LOCK_INIT and
RB_NATIVETHREAD_COND_INIT macros
* thread_pthread.c (native_mutex_lock, native_mutex_unlock,
native_mutex_trylock, native_mutex_initialize,
native_mutex_destroy, native_cond_wait):
use rb_nativethread_lock_t instead of pthread_mutex_t
[Feature #10134]
* thread_pthread.c (native_mutex_debug): make argument type-agnostic
to avoid later cast.
* thread_pthread.c (register_cached_thread_and_wait):
replace PTHREAD_COND_INITIALIZER with RB_NATIVETHREAD_COND_INIT,
use native_mutex_{lock,unlock}
* thread_pthread.c (use_cached_thread):
use native_mutex_{lock,unlock}
* thread_pthread.c (native_sleep):
use rb_nativethread_lock_t to match th->interrupt_lock,
use native_mutex_{lock,unlock}
* thread_pthread.c (timer_thread_lock): use rb_nativethread_lock_t type
Fri Aug 15 08:10:29 2014 Eric Wong <e@80x24.org> Fri Aug 15 08:10:29 2014 Eric Wong <e@80x24.org>
* cont.c (cont_mark): fix typo in unused path [ci skip] * cont.c (cont_mark): fix typo in unused path [ci skip]

Просмотреть файл

@ -34,14 +34,14 @@
#include <sys/time.h> #include <sys/time.h>
#endif #endif
static void native_mutex_lock(pthread_mutex_t *lock); static void native_mutex_lock(rb_nativethread_lock_t *lock);
static void native_mutex_unlock(pthread_mutex_t *lock); static void native_mutex_unlock(rb_nativethread_lock_t *lock);
static int native_mutex_trylock(pthread_mutex_t *lock); static int native_mutex_trylock(rb_nativethread_lock_t *lock);
static void native_mutex_initialize(pthread_mutex_t *lock); static void native_mutex_initialize(rb_nativethread_lock_t *lock);
static void native_mutex_destroy(pthread_mutex_t *lock); static void native_mutex_destroy(rb_nativethread_lock_t *lock);
static void native_cond_signal(rb_nativethread_cond_t *cond); static void native_cond_signal(rb_nativethread_cond_t *cond);
static void native_cond_broadcast(rb_nativethread_cond_t *cond); static void native_cond_broadcast(rb_nativethread_cond_t *cond);
static void native_cond_wait(rb_nativethread_cond_t *cond, pthread_mutex_t *mutex); static void native_cond_wait(rb_nativethread_cond_t *cond, rb_nativethread_lock_t *mutex);
static void native_cond_initialize(rb_nativethread_cond_t *cond, int flags); static void native_cond_initialize(rb_nativethread_cond_t *cond, int flags);
static void native_cond_destroy(rb_nativethread_cond_t *cond); static void native_cond_destroy(rb_nativethread_cond_t *cond);
static void rb_thread_wakeup_timer_thread_low(void); static void rb_thread_wakeup_timer_thread_low(void);
@ -188,14 +188,14 @@ gvl_atfork(rb_vm_t *vm)
#define NATIVE_MUTEX_LOCK_DEBUG 0 #define NATIVE_MUTEX_LOCK_DEBUG 0
static void static void
mutex_debug(const char *msg, pthread_mutex_t *lock) mutex_debug(const char *msg, void *lock)
{ {
if (NATIVE_MUTEX_LOCK_DEBUG) { if (NATIVE_MUTEX_LOCK_DEBUG) {
int r; int r;
static pthread_mutex_t dbglock = PTHREAD_MUTEX_INITIALIZER; static pthread_mutex_t dbglock = PTHREAD_MUTEX_INITIALIZER;
if ((r = pthread_mutex_lock(&dbglock)) != 0) {exit(EXIT_FAILURE);} if ((r = pthread_mutex_lock(&dbglock)) != 0) {exit(EXIT_FAILURE);}
fprintf(stdout, "%s: %p\n", msg, (void *)lock); fprintf(stdout, "%s: %p\n", msg, lock);
if ((r = pthread_mutex_unlock(&dbglock)) != 0) {exit(EXIT_FAILURE);} if ((r = pthread_mutex_unlock(&dbglock)) != 0) {exit(EXIT_FAILURE);}
} }
} }
@ -833,13 +833,13 @@ struct cached_thread_entry {
#if USE_THREAD_CACHE #if USE_THREAD_CACHE
static pthread_mutex_t thread_cache_lock = PTHREAD_MUTEX_INITIALIZER; static rb_nativethread_lock_t thread_cache_lock = RB_NATIVETHREAD_LOCK_INIT;
struct cached_thread_entry *cached_thread_root; struct cached_thread_entry *cached_thread_root;
static rb_thread_t * static rb_thread_t *
register_cached_thread_and_wait(void) register_cached_thread_and_wait(void)
{ {
rb_nativethread_cond_t cond = { PTHREAD_COND_INITIALIZER, }; rb_nativethread_cond_t cond = RB_NATIVETHREAD_COND_INIT;
volatile rb_thread_t *th_area = 0; volatile rb_thread_t *th_area = 0;
struct timeval tv; struct timeval tv;
struct timespec ts; struct timespec ts;
@ -854,7 +854,7 @@ register_cached_thread_and_wait(void)
ts.tv_sec = tv.tv_sec + 60; ts.tv_sec = tv.tv_sec + 60;
ts.tv_nsec = tv.tv_usec * 1000; ts.tv_nsec = tv.tv_usec * 1000;
pthread_mutex_lock(&thread_cache_lock); native_mutex_lock(&thread_cache_lock);
{ {
entry->th_area = &th_area; entry->th_area = &th_area;
entry->cond = &cond; entry->cond = &cond;
@ -878,7 +878,7 @@ register_cached_thread_and_wait(void)
free(entry); /* ok */ free(entry); /* ok */
native_cond_destroy(&cond); native_cond_destroy(&cond);
} }
pthread_mutex_unlock(&thread_cache_lock); native_mutex_unlock(&thread_cache_lock);
return (rb_thread_t *)th_area; return (rb_thread_t *)th_area;
} }
@ -892,7 +892,7 @@ use_cached_thread(rb_thread_t *th)
struct cached_thread_entry *entry; struct cached_thread_entry *entry;
if (cached_thread_root) { if (cached_thread_root) {
pthread_mutex_lock(&thread_cache_lock); native_mutex_lock(&thread_cache_lock);
entry = cached_thread_root; entry = cached_thread_root;
{ {
if (cached_thread_root) { if (cached_thread_root) {
@ -904,7 +904,7 @@ use_cached_thread(rb_thread_t *th)
if (result) { if (result) {
native_cond_signal(entry->cond); native_cond_signal(entry->cond);
} }
pthread_mutex_unlock(&thread_cache_lock); native_mutex_unlock(&thread_cache_lock);
} }
#endif #endif
return result; return result;
@ -1027,7 +1027,7 @@ static void
native_sleep(rb_thread_t *th, struct timeval *timeout_tv) native_sleep(rb_thread_t *th, struct timeval *timeout_tv)
{ {
struct timespec timeout; struct timespec timeout;
pthread_mutex_t *lock = &th->interrupt_lock; rb_nativethread_lock_t *lock = &th->interrupt_lock;
rb_nativethread_cond_t *cond = &th->native_thread_data.sleep_cond; rb_nativethread_cond_t *cond = &th->native_thread_data.sleep_cond;
if (timeout_tv) { if (timeout_tv) {
@ -1054,7 +1054,7 @@ native_sleep(rb_thread_t *th, struct timeval *timeout_tv)
GVL_UNLOCK_BEGIN(); GVL_UNLOCK_BEGIN();
{ {
pthread_mutex_lock(lock); native_mutex_lock(lock);
th->unblock.func = ubf_pthread_cond_signal; th->unblock.func = ubf_pthread_cond_signal;
th->unblock.arg = th; th->unblock.arg = th;
@ -1071,7 +1071,7 @@ native_sleep(rb_thread_t *th, struct timeval *timeout_tv)
th->unblock.func = 0; th->unblock.func = 0;
th->unblock.arg = 0; th->unblock.arg = 0;
pthread_mutex_unlock(lock); native_mutex_unlock(lock);
} }
GVL_UNLOCK_END(); GVL_UNLOCK_END();
@ -1425,7 +1425,7 @@ timer_thread_sleep(rb_global_vm_lock_t* gvl)
void rb_thread_wakeup_timer_thread(void) {} void rb_thread_wakeup_timer_thread(void) {}
static void rb_thread_wakeup_timer_thread_low(void) {} static void rb_thread_wakeup_timer_thread_low(void) {}
static pthread_mutex_t timer_thread_lock; static rb_nativethread_lock_t timer_thread_lock;
static rb_nativethread_cond_t timer_thread_cond; static rb_nativethread_cond_t timer_thread_cond;
static inline void static inline void

Просмотреть файл

@ -15,6 +15,9 @@
#include <pthread_np.h> #include <pthread_np.h>
#endif #endif
#define RB_NATIVETHREAD_LOCK_INIT PTHREAD_MUTEX_INITIALIZER
#define RB_NATIVETHREAD_COND_INIT { PTHREAD_COND_INITIALIZER, }
typedef struct rb_thread_cond_struct { typedef struct rb_thread_cond_struct {
pthread_cond_t cond; pthread_cond_t cond;
#ifdef HAVE_CLOCKID_T #ifdef HAVE_CLOCKID_T