2006-12-31 10:02:22 -05:00
|
|
|
/* -*-c-*- */
|
|
|
|
/**********************************************************************
|
|
|
|
|
2007-12-20 04:29:46 -05:00
|
|
|
thread_pthread.c -
|
2006-12-31 10:02:22 -05:00
|
|
|
|
|
|
|
$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
|
|
|
|
|
|
|
**********************************************************************/
|
|
|
|
|
|
|
|
#ifdef THREAD_SYSTEM_DEPENDENT_IMPLEMENTATION
|
|
|
|
|
2008-06-13 22:59:19 -04:00
|
|
|
#include "gc.h"
|
|
|
|
|
|
|
|
#ifdef HAVE_SYS_RESOURCE_H
|
|
|
|
#include <sys/resource.h>
|
|
|
|
#endif
|
2011-06-15 20:12:55 -04:00
|
|
|
#ifdef HAVE_THR_STKSEGMENT
|
|
|
|
#include <thread.h>
|
|
|
|
#endif
|
2011-06-26 20:30:41 -04:00
|
|
|
#if HAVE_FCNTL_H
|
|
|
|
#include <fcntl.h>
|
|
|
|
#elif HAVE_SYS_FCNTL_H
|
|
|
|
#include <sys/fcntl.h>
|
|
|
|
#endif
|
2012-07-19 14:43:24 -04:00
|
|
|
#ifdef HAVE_SYS_PRCTL_H
|
2011-11-30 17:17:48 -05:00
|
|
|
#include <sys/prctl.h>
|
|
|
|
#endif
|
2012-05-16 22:48:59 -04:00
|
|
|
#if defined(__native_client__) && defined(NACL_NEWLIB)
|
|
|
|
# include "nacl/select.h"
|
|
|
|
#endif
|
2013-03-16 01:06:47 -04:00
|
|
|
#if defined(HAVE_SYS_TIME_H)
|
|
|
|
#include <sys/time.h>
|
|
|
|
#endif
|
2008-06-13 22:59:19 -04:00
|
|
|
|
2007-12-24 23:35:17 -05:00
|
|
|
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);
|
2013-07-23 06:50:32 -04:00
|
|
|
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_initialize(rb_nativethread_cond_t *cond, int flags);
|
|
|
|
static void native_cond_destroy(rb_nativethread_cond_t *cond);
|
2013-03-09 23:00:21 -05:00
|
|
|
static void rb_thread_wakeup_timer_thread_low(void);
|
2011-12-13 21:26:20 -05:00
|
|
|
static pthread_t timer_thread_id;
|
2007-12-24 23:35:17 -05:00
|
|
|
|
2011-05-06 13:50:23 -04:00
|
|
|
#define RB_CONDATTR_CLOCK_MONOTONIC 1
|
|
|
|
|
2011-05-07 05:28:43 -04:00
|
|
|
#if defined(HAVE_PTHREAD_CONDATTR_SETCLOCK) && defined(HAVE_CLOCKID_T) && \
|
2012-09-20 10:15:46 -04:00
|
|
|
defined(CLOCK_REALTIME) && defined(CLOCK_MONOTONIC) && \
|
|
|
|
defined(HAVE_CLOCK_GETTIME) && defined(HAVE_PTHREAD_CONDATTR_INIT)
|
2011-05-06 12:47:38 -04:00
|
|
|
#define USE_MONOTONIC_COND 1
|
2011-05-07 05:28:43 -04:00
|
|
|
#else
|
|
|
|
#define USE_MONOTONIC_COND 0
|
2011-05-06 12:47:38 -04:00
|
|
|
#endif
|
2007-12-24 23:35:17 -05:00
|
|
|
|
2013-03-09 22:59:29 -05:00
|
|
|
#if defined(HAVE_POLL) && defined(HAVE_FCNTL) && defined(F_GETFL) && defined(F_SETFL) && defined(O_NONBLOCK) && !defined(__native_client__)
|
2012-06-10 08:51:37 -04:00
|
|
|
/* The timer thread sleeps while only one Ruby thread is running. */
|
|
|
|
# define USE_SLEEPY_TIMER_THREAD 1
|
2013-03-09 22:59:19 -05:00
|
|
|
#else
|
|
|
|
# define USE_SLEEPY_TIMER_THREAD 0
|
2012-06-10 08:51:37 -04:00
|
|
|
#endif
|
|
|
|
|
2010-11-27 15:15:59 -05:00
|
|
|
static void
|
2011-06-27 08:18:00 -04:00
|
|
|
gvl_acquire_common(rb_vm_t *vm)
|
2010-11-27 15:15:59 -05:00
|
|
|
{
|
2011-06-13 10:14:53 -04:00
|
|
|
if (vm->gvl.acquired) {
|
2011-06-26 20:30:41 -04:00
|
|
|
|
2011-06-13 10:14:53 -04:00
|
|
|
vm->gvl.waiting++;
|
2011-06-26 20:30:41 -04:00
|
|
|
if (vm->gvl.waiting == 1) {
|
2013-03-09 23:00:21 -05:00
|
|
|
/*
|
|
|
|
* Wake up timer thread iff timer thread is slept.
|
|
|
|
* When timer thread is polling mode, we don't want to
|
|
|
|
* make confusing timer thread interval time.
|
|
|
|
*/
|
|
|
|
rb_thread_wakeup_timer_thread_low();
|
2011-06-26 20:30:41 -04:00
|
|
|
}
|
|
|
|
|
2011-06-13 10:14:53 -04:00
|
|
|
while (vm->gvl.acquired) {
|
|
|
|
native_cond_wait(&vm->gvl.cond, &vm->gvl.lock);
|
|
|
|
}
|
2011-06-26 20:30:41 -04:00
|
|
|
|
2011-06-13 10:14:53 -04:00
|
|
|
vm->gvl.waiting--;
|
|
|
|
|
|
|
|
if (vm->gvl.need_yield) {
|
|
|
|
vm->gvl.need_yield = 0;
|
|
|
|
native_cond_signal(&vm->gvl.switch_cond);
|
|
|
|
}
|
2010-11-27 15:15:59 -05:00
|
|
|
}
|
2011-06-13 10:14:53 -04:00
|
|
|
|
|
|
|
vm->gvl.acquired = 1;
|
2010-11-27 15:15:59 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2011-06-13 10:14:53 -04:00
|
|
|
gvl_acquire(rb_vm_t *vm, rb_thread_t *th)
|
2010-11-27 15:15:59 -05:00
|
|
|
{
|
2011-06-13 10:14:53 -04:00
|
|
|
native_mutex_lock(&vm->gvl.lock);
|
2011-06-27 08:18:00 -04:00
|
|
|
gvl_acquire_common(vm);
|
2011-06-13 10:14:53 -04:00
|
|
|
native_mutex_unlock(&vm->gvl.lock);
|
2010-11-27 15:15:59 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2011-06-27 08:18:00 -04:00
|
|
|
gvl_release_common(rb_vm_t *vm)
|
2010-11-27 15:15:59 -05:00
|
|
|
{
|
2011-06-13 10:14:53 -04:00
|
|
|
vm->gvl.acquired = 0;
|
|
|
|
if (vm->gvl.waiting > 0)
|
|
|
|
native_cond_signal(&vm->gvl.cond);
|
2010-11-27 15:15:59 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2011-06-13 10:14:53 -04:00
|
|
|
gvl_release(rb_vm_t *vm)
|
2010-11-27 15:15:59 -05:00
|
|
|
{
|
|
|
|
native_mutex_lock(&vm->gvl.lock);
|
2011-06-27 08:18:00 -04:00
|
|
|
gvl_release_common(vm);
|
2010-11-27 15:15:59 -05:00
|
|
|
native_mutex_unlock(&vm->gvl.lock);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2011-06-13 10:14:53 -04:00
|
|
|
gvl_yield(rb_vm_t *vm, rb_thread_t *th)
|
2010-11-27 15:15:59 -05:00
|
|
|
{
|
|
|
|
native_mutex_lock(&vm->gvl.lock);
|
2011-06-13 10:14:53 -04:00
|
|
|
|
2011-06-27 08:18:00 -04:00
|
|
|
gvl_release_common(vm);
|
2011-06-14 12:31:23 -04:00
|
|
|
|
2011-06-13 10:14:53 -04:00
|
|
|
/* An another thread is processing GVL yield. */
|
2011-06-14 12:31:23 -04:00
|
|
|
if (UNLIKELY(vm->gvl.wait_yield)) {
|
|
|
|
while (vm->gvl.wait_yield)
|
|
|
|
native_cond_wait(&vm->gvl.switch_wait_cond, &vm->gvl.lock);
|
|
|
|
goto acquire;
|
2010-11-27 15:15:59 -05:00
|
|
|
}
|
2011-06-13 10:14:53 -04:00
|
|
|
|
2011-11-09 12:10:48 -05:00
|
|
|
if (vm->gvl.waiting > 0) {
|
2011-06-13 10:14:53 -04:00
|
|
|
/* Wait until another thread task take GVL. */
|
2011-11-09 12:10:48 -05:00
|
|
|
vm->gvl.need_yield = 1;
|
|
|
|
vm->gvl.wait_yield = 1;
|
|
|
|
while (vm->gvl.need_yield)
|
2011-06-14 12:31:23 -04:00
|
|
|
native_cond_wait(&vm->gvl.switch_cond, &vm->gvl.lock);
|
2011-11-09 12:10:48 -05:00
|
|
|
vm->gvl.wait_yield = 0;
|
2011-06-14 12:31:23 -04:00
|
|
|
}
|
|
|
|
else {
|
2011-06-13 10:14:53 -04:00
|
|
|
native_mutex_unlock(&vm->gvl.lock);
|
|
|
|
sched_yield();
|
|
|
|
native_mutex_lock(&vm->gvl.lock);
|
2010-11-27 15:15:59 -05:00
|
|
|
}
|
2011-06-13 10:14:53 -04:00
|
|
|
|
2011-06-14 12:31:23 -04:00
|
|
|
native_cond_broadcast(&vm->gvl.switch_wait_cond);
|
|
|
|
acquire:
|
2011-06-27 08:18:00 -04:00
|
|
|
gvl_acquire_common(vm);
|
2010-11-27 15:15:59 -05:00
|
|
|
native_mutex_unlock(&vm->gvl.lock);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
gvl_init(rb_vm_t *vm)
|
|
|
|
{
|
2010-11-28 00:46:32 -05:00
|
|
|
native_mutex_initialize(&vm->gvl.lock);
|
2011-06-13 10:14:53 -04:00
|
|
|
native_cond_initialize(&vm->gvl.cond, RB_CONDATTR_CLOCK_MONOTONIC);
|
|
|
|
native_cond_initialize(&vm->gvl.switch_cond, RB_CONDATTR_CLOCK_MONOTONIC);
|
2011-06-14 12:31:23 -04:00
|
|
|
native_cond_initialize(&vm->gvl.switch_wait_cond, RB_CONDATTR_CLOCK_MONOTONIC);
|
2010-11-27 15:15:59 -05:00
|
|
|
vm->gvl.acquired = 0;
|
2011-06-13 10:14:53 -04:00
|
|
|
vm->gvl.waiting = 0;
|
|
|
|
vm->gvl.need_yield = 0;
|
2013-01-14 19:32:23 -05:00
|
|
|
vm->gvl.wait_yield = 0;
|
2010-11-27 15:15:59 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
gvl_destroy(rb_vm_t *vm)
|
|
|
|
{
|
2011-07-07 21:40:54 -04:00
|
|
|
native_cond_destroy(&vm->gvl.switch_wait_cond);
|
|
|
|
native_cond_destroy(&vm->gvl.switch_cond);
|
|
|
|
native_cond_destroy(&vm->gvl.cond);
|
2010-11-27 15:15:59 -05:00
|
|
|
native_mutex_destroy(&vm->gvl.lock);
|
|
|
|
}
|
|
|
|
|
2010-11-28 08:00:49 -05:00
|
|
|
static void
|
|
|
|
gvl_atfork(rb_vm_t *vm)
|
|
|
|
{
|
|
|
|
gvl_init(vm);
|
|
|
|
gvl_acquire(vm, GET_THREAD());
|
|
|
|
}
|
|
|
|
|
2010-11-28 08:16:50 -05:00
|
|
|
#define NATIVE_MUTEX_LOCK_DEBUG 0
|
|
|
|
|
2010-11-27 15:15:59 -05:00
|
|
|
static void
|
|
|
|
mutex_debug(const char *msg, pthread_mutex_t *lock)
|
|
|
|
{
|
2010-11-28 08:16:50 -05:00
|
|
|
if (NATIVE_MUTEX_LOCK_DEBUG) {
|
2010-11-27 15:15:59 -05:00
|
|
|
int r;
|
|
|
|
static pthread_mutex_t dbglock = PTHREAD_MUTEX_INITIALIZER;
|
|
|
|
|
2011-07-10 04:29:46 -04:00
|
|
|
if ((r = pthread_mutex_lock(&dbglock)) != 0) {exit(EXIT_FAILURE);}
|
2011-04-25 03:56:42 -04:00
|
|
|
fprintf(stdout, "%s: %p\n", msg, (void *)lock);
|
2011-07-10 04:29:46 -04:00
|
|
|
if ((r = pthread_mutex_unlock(&dbglock)) != 0) {exit(EXIT_FAILURE);}
|
2010-11-27 15:15:59 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-12-24 23:35:17 -05:00
|
|
|
static void
|
2007-02-08 15:24:55 -05:00
|
|
|
native_mutex_lock(pthread_mutex_t *lock)
|
|
|
|
{
|
|
|
|
int r;
|
2010-11-27 15:15:59 -05:00
|
|
|
mutex_debug("lock", lock);
|
2007-02-22 20:56:43 -05:00
|
|
|
if ((r = pthread_mutex_lock(lock)) != 0) {
|
2009-11-24 06:03:51 -05:00
|
|
|
rb_bug_errno("pthread_mutex_lock", r);
|
2007-02-08 15:24:55 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-12-24 23:35:17 -05:00
|
|
|
static void
|
2007-02-08 15:24:55 -05:00
|
|
|
native_mutex_unlock(pthread_mutex_t *lock)
|
|
|
|
{
|
|
|
|
int r;
|
2010-11-27 15:15:59 -05:00
|
|
|
mutex_debug("unlock", lock);
|
2007-02-08 15:24:55 -05:00
|
|
|
if ((r = pthread_mutex_unlock(lock)) != 0) {
|
2009-11-24 06:03:51 -05:00
|
|
|
rb_bug_errno("pthread_mutex_unlock", r);
|
2007-02-08 15:24:55 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-12-24 23:35:17 -05:00
|
|
|
static inline int
|
2007-02-08 15:24:55 -05:00
|
|
|
native_mutex_trylock(pthread_mutex_t *lock)
|
|
|
|
{
|
|
|
|
int r;
|
2010-11-27 15:15:59 -05:00
|
|
|
mutex_debug("trylock", lock);
|
2007-02-08 15:24:55 -05:00
|
|
|
if ((r = pthread_mutex_trylock(lock)) != 0) {
|
|
|
|
if (r == EBUSY) {
|
|
|
|
return EBUSY;
|
|
|
|
}
|
|
|
|
else {
|
2009-11-24 06:03:51 -05:00
|
|
|
rb_bug_errno("pthread_mutex_trylock", r);
|
2007-02-08 15:24:55 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2007-12-24 23:35:17 -05:00
|
|
|
static void
|
2007-02-08 15:24:55 -05:00
|
|
|
native_mutex_initialize(pthread_mutex_t *lock)
|
|
|
|
{
|
2007-04-12 04:14:54 -04:00
|
|
|
int r = pthread_mutex_init(lock, 0);
|
2010-11-27 15:15:59 -05:00
|
|
|
mutex_debug("init", lock);
|
2007-04-12 04:14:54 -04:00
|
|
|
if (r != 0) {
|
2009-11-24 06:03:51 -05:00
|
|
|
rb_bug_errno("pthread_mutex_init", r);
|
2007-02-08 15:24:55 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-12-24 23:35:17 -05:00
|
|
|
static void
|
2007-02-08 15:24:55 -05:00
|
|
|
native_mutex_destroy(pthread_mutex_t *lock)
|
|
|
|
{
|
2007-04-12 04:14:54 -04:00
|
|
|
int r = pthread_mutex_destroy(lock);
|
2010-11-27 15:15:59 -05:00
|
|
|
mutex_debug("destroy", lock);
|
2007-04-12 04:14:54 -04:00
|
|
|
if (r != 0) {
|
2009-11-24 06:03:51 -05:00
|
|
|
rb_bug_errno("pthread_mutex_destroy", r);
|
2007-02-08 15:24:55 -05:00
|
|
|
}
|
|
|
|
}
|
2006-12-31 10:02:22 -05:00
|
|
|
|
2007-12-24 23:35:17 -05:00
|
|
|
static void
|
2013-07-23 06:50:32 -04:00
|
|
|
native_cond_initialize(rb_nativethread_cond_t *cond, int flags)
|
2007-08-27 12:48:14 -04:00
|
|
|
{
|
2012-05-17 10:04:02 -04:00
|
|
|
#ifdef HAVE_PTHREAD_COND_INIT
|
2011-05-06 12:47:38 -04:00
|
|
|
int r;
|
2012-09-20 10:15:46 -04:00
|
|
|
# if USE_MONOTONIC_COND
|
2011-05-06 12:47:38 -04:00
|
|
|
pthread_condattr_t attr;
|
|
|
|
|
|
|
|
pthread_condattr_init(&attr);
|
|
|
|
|
2011-05-07 05:28:43 -04:00
|
|
|
cond->clockid = CLOCK_REALTIME;
|
2011-05-06 12:47:38 -04:00
|
|
|
if (flags & RB_CONDATTR_CLOCK_MONOTONIC) {
|
|
|
|
r = pthread_condattr_setclock(&attr, CLOCK_MONOTONIC);
|
|
|
|
if (r == 0) {
|
|
|
|
cond->clockid = CLOCK_MONOTONIC;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
r = pthread_cond_init(&cond->cond, &attr);
|
2012-09-20 10:16:02 -04:00
|
|
|
pthread_condattr_destroy(&attr);
|
2012-06-10 08:51:37 -04:00
|
|
|
# else
|
|
|
|
r = pthread_cond_init(&cond->cond, NULL);
|
|
|
|
# endif
|
2007-08-27 12:48:14 -04:00
|
|
|
if (r != 0) {
|
2009-11-24 06:03:51 -05:00
|
|
|
rb_bug_errno("pthread_cond_init", r);
|
2007-08-27 12:48:14 -04:00
|
|
|
}
|
2011-05-06 12:47:38 -04:00
|
|
|
|
|
|
|
return;
|
2012-05-16 23:54:50 -04:00
|
|
|
#endif
|
|
|
|
}
|
2007-08-27 12:48:14 -04:00
|
|
|
|
2007-12-24 23:35:17 -05:00
|
|
|
static void
|
2013-07-23 06:50:32 -04:00
|
|
|
native_cond_destroy(rb_nativethread_cond_t *cond)
|
2007-08-27 12:48:14 -04:00
|
|
|
{
|
2012-05-17 10:04:02 -04:00
|
|
|
#ifdef HAVE_PTHREAD_COND_INIT
|
2011-05-06 12:47:38 -04:00
|
|
|
int r = pthread_cond_destroy(&cond->cond);
|
2007-08-27 12:48:14 -04:00
|
|
|
if (r != 0) {
|
2009-11-24 06:03:51 -05:00
|
|
|
rb_bug_errno("pthread_cond_destroy", r);
|
2007-08-27 12:48:14 -04:00
|
|
|
}
|
2012-05-16 23:54:50 -04:00
|
|
|
#endif
|
2007-08-27 12:48:14 -04:00
|
|
|
}
|
|
|
|
|
2011-08-04 11:06:20 -04:00
|
|
|
/*
|
|
|
|
* In OS X 10.7 (Lion), pthread_cond_signal and pthread_cond_broadcast return
|
2011-08-04 21:11:12 -04:00
|
|
|
* EAGAIN after retrying 8192 times. You can see them in the following page:
|
2011-08-04 11:06:20 -04:00
|
|
|
*
|
|
|
|
* http://www.opensource.apple.com/source/Libc/Libc-763.11/pthreads/pthread_cond.c
|
|
|
|
*
|
|
|
|
* The following native_cond_signal and native_cond_broadcast functions
|
|
|
|
* need to retrying until pthread functions don't return EAGAIN.
|
|
|
|
*/
|
|
|
|
|
2007-12-24 23:35:17 -05:00
|
|
|
static void
|
2013-07-23 06:50:32 -04:00
|
|
|
native_cond_signal(rb_nativethread_cond_t *cond)
|
2007-08-27 12:48:14 -04:00
|
|
|
{
|
2011-08-04 11:06:20 -04:00
|
|
|
int r;
|
|
|
|
do {
|
|
|
|
r = pthread_cond_signal(&cond->cond);
|
|
|
|
} while (r == EAGAIN);
|
2010-11-28 07:55:43 -05:00
|
|
|
if (r != 0) {
|
|
|
|
rb_bug_errno("pthread_cond_signal", r);
|
|
|
|
}
|
2007-08-27 12:48:14 -04:00
|
|
|
}
|
|
|
|
|
2007-12-24 23:35:17 -05:00
|
|
|
static void
|
2013-07-23 06:50:32 -04:00
|
|
|
native_cond_broadcast(rb_nativethread_cond_t *cond)
|
2007-08-27 12:48:14 -04:00
|
|
|
{
|
2011-08-04 11:06:20 -04:00
|
|
|
int r;
|
|
|
|
do {
|
|
|
|
r = pthread_cond_broadcast(&cond->cond);
|
|
|
|
} while (r == EAGAIN);
|
2010-11-28 07:55:43 -05:00
|
|
|
if (r != 0) {
|
|
|
|
rb_bug_errno("native_cond_broadcast", r);
|
|
|
|
}
|
2007-08-27 12:48:14 -04:00
|
|
|
}
|
|
|
|
|
2007-12-24 23:35:17 -05:00
|
|
|
static void
|
2013-07-23 06:50:32 -04:00
|
|
|
native_cond_wait(rb_nativethread_cond_t *cond, pthread_mutex_t *mutex)
|
2007-08-27 12:48:14 -04:00
|
|
|
{
|
2011-05-06 12:47:38 -04:00
|
|
|
int r = pthread_cond_wait(&cond->cond, mutex);
|
2010-11-28 07:55:43 -05:00
|
|
|
if (r != 0) {
|
|
|
|
rb_bug_errno("pthread_cond_wait", r);
|
|
|
|
}
|
2007-08-27 12:48:14 -04:00
|
|
|
}
|
|
|
|
|
2008-11-07 11:42:31 -05:00
|
|
|
static int
|
2013-07-23 06:50:32 -04:00
|
|
|
native_cond_timedwait(rb_nativethread_cond_t *cond, pthread_mutex_t *mutex, struct timespec *ts)
|
2008-11-07 11:14:48 -05:00
|
|
|
{
|
2011-05-08 06:46:27 -04:00
|
|
|
int r;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* An old Linux may return EINTR. Even though POSIX says
|
|
|
|
* "These functions shall not return an error code of [EINTR]".
|
|
|
|
* http://pubs.opengroup.org/onlinepubs/009695399/functions/pthread_cond_timedwait.html
|
|
|
|
* Let's hide it from arch generic code.
|
|
|
|
*/
|
|
|
|
do {
|
|
|
|
r = pthread_cond_timedwait(&cond->cond, mutex, ts);
|
|
|
|
} while (r == EINTR);
|
|
|
|
|
|
|
|
if (r != 0 && r != ETIMEDOUT) {
|
2010-11-28 07:55:43 -05:00
|
|
|
rb_bug_errno("pthread_cond_timedwait", r);
|
|
|
|
}
|
2011-05-08 06:46:27 -04:00
|
|
|
|
2010-11-28 07:55:43 -05:00
|
|
|
return r;
|
2008-11-07 11:14:48 -05:00
|
|
|
}
|
|
|
|
|
2011-05-06 13:39:32 -04:00
|
|
|
static struct timespec
|
2013-07-23 06:50:32 -04:00
|
|
|
native_cond_timeout(rb_nativethread_cond_t *cond, struct timespec timeout_rel)
|
2011-05-06 13:39:32 -04:00
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
struct timeval tv;
|
|
|
|
struct timespec timeout;
|
2011-05-06 14:17:14 -04:00
|
|
|
struct timespec now;
|
2011-05-06 13:39:32 -04:00
|
|
|
|
|
|
|
#if USE_MONOTONIC_COND
|
|
|
|
if (cond->clockid == CLOCK_MONOTONIC) {
|
2011-05-06 14:17:14 -04:00
|
|
|
ret = clock_gettime(cond->clockid, &now);
|
2011-05-06 13:39:32 -04:00
|
|
|
if (ret != 0)
|
|
|
|
rb_sys_fail("clock_gettime()");
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (cond->clockid != CLOCK_REALTIME)
|
2012-03-02 02:36:34 -05:00
|
|
|
rb_bug("unsupported clockid %"PRIdVALUE, (SIGNED_VALUE)cond->clockid);
|
2011-05-07 05:28:43 -04:00
|
|
|
#endif
|
2011-05-06 13:39:32 -04:00
|
|
|
|
|
|
|
ret = gettimeofday(&tv, 0);
|
|
|
|
if (ret != 0)
|
|
|
|
rb_sys_fail(0);
|
2011-05-06 14:17:14 -04:00
|
|
|
now.tv_sec = tv.tv_sec;
|
|
|
|
now.tv_nsec = tv.tv_usec * 1000;
|
2011-05-06 13:39:32 -04:00
|
|
|
|
2011-06-10 21:21:10 -04:00
|
|
|
#if USE_MONOTONIC_COND
|
2011-05-06 13:39:32 -04:00
|
|
|
out:
|
2011-06-10 21:21:10 -04:00
|
|
|
#endif
|
2011-05-06 14:17:14 -04:00
|
|
|
timeout.tv_sec = now.tv_sec;
|
|
|
|
timeout.tv_nsec = now.tv_nsec;
|
2011-05-06 13:39:32 -04:00
|
|
|
timeout.tv_sec += timeout_rel.tv_sec;
|
|
|
|
timeout.tv_nsec += timeout_rel.tv_nsec;
|
2011-05-06 14:17:14 -04:00
|
|
|
|
2011-05-06 13:39:32 -04:00
|
|
|
if (timeout.tv_nsec >= 1000*1000*1000) {
|
|
|
|
timeout.tv_sec++;
|
|
|
|
timeout.tv_nsec -= 1000*1000*1000;
|
|
|
|
}
|
2011-05-06 14:17:14 -04:00
|
|
|
|
|
|
|
if (timeout.tv_sec < now.tv_sec)
|
|
|
|
timeout.tv_sec = TIMET_MAX;
|
|
|
|
|
2011-05-06 13:39:32 -04:00
|
|
|
return timeout;
|
|
|
|
}
|
|
|
|
|
2006-12-31 10:02:22 -05:00
|
|
|
#define native_cleanup_push pthread_cleanup_push
|
|
|
|
#define native_cleanup_pop pthread_cleanup_pop
|
2008-08-08 04:03:19 -04:00
|
|
|
#ifdef HAVE_SCHED_YIELD
|
|
|
|
#define native_thread_yield() (void)sched_yield()
|
2008-08-06 16:52:44 -04:00
|
|
|
#else
|
2008-08-08 04:03:19 -04:00
|
|
|
#define native_thread_yield() ((void)0)
|
2008-08-06 16:52:44 -04:00
|
|
|
#endif
|
2006-12-31 10:02:22 -05:00
|
|
|
|
2011-05-07 21:50:36 -04:00
|
|
|
#if defined(SIGVTALRM) && !defined(__CYGWIN__) && !defined(__SYMBIAN32__)
|
|
|
|
#define USE_SIGNAL_THREAD_LIST 1
|
2008-04-20 02:14:49 -04:00
|
|
|
#endif
|
2011-05-07 21:50:36 -04:00
|
|
|
#ifdef USE_SIGNAL_THREAD_LIST
|
|
|
|
static void add_signal_thread_list(rb_thread_t *th);
|
2007-02-08 06:51:40 -05:00
|
|
|
static void remove_signal_thread_list(rb_thread_t *th);
|
2013-07-23 05:53:14 -04:00
|
|
|
static rb_nativethread_lock_t signal_thread_list_lock;
|
2011-05-07 21:50:36 -04:00
|
|
|
#endif
|
2006-12-31 10:02:22 -05:00
|
|
|
|
2007-08-18 04:40:13 -04:00
|
|
|
static pthread_key_t ruby_native_thread_key;
|
|
|
|
|
2006-12-31 10:02:22 -05:00
|
|
|
static void
|
2007-12-21 03:13:39 -05:00
|
|
|
null_func(int i)
|
2006-12-31 10:02:22 -05:00
|
|
|
{
|
2007-02-08 06:51:40 -05:00
|
|
|
/* null */
|
2006-12-31 10:02:22 -05:00
|
|
|
}
|
|
|
|
|
2007-08-18 04:40:13 -04:00
|
|
|
static rb_thread_t *
|
|
|
|
ruby_thread_from_native(void)
|
|
|
|
{
|
|
|
|
return pthread_getspecific(ruby_native_thread_key);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
ruby_thread_set_native(rb_thread_t *th)
|
|
|
|
{
|
|
|
|
return pthread_setspecific(ruby_native_thread_key, th) == 0;
|
|
|
|
}
|
|
|
|
|
2010-11-28 07:46:27 -05:00
|
|
|
static void native_thread_init(rb_thread_t *th);
|
|
|
|
|
2010-06-05 19:26:43 -04:00
|
|
|
void
|
2007-08-18 04:40:13 -04:00
|
|
|
Init_native_thread(void)
|
2006-12-31 10:02:22 -05:00
|
|
|
{
|
2007-08-18 04:40:13 -04:00
|
|
|
rb_thread_t *th = GET_THREAD();
|
|
|
|
|
|
|
|
pthread_key_create(&ruby_native_thread_key, NULL);
|
|
|
|
th->thread_id = pthread_self();
|
2010-11-28 07:46:27 -05:00
|
|
|
native_thread_init(th);
|
2011-05-07 21:50:36 -04:00
|
|
|
#ifdef USE_SIGNAL_THREAD_LIST
|
2010-11-28 07:46:27 -05:00
|
|
|
native_mutex_initialize(&signal_thread_list_lock);
|
2011-05-07 21:50:36 -04:00
|
|
|
#endif
|
2012-05-16 22:48:59 -04:00
|
|
|
#ifndef __native_client__
|
2010-11-28 07:46:27 -05:00
|
|
|
posix_signal(SIGVTALRM, null_func);
|
2012-05-16 22:48:59 -04:00
|
|
|
#endif
|
2010-11-28 07:46:27 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
native_thread_init(rb_thread_t *th)
|
|
|
|
{
|
2011-05-06 14:17:14 -04:00
|
|
|
native_cond_initialize(&th->native_thread_data.sleep_cond, RB_CONDATTR_CLOCK_MONOTONIC);
|
2007-08-18 04:40:13 -04:00
|
|
|
ruby_thread_set_native(th);
|
2006-12-31 10:02:22 -05:00
|
|
|
}
|
|
|
|
|
2007-02-08 15:24:55 -05:00
|
|
|
static void
|
|
|
|
native_thread_destroy(rb_thread_t *th)
|
|
|
|
{
|
2011-05-06 12:47:38 -04:00
|
|
|
native_cond_destroy(&th->native_thread_data.sleep_cond);
|
2007-02-08 15:24:55 -05:00
|
|
|
}
|
2006-12-31 10:02:22 -05:00
|
|
|
|
2012-06-26 04:01:35 -04:00
|
|
|
#ifndef USE_THREAD_CACHE
|
2006-12-31 10:02:22 -05:00
|
|
|
#define USE_THREAD_CACHE 0
|
2012-06-26 04:01:35 -04:00
|
|
|
#endif
|
2006-12-31 10:02:22 -05:00
|
|
|
|
2011-02-06 05:59:03 -05:00
|
|
|
#if USE_THREAD_CACHE
|
|
|
|
static rb_thread_t *register_cached_thread_and_wait(void);
|
|
|
|
#endif
|
|
|
|
|
2008-11-27 01:05:07 -05:00
|
|
|
#if defined HAVE_PTHREAD_GETATTR_NP || defined HAVE_PTHREAD_ATTR_GET_NP
|
|
|
|
#define STACKADDR_AVAILABLE 1
|
|
|
|
#elif defined HAVE_PTHREAD_GET_STACKADDR_NP && defined HAVE_PTHREAD_GET_STACKSIZE_NP
|
|
|
|
#define STACKADDR_AVAILABLE 1
|
2012-10-03 02:33:08 -04:00
|
|
|
#undef MAINSTACKADDR_AVAILABLE
|
|
|
|
#define MAINSTACKADDR_AVAILABLE 0
|
2012-04-10 03:53:24 -04:00
|
|
|
void *pthread_get_stackaddr_np(pthread_t);
|
|
|
|
size_t pthread_get_stacksize_np(pthread_t);
|
2008-11-27 01:05:07 -05:00
|
|
|
#elif defined HAVE_THR_STKSEGMENT || defined HAVE_PTHREAD_STACKSEG_NP
|
|
|
|
#define STACKADDR_AVAILABLE 1
|
2010-06-29 13:29:27 -04:00
|
|
|
#elif defined HAVE_PTHREAD_GETTHRDS_NP
|
|
|
|
#define STACKADDR_AVAILABLE 1
|
2008-11-27 01:05:07 -05:00
|
|
|
#endif
|
|
|
|
|
2012-10-03 02:33:08 -04:00
|
|
|
#ifndef MAINSTACKADDR_AVAILABLE
|
|
|
|
# ifdef STACKADDR_AVAILABLE
|
|
|
|
# define MAINSTACKADDR_AVAILABLE 1
|
|
|
|
# else
|
|
|
|
# define MAINSTACKADDR_AVAILABLE 0
|
|
|
|
# endif
|
|
|
|
#endif
|
|
|
|
|
2008-11-27 01:05:07 -05:00
|
|
|
#ifdef STACKADDR_AVAILABLE
|
2011-07-03 18:16:02 -04:00
|
|
|
/*
|
2011-07-04 08:58:07 -04:00
|
|
|
* Get the initial address and size of current thread's stack
|
2011-07-03 18:16:02 -04:00
|
|
|
*/
|
2008-11-27 01:05:07 -05:00
|
|
|
static int
|
|
|
|
get_stack(void **addr, size_t *size)
|
|
|
|
{
|
|
|
|
#define CHECK_ERR(expr) \
|
|
|
|
{int err = (expr); if (err) return err;}
|
2012-06-13 19:18:35 -04:00
|
|
|
#ifdef HAVE_PTHREAD_GETATTR_NP /* Linux */
|
2008-11-27 01:05:07 -05:00
|
|
|
pthread_attr_t attr;
|
|
|
|
size_t guard = 0;
|
2011-07-02 17:18:16 -04:00
|
|
|
STACK_GROW_DIR_DETECTION;
|
2008-11-27 01:05:07 -05:00
|
|
|
CHECK_ERR(pthread_getattr_np(pthread_self(), &attr));
|
2012-06-13 19:18:35 -04:00
|
|
|
# ifdef HAVE_PTHREAD_ATTR_GETSTACK
|
2008-11-27 01:05:07 -05:00
|
|
|
CHECK_ERR(pthread_attr_getstack(&attr, addr, size));
|
2011-07-02 17:18:16 -04:00
|
|
|
STACK_DIR_UPPER((void)0, (void)(*addr = (char *)*addr + *size));
|
2012-06-13 19:18:35 -04:00
|
|
|
# else
|
2008-11-27 01:05:07 -05:00
|
|
|
CHECK_ERR(pthread_attr_getstackaddr(&attr, addr));
|
|
|
|
CHECK_ERR(pthread_attr_getstacksize(&attr, size));
|
2012-06-13 19:18:35 -04:00
|
|
|
# endif
|
|
|
|
CHECK_ERR(pthread_attr_getguardsize(&attr, &guard));
|
|
|
|
*size -= guard;
|
|
|
|
pthread_attr_destroy(&attr);
|
|
|
|
#elif defined HAVE_PTHREAD_ATTR_GET_NP /* FreeBSD, DragonFly BSD, NetBSD */
|
|
|
|
pthread_attr_t attr;
|
2008-11-27 01:05:07 -05:00
|
|
|
CHECK_ERR(pthread_attr_init(&attr));
|
|
|
|
CHECK_ERR(pthread_attr_get_np(pthread_self(), &attr));
|
2012-06-13 19:18:35 -04:00
|
|
|
# ifdef HAVE_PTHREAD_ATTR_GETSTACK
|
2011-07-04 09:27:31 -04:00
|
|
|
CHECK_ERR(pthread_attr_getstack(&attr, addr, size));
|
|
|
|
STACK_DIR_UPPER((void)0, (void)(*addr = (char *)*addr + *size));
|
2012-06-13 19:18:35 -04:00
|
|
|
# else
|
2008-11-27 01:05:07 -05:00
|
|
|
CHECK_ERR(pthread_attr_getstackaddr(&attr, addr));
|
|
|
|
CHECK_ERR(pthread_attr_getstacksize(&attr, size));
|
2011-07-04 09:27:31 -04:00
|
|
|
STACK_DIR_UPPER((void)0, (void)(*addr = (char *)*addr + *size));
|
2012-06-10 04:54:32 -04:00
|
|
|
# endif
|
2012-06-13 19:18:35 -04:00
|
|
|
pthread_attr_destroy(&attr);
|
2012-06-10 04:54:32 -04:00
|
|
|
#elif (defined HAVE_PTHREAD_GET_STACKADDR_NP && defined HAVE_PTHREAD_GET_STACKSIZE_NP) /* MacOS X */
|
|
|
|
pthread_t th = pthread_self();
|
|
|
|
*addr = pthread_get_stackaddr_np(th);
|
|
|
|
*size = pthread_get_stacksize_np(th);
|
2008-11-27 01:05:07 -05:00
|
|
|
#elif defined HAVE_THR_STKSEGMENT || defined HAVE_PTHREAD_STACKSEG_NP
|
|
|
|
stack_t stk;
|
2011-07-03 18:16:02 -04:00
|
|
|
# if defined HAVE_THR_STKSEGMENT /* Solaris */
|
2008-11-27 01:05:07 -05:00
|
|
|
CHECK_ERR(thr_stksegment(&stk));
|
2011-07-03 18:16:02 -04:00
|
|
|
# else /* OpenBSD */
|
2008-11-27 01:05:07 -05:00
|
|
|
CHECK_ERR(pthread_stackseg_np(pthread_self(), &stk));
|
|
|
|
# endif
|
|
|
|
*addr = stk.ss_sp;
|
|
|
|
*size = stk.ss_size;
|
2011-07-03 18:16:02 -04:00
|
|
|
#elif defined HAVE_PTHREAD_GETTHRDS_NP /* AIX */
|
2010-06-29 13:29:27 -04:00
|
|
|
pthread_t th = pthread_self();
|
|
|
|
struct __pthrdsinfo thinfo;
|
|
|
|
char reg[256];
|
|
|
|
int regsiz=sizeof(reg);
|
|
|
|
CHECK_ERR(pthread_getthrds_np(&th, PTHRDSINFO_QUERY_ALL,
|
|
|
|
&thinfo, sizeof(thinfo),
|
|
|
|
®, ®siz));
|
|
|
|
*addr = thinfo.__pi_stackaddr;
|
|
|
|
*size = thinfo.__pi_stacksize;
|
2011-07-10 16:39:35 -04:00
|
|
|
STACK_DIR_UPPER((void)0, (void)(*addr = (char *)*addr + *size));
|
2011-07-03 18:16:02 -04:00
|
|
|
#else
|
|
|
|
#error STACKADDR_AVAILABLE is defined but not implemented.
|
2008-11-27 01:05:07 -05:00
|
|
|
#endif
|
|
|
|
return 0;
|
|
|
|
#undef CHECK_ERR
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2008-06-13 22:59:19 -04:00
|
|
|
static struct {
|
2013-07-23 06:38:36 -04:00
|
|
|
rb_nativethread_id_t id;
|
2008-06-13 22:59:19 -04:00
|
|
|
size_t stack_maxsize;
|
|
|
|
VALUE *stack_start;
|
|
|
|
#ifdef __ia64
|
|
|
|
VALUE *register_stack_start;
|
|
|
|
#endif
|
|
|
|
} native_main_thread;
|
|
|
|
|
2008-07-06 11:59:40 -04:00
|
|
|
#ifdef STACK_END_ADDRESS
|
|
|
|
extern void *STACK_END_ADDRESS;
|
|
|
|
#endif
|
|
|
|
|
2013-04-04 04:01:23 -04:00
|
|
|
enum {
|
|
|
|
RUBY_STACK_SPACE_LIMIT = 1024 * 1024, /* 1024KB */
|
|
|
|
RUBY_STACK_SPACE_RATIO = 5
|
|
|
|
};
|
|
|
|
|
|
|
|
static size_t
|
|
|
|
space_size(size_t stack_size)
|
|
|
|
{
|
|
|
|
size_t space_size = stack_size / RUBY_STACK_SPACE_RATIO;
|
|
|
|
if (space_size > RUBY_STACK_SPACE_LIMIT) {
|
|
|
|
return RUBY_STACK_SPACE_LIMIT;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
return space_size;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-06-13 22:59:19 -04:00
|
|
|
#undef ruby_init_stack
|
2012-06-13 22:21:51 -04:00
|
|
|
/* Set stack bottom of Ruby implementation.
|
|
|
|
*
|
|
|
|
* You must call this function before any heap allocation by Ruby implementation.
|
|
|
|
* Or GC will break living objects */
|
2008-06-13 22:59:19 -04:00
|
|
|
void
|
2009-04-19 01:43:20 -04:00
|
|
|
ruby_init_stack(volatile VALUE *addr
|
2008-06-13 22:59:19 -04:00
|
|
|
#ifdef __ia64
|
|
|
|
, void *bsp
|
|
|
|
#endif
|
|
|
|
)
|
|
|
|
{
|
|
|
|
native_main_thread.id = pthread_self();
|
2008-07-06 11:59:40 -04:00
|
|
|
#ifdef STACK_END_ADDRESS
|
|
|
|
native_main_thread.stack_start = STACK_END_ADDRESS;
|
|
|
|
#else
|
2008-06-13 22:59:19 -04:00
|
|
|
if (!native_main_thread.stack_start ||
|
2009-02-27 04:01:21 -05:00
|
|
|
STACK_UPPER((VALUE *)(void *)&addr,
|
2008-06-13 22:59:19 -04:00
|
|
|
native_main_thread.stack_start > addr,
|
|
|
|
native_main_thread.stack_start < addr)) {
|
2009-04-19 01:43:20 -04:00
|
|
|
native_main_thread.stack_start = (VALUE *)addr;
|
2008-06-13 22:59:19 -04:00
|
|
|
}
|
2008-07-06 11:59:40 -04:00
|
|
|
#endif
|
2008-06-13 22:59:19 -04:00
|
|
|
#ifdef __ia64
|
|
|
|
if (!native_main_thread.register_stack_start ||
|
|
|
|
(VALUE*)bsp < native_main_thread.register_stack_start) {
|
|
|
|
native_main_thread.register_stack_start = (VALUE*)bsp;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
{
|
2013-04-04 04:01:23 -04:00
|
|
|
#if defined(PTHREAD_STACK_DEFAULT)
|
|
|
|
# if PTHREAD_STACK_DEFAULT < RUBY_STACK_SPACE*5
|
|
|
|
# error "PTHREAD_STACK_DEFAULT is too small"
|
|
|
|
# endif
|
|
|
|
size_t size = PTHREAD_STACK_DEFAULT;
|
|
|
|
#else
|
|
|
|
size_t size = RUBY_VM_THREAD_VM_STACK_SIZE;
|
|
|
|
#endif
|
|
|
|
size_t space = space_size(size);
|
2012-10-03 02:33:08 -04:00
|
|
|
#if MAINSTACKADDR_AVAILABLE
|
2012-06-10 04:54:28 -04:00
|
|
|
void* stackaddr;
|
2012-07-21 10:12:55 -04:00
|
|
|
STACK_GROW_DIR_DETECTION;
|
2013-04-04 04:01:23 -04:00
|
|
|
if (get_stack(&stackaddr, &size) == 0) {
|
|
|
|
space = STACK_DIR_UPPER((char *)addr - (char *)stackaddr, (char *)stackaddr - (char *)addr);
|
|
|
|
}
|
2012-10-03 22:43:30 -04:00
|
|
|
native_main_thread.stack_maxsize = size - space;
|
2010-02-02 22:35:11 -05:00
|
|
|
#elif defined(HAVE_GETRLIMIT)
|
2012-10-03 22:43:30 -04:00
|
|
|
int pagesize = getpagesize();
|
2008-06-13 22:59:19 -04:00
|
|
|
struct rlimit rlim;
|
2013-04-27 07:21:10 -04:00
|
|
|
STACK_GROW_DIR_DETECTION;
|
2008-06-13 22:59:19 -04:00
|
|
|
if (getrlimit(RLIMIT_STACK, &rlim) == 0) {
|
2010-02-02 22:35:11 -05:00
|
|
|
size = (size_t)rlim.rlim_cur;
|
2008-06-13 22:59:19 -04:00
|
|
|
}
|
2012-10-03 22:43:30 -04:00
|
|
|
addr = native_main_thread.stack_start;
|
|
|
|
if (IS_STACK_DIR_UPPER()) {
|
|
|
|
space = ((size_t)((char *)addr + size) / pagesize) * pagesize - (size_t)addr;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
space = (size_t)addr - ((size_t)((char *)addr - size) / pagesize + 1) * pagesize;
|
|
|
|
}
|
|
|
|
native_main_thread.stack_maxsize = space;
|
2012-06-08 19:30:55 -04:00
|
|
|
#endif
|
2010-02-02 22:35:11 -05:00
|
|
|
}
|
2013-01-22 23:39:02 -05:00
|
|
|
|
|
|
|
/* If addr is out of range of main-thread stack range estimation, */
|
|
|
|
/* it should be on co-routine (alternative stack). [Feature #2294] */
|
|
|
|
{
|
|
|
|
void *start, *end;
|
2013-04-26 04:43:37 -04:00
|
|
|
STACK_GROW_DIR_DETECTION;
|
2013-01-22 23:39:02 -05:00
|
|
|
|
|
|
|
if (IS_STACK_DIR_UPPER()) {
|
|
|
|
start = native_main_thread.stack_start;
|
|
|
|
end = (char *)native_main_thread.stack_start + native_main_thread.stack_maxsize;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
start = (char *)native_main_thread.stack_start - native_main_thread.stack_maxsize;
|
|
|
|
end = native_main_thread.stack_start;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ((void *)addr < start || (void *)addr > end) {
|
|
|
|
/* out of range */
|
|
|
|
native_main_thread.stack_start = (VALUE *)addr;
|
|
|
|
native_main_thread.stack_maxsize = 0; /* unknown */
|
|
|
|
}
|
|
|
|
}
|
2008-06-13 22:59:19 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
#define CHECK_ERR(expr) \
|
2009-11-24 06:03:51 -05:00
|
|
|
{int err = (expr); if (err) {rb_bug_errno(#expr, err);}}
|
2008-06-13 22:59:19 -04:00
|
|
|
|
|
|
|
static int
|
|
|
|
native_thread_init_stack(rb_thread_t *th)
|
|
|
|
{
|
2013-07-23 06:38:36 -04:00
|
|
|
rb_nativethread_id_t curr = pthread_self();
|
2008-06-13 22:59:19 -04:00
|
|
|
|
|
|
|
if (pthread_equal(curr, native_main_thread.id)) {
|
|
|
|
th->machine_stack_start = native_main_thread.stack_start;
|
|
|
|
th->machine_stack_maxsize = native_main_thread.stack_maxsize;
|
|
|
|
}
|
|
|
|
else {
|
2010-02-04 02:17:03 -05:00
|
|
|
#ifdef STACKADDR_AVAILABLE
|
2008-06-17 08:58:31 -04:00
|
|
|
void *start;
|
2010-02-04 02:17:03 -05:00
|
|
|
size_t size;
|
|
|
|
|
|
|
|
if (get_stack(&start, &size) == 0) {
|
|
|
|
th->machine_stack_start = start;
|
|
|
|
th->machine_stack_maxsize = size;
|
|
|
|
}
|
2008-06-13 22:59:19 -04:00
|
|
|
#else
|
|
|
|
rb_raise(rb_eNotImpError, "ruby engine can initialize only in the main thread");
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
#ifdef __ia64
|
|
|
|
th->machine_register_stack_start = native_main_thread.register_stack_start;
|
|
|
|
th->machine_stack_maxsize /= 2;
|
|
|
|
th->machine_register_stack_maxsize = th->machine_stack_maxsize;
|
|
|
|
#endif
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2011-07-02 17:18:16 -04:00
|
|
|
#ifndef __CYGWIN__
|
|
|
|
#define USE_NATIVE_THREAD_INIT 1
|
|
|
|
#endif
|
|
|
|
|
2006-12-31 10:02:22 -05:00
|
|
|
static void *
|
|
|
|
thread_start_func_1(void *th_ptr)
|
|
|
|
{
|
|
|
|
#if USE_THREAD_CACHE
|
|
|
|
thread_start:
|
|
|
|
#endif
|
|
|
|
{
|
* blockinlining.c, error.c, eval.c, eval_error.h, eval_intern.h,
eval_jump.h, eval_load.c, eval_safe.h, gc.c, proc.c, signal.c,
thread.c, thread_pthread.ci, thread_win32.ci, vm.c, vm.h,
vm_dump.c, vm_evalbody.ci, yarvcore.c, yarvcore.h:
fix typo (rb_thead_t -> rb_thread_t).
* eval_intern.h: remove unused definitions.
* common.mk: fix around vm_opts.h path
and remove harmful argument passed to insns2vm.rb.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@11658 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2007-02-08 01:37:46 -05:00
|
|
|
rb_thread_t *th = th_ptr;
|
2011-07-04 02:52:35 -04:00
|
|
|
#if !defined USE_NATIVE_THREAD_INIT
|
2006-12-31 10:02:22 -05:00
|
|
|
VALUE stack_start;
|
2011-07-04 02:52:35 -04:00
|
|
|
#endif
|
2006-12-31 10:02:22 -05:00
|
|
|
|
2011-07-02 17:18:16 -04:00
|
|
|
#if defined USE_NATIVE_THREAD_INIT
|
2010-05-13 12:20:26 -04:00
|
|
|
native_thread_init_stack(th);
|
2010-07-01 03:25:20 -04:00
|
|
|
#endif
|
2010-11-28 07:46:27 -05:00
|
|
|
native_thread_init(th);
|
2006-12-31 10:02:22 -05:00
|
|
|
/* run */
|
2011-07-02 17:18:16 -04:00
|
|
|
#if defined USE_NATIVE_THREAD_INIT
|
|
|
|
thread_start_func_2(th, th->machine_stack_start, rb_ia64_bsp());
|
|
|
|
#else
|
2007-07-11 21:19:18 -04:00
|
|
|
thread_start_func_2(th, &stack_start, rb_ia64_bsp());
|
2011-07-02 17:18:16 -04:00
|
|
|
#endif
|
2006-12-31 10:02:22 -05:00
|
|
|
}
|
|
|
|
#if USE_THREAD_CACHE
|
|
|
|
if (1) {
|
|
|
|
/* cache thread */
|
* blockinlining.c, error.c, eval.c, eval_error.h, eval_intern.h,
eval_jump.h, eval_load.c, eval_safe.h, gc.c, proc.c, signal.c,
thread.c, thread_pthread.ci, thread_win32.ci, vm.c, vm.h,
vm_dump.c, vm_evalbody.ci, yarvcore.c, yarvcore.h:
fix typo (rb_thead_t -> rb_thread_t).
* eval_intern.h: remove unused definitions.
* common.mk: fix around vm_opts.h path
and remove harmful argument passed to insns2vm.rb.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@11658 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2007-02-08 01:37:46 -05:00
|
|
|
rb_thread_t *th;
|
2006-12-31 10:02:22 -05:00
|
|
|
if ((th = register_cached_thread_and_wait()) != 0) {
|
|
|
|
th_ptr = (void *)th;
|
|
|
|
th->thread_id = pthread_self();
|
|
|
|
goto thread_start;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
struct cached_thread_entry {
|
* blockinlining.c, error.c, eval.c, eval_error.h, eval_intern.h,
eval_jump.h, eval_load.c, eval_safe.h, gc.c, proc.c, signal.c,
thread.c, thread_pthread.ci, thread_win32.ci, vm.c, vm.h,
vm_dump.c, vm_evalbody.ci, yarvcore.c, yarvcore.h:
fix typo (rb_thead_t -> rb_thread_t).
* eval_intern.h: remove unused definitions.
* common.mk: fix around vm_opts.h path
and remove harmful argument passed to insns2vm.rb.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@11658 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2007-02-08 01:37:46 -05:00
|
|
|
volatile rb_thread_t **th_area;
|
2013-07-23 06:50:32 -04:00
|
|
|
rb_nativethread_cond_t *cond;
|
2006-12-31 10:02:22 -05:00
|
|
|
struct cached_thread_entry *next;
|
|
|
|
};
|
|
|
|
|
* this commit is a result of refactoring. only renaming functions,
moving definitions place, add/remove prototypes, deleting
unused variables and removing yarv.h.
This commit doesn't change any behavior of ruby/vm.
* yarv.h, common.mk: remove yarv.h (contents are moved to yarvcore.h).
* error.c, eval_intern.h: include yarvcore.h instead yarv.h
* rename some functions:
* debug.[ch]: debug_*() -> ruby_debug_*()
* iseq.c: iseq_*() -> rb_iseq_*(), ruby_iseq_disasm()
* iseq.c: node_name() -> ruby_node_name()
* vm.c: yarv_check_redefinition_opt_method() ->
rb_vm_check_redefinition_opt_method()
* some refactoring with checking -Wall.
* array.c: remove rb_ary_ptr() (unused) and remove unused
local variables.
* object.c: add a prototype of rb_mod_module_exec().
* eval_intern.h (ruby_cref): set it inline.
* eval_load.c (rb_load), yarvcore.c: yarv_load() -> rb_load_internal().
* parse.y: add a prototype of rb_parse_in_eval() (in eval.c).
* process.c: add a prototype of rb_thread_stop_timer_thread() (in thread.c).
* thread.c: remove raw_gets() function (unused) and fix some format
mismatch (format mismatchs have remained yet. this is todo).
* thread.c (rb_thread_wait_fd_rw): fix typo on label name.
* thread_pthread.ci: comment out codes with USE_THREAD_CACHE.
* vm.c (rb_svar, rb_backref_get, rb_backref_get,
rb_lastline_get, rb_lastline_set) : moved from yarvcore.c.
* vm.c (yarv_init_redefined_flag): add a prototype and rename
yarv_opt_method_table to vm_opt_method_table.
* vm.c (rb_thread_eval): moved from yarvcore.c.
* yarvcore.c: remove unused global variables and fix to use nsdr().
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@11652 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2007-02-06 20:25:05 -05:00
|
|
|
|
|
|
|
#if USE_THREAD_CACHE
|
|
|
|
static pthread_mutex_t thread_cache_lock = PTHREAD_MUTEX_INITIALIZER;
|
2006-12-31 10:02:22 -05:00
|
|
|
struct cached_thread_entry *cached_thread_root;
|
|
|
|
|
* blockinlining.c, error.c, eval.c, eval_error.h, eval_intern.h,
eval_jump.h, eval_load.c, eval_safe.h, gc.c, proc.c, signal.c,
thread.c, thread_pthread.ci, thread_win32.ci, vm.c, vm.h,
vm_dump.c, vm_evalbody.ci, yarvcore.c, yarvcore.h:
fix typo (rb_thead_t -> rb_thread_t).
* eval_intern.h: remove unused definitions.
* common.mk: fix around vm_opts.h path
and remove harmful argument passed to insns2vm.rb.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@11658 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2007-02-08 01:37:46 -05:00
|
|
|
static rb_thread_t *
|
2006-12-31 10:02:22 -05:00
|
|
|
register_cached_thread_and_wait(void)
|
|
|
|
{
|
2013-07-23 06:50:32 -04:00
|
|
|
rb_nativethread_cond_t cond = { PTHREAD_COND_INITIALIZER, };
|
* blockinlining.c, error.c, eval.c, eval_error.h, eval_intern.h,
eval_jump.h, eval_load.c, eval_safe.h, gc.c, proc.c, signal.c,
thread.c, thread_pthread.ci, thread_win32.ci, vm.c, vm.h,
vm_dump.c, vm_evalbody.ci, yarvcore.c, yarvcore.h:
fix typo (rb_thead_t -> rb_thread_t).
* eval_intern.h: remove unused definitions.
* common.mk: fix around vm_opts.h path
and remove harmful argument passed to insns2vm.rb.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@11658 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2007-02-08 01:37:46 -05:00
|
|
|
volatile rb_thread_t *th_area = 0;
|
2012-06-26 04:01:35 -04:00
|
|
|
struct timeval tv;
|
|
|
|
struct timespec ts;
|
2006-12-31 10:02:22 -05:00
|
|
|
struct cached_thread_entry *entry =
|
|
|
|
(struct cached_thread_entry *)malloc(sizeof(struct cached_thread_entry));
|
|
|
|
|
2012-06-26 04:01:35 -04:00
|
|
|
if (entry == 0) {
|
|
|
|
return 0; /* failed -> terminate thread immediately */
|
|
|
|
}
|
|
|
|
|
2006-12-31 10:02:22 -05:00
|
|
|
gettimeofday(&tv, 0);
|
|
|
|
ts.tv_sec = tv.tv_sec + 60;
|
|
|
|
ts.tv_nsec = tv.tv_usec * 1000;
|
* compile.c, dir.c, eval.c, eval_jump.h, eval_method.h, numeric.c,
pack.c, parse.y, re.c, thread.c, vm.c, vm_dump.c, call_cfunc.ci,
thread_pthread.ci, thread_win32.ci: fixed indentation.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@12431 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2007-06-05 00:25:10 -04:00
|
|
|
|
2006-12-31 10:02:22 -05:00
|
|
|
pthread_mutex_lock(&thread_cache_lock);
|
|
|
|
{
|
|
|
|
entry->th_area = &th_area;
|
|
|
|
entry->cond = &cond;
|
|
|
|
entry->next = cached_thread_root;
|
|
|
|
cached_thread_root = entry;
|
|
|
|
|
2011-08-04 11:06:20 -04:00
|
|
|
native_cond_timedwait(&cond, &thread_cache_lock, &ts);
|
2006-12-31 10:02:22 -05:00
|
|
|
|
|
|
|
{
|
|
|
|
struct cached_thread_entry *e = cached_thread_root;
|
|
|
|
struct cached_thread_entry *prev = cached_thread_root;
|
|
|
|
|
|
|
|
while (e) {
|
|
|
|
if (e == entry) {
|
|
|
|
if (prev == cached_thread_root) {
|
|
|
|
cached_thread_root = e->next;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
prev->next = e->next;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
prev = e;
|
|
|
|
e = e->next;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
* array.c, bignum.c, cont.c, dir.c, dln.c, encoding.c, enumerator.c,
enumerator.c (enumerator_allocate), eval_jump.c, file.c, hash.c,
io.c, load.c, pack.c, proc.c, random.c, re.c, ruby.c, st.c,
string.c, thread.c, thread_pthread.c, time.c, util.c, variable.c,
vm.c, gc.c:
allocated memory objects by xmalloc (ruby_xmalloc) should be
freed by xfree (ruby_xfree).
* ext/curses/curses.c, ext/dbm/dbm.c, ext/digest/digest.c,
ext/gdbm/gdbm.c, ext/json/ext/parser/parser.c,
ext/json/ext/parser/unicode.c, ext/openssl/ossl_cipher.c,
ext/openssl/ossl_hmac.c, ext/openssl/ossl_pkey_ec.c,
ext/sdbm/init.c, ext/strscan/strscan.c, ext/zlib/zlib.c:
ditto.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@17017 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2008-06-08 06:01:40 -04:00
|
|
|
free(entry); /* ok */
|
2011-08-04 11:06:20 -04:00
|
|
|
native_cond_destroy(&cond);
|
2006-12-31 10:02:22 -05:00
|
|
|
}
|
|
|
|
pthread_mutex_unlock(&thread_cache_lock);
|
|
|
|
|
* blockinlining.c, error.c, eval.c, eval_error.h, eval_intern.h,
eval_jump.h, eval_load.c, eval_safe.h, gc.c, proc.c, signal.c,
thread.c, thread_pthread.ci, thread_win32.ci, vm.c, vm.h,
vm_dump.c, vm_evalbody.ci, yarvcore.c, yarvcore.h:
fix typo (rb_thead_t -> rb_thread_t).
* eval_intern.h: remove unused definitions.
* common.mk: fix around vm_opts.h path
and remove harmful argument passed to insns2vm.rb.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@11658 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2007-02-08 01:37:46 -05:00
|
|
|
return (rb_thread_t *)th_area;
|
2006-12-31 10:02:22 -05:00
|
|
|
}
|
* this commit is a result of refactoring. only renaming functions,
moving definitions place, add/remove prototypes, deleting
unused variables and removing yarv.h.
This commit doesn't change any behavior of ruby/vm.
* yarv.h, common.mk: remove yarv.h (contents are moved to yarvcore.h).
* error.c, eval_intern.h: include yarvcore.h instead yarv.h
* rename some functions:
* debug.[ch]: debug_*() -> ruby_debug_*()
* iseq.c: iseq_*() -> rb_iseq_*(), ruby_iseq_disasm()
* iseq.c: node_name() -> ruby_node_name()
* vm.c: yarv_check_redefinition_opt_method() ->
rb_vm_check_redefinition_opt_method()
* some refactoring with checking -Wall.
* array.c: remove rb_ary_ptr() (unused) and remove unused
local variables.
* object.c: add a prototype of rb_mod_module_exec().
* eval_intern.h (ruby_cref): set it inline.
* eval_load.c (rb_load), yarvcore.c: yarv_load() -> rb_load_internal().
* parse.y: add a prototype of rb_parse_in_eval() (in eval.c).
* process.c: add a prototype of rb_thread_stop_timer_thread() (in thread.c).
* thread.c: remove raw_gets() function (unused) and fix some format
mismatch (format mismatchs have remained yet. this is todo).
* thread.c (rb_thread_wait_fd_rw): fix typo on label name.
* thread_pthread.ci: comment out codes with USE_THREAD_CACHE.
* vm.c (rb_svar, rb_backref_get, rb_backref_get,
rb_lastline_get, rb_lastline_set) : moved from yarvcore.c.
* vm.c (yarv_init_redefined_flag): add a prototype and rename
yarv_opt_method_table to vm_opt_method_table.
* vm.c (rb_thread_eval): moved from yarvcore.c.
* yarvcore.c: remove unused global variables and fix to use nsdr().
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@11652 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2007-02-06 20:25:05 -05:00
|
|
|
#endif
|
2006-12-31 10:02:22 -05:00
|
|
|
|
|
|
|
static int
|
* blockinlining.c, error.c, eval.c, eval_error.h, eval_intern.h,
eval_jump.h, eval_load.c, eval_safe.h, gc.c, proc.c, signal.c,
thread.c, thread_pthread.ci, thread_win32.ci, vm.c, vm.h,
vm_dump.c, vm_evalbody.ci, yarvcore.c, yarvcore.h:
fix typo (rb_thead_t -> rb_thread_t).
* eval_intern.h: remove unused definitions.
* common.mk: fix around vm_opts.h path
and remove harmful argument passed to insns2vm.rb.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@11658 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2007-02-08 01:37:46 -05:00
|
|
|
use_cached_thread(rb_thread_t *th)
|
2006-12-31 10:02:22 -05:00
|
|
|
{
|
|
|
|
int result = 0;
|
|
|
|
#if USE_THREAD_CACHE
|
|
|
|
struct cached_thread_entry *entry;
|
|
|
|
|
|
|
|
if (cached_thread_root) {
|
|
|
|
pthread_mutex_lock(&thread_cache_lock);
|
|
|
|
entry = cached_thread_root;
|
|
|
|
{
|
|
|
|
if (cached_thread_root) {
|
|
|
|
cached_thread_root = entry->next;
|
|
|
|
*entry->th_area = th;
|
|
|
|
result = 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (result) {
|
2011-08-04 11:06:20 -04:00
|
|
|
native_cond_signal(entry->cond);
|
2006-12-31 10:02:22 -05:00
|
|
|
}
|
|
|
|
pthread_mutex_unlock(&thread_cache_lock);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
* blockinlining.c, error.c, eval.c, eval_error.h, eval_intern.h,
eval_jump.h, eval_load.c, eval_safe.h, gc.c, proc.c, signal.c,
thread.c, thread_pthread.ci, thread_win32.ci, vm.c, vm.h,
vm_dump.c, vm_evalbody.ci, yarvcore.c, yarvcore.h:
fix typo (rb_thead_t -> rb_thread_t).
* eval_intern.h: remove unused definitions.
* common.mk: fix around vm_opts.h path
and remove harmful argument passed to insns2vm.rb.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@11658 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2007-02-08 01:37:46 -05:00
|
|
|
native_thread_create(rb_thread_t *th)
|
2006-12-31 10:02:22 -05:00
|
|
|
{
|
|
|
|
int err = 0;
|
|
|
|
|
|
|
|
if (use_cached_thread(th)) {
|
* compile.c (iseq_compile_each), gc.c (assign_heap_slot),
(gc_mark_children), parse.y (vtable_alloc, vtable_free, vtable_add),
proc.c (proc_to_s), thread.c (terminate_i, rb_thread_terminate_all),
(thread_start_func_2, blocking_region_begin, blocking_region_end),
(rb_thread_kill), thread_pthread.c (native_thread_create),
(ubf_pthread_cond_signal), vm.c (check_env, thread_free), vm_dump.c
(vm_env_dump_raw, vm_stack_dump_each, vm_thread_dump_state),
(vm_call0): use void pointer for %p.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@20593 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2008-12-08 23:33:55 -05:00
|
|
|
thread_debug("create (use cached thread): %p\n", (void *)th);
|
2006-12-31 10:02:22 -05:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
pthread_attr_t attr;
|
2012-12-19 17:29:18 -05:00
|
|
|
const size_t stack_size = th->vm->default_params.thread_machine_stack_size;
|
|
|
|
const size_t space = space_size(stack_size);
|
2006-12-31 10:02:22 -05:00
|
|
|
|
2007-12-14 23:09:24 -05:00
|
|
|
th->machine_stack_maxsize = stack_size - space;
|
|
|
|
#ifdef __ia64
|
|
|
|
th->machine_stack_maxsize /= 2;
|
|
|
|
th->machine_register_stack_maxsize = th->machine_stack_maxsize;
|
|
|
|
#endif
|
2006-12-31 10:02:22 -05:00
|
|
|
|
2012-06-10 08:51:37 -04:00
|
|
|
#ifdef HAVE_PTHREAD_ATTR_INIT
|
2007-02-08 15:24:55 -05:00
|
|
|
CHECK_ERR(pthread_attr_init(&attr));
|
|
|
|
|
2012-06-10 08:51:37 -04:00
|
|
|
# ifdef PTHREAD_STACK_MIN
|
2007-03-07 19:23:14 -05:00
|
|
|
thread_debug("create - stack size: %lu\n", (unsigned long)stack_size);
|
2007-02-08 15:24:55 -05:00
|
|
|
CHECK_ERR(pthread_attr_setstacksize(&attr, stack_size));
|
2012-06-10 08:51:37 -04:00
|
|
|
# endif
|
2007-02-08 15:24:55 -05:00
|
|
|
|
2012-06-10 08:51:37 -04:00
|
|
|
# ifdef HAVE_PTHREAD_ATTR_SETINHERITSCHED
|
2007-02-08 15:24:55 -05:00
|
|
|
CHECK_ERR(pthread_attr_setinheritsched(&attr, PTHREAD_INHERIT_SCHED));
|
2012-06-10 08:51:37 -04:00
|
|
|
# endif
|
2007-02-08 15:24:55 -05:00
|
|
|
CHECK_ERR(pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED));
|
2006-12-31 10:02:22 -05:00
|
|
|
|
|
|
|
err = pthread_create(&th->thread_id, &attr, thread_start_func_1, th);
|
2012-06-10 08:51:37 -04:00
|
|
|
#else
|
|
|
|
err = pthread_create(&th->thread_id, NULL, thread_start_func_1, th);
|
|
|
|
#endif
|
2011-06-16 13:28:54 -04:00
|
|
|
thread_debug("create: %p (%d)\n", (void *)th, err);
|
2012-06-10 08:51:37 -04:00
|
|
|
#ifdef HAVE_PTHREAD_ATTR_INIT
|
2007-02-08 15:24:55 -05:00
|
|
|
CHECK_ERR(pthread_attr_destroy(&attr));
|
2012-06-10 08:51:37 -04:00
|
|
|
#endif
|
2006-12-31 10:02:22 -05:00
|
|
|
}
|
|
|
|
return err;
|
|
|
|
}
|
|
|
|
|
2007-01-07 04:47:52 -05:00
|
|
|
static void
|
|
|
|
native_thread_join(pthread_t th)
|
|
|
|
{
|
|
|
|
int err = pthread_join(th, 0);
|
|
|
|
if (err) {
|
|
|
|
rb_raise(rb_eThreadError, "native_thread_join() failed (%d)", err);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-08-13 03:53:35 -04:00
|
|
|
|
|
|
|
#if USE_NATIVE_THREAD_PRIORITY
|
|
|
|
|
2006-12-31 10:02:22 -05:00
|
|
|
static void
|
* blockinlining.c, error.c, eval.c, eval_error.h, eval_intern.h,
eval_jump.h, eval_load.c, eval_safe.h, gc.c, proc.c, signal.c,
thread.c, thread_pthread.ci, thread_win32.ci, vm.c, vm.h,
vm_dump.c, vm_evalbody.ci, yarvcore.c, yarvcore.h:
fix typo (rb_thead_t -> rb_thread_t).
* eval_intern.h: remove unused definitions.
* common.mk: fix around vm_opts.h path
and remove harmful argument passed to insns2vm.rb.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@11658 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2007-02-08 01:37:46 -05:00
|
|
|
native_thread_apply_priority(rb_thread_t *th)
|
2006-12-31 10:02:22 -05:00
|
|
|
{
|
2007-12-20 05:14:16 -05:00
|
|
|
#if defined(_POSIX_PRIORITY_SCHEDULING) && (_POSIX_PRIORITY_SCHEDULING > 0)
|
2006-12-31 10:02:22 -05:00
|
|
|
struct sched_param sp;
|
|
|
|
int policy;
|
|
|
|
int priority = 0 - th->priority;
|
|
|
|
int max, min;
|
|
|
|
pthread_getschedparam(th->thread_id, &policy, &sp);
|
|
|
|
max = sched_get_priority_max(policy);
|
|
|
|
min = sched_get_priority_min(policy);
|
|
|
|
|
2008-05-19 08:09:14 -04:00
|
|
|
if (min > priority) {
|
2006-12-31 10:02:22 -05:00
|
|
|
priority = min;
|
|
|
|
}
|
2008-05-19 08:09:14 -04:00
|
|
|
else if (max < priority) {
|
|
|
|
priority = max;
|
|
|
|
}
|
2006-12-31 10:02:22 -05:00
|
|
|
|
|
|
|
sp.sched_priority = priority;
|
|
|
|
pthread_setschedparam(th->thread_id, policy, &sp);
|
2007-12-20 05:14:16 -05:00
|
|
|
#else
|
|
|
|
/* not touched */
|
|
|
|
#endif
|
2006-12-31 10:02:22 -05:00
|
|
|
}
|
|
|
|
|
2008-08-13 03:53:35 -04:00
|
|
|
#endif /* USE_NATIVE_THREAD_PRIORITY */
|
|
|
|
|
2011-09-26 20:59:04 -04:00
|
|
|
static int
|
|
|
|
native_fd_select(int n, rb_fdset_t *readfds, rb_fdset_t *writefds, rb_fdset_t *exceptfds, struct timeval *timeout, rb_thread_t *th)
|
|
|
|
{
|
|
|
|
return rb_fd_select(n, readfds, writefds, exceptfds, timeout);
|
|
|
|
}
|
|
|
|
|
2006-12-31 10:02:22 -05:00
|
|
|
static void
|
2007-11-20 05:47:53 -05:00
|
|
|
ubf_pthread_cond_signal(void *ptr)
|
2006-12-31 10:02:22 -05:00
|
|
|
{
|
2007-11-20 05:47:53 -05:00
|
|
|
rb_thread_t *th = (rb_thread_t *)ptr;
|
* compile.c (iseq_compile_each), gc.c (assign_heap_slot),
(gc_mark_children), parse.y (vtable_alloc, vtable_free, vtable_add),
proc.c (proc_to_s), thread.c (terminate_i, rb_thread_terminate_all),
(thread_start_func_2, blocking_region_begin, blocking_region_end),
(rb_thread_kill), thread_pthread.c (native_thread_create),
(ubf_pthread_cond_signal), vm.c (check_env, thread_free), vm_dump.c
(vm_env_dump_raw, vm_stack_dump_each, vm_thread_dump_state),
(vm_call0): use void pointer for %p.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@20593 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2008-12-08 23:33:55 -05:00
|
|
|
thread_debug("ubf_pthread_cond_signal (%p)\n", (void *)th);
|
2011-05-06 12:47:38 -04:00
|
|
|
native_cond_signal(&th->native_thread_data.sleep_cond);
|
2006-12-31 10:02:22 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2011-05-06 14:17:14 -04:00
|
|
|
native_sleep(rb_thread_t *th, struct timeval *timeout_tv)
|
2006-12-31 10:02:22 -05:00
|
|
|
{
|
2011-05-06 14:17:14 -04:00
|
|
|
struct timespec timeout;
|
|
|
|
pthread_mutex_t *lock = &th->interrupt_lock;
|
2013-07-23 06:50:32 -04:00
|
|
|
rb_nativethread_cond_t *cond = &th->native_thread_data.sleep_cond;
|
2006-12-31 10:02:22 -05:00
|
|
|
|
2011-05-06 14:17:14 -04:00
|
|
|
if (timeout_tv) {
|
|
|
|
struct timespec timeout_rel;
|
|
|
|
|
|
|
|
timeout_rel.tv_sec = timeout_tv->tv_sec;
|
2011-06-01 09:03:25 -04:00
|
|
|
timeout_rel.tv_nsec = timeout_tv->tv_usec * 1000;
|
2011-05-06 14:17:14 -04:00
|
|
|
|
2011-07-04 12:32:21 -04:00
|
|
|
/* Solaris cond_timedwait() return EINVAL if an argument is greater than
|
|
|
|
* current_time + 100,000,000. So cut up to 100,000,000. This is
|
|
|
|
* considered as a kind of spurious wakeup. The caller to native_sleep
|
|
|
|
* should care about spurious wakeup.
|
|
|
|
*
|
|
|
|
* See also [Bug #1341] [ruby-core:29702]
|
|
|
|
* http://download.oracle.com/docs/cd/E19683-01/816-0216/6m6ngupgv/index.html
|
|
|
|
*/
|
|
|
|
if (timeout_rel.tv_sec > 100000000) {
|
|
|
|
timeout_rel.tv_sec = 100000000;
|
|
|
|
timeout_rel.tv_nsec = 0;
|
|
|
|
}
|
|
|
|
|
2011-05-06 14:17:14 -04:00
|
|
|
timeout = native_cond_timeout(cond, timeout_rel);
|
2006-12-31 10:02:22 -05:00
|
|
|
}
|
* compile.c, dir.c, eval.c, eval_jump.h, eval_method.h, numeric.c,
pack.c, parse.y, re.c, thread.c, vm.c, vm_dump.c, call_cfunc.ci,
thread_pthread.ci, thread_win32.ci: fixed indentation.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@12431 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2007-06-05 00:25:10 -04:00
|
|
|
|
2006-12-31 10:02:22 -05:00
|
|
|
GVL_UNLOCK_BEGIN();
|
|
|
|
{
|
2011-05-06 14:17:14 -04:00
|
|
|
pthread_mutex_lock(lock);
|
2008-05-29 21:52:38 -04:00
|
|
|
th->unblock.func = ubf_pthread_cond_signal;
|
|
|
|
th->unblock.arg = th;
|
2007-06-05 00:49:54 -04:00
|
|
|
|
2007-12-24 23:16:06 -05:00
|
|
|
if (RUBY_VM_INTERRUPTED(th)) {
|
2006-12-31 10:02:22 -05:00
|
|
|
/* interrupted. return immediate */
|
|
|
|
thread_debug("native_sleep: interrupted before sleep\n");
|
|
|
|
}
|
|
|
|
else {
|
2011-05-06 14:17:14 -04:00
|
|
|
if (!timeout_tv)
|
|
|
|
native_cond_wait(cond, lock);
|
|
|
|
else
|
|
|
|
native_cond_timedwait(cond, lock, &timeout);
|
2006-12-31 10:02:22 -05:00
|
|
|
}
|
2008-05-29 21:52:38 -04:00
|
|
|
th->unblock.func = 0;
|
|
|
|
th->unblock.arg = 0;
|
2006-12-31 10:02:22 -05:00
|
|
|
|
2011-05-06 14:17:14 -04:00
|
|
|
pthread_mutex_unlock(lock);
|
2006-12-31 10:02:22 -05:00
|
|
|
}
|
|
|
|
GVL_UNLOCK_END();
|
2007-12-24 23:16:06 -05:00
|
|
|
|
2006-12-31 10:02:22 -05:00
|
|
|
thread_debug("native_sleep done\n");
|
|
|
|
}
|
|
|
|
|
2011-05-07 21:50:36 -04:00
|
|
|
#ifdef USE_SIGNAL_THREAD_LIST
|
2007-02-08 06:51:40 -05:00
|
|
|
struct signal_thread_list {
|
* blockinlining.c, error.c, eval.c, eval_error.h, eval_intern.h,
eval_jump.h, eval_load.c, eval_safe.h, gc.c, proc.c, signal.c,
thread.c, thread_pthread.ci, thread_win32.ci, vm.c, vm.h,
vm_dump.c, vm_evalbody.ci, yarvcore.c, yarvcore.h:
fix typo (rb_thead_t -> rb_thread_t).
* eval_intern.h: remove unused definitions.
* common.mk: fix around vm_opts.h path
and remove harmful argument passed to insns2vm.rb.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@11658 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2007-02-08 01:37:46 -05:00
|
|
|
rb_thread_t *th;
|
2007-02-08 06:51:40 -05:00
|
|
|
struct signal_thread_list *prev;
|
|
|
|
struct signal_thread_list *next;
|
2006-12-31 10:02:22 -05:00
|
|
|
};
|
|
|
|
|
2007-02-08 06:51:40 -05:00
|
|
|
static struct signal_thread_list signal_thread_list_anchor = {
|
2006-12-31 10:02:22 -05:00
|
|
|
0, 0, 0,
|
|
|
|
};
|
|
|
|
|
|
|
|
#define FGLOCK(lock, body) do { \
|
|
|
|
native_mutex_lock(lock); \
|
|
|
|
{ \
|
|
|
|
body; \
|
|
|
|
} \
|
|
|
|
native_mutex_unlock(lock); \
|
|
|
|
} while (0)
|
|
|
|
|
* this commit is a result of refactoring. only renaming functions,
moving definitions place, add/remove prototypes, deleting
unused variables and removing yarv.h.
This commit doesn't change any behavior of ruby/vm.
* yarv.h, common.mk: remove yarv.h (contents are moved to yarvcore.h).
* error.c, eval_intern.h: include yarvcore.h instead yarv.h
* rename some functions:
* debug.[ch]: debug_*() -> ruby_debug_*()
* iseq.c: iseq_*() -> rb_iseq_*(), ruby_iseq_disasm()
* iseq.c: node_name() -> ruby_node_name()
* vm.c: yarv_check_redefinition_opt_method() ->
rb_vm_check_redefinition_opt_method()
* some refactoring with checking -Wall.
* array.c: remove rb_ary_ptr() (unused) and remove unused
local variables.
* object.c: add a prototype of rb_mod_module_exec().
* eval_intern.h (ruby_cref): set it inline.
* eval_load.c (rb_load), yarvcore.c: yarv_load() -> rb_load_internal().
* parse.y: add a prototype of rb_parse_in_eval() (in eval.c).
* process.c: add a prototype of rb_thread_stop_timer_thread() (in thread.c).
* thread.c: remove raw_gets() function (unused) and fix some format
mismatch (format mismatchs have remained yet. this is todo).
* thread.c (rb_thread_wait_fd_rw): fix typo on label name.
* thread_pthread.ci: comment out codes with USE_THREAD_CACHE.
* vm.c (rb_svar, rb_backref_get, rb_backref_get,
rb_lastline_get, rb_lastline_set) : moved from yarvcore.c.
* vm.c (yarv_init_redefined_flag): add a prototype and rename
yarv_opt_method_table to vm_opt_method_table.
* vm.c (rb_thread_eval): moved from yarvcore.c.
* yarvcore.c: remove unused global variables and fix to use nsdr().
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@11652 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2007-02-06 20:25:05 -05:00
|
|
|
#if 0 /* for debug */
|
2006-12-31 10:02:22 -05:00
|
|
|
static void
|
|
|
|
print_signal_list(char *str)
|
|
|
|
{
|
2007-02-08 06:51:40 -05:00
|
|
|
struct signal_thread_list *list =
|
2006-12-31 10:02:22 -05:00
|
|
|
signal_thread_list_anchor.next;
|
|
|
|
thread_debug("list (%s)> ", str);
|
2012-12-29 07:22:04 -05:00
|
|
|
while (list) {
|
2006-12-31 10:02:22 -05:00
|
|
|
thread_debug("%p (%p), ", list->th, list->th->thread_id);
|
|
|
|
list = list->next;
|
|
|
|
}
|
|
|
|
thread_debug("\n");
|
|
|
|
}
|
* this commit is a result of refactoring. only renaming functions,
moving definitions place, add/remove prototypes, deleting
unused variables and removing yarv.h.
This commit doesn't change any behavior of ruby/vm.
* yarv.h, common.mk: remove yarv.h (contents are moved to yarvcore.h).
* error.c, eval_intern.h: include yarvcore.h instead yarv.h
* rename some functions:
* debug.[ch]: debug_*() -> ruby_debug_*()
* iseq.c: iseq_*() -> rb_iseq_*(), ruby_iseq_disasm()
* iseq.c: node_name() -> ruby_node_name()
* vm.c: yarv_check_redefinition_opt_method() ->
rb_vm_check_redefinition_opt_method()
* some refactoring with checking -Wall.
* array.c: remove rb_ary_ptr() (unused) and remove unused
local variables.
* object.c: add a prototype of rb_mod_module_exec().
* eval_intern.h (ruby_cref): set it inline.
* eval_load.c (rb_load), yarvcore.c: yarv_load() -> rb_load_internal().
* parse.y: add a prototype of rb_parse_in_eval() (in eval.c).
* process.c: add a prototype of rb_thread_stop_timer_thread() (in thread.c).
* thread.c: remove raw_gets() function (unused) and fix some format
mismatch (format mismatchs have remained yet. this is todo).
* thread.c (rb_thread_wait_fd_rw): fix typo on label name.
* thread_pthread.ci: comment out codes with USE_THREAD_CACHE.
* vm.c (rb_svar, rb_backref_get, rb_backref_get,
rb_lastline_get, rb_lastline_set) : moved from yarvcore.c.
* vm.c (yarv_init_redefined_flag): add a prototype and rename
yarv_opt_method_table to vm_opt_method_table.
* vm.c (rb_thread_eval): moved from yarvcore.c.
* yarvcore.c: remove unused global variables and fix to use nsdr().
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@11652 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2007-02-06 20:25:05 -05:00
|
|
|
#endif
|
2006-12-31 10:02:22 -05:00
|
|
|
|
|
|
|
static void
|
2007-02-08 06:51:40 -05:00
|
|
|
add_signal_thread_list(rb_thread_t *th)
|
2006-12-31 10:02:22 -05:00
|
|
|
{
|
|
|
|
if (!th->native_thread_data.signal_thread_list) {
|
|
|
|
FGLOCK(&signal_thread_list_lock, {
|
2007-02-08 06:51:40 -05:00
|
|
|
struct signal_thread_list *list =
|
2011-05-08 04:52:25 -04:00
|
|
|
malloc(sizeof(struct signal_thread_list));
|
2006-12-31 10:02:22 -05:00
|
|
|
|
|
|
|
if (list == 0) {
|
|
|
|
fprintf(stderr, "[FATAL] failed to allocate memory\n");
|
2011-07-10 04:29:46 -04:00
|
|
|
exit(EXIT_FAILURE);
|
2006-12-31 10:02:22 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
list->th = th;
|
|
|
|
|
|
|
|
list->prev = &signal_thread_list_anchor;
|
|
|
|
list->next = signal_thread_list_anchor.next;
|
|
|
|
if (list->next) {
|
|
|
|
list->next->prev = list;
|
|
|
|
}
|
|
|
|
signal_thread_list_anchor.next = list;
|
|
|
|
th->native_thread_data.signal_thread_list = list;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2007-02-08 06:51:40 -05:00
|
|
|
remove_signal_thread_list(rb_thread_t *th)
|
2006-12-31 10:02:22 -05:00
|
|
|
{
|
|
|
|
if (th->native_thread_data.signal_thread_list) {
|
|
|
|
FGLOCK(&signal_thread_list_lock, {
|
2007-02-08 06:51:40 -05:00
|
|
|
struct signal_thread_list *list =
|
|
|
|
(struct signal_thread_list *)
|
2006-12-31 10:02:22 -05:00
|
|
|
th->native_thread_data.signal_thread_list;
|
|
|
|
|
|
|
|
list->prev->next = list->next;
|
|
|
|
if (list->next) {
|
|
|
|
list->next->prev = list->prev;
|
|
|
|
}
|
|
|
|
th->native_thread_data.signal_thread_list = 0;
|
|
|
|
list->th = 0;
|
2011-05-08 04:52:25 -04:00
|
|
|
free(list); /* ok */
|
2006-12-31 10:02:22 -05:00
|
|
|
});
|
|
|
|
}
|
2011-05-07 21:50:36 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
ubf_select_each(rb_thread_t *th)
|
|
|
|
{
|
|
|
|
thread_debug("ubf_select_each (%p)\n", (void *)th->thread_id);
|
|
|
|
if (th) {
|
|
|
|
pthread_kill(th->thread_id, SIGVTALRM);
|
2006-12-31 10:02:22 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-05-07 21:50:36 -04:00
|
|
|
static void
|
|
|
|
ubf_select(void *ptr)
|
|
|
|
{
|
|
|
|
rb_thread_t *th = (rb_thread_t *)ptr;
|
|
|
|
add_signal_thread_list(th);
|
2013-03-09 23:00:00 -05:00
|
|
|
|
|
|
|
/*
|
|
|
|
* ubf_select_each() doesn't guarantee to wake up the target thread.
|
|
|
|
* Therefore, we need to activate timer thread when called from
|
|
|
|
* Thread#kill etc.
|
|
|
|
* In the other hands, we shouldn't call rb_thread_wakeup_timer_thread()
|
|
|
|
* if running on timer thread because it may make endless wakeups.
|
|
|
|
*/
|
2011-12-13 21:26:20 -05:00
|
|
|
if (pthread_self() != timer_thread_id)
|
2013-03-09 23:00:00 -05:00
|
|
|
rb_thread_wakeup_timer_thread();
|
2011-05-07 21:50:36 -04:00
|
|
|
ubf_select_each(th);
|
|
|
|
}
|
|
|
|
|
2011-12-22 08:45:58 -05:00
|
|
|
static void
|
2012-03-14 23:31:50 -04:00
|
|
|
ping_signal_thread_list(void)
|
|
|
|
{
|
2011-05-07 21:50:36 -04:00
|
|
|
if (signal_thread_list_anchor.next) {
|
|
|
|
FGLOCK(&signal_thread_list_lock, {
|
|
|
|
struct signal_thread_list *list;
|
|
|
|
|
|
|
|
list = signal_thread_list_anchor.next;
|
|
|
|
while (list) {
|
|
|
|
ubf_select_each(list->th);
|
|
|
|
list = list->next;
|
|
|
|
}
|
|
|
|
});
|
2011-06-30 04:27:13 -04:00
|
|
|
}
|
2011-12-22 08:45:58 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
check_signal_thread_list(void)
|
|
|
|
{
|
|
|
|
if (signal_thread_list_anchor.next)
|
|
|
|
return 1;
|
|
|
|
else
|
2011-06-30 04:27:13 -04:00
|
|
|
return 0;
|
2011-05-07 21:50:36 -04:00
|
|
|
}
|
|
|
|
#else /* USE_SIGNAL_THREAD_LIST */
|
2011-11-27 03:24:26 -05:00
|
|
|
#define add_signal_thread_list(th) (void)(th)
|
|
|
|
#define remove_signal_thread_list(th) (void)(th)
|
2011-05-07 21:50:36 -04:00
|
|
|
#define ubf_select 0
|
2011-12-22 08:45:58 -05:00
|
|
|
static void ping_signal_thread_list(void) { return; }
|
|
|
|
static int check_signal_thread_list(void) { return 0; }
|
2011-05-07 21:50:36 -04:00
|
|
|
#endif /* USE_SIGNAL_THREAD_LIST */
|
|
|
|
|
2012-06-10 08:51:37 -04:00
|
|
|
#define TT_DEBUG 0
|
|
|
|
#define WRITE_CONST(fd, str) (void)(write((fd),(str),sizeof(str)-1)<0)
|
|
|
|
|
|
|
|
/* 100ms. 10ms is too small for user level thread scheduling
|
|
|
|
* on recent Linux (tested on 2.6.35)
|
|
|
|
*/
|
|
|
|
#define TIME_QUANTUM_USEC (100 * 1000)
|
|
|
|
|
|
|
|
#if USE_SLEEPY_TIMER_THREAD
|
2011-06-26 20:30:41 -04:00
|
|
|
static int timer_thread_pipe[2] = {-1, -1};
|
2013-03-09 23:00:21 -05:00
|
|
|
static int timer_thread_pipe_low[2] = {-1, -1}; /* low priority */
|
2011-06-26 20:30:41 -04:00
|
|
|
static int timer_thread_pipe_owner_process;
|
|
|
|
|
2011-06-28 17:17:29 -04:00
|
|
|
/* only use signal-safe system calls here */
|
2013-03-09 23:00:10 -05:00
|
|
|
static void
|
|
|
|
rb_thread_wakeup_timer_thread_fd(int fd)
|
2011-06-26 20:30:41 -04:00
|
|
|
{
|
2011-06-26 22:43:03 -04:00
|
|
|
ssize_t result;
|
2011-06-26 20:30:41 -04:00
|
|
|
|
|
|
|
/* already opened */
|
|
|
|
if (timer_thread_pipe_owner_process == getpid()) {
|
|
|
|
const char *buff = "!";
|
|
|
|
retry:
|
2013-03-09 23:00:10 -05:00
|
|
|
if ((result = write(fd, buff, 1)) <= 0) {
|
2011-06-26 20:30:41 -04:00
|
|
|
switch (errno) {
|
|
|
|
case EINTR: goto retry;
|
|
|
|
case EAGAIN:
|
|
|
|
#if defined(EWOULDBLOCK) && EWOULDBLOCK != EAGAIN
|
|
|
|
case EWOULDBLOCK:
|
|
|
|
#endif
|
|
|
|
break;
|
|
|
|
default:
|
2011-06-28 17:17:29 -04:00
|
|
|
rb_async_bug_errno("rb_thread_wakeup_timer_thread - write", errno);
|
2011-06-26 20:30:41 -04:00
|
|
|
}
|
|
|
|
}
|
2011-06-28 17:17:29 -04:00
|
|
|
if (TT_DEBUG) WRITE_CONST(2, "rb_thread_wakeup_timer_thread: write\n");
|
2011-06-26 20:30:41 -04:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
/* ignore wakeup */
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-03-09 23:00:10 -05:00
|
|
|
void
|
|
|
|
rb_thread_wakeup_timer_thread(void)
|
|
|
|
{
|
|
|
|
rb_thread_wakeup_timer_thread_fd(timer_thread_pipe[1]);
|
|
|
|
}
|
|
|
|
|
2013-03-09 23:00:21 -05:00
|
|
|
static void
|
|
|
|
rb_thread_wakeup_timer_thread_low(void)
|
|
|
|
{
|
|
|
|
rb_thread_wakeup_timer_thread_fd(timer_thread_pipe_low[1]);
|
|
|
|
}
|
|
|
|
|
2011-06-28 17:17:29 -04:00
|
|
|
/* VM-dependent API is not available for this function */
|
2011-06-27 09:05:22 -04:00
|
|
|
static void
|
2013-03-09 23:00:10 -05:00
|
|
|
consume_communication_pipe(int fd)
|
2011-06-26 20:30:41 -04:00
|
|
|
{
|
2011-06-27 22:53:15 -04:00
|
|
|
#define CCP_READ_BUFF_SIZE 1024
|
|
|
|
/* buffer can be shared because no one refers to them. */
|
2011-11-04 03:19:23 -04:00
|
|
|
static char buff[CCP_READ_BUFF_SIZE];
|
2011-06-26 22:43:03 -04:00
|
|
|
ssize_t result;
|
2011-06-27 22:53:15 -04:00
|
|
|
|
2013-03-09 22:59:39 -05:00
|
|
|
while (1) {
|
2013-03-09 23:00:10 -05:00
|
|
|
result = read(fd, buff, sizeof(buff));
|
2013-03-09 22:59:39 -05:00
|
|
|
if (result == 0) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
else if (result < 0) {
|
|
|
|
switch (errno) {
|
|
|
|
case EINTR:
|
|
|
|
continue; /* retry */
|
|
|
|
case EAGAIN:
|
|
|
|
return;
|
|
|
|
default:
|
|
|
|
rb_async_bug_errno("consume_communication_pipe: read\n", errno);
|
|
|
|
}
|
2011-06-26 20:30:41 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2013-03-09 23:00:10 -05:00
|
|
|
close_communication_pipe(int pipes[2])
|
2011-06-26 20:30:41 -04:00
|
|
|
{
|
2013-03-09 23:00:10 -05:00
|
|
|
if (close(pipes[0]) < 0) {
|
2011-06-26 20:30:41 -04:00
|
|
|
rb_bug_errno("native_stop_timer_thread - close(ttp[0])", errno);
|
|
|
|
}
|
2013-03-09 23:00:10 -05:00
|
|
|
if (close(pipes[1]) < 0) {
|
2011-06-26 20:30:41 -04:00
|
|
|
rb_bug_errno("native_stop_timer_thread - close(ttp[1])", errno);
|
|
|
|
}
|
2013-03-09 23:00:10 -05:00
|
|
|
pipes[0] = pipes[1] = -1;
|
2011-06-26 20:30:41 -04:00
|
|
|
}
|
2008-11-06 14:02:35 -05:00
|
|
|
|
2013-03-09 22:59:49 -05:00
|
|
|
static void
|
|
|
|
set_nonblock(int fd)
|
|
|
|
{
|
|
|
|
int oflags;
|
|
|
|
int err;
|
|
|
|
|
|
|
|
oflags = fcntl(fd, F_GETFL);
|
|
|
|
if (oflags == -1)
|
|
|
|
rb_sys_fail(0);
|
|
|
|
oflags |= O_NONBLOCK;
|
|
|
|
err = fcntl(fd, F_SETFL, oflags);
|
|
|
|
if (err == -1)
|
|
|
|
rb_sys_fail(0);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2013-03-09 23:00:10 -05:00
|
|
|
setup_communication_pipe_internal(int pipes[2])
|
2013-03-09 22:59:49 -05:00
|
|
|
{
|
|
|
|
int err;
|
|
|
|
|
2013-03-09 23:00:10 -05:00
|
|
|
if (pipes[0] != -1) {
|
|
|
|
/* close pipe of parent process */
|
|
|
|
close_communication_pipe(pipes);
|
|
|
|
}
|
2013-03-09 22:59:49 -05:00
|
|
|
|
2013-03-09 23:00:10 -05:00
|
|
|
err = rb_cloexec_pipe(pipes);
|
|
|
|
if (err != 0) {
|
|
|
|
rb_bug_errno("setup_communication_pipe: Failed to create communication pipe for timer thread", errno);
|
|
|
|
}
|
|
|
|
rb_update_max_fd(pipes[0]);
|
|
|
|
rb_update_max_fd(pipes[1]);
|
|
|
|
set_nonblock(pipes[0]);
|
|
|
|
set_nonblock(pipes[1]);
|
|
|
|
}
|
2013-03-09 22:59:49 -05:00
|
|
|
|
2013-03-09 23:00:10 -05:00
|
|
|
/* communication pipe with timer thread and signal handler */
|
|
|
|
static void
|
|
|
|
setup_communication_pipe(void)
|
|
|
|
{
|
|
|
|
if (timer_thread_pipe_owner_process == getpid()) {
|
|
|
|
/* already set up. */
|
|
|
|
return;
|
2013-03-09 22:59:49 -05:00
|
|
|
}
|
2013-03-09 23:00:10 -05:00
|
|
|
setup_communication_pipe_internal(timer_thread_pipe);
|
2013-03-09 23:00:21 -05:00
|
|
|
setup_communication_pipe_internal(timer_thread_pipe_low);
|
2013-03-09 23:00:10 -05:00
|
|
|
|
|
|
|
/* validate pipe on this process */
|
|
|
|
timer_thread_pipe_owner_process = getpid();
|
2013-03-09 22:59:49 -05:00
|
|
|
}
|
|
|
|
|
2012-06-10 08:51:37 -04:00
|
|
|
/**
|
|
|
|
* Let the timer thread sleep a while.
|
|
|
|
*
|
|
|
|
* The timer thread sleeps until woken up by rb_thread_wakeup_timer_thread() if only one Ruby thread is running.
|
|
|
|
* @pre the calling context is in the timer thread.
|
2011-06-13 10:14:53 -04:00
|
|
|
*/
|
2012-06-10 08:51:37 -04:00
|
|
|
static inline void
|
|
|
|
timer_thread_sleep(rb_global_vm_lock_t* gvl)
|
|
|
|
{
|
|
|
|
int result;
|
|
|
|
int need_polling;
|
2013-03-09 23:00:21 -05:00
|
|
|
struct pollfd pollfds[2];
|
2013-03-09 22:59:29 -05:00
|
|
|
|
2013-03-09 23:00:21 -05:00
|
|
|
pollfds[0].fd = timer_thread_pipe[0];
|
|
|
|
pollfds[0].events = POLLIN;
|
|
|
|
pollfds[1].fd = timer_thread_pipe_low[0];
|
|
|
|
pollfds[1].events = POLLIN;
|
2012-06-10 08:51:37 -04:00
|
|
|
|
|
|
|
need_polling = check_signal_thread_list();
|
|
|
|
|
|
|
|
if (gvl->waiting > 0 || need_polling) {
|
|
|
|
/* polling (TIME_QUANTUM_USEC usec) */
|
2013-03-09 23:00:21 -05:00
|
|
|
result = poll(pollfds, 1, TIME_QUANTUM_USEC/1000);
|
2012-06-10 08:51:37 -04:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
/* wait (infinite) */
|
2013-03-15 17:26:36 -04:00
|
|
|
result = poll(pollfds, numberof(pollfds), -1);
|
2012-06-10 08:51:37 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
if (result == 0) {
|
|
|
|
/* maybe timeout */
|
|
|
|
}
|
|
|
|
else if (result > 0) {
|
2013-03-09 23:00:10 -05:00
|
|
|
consume_communication_pipe(timer_thread_pipe[0]);
|
2013-03-09 23:00:21 -05:00
|
|
|
consume_communication_pipe(timer_thread_pipe_low[0]);
|
2012-06-10 08:51:37 -04:00
|
|
|
}
|
|
|
|
else { /* result < 0 */
|
|
|
|
switch (errno) {
|
|
|
|
case EBADF:
|
|
|
|
case EINVAL:
|
|
|
|
case ENOMEM: /* from Linux man */
|
|
|
|
case EFAULT: /* from FreeBSD man */
|
|
|
|
rb_async_bug_errno("thread_timer: select", errno);
|
|
|
|
default:
|
|
|
|
/* ignore */;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#else /* USE_SLEEPY_TIMER_THREAD */
|
|
|
|
# define PER_NANO 1000000000
|
|
|
|
void rb_thread_wakeup_timer_thread(void) {}
|
2013-04-04 04:01:27 -04:00
|
|
|
static void rb_thread_wakeup_timer_thread_low(void) {}
|
2012-06-10 08:51:37 -04:00
|
|
|
|
|
|
|
static pthread_mutex_t timer_thread_lock;
|
2013-07-23 06:50:32 -04:00
|
|
|
static rb_nativethread_cond_t timer_thread_cond;
|
2012-06-10 08:51:37 -04:00
|
|
|
|
|
|
|
static inline void
|
2012-09-03 22:52:44 -04:00
|
|
|
timer_thread_sleep(rb_global_vm_lock_t* unused)
|
|
|
|
{
|
2012-06-10 08:51:37 -04:00
|
|
|
struct timespec ts;
|
|
|
|
ts.tv_sec = 0;
|
|
|
|
ts.tv_nsec = TIME_QUANTUM_USEC * 1000;
|
|
|
|
ts = native_cond_timeout(&timer_thread_cond, ts);
|
|
|
|
|
|
|
|
native_cond_timedwait(&timer_thread_cond, &timer_thread_lock, &ts);
|
|
|
|
}
|
|
|
|
#endif /* USE_SLEEPY_TIMER_THREAD */
|
2011-06-13 10:14:53 -04:00
|
|
|
|
2006-12-31 10:02:22 -05:00
|
|
|
static void *
|
2011-06-26 20:30:41 -04:00
|
|
|
thread_timer(void *p)
|
2006-12-31 10:02:22 -05:00
|
|
|
{
|
2011-06-26 20:30:41 -04:00
|
|
|
rb_global_vm_lock_t *gvl = (rb_global_vm_lock_t *)p;
|
2011-05-06 13:39:32 -04:00
|
|
|
|
2011-06-28 17:17:29 -04:00
|
|
|
if (TT_DEBUG) WRITE_CONST(2, "start timer thread\n");
|
2011-06-05 08:48:23 -04:00
|
|
|
|
2011-12-01 09:28:42 -05:00
|
|
|
#if defined(__linux__) && defined(PR_SET_NAME)
|
2011-11-30 17:17:48 -05:00
|
|
|
prctl(PR_SET_NAME, "ruby-timer-thr");
|
|
|
|
#endif
|
|
|
|
|
2012-06-10 08:51:37 -04:00
|
|
|
#if !USE_SLEEPY_TIMER_THREAD
|
|
|
|
native_mutex_initialize(&timer_thread_lock);
|
|
|
|
native_cond_initialize(&timer_thread_cond, RB_CONDATTR_CLOCK_MONOTONIC);
|
|
|
|
native_mutex_lock(&timer_thread_lock);
|
|
|
|
#endif
|
2009-01-01 16:50:01 -05:00
|
|
|
while (system_working > 0) {
|
2011-05-06 13:39:32 -04:00
|
|
|
|
2011-06-26 20:30:41 -04:00
|
|
|
/* timer function */
|
2011-12-22 08:45:58 -05:00
|
|
|
ping_signal_thread_list();
|
2011-06-26 20:30:41 -04:00
|
|
|
timer_thread_function(0);
|
2011-06-30 04:27:13 -04:00
|
|
|
|
2011-06-28 17:17:29 -04:00
|
|
|
if (TT_DEBUG) WRITE_CONST(2, "tick\n");
|
2011-06-26 20:30:41 -04:00
|
|
|
|
2012-06-10 08:51:37 -04:00
|
|
|
/* wait */
|
|
|
|
timer_thread_sleep(gvl);
|
2006-12-31 10:02:22 -05:00
|
|
|
}
|
2012-06-10 08:51:37 -04:00
|
|
|
#if !USE_SLEEPY_TIMER_THREAD
|
|
|
|
native_mutex_unlock(&timer_thread_lock);
|
|
|
|
native_cond_destroy(&timer_thread_cond);
|
|
|
|
native_mutex_destroy(&timer_thread_lock);
|
|
|
|
#endif
|
2011-06-26 20:30:41 -04:00
|
|
|
|
2011-06-28 17:17:29 -04:00
|
|
|
if (TT_DEBUG) WRITE_CONST(2, "finish timer thread\n");
|
2006-12-31 10:02:22 -05:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
rb_thread_create_timer_thread(void)
|
|
|
|
{
|
|
|
|
if (!timer_thread_id) {
|
|
|
|
int err;
|
2012-06-10 08:51:37 -04:00
|
|
|
#ifdef HAVE_PTHREAD_ATTR_INIT
|
|
|
|
pthread_attr_t attr;
|
2006-12-31 10:02:22 -05:00
|
|
|
|
2012-06-10 17:21:51 -04:00
|
|
|
err = pthread_attr_init(&attr);
|
|
|
|
if (err != 0) {
|
2012-06-10 08:51:37 -04:00
|
|
|
fprintf(stderr, "[FATAL] Failed to initialize pthread attr(errno: %d)\n", err);
|
|
|
|
exit(EXIT_FAILURE);
|
|
|
|
}
|
|
|
|
# ifdef PTHREAD_STACK_MIN
|
2011-06-29 23:34:16 -04:00
|
|
|
if (PTHREAD_STACK_MIN < 4096 * 3) {
|
|
|
|
/* Allocate the machine stack for the timer thread
|
|
|
|
* at least 12KB (3 pages). FreeBSD 8.2 AMD64 causes
|
|
|
|
* machine stack overflow only with PTHREAD_STACK_MIN.
|
|
|
|
*/
|
|
|
|
pthread_attr_setstacksize(&attr,
|
|
|
|
4096 * 3 + (THREAD_DEBUG ? BUFSIZ : 0));
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
pthread_attr_setstacksize(&attr,
|
|
|
|
PTHREAD_STACK_MIN + (THREAD_DEBUG ? BUFSIZ : 0));
|
|
|
|
}
|
2012-06-10 08:51:37 -04:00
|
|
|
# endif
|
2007-01-03 20:38:01 -05:00
|
|
|
#endif
|
2011-06-26 20:30:41 -04:00
|
|
|
|
2013-04-04 04:01:27 -04:00
|
|
|
#if USE_SLEEPY_TIMER_THREAD
|
2013-03-09 22:59:49 -05:00
|
|
|
setup_communication_pipe();
|
2013-04-04 04:01:27 -04:00
|
|
|
#endif /* USE_SLEEPY_TIMER_THREAD */
|
2011-06-26 20:30:41 -04:00
|
|
|
|
|
|
|
/* create timer thread */
|
|
|
|
if (timer_thread_id) {
|
|
|
|
rb_bug("rb_thread_create_timer_thread: Timer thread was already created\n");
|
|
|
|
}
|
2012-06-10 08:51:37 -04:00
|
|
|
#ifdef HAVE_PTHREAD_ATTR_INIT
|
2011-06-26 20:30:41 -04:00
|
|
|
err = pthread_create(&timer_thread_id, &attr, thread_timer, &GET_VM()->gvl);
|
2012-06-10 08:51:37 -04:00
|
|
|
#else
|
|
|
|
err = pthread_create(&timer_thread_id, NULL, thread_timer, &GET_VM()->gvl);
|
|
|
|
#endif
|
2006-12-31 10:02:22 -05:00
|
|
|
if (err != 0) {
|
2009-06-07 19:59:58 -04:00
|
|
|
fprintf(stderr, "[FATAL] Failed to create timer thread (errno: %d)\n", err);
|
|
|
|
exit(EXIT_FAILURE);
|
2006-12-31 10:02:22 -05:00
|
|
|
}
|
2012-06-10 08:51:37 -04:00
|
|
|
#ifdef HAVE_PTHREAD_ATTR_INIT
|
2011-12-13 11:33:15 -05:00
|
|
|
pthread_attr_destroy(&attr);
|
2012-05-16 22:48:59 -04:00
|
|
|
#endif
|
2012-06-10 08:51:37 -04:00
|
|
|
}
|
2006-12-31 10:02:22 -05:00
|
|
|
}
|
|
|
|
|
2008-11-08 10:31:05 -05:00
|
|
|
static int
|
2011-06-26 20:30:41 -04:00
|
|
|
native_stop_timer_thread(int close_anyway)
|
2008-11-07 11:14:48 -05:00
|
|
|
{
|
2008-11-08 10:31:05 -05:00
|
|
|
int stopped;
|
|
|
|
stopped = --system_working <= 0;
|
2011-06-26 20:30:41 -04:00
|
|
|
|
|
|
|
if (TT_DEBUG) fprintf(stderr, "stop timer thread\n");
|
2008-11-08 10:31:05 -05:00
|
|
|
if (stopped) {
|
2011-06-26 20:30:41 -04:00
|
|
|
/* join */
|
|
|
|
rb_thread_wakeup_timer_thread();
|
2009-11-02 00:45:35 -05:00
|
|
|
native_thread_join(timer_thread_id);
|
2011-06-26 20:30:41 -04:00
|
|
|
if (TT_DEBUG) fprintf(stderr, "joined timer thread\n");
|
|
|
|
timer_thread_id = 0;
|
|
|
|
|
|
|
|
/* close communication pipe */
|
|
|
|
if (close_anyway) {
|
|
|
|
/* TODO: Uninstall all signal handlers or mask all signals.
|
2011-06-28 17:23:40 -04:00
|
|
|
* This pass is cleaning phase (terminate ruby process).
|
|
|
|
* To avoid such race, we skip to close communication
|
|
|
|
* pipe. OS will close it at process termination.
|
|
|
|
* It may not good practice, but pragmatic.
|
|
|
|
* We remain it is TODO.
|
2011-06-26 20:30:41 -04:00
|
|
|
*/
|
2011-06-28 17:23:40 -04:00
|
|
|
/* close_communication_pipe(); */
|
2011-06-26 20:30:41 -04:00
|
|
|
}
|
2009-11-02 00:45:35 -05:00
|
|
|
}
|
2008-11-08 10:31:05 -05:00
|
|
|
return stopped;
|
2008-11-07 11:14:48 -05:00
|
|
|
}
|
2008-11-06 08:21:26 -05:00
|
|
|
|
2009-11-01 22:58:25 -05:00
|
|
|
static void
|
|
|
|
native_reset_timer_thread(void)
|
|
|
|
{
|
2011-06-26 20:30:41 -04:00
|
|
|
if (TT_DEBUG) fprintf(stderr, "reset timer thread\n");
|
2009-11-01 22:58:25 -05:00
|
|
|
}
|
|
|
|
|
2008-11-27 01:05:07 -05:00
|
|
|
#ifdef HAVE_SIGALTSTACK
|
|
|
|
int
|
|
|
|
ruby_stack_overflowed_p(const rb_thread_t *th, const void *addr)
|
|
|
|
{
|
|
|
|
void *base;
|
|
|
|
size_t size;
|
|
|
|
const size_t water_mark = 1024 * 1024;
|
|
|
|
STACK_GROW_DIR_DETECTION;
|
|
|
|
|
|
|
|
if (th) {
|
|
|
|
size = th->machine_stack_maxsize;
|
|
|
|
base = (char *)th->machine_stack_start - STACK_DIR_UPPER(0, size);
|
|
|
|
}
|
|
|
|
#ifdef STACKADDR_AVAILABLE
|
|
|
|
else if (get_stack(&base, &size) == 0) {
|
2010-04-14 03:29:04 -04:00
|
|
|
STACK_DIR_UPPER((void)(base = (char *)base + size), (void)0);
|
2008-11-27 01:05:07 -05:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
else {
|
|
|
|
return 0;
|
|
|
|
}
|
2012-10-03 22:39:34 -04:00
|
|
|
size /= RUBY_STACK_SPACE_RATIO;
|
2008-11-27 01:05:07 -05:00
|
|
|
if (size > water_mark) size = water_mark;
|
2012-06-10 04:54:38 -04:00
|
|
|
if (IS_STACK_DIR_UPPER()) {
|
2008-11-27 01:05:07 -05:00
|
|
|
if (size > ~(size_t)base+1) size = ~(size_t)base+1;
|
|
|
|
if (addr > base && addr <= (void *)((char *)base + size)) return 1;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
if (size > (size_t)base) size = (size_t)base;
|
|
|
|
if (addr > (void *)((char *)base - size) && addr <= base) return 1;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2011-07-03 17:56:59 -04:00
|
|
|
int
|
|
|
|
rb_reserved_fd_p(int fd)
|
|
|
|
{
|
2013-01-03 00:46:29 -05:00
|
|
|
#if USE_SLEEPY_TIMER_THREAD
|
2013-03-09 23:00:21 -05:00
|
|
|
if (fd == timer_thread_pipe[0] ||
|
|
|
|
fd == timer_thread_pipe[1] ||
|
|
|
|
fd == timer_thread_pipe_low[0] ||
|
|
|
|
fd == timer_thread_pipe_low[1]) {
|
2011-07-03 17:56:59 -04:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
return 0;
|
|
|
|
}
|
2012-06-10 08:51:37 -04:00
|
|
|
#else
|
|
|
|
return 0;
|
|
|
|
#endif
|
2011-07-03 17:56:59 -04:00
|
|
|
}
|
|
|
|
|
2013-07-23 06:46:37 -04:00
|
|
|
rb_nativethread_id_t
|
|
|
|
rb_nativethread_self(void)
|
|
|
|
{
|
|
|
|
return pthread_self();
|
|
|
|
}
|
|
|
|
|
2006-12-31 10:02:22 -05:00
|
|
|
#endif /* THREAD_SYSTEM_DEPENDENT_IMPLEMENTATION */
|