ruby-changes:35103
From: normal <ko1@a...>
Date: Fri, 15 Aug 2014 09:17:59 +0900 (JST)
Subject: [ruby-changes:35103] normal:r47185 (trunk): thread_pthread: prefer rb_nativethread* types/functions
normal 2014-08-15 09:17:53 +0900 (Fri, 15 Aug 2014) New Revision: 47185 http://svn.ruby-lang.org/cgi-bin/viewvc.cgi?view=revision&revision=47185 Log: 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 Modified files: trunk/ChangeLog trunk/thread_pthread.c trunk/thread_pthread.h Index: ChangeLog =================================================================== --- ChangeLog (revision 47184) +++ ChangeLog (revision 47185) @@ -1,3 +1,30 @@ https://github.com/ruby/ruby/blob/trunk/ChangeLog#L1 +Fri Aug 15 09:06:31 2014 Eric Wong <e@8...> + + * 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@8...> * cont.c (cont_mark): fix typo in unused path [ci skip] Index: thread_pthread.c =================================================================== --- thread_pthread.c (revision 47184) +++ thread_pthread.c (revision 47185) @@ -34,14 +34,14 @@ https://github.com/ruby/ruby/blob/trunk/thread_pthread.c#L34 #include <sys/time.h> #endif -static void native_mutex_lock(pthread_mutex_t *lock); -static void native_mutex_unlock(pthread_mutex_t *lock); -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_mutex_lock(rb_nativethread_lock_t *lock); +static void native_mutex_unlock(rb_nativethread_lock_t *lock); +static int native_mutex_trylock(rb_nativethread_lock_t *lock); +static void native_mutex_initialize(rb_nativethread_lock_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_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_destroy(rb_nativethread_cond_t *cond); static void rb_thread_wakeup_timer_thread_low(void); @@ -188,14 +188,14 @@ gvl_atfork(rb_vm_t *vm) https://github.com/ruby/ruby/blob/trunk/thread_pthread.c#L188 #define NATIVE_MUTEX_LOCK_DEBUG 0 static void -mutex_debug(const char *msg, pthread_mutex_t *lock) +mutex_debug(const char *msg, void *lock) { if (NATIVE_MUTEX_LOCK_DEBUG) { int r; static pthread_mutex_t dbglock = PTHREAD_MUTEX_INITIALIZER; 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);} } } @@ -833,13 +833,13 @@ struct cached_thread_entry { https://github.com/ruby/ruby/blob/trunk/thread_pthread.c#L833 #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; static rb_thread_t * 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; struct timeval tv; struct timespec ts; @@ -854,7 +854,7 @@ register_cached_thread_and_wait(void) https://github.com/ruby/ruby/blob/trunk/thread_pthread.c#L854 ts.tv_sec = tv.tv_sec + 60; 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->cond = &cond; @@ -878,7 +878,7 @@ register_cached_thread_and_wait(void) https://github.com/ruby/ruby/blob/trunk/thread_pthread.c#L878 free(entry); /* ok */ native_cond_destroy(&cond); } - pthread_mutex_unlock(&thread_cache_lock); + native_mutex_unlock(&thread_cache_lock); return (rb_thread_t *)th_area; } @@ -892,7 +892,7 @@ use_cached_thread(rb_thread_t *th) https://github.com/ruby/ruby/blob/trunk/thread_pthread.c#L892 struct cached_thread_entry *entry; if (cached_thread_root) { - pthread_mutex_lock(&thread_cache_lock); + native_mutex_lock(&thread_cache_lock); entry = cached_thread_root; { if (cached_thread_root) { @@ -904,7 +904,7 @@ use_cached_thread(rb_thread_t *th) https://github.com/ruby/ruby/blob/trunk/thread_pthread.c#L904 if (result) { native_cond_signal(entry->cond); } - pthread_mutex_unlock(&thread_cache_lock); + native_mutex_unlock(&thread_cache_lock); } #endif return result; @@ -1027,7 +1027,7 @@ static void https://github.com/ruby/ruby/blob/trunk/thread_pthread.c#L1027 native_sleep(rb_thread_t *th, struct timeval *timeout_tv) { 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; if (timeout_tv) { @@ -1054,7 +1054,7 @@ native_sleep(rb_thread_t *th, struct tim https://github.com/ruby/ruby/blob/trunk/thread_pthread.c#L1054 GVL_UNLOCK_BEGIN(); { - pthread_mutex_lock(lock); + native_mutex_lock(lock); th->unblock.func = ubf_pthread_cond_signal; th->unblock.arg = th; @@ -1071,7 +1071,7 @@ native_sleep(rb_thread_t *th, struct tim https://github.com/ruby/ruby/blob/trunk/thread_pthread.c#L1071 th->unblock.func = 0; th->unblock.arg = 0; - pthread_mutex_unlock(lock); + native_mutex_unlock(lock); } GVL_UNLOCK_END(); @@ -1425,7 +1425,7 @@ timer_thread_sleep(rb_global_vm_lock_t* https://github.com/ruby/ruby/blob/trunk/thread_pthread.c#L1425 void rb_thread_wakeup_timer_thread(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 inline void Index: thread_pthread.h =================================================================== --- thread_pthread.h (revision 47184) +++ thread_pthread.h (revision 47185) @@ -15,6 +15,9 @@ https://github.com/ruby/ruby/blob/trunk/thread_pthread.h#L15 #include <pthread_np.h> #endif +#define RB_NATIVETHREAD_LOCK_INIT PTHREAD_MUTEX_INITIALIZER +#define RB_NATIVETHREAD_COND_INIT { PTHREAD_COND_INITIALIZER, } + typedef struct rb_thread_cond_struct { pthread_cond_t cond; #ifdef HAVE_CLOCKID_T -- ML: ruby-changes@q... Info: http://www.atdot.net/~ko1/quickml/