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
|
|
|
|
2022-04-22 08:19:03 -04:00
|
|
|
// per-Thead scheduler helper data
|
|
|
|
struct rb_thread_sched_item {
|
2018-08-15 01:31:31 -04:00
|
|
|
union {
|
2022-03-30 03:36:31 -04:00
|
|
|
struct ccan_list_node ubf;
|
2022-04-16 14:40:23 -04:00
|
|
|
struct ccan_list_node readyq; // protected by sched->lock
|
2018-08-15 01:31:31 -04:00
|
|
|
} node;
|
2022-04-22 08:19:03 -04:00
|
|
|
};
|
|
|
|
|
|
|
|
struct rb_native_thread {
|
|
|
|
int id;
|
|
|
|
|
|
|
|
rb_nativethread_id_t thread_id;
|
|
|
|
|
|
|
|
#ifdef RB_THREAD_T_HAS_NATIVE_ID
|
|
|
|
int tid;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
struct rb_thread_struct *running_thread;
|
|
|
|
|
|
|
|
// to control native thread
|
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
|
2022-04-22 08:19:03 -04:00
|
|
|
{
|
2018-08-13 17:34:20 -04:00
|
|
|
rb_nativethread_cond_t intr; /* th->interrupt_lock */
|
2022-04-16 14:40:23 -04:00
|
|
|
rb_nativethread_cond_t readyq; /* use sched->lock */
|
2018-08-13 17:34:20 -04:00
|
|
|
} cond;
|
2022-05-24 03:39:45 -04:00
|
|
|
|
|
|
|
#ifdef USE_SIGALTSTACK
|
|
|
|
void *altstack;
|
|
|
|
#endif
|
2022-04-22 08:19:03 -04:00
|
|
|
};
|
2006-12-31 10:02:22 -05:00
|
|
|
|
2011-10-28 20:05:11 -04:00
|
|
|
#undef except
|
|
|
|
#undef try
|
|
|
|
#undef leave
|
|
|
|
#undef finally
|
|
|
|
|
2022-04-16 14:40:23 -04:00
|
|
|
// per-Ractor
|
|
|
|
struct rb_thread_sched {
|
2011-06-13 10:14:53 -04:00
|
|
|
/* fast path */
|
2022-04-16 14:40:23 -04:00
|
|
|
|
|
|
|
const struct rb_thread_struct *running; // running thread or NULL
|
|
|
|
rb_nativethread_lock_t lock;
|
2011-06-13 10:14:53 -04:00
|
|
|
|
2018-08-27 20:24:08 -04:00
|
|
|
/*
|
2022-04-16 14:40:23 -04:00
|
|
|
* slow path, protected by ractor->thread_sched->lock
|
|
|
|
* - @readyq - FIFO queue of threads waiting for running
|
|
|
|
* - @timer - it handles timeslices for @current. It is any one thread
|
2018-08-27 20:24:08 -04:00
|
|
|
* 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.
|
|
|
|
*/
|
2022-04-16 14:40:23 -04:00
|
|
|
struct ccan_list_head readyq;
|
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;
|
2022-04-16 14:40:23 -04:00
|
|
|
};
|
2020-10-19 03:47:32 -04:00
|
|
|
|
|
|
|
#if __STDC_VERSION__ >= 201112
|
|
|
|
#define RB_THREAD_LOCAL_SPECIFIER _Thread_local
|
2022-05-22 03:49:48 -04:00
|
|
|
#elif defined(__GNUC__) && !defined(RB_THREAD_LOCAL_SPECIFIER_IS_UNSUPPORTED)
|
2020-10-19 03:47:32 -04:00
|
|
|
/* 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)
|
|
|
|
{
|
2022-05-23 15:54:26 -04:00
|
|
|
// return value should be checked by caller
|
|
|
|
return pthread_getspecific(key);
|
2020-03-09 13:22:11 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
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
|
2021-04-29 08:31:05 -04:00
|
|
|
#ifdef __APPLE__
|
2020-10-19 03:47:32 -04:00
|
|
|
// on Darwin, TLS can not be accessed across .so
|
2021-05-06 11:04:36 -04:00
|
|
|
struct rb_execution_context_struct *rb_current_ec(void);
|
2020-10-19 03:47:32 -04:00
|
|
|
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 */
|