2020-04-10 01:11:40 -04:00
|
|
|
#ifndef RUBY_THREAD_PTHREAD_H
|
|
|
|
#define RUBY_THREAD_PTHREAD_H
|
2006-12-31 10:02:22 -05:00
|
|
|
/**********************************************************************
|
|
|
|
|
|
|
|
thread_pthread.h -
|
|
|
|
|
|
|
|
$Author$
|
|
|
|
|
* blockinlining.c, compile.c, compile.h, debug.c, debug.h,
id.c, insnhelper.h, insns.def, thread.c, thread_pthread.ci,
thread_pthread.h, thread_win32.ci, thread_win32.h, vm.h,
vm_dump.c, vm_evalbody.ci, vm_opts.h: fix comments and
copyright year.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@13920 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2007-11-13 17:13:04 -05:00
|
|
|
Copyright (C) 2004-2007 Koichi Sasada
|
2006-12-31 10:02:22 -05:00
|
|
|
|
|
|
|
**********************************************************************/
|
|
|
|
|
2010-02-04 02:17:03 -05:00
|
|
|
#ifdef HAVE_PTHREAD_NP_H
|
|
|
|
#include <pthread_np.h>
|
|
|
|
#endif
|
2011-05-06 12:47:38 -04:00
|
|
|
|
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
2014-08-14 20:17:53 -04:00
|
|
|
#define RB_NATIVETHREAD_LOCK_INIT PTHREAD_MUTEX_INITIALIZER
|
2018-04-22 08:09:07 -04:00
|
|
|
#define RB_NATIVETHREAD_COND_INIT PTHREAD_COND_INITIALIZER
|
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
2014-08-14 20:17:53 -04:00
|
|
|
|
2006-12-31 10:02:22 -05:00
|
|
|
typedef struct native_thread_data_struct {
|
2018-08-15 01:31:31 -04:00
|
|
|
union {
|
|
|
|
struct list_node ubf;
|
|
|
|
struct list_node gvl;
|
|
|
|
} node;
|
2018-08-13 17:34:20 -04:00
|
|
|
#if defined(__GLIBC__) || defined(__FreeBSD__)
|
|
|
|
union
|
|
|
|
#else
|
|
|
|
/*
|
|
|
|
* assume the platform condvars are badly implemented and have a
|
|
|
|
* "memory" of which mutex they're associated with
|
|
|
|
*/
|
|
|
|
struct
|
|
|
|
#endif
|
|
|
|
{
|
|
|
|
rb_nativethread_cond_t intr; /* th->interrupt_lock */
|
|
|
|
rb_nativethread_cond_t gvlq; /* vm->gvl.lock */
|
|
|
|
} cond;
|
2006-12-31 10:02:22 -05:00
|
|
|
} native_thread_data_t;
|
|
|
|
|
2011-10-28 20:05:11 -04:00
|
|
|
#undef except
|
|
|
|
#undef try
|
|
|
|
#undef leave
|
|
|
|
#undef finally
|
|
|
|
|
2010-11-27 15:15:59 -05:00
|
|
|
typedef struct rb_global_vm_lock_struct {
|
2011-06-13 10:14:53 -04:00
|
|
|
/* fast path */
|
2018-08-27 20:24:08 -04:00
|
|
|
const struct rb_thread_struct *owner;
|
|
|
|
rb_nativethread_lock_t lock; /* AKA vm->gvl.lock */
|
2011-06-13 10:14:53 -04:00
|
|
|
|
2018-08-27 20:24:08 -04:00
|
|
|
/*
|
|
|
|
* slow path, protected by vm->gvl.lock
|
|
|
|
* - @waitq - FIFO queue of threads waiting for GVL
|
|
|
|
* - @timer - it handles timeslices for @owner. It is any one thread
|
|
|
|
* in @waitq, there is no @timer if @waitq is empty, but always
|
|
|
|
* a @timer if @waitq has entries
|
|
|
|
* - @timer_err tracks timeslice limit, the timeslice only resets
|
|
|
|
* when pthread_cond_timedwait returns ETIMEDOUT, so frequent
|
|
|
|
* switching between contended/uncontended GVL won't reset the
|
|
|
|
* timer.
|
|
|
|
*/
|
|
|
|
struct list_head waitq; /* <=> native_thread_data_t.node.ubf */
|
2018-08-13 17:34:20 -04:00
|
|
|
const struct rb_thread_struct *timer;
|
2018-08-18 20:01:08 -04:00
|
|
|
int timer_err;
|
2011-06-13 10:14:53 -04:00
|
|
|
|
|
|
|
/* yield */
|
2013-07-23 06:50:32 -04:00
|
|
|
rb_nativethread_cond_t switch_cond;
|
|
|
|
rb_nativethread_cond_t switch_wait_cond;
|
2011-06-14 12:31:23 -04:00
|
|
|
int need_yield;
|
|
|
|
int wait_yield;
|
2010-11-27 15:15:59 -05:00
|
|
|
} rb_global_vm_lock_t;
|
|
|
|
|
2020-10-19 03:47:32 -04:00
|
|
|
|
|
|
|
#if __STDC_VERSION__ >= 201112
|
|
|
|
#define RB_THREAD_LOCAL_SPECIFIER _Thread_local
|
|
|
|
#elif defined(__GNUC__)
|
|
|
|
/* note that ICC (linux) and Clang are covered by __GNUC__ */
|
|
|
|
#define RB_THREAD_LOCAL_SPECIFIER __thread
|
|
|
|
#else
|
|
|
|
|
2020-03-09 13:22:11 -04:00
|
|
|
typedef pthread_key_t native_tls_key_t;
|
|
|
|
|
|
|
|
static inline void *
|
|
|
|
native_tls_get(native_tls_key_t key)
|
|
|
|
{
|
|
|
|
void *ptr = pthread_getspecific(key);
|
|
|
|
if (UNLIKELY(ptr == NULL)) {
|
|
|
|
rb_bug("pthread_getspecific returns NULL");
|
|
|
|
}
|
|
|
|
return ptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline void
|
|
|
|
native_tls_set(native_tls_key_t key, void *ptr)
|
|
|
|
{
|
|
|
|
if (UNLIKELY(pthread_setspecific(key, ptr) != 0)) {
|
|
|
|
rb_bug("pthread_setspecific error");
|
|
|
|
}
|
|
|
|
}
|
2020-10-19 03:47:32 -04:00
|
|
|
#endif
|
|
|
|
|
|
|
|
RUBY_SYMBOL_EXPORT_BEGIN
|
|
|
|
#ifdef RB_THREAD_LOCAL_SPECIFIER
|
|
|
|
#if __APPLE__
|
|
|
|
// on Darwin, TLS can not be accessed across .so
|
|
|
|
struct rb_execution_context_struct *rb_current_ec();
|
|
|
|
void rb_current_ec_set(struct rb_execution_context_struct *);
|
|
|
|
#else
|
|
|
|
RUBY_EXTERN RB_THREAD_LOCAL_SPECIFIER struct rb_execution_context_struct *ruby_current_ec;
|
|
|
|
#endif
|
|
|
|
#else
|
|
|
|
RUBY_EXTERN native_tls_key_t ruby_current_ec_key;
|
|
|
|
#endif
|
|
|
|
RUBY_SYMBOL_EXPORT_END
|
2020-03-09 13:22:11 -04:00
|
|
|
|
2008-01-18 03:56:11 -05:00
|
|
|
#endif /* RUBY_THREAD_PTHREAD_H */
|