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
|
|
|
|
|
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);
|
|
|
|
|
|
|
|
static void native_cond_signal(pthread_cond_t *cond);
|
|
|
|
static void native_cond_broadcast(pthread_cond_t *cond);
|
|
|
|
static void native_cond_wait(pthread_cond_t *cond, pthread_mutex_t *mutex);
|
|
|
|
static void native_cond_initialize(pthread_cond_t *cond);
|
|
|
|
static void native_cond_destroy(pthread_cond_t *cond);
|
|
|
|
|
|
|
|
static void
|
2007-02-08 15:24:55 -05:00
|
|
|
native_mutex_lock(pthread_mutex_t *lock)
|
|
|
|
{
|
|
|
|
int r;
|
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;
|
|
|
|
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;
|
|
|
|
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);
|
|
|
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-12-10 01:49:15 -05:00
|
|
|
#define native_mutex_reinitialize_atfork(lock) (\
|
|
|
|
native_mutex_unlock(lock), \
|
|
|
|
native_mutex_initialize(lock), \
|
|
|
|
native_mutex_lock(lock))
|
2009-11-18 04:25:46 -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);
|
|
|
|
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
|
2007-08-27 12:48:14 -04:00
|
|
|
native_cond_initialize(pthread_cond_t *cond)
|
|
|
|
{
|
|
|
|
int r = pthread_cond_init(cond, 0);
|
|
|
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-12-24 23:35:17 -05:00
|
|
|
static void
|
2007-08-27 12:48:14 -04:00
|
|
|
native_cond_destroy(pthread_cond_t *cond)
|
|
|
|
{
|
|
|
|
int r = pthread_cond_destroy(cond);
|
|
|
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-12-24 23:35:17 -05:00
|
|
|
static void
|
2007-08-27 12:48:14 -04:00
|
|
|
native_cond_signal(pthread_cond_t *cond)
|
|
|
|
{
|
|
|
|
pthread_cond_signal(cond);
|
|
|
|
}
|
|
|
|
|
2007-12-24 23:35:17 -05:00
|
|
|
static void
|
2007-08-27 12:48:14 -04:00
|
|
|
native_cond_broadcast(pthread_cond_t *cond)
|
|
|
|
{
|
|
|
|
pthread_cond_broadcast(cond);
|
|
|
|
}
|
|
|
|
|
2007-12-24 23:35:17 -05:00
|
|
|
static void
|
2007-08-27 12:48:14 -04:00
|
|
|
native_cond_wait(pthread_cond_t *cond, pthread_mutex_t *mutex)
|
|
|
|
{
|
|
|
|
pthread_cond_wait(cond, mutex);
|
|
|
|
}
|
|
|
|
|
2008-11-07 11:42:31 -05:00
|
|
|
static int
|
2008-11-07 11:14:48 -05:00
|
|
|
native_cond_timedwait(pthread_cond_t *cond, pthread_mutex_t *mutex, struct timespec *ts)
|
|
|
|
{
|
2008-11-07 11:42:31 -05:00
|
|
|
return pthread_cond_timedwait(cond, mutex, ts);
|
2008-11-07 11:14:48 -05:00
|
|
|
}
|
|
|
|
|
2007-08-27 12:48:14 -04:00
|
|
|
|
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
|
|
|
|
2008-04-20 02:14:49 -04:00
|
|
|
#ifndef __CYGWIN__
|
2007-02-08 06:51:40 -05:00
|
|
|
static void add_signal_thread_list(rb_thread_t *th);
|
2008-04-20 02:14:49 -04:00
|
|
|
#endif
|
2007-02-08 06:51:40 -05:00
|
|
|
static void remove_signal_thread_list(rb_thread_t *th);
|
2006-12-31 10:02:22 -05:00
|
|
|
|
* blockinlining.c, compile.c, compile.h, error.c, eval.c,
eval_intern.h, eval_jump.h, eval_load.c, eval_method.h,
eval_safe.h, gc.c, insnhelper.h, insns.def, iseq.c, proc.c,
process.c, signal.c, thread.c, thread_pthread.ci, thread_win32.ci,
vm.c, vm.h, vm_dump.c, vm_evalbody.ci, vm_macro.def,
yarv.h, yarvcore.h, yarvcore.c: change type and macro names:
* yarv_*_t -> rb_*_t
* yarv_*_struct -> rb_*_struct
* yarv_tag -> rb_vm_tag
* YARV_* -> RUBY_VM_*
* proc.c, vm.c: move functions about env object creation
from proc.c to vm.c.
* proc.c, yarvcore.c: fix rb_cVM initialization place.
* inits.c: change Init_ISeq() order (after Init_VM).
* ruby.h, proc.c: change declaration place of rb_cEnv
from proc.c to ruby.c.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@11651 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2007-02-06 14:00:03 -05:00
|
|
|
static rb_thread_lock_t signal_thread_list_lock;
|
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;
|
|
|
|
}
|
|
|
|
|
2006-12-31 10:02:22 -05:00
|
|
|
static 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();
|
2008-05-16 23:37:37 -04:00
|
|
|
native_cond_initialize(&th->native_thread_data.sleep_cond);
|
2007-08-18 04:40:13 -04:00
|
|
|
ruby_thread_set_native(th);
|
2006-12-31 10:02:22 -05:00
|
|
|
native_mutex_initialize(&signal_thread_list_lock);
|
|
|
|
posix_signal(SIGVTALRM, null_func);
|
|
|
|
}
|
|
|
|
|
2007-02-08 15:24:55 -05:00
|
|
|
static void
|
|
|
|
native_thread_destroy(rb_thread_t *th)
|
|
|
|
{
|
2008-01-18 01:57:08 -05:00
|
|
|
pthread_mutex_destroy(&th->interrupt_lock);
|
2007-02-08 15:24:55 -05:00
|
|
|
pthread_cond_destroy(&th->native_thread_data.sleep_cond);
|
|
|
|
}
|
2006-12-31 10:02:22 -05:00
|
|
|
|
|
|
|
#define USE_THREAD_CACHE 0
|
|
|
|
|
2008-11-27 01:05:07 -05:00
|
|
|
#if STACK_GROW_DIRECTION
|
|
|
|
#define STACK_GROW_DIR_DETECTION
|
|
|
|
#define STACK_DIR_UPPER(a,b) STACK_UPPER(0, a, b)
|
|
|
|
#else
|
|
|
|
#define STACK_GROW_DIR_DETECTION VALUE stack_grow_dir_detection
|
|
|
|
#define STACK_DIR_UPPER(a,b) STACK_UPPER(&stack_grow_dir_detection, a, b)
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#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
|
|
|
|
#elif defined HAVE_THR_STKSEGMENT || defined HAVE_PTHREAD_STACKSEG_NP
|
|
|
|
#define STACKADDR_AVAILABLE 1
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifdef STACKADDR_AVAILABLE
|
|
|
|
static int
|
|
|
|
get_stack(void **addr, size_t *size)
|
|
|
|
{
|
|
|
|
#define CHECK_ERR(expr) \
|
|
|
|
{int err = (expr); if (err) return err;}
|
|
|
|
#if defined HAVE_PTHREAD_GETATTR_NP || defined HAVE_PTHREAD_ATTR_GET_NP
|
|
|
|
pthread_attr_t attr;
|
|
|
|
size_t guard = 0;
|
|
|
|
|
|
|
|
# ifdef HAVE_PTHREAD_GETATTR_NP
|
|
|
|
CHECK_ERR(pthread_getattr_np(pthread_self(), &attr));
|
|
|
|
# ifdef HAVE_PTHREAD_ATTR_GETSTACK
|
|
|
|
CHECK_ERR(pthread_attr_getstack(&attr, addr, size));
|
|
|
|
# else
|
|
|
|
CHECK_ERR(pthread_attr_getstackaddr(&attr, addr));
|
|
|
|
CHECK_ERR(pthread_attr_getstacksize(&attr, size));
|
|
|
|
# endif
|
|
|
|
if (pthread_attr_getguardsize(&attr, &guard) == 0) {
|
|
|
|
STACK_GROW_DIR_DETECTION;
|
2010-04-14 03:29:04 -04:00
|
|
|
STACK_DIR_UPPER((void)0, (void)(*addr = (char *)*addr + guard));
|
2008-11-27 01:05:07 -05:00
|
|
|
*size -= guard;
|
|
|
|
}
|
|
|
|
# else
|
|
|
|
CHECK_ERR(pthread_attr_init(&attr));
|
|
|
|
CHECK_ERR(pthread_attr_get_np(pthread_self(), &attr));
|
|
|
|
CHECK_ERR(pthread_attr_getstackaddr(&attr, addr));
|
|
|
|
CHECK_ERR(pthread_attr_getstacksize(&attr, size));
|
|
|
|
# endif
|
|
|
|
CHECK_ERR(pthread_attr_getguardsize(&attr, &guard));
|
2010-02-04 02:17:03 -05:00
|
|
|
*size -= guard;
|
2008-11-27 01:05:07 -05:00
|
|
|
# ifndef HAVE_PTHREAD_GETATTR_NP
|
|
|
|
pthread_attr_destroy(&attr);
|
|
|
|
# endif
|
|
|
|
#elif defined HAVE_PTHREAD_GET_STACKADDR_NP && defined HAVE_PTHREAD_GET_STACKSIZE_NP
|
|
|
|
pthread_t th = pthread_self();
|
|
|
|
*addr = pthread_get_stackaddr_np(th);
|
|
|
|
*size = pthread_get_stacksize_np(th);
|
|
|
|
#elif defined HAVE_THR_STKSEGMENT || defined HAVE_PTHREAD_STACKSEG_NP
|
|
|
|
stack_t stk;
|
|
|
|
# if defined HAVE_THR_STKSEGMENT
|
|
|
|
CHECK_ERR(thr_stksegment(&stk));
|
|
|
|
# else
|
|
|
|
CHECK_ERR(pthread_stackseg_np(pthread_self(), &stk));
|
|
|
|
# endif
|
|
|
|
*addr = stk.ss_sp;
|
|
|
|
*size = stk.ss_size;
|
|
|
|
#endif
|
|
|
|
return 0;
|
|
|
|
#undef CHECK_ERR
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2008-06-13 22:59:19 -04:00
|
|
|
static struct {
|
|
|
|
rb_thread_id_t id;
|
|
|
|
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
|
|
|
|
|
2008-06-13 22:59:19 -04:00
|
|
|
#undef ruby_init_stack
|
|
|
|
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
|
|
|
|
{
|
2010-02-04 02:17:03 -05:00
|
|
|
size_t size = 0;
|
|
|
|
size_t space = 0;
|
|
|
|
#if defined(HAVE_PTHREAD_ATTR_GET_NP)
|
|
|
|
void* addr;
|
|
|
|
get_stack(&addr, &size);
|
2010-02-02 22:35:11 -05:00
|
|
|
#elif defined(HAVE_GETRLIMIT)
|
2008-06-13 22:59:19 -04:00
|
|
|
struct rlimit rlim;
|
|
|
|
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
|
|
|
}
|
|
|
|
#endif
|
2010-02-02 22:35:11 -05:00
|
|
|
space = size > 5 * 1024 * 1024 ? 1024 * 1024 : size / 5;
|
|
|
|
native_main_thread.stack_maxsize = size - space;
|
|
|
|
}
|
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)
|
|
|
|
{
|
|
|
|
rb_thread_id_t curr = pthread_self();
|
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
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;
|
2006-12-31 10:02:22 -05:00
|
|
|
VALUE stack_start;
|
|
|
|
|
|
|
|
/* run */
|
2007-07-11 21:19:18 -04:00
|
|
|
thread_start_func_2(th, &stack_start, rb_ia64_bsp());
|
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;
|
|
|
|
static rb_thread_t *register_cached_thread_and_wait(void);
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
void rb_thread_create_control_thread(void);
|
|
|
|
|
|
|
|
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;
|
2006-12-31 10:02:22 -05:00
|
|
|
pthread_cond_t *cond;
|
|
|
|
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)
|
|
|
|
{
|
|
|
|
pthread_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;
|
2006-12-31 10:02:22 -05:00
|
|
|
struct cached_thread_entry *entry =
|
|
|
|
(struct cached_thread_entry *)malloc(sizeof(struct cached_thread_entry));
|
|
|
|
|
|
|
|
struct timeval tv;
|
|
|
|
struct timespec ts;
|
|
|
|
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;
|
|
|
|
|
|
|
|
pthread_cond_timedwait(&cond, &thread_cache_lock, &ts);
|
|
|
|
|
|
|
|
{
|
|
|
|
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 */
|
2006-12-31 10:02:22 -05:00
|
|
|
pthread_cond_destroy(&cond);
|
|
|
|
}
|
|
|
|
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) {
|
|
|
|
pthread_cond_signal(entry->cond);
|
|
|
|
}
|
|
|
|
pthread_mutex_unlock(&thread_cache_lock);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2009-11-11 23:31:02 -05:00
|
|
|
enum {
|
|
|
|
#ifdef __SYMBIAN32__
|
|
|
|
RUBY_STACK_MIN_LIMIT = 64 * 1024, /* 64KB: Let's be slightly more frugal on mobile platform */
|
|
|
|
#else
|
|
|
|
RUBY_STACK_MIN_LIMIT = 512 * 1024, /* 512KB */
|
|
|
|
#endif
|
2009-11-23 02:06:54 -05:00
|
|
|
RUBY_STACK_SPACE_LIMIT = 1024 * 1024
|
|
|
|
};
|
|
|
|
|
2009-11-11 23:31:02 -05:00
|
|
|
#ifdef PTHREAD_STACK_MIN
|
2009-11-23 02:06:54 -05:00
|
|
|
#define RUBY_STACK_MIN ((RUBY_STACK_MIN_LIMIT < PTHREAD_STACK_MIN) ? \
|
|
|
|
PTHREAD_STACK_MIN * 2 : RUBY_STACK_MIN_LIMIT)
|
|
|
|
#else
|
|
|
|
#define RUBY_STACK_MIN (RUBY_STACK_MIN_LIMIT)
|
2009-11-11 23:31:02 -05:00
|
|
|
#endif
|
2009-11-23 02:06:54 -05:00
|
|
|
#define RUBY_STACK_SPACE (RUBY_STACK_MIN/5 > RUBY_STACK_SPACE_LIMIT ? \
|
|
|
|
RUBY_STACK_SPACE_LIMIT : RUBY_STACK_MIN/5)
|
2009-11-11 23:31:02 -05:00
|
|
|
|
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
|
|
|
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;
|
2009-11-11 23:31:02 -05:00
|
|
|
const size_t stack_size = RUBY_STACK_MIN;
|
|
|
|
const size_t space = RUBY_STACK_SPACE;
|
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
|
|
|
|
2007-02-08 15:24:55 -05:00
|
|
|
CHECK_ERR(pthread_attr_init(&attr));
|
|
|
|
|
2007-01-03 20:38:01 -05: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));
|
2007-01-03 20:38:01 -05:00
|
|
|
#endif
|
2007-02-08 15:24:55 -05:00
|
|
|
|
2008-08-08 04:03:19 -04:00
|
|
|
#ifdef HAVE_PTHREAD_ATTR_SETINHERITSCHED
|
2007-02-08 15:24:55 -05:00
|
|
|
CHECK_ERR(pthread_attr_setinheritsched(&attr, PTHREAD_INHERIT_SCHED));
|
2008-08-06 16:52:44 -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);
|
* 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: %p (%d)", (void *)th, err);
|
2007-02-08 15:24:55 -05:00
|
|
|
CHECK_ERR(pthread_attr_destroy(&attr));
|
2006-12-31 10:02:22 -05:00
|
|
|
|
2007-04-10 02:53:39 -04:00
|
|
|
if (!err) {
|
|
|
|
pthread_cond_init(&th->native_thread_data.sleep_cond, 0);
|
|
|
|
}
|
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 */
|
|
|
|
|
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);
|
2006-12-31 10:02:22 -05:00
|
|
|
pthread_cond_signal(&th->native_thread_data.sleep_cond);
|
|
|
|
}
|
|
|
|
|
2009-02-06 08:35:15 -05:00
|
|
|
#if !defined(__CYGWIN__) && !defined(__SYMBIAN32__)
|
2006-12-31 10:02:22 -05:00
|
|
|
static void
|
2007-02-08 06:51:40 -05:00
|
|
|
ubf_select_each(rb_thread_t *th)
|
2006-12-31 10:02:22 -05:00
|
|
|
{
|
* blockinlining.c: remove "yarv" prefix.
* array.c, numeric.c: ditto.
* insnhelper.ci, insns.def, vm_evalbody.ci: ditto.
* yarvcore.c: removed.
* yarvcore.h: renamed to core.h.
* cont.c, debug.c, error.c, process.c, signal.c : ditto.
* ext/probeprofiler/probeprofiler.c: ditto.
* id.c, id.h: added.
* inits.c: ditto.
* compile.c: rename internal functions.
* compile.h: fix debug flag.
* eval.c, object.c, vm.c: remove ruby_top_self.
use rb_vm_top_self() instead.
* eval_intern.h, eval_load: ditto.
* gc.c: rename yarv_machine_stack_mark() to
rb_gc_mark_machine_stack().
* insnhelper.h: remove unused macros.
* iseq.c: add iseq_compile() to create iseq object
from source string.
* proc.c: rename a internal function.
* template/insns.inc.tmpl: remove YARV prefix.
* thread.c:
* vm.c (rb_iseq_eval): added.
* vm.c: move some functions from yarvcore.c.
* vm_dump.c: fix to remove compiler warning.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@12741 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2007-07-12 00:25:46 -04:00
|
|
|
thread_debug("ubf_select_each (%p)\n", (void *)th->thread_id);
|
2006-12-31 10:02:22 -05:00
|
|
|
if (th) {
|
|
|
|
pthread_kill(th->thread_id, SIGVTALRM);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-02-08 06:51:40 -05:00
|
|
|
static void
|
2007-11-20 05:47:53 -05:00
|
|
|
ubf_select(void *ptr)
|
2007-02-08 06:51:40 -05:00
|
|
|
{
|
2007-11-20 05:47:53 -05:00
|
|
|
rb_thread_t *th = (rb_thread_t *)ptr;
|
2007-02-08 06:51:40 -05:00
|
|
|
add_signal_thread_list(th);
|
|
|
|
ubf_select_each(th);
|
|
|
|
}
|
2007-02-24 04:41:41 -05:00
|
|
|
#else
|
|
|
|
#define ubf_select 0
|
2007-02-08 06:51:40 -05:00
|
|
|
#endif
|
|
|
|
|
2008-11-06 08:21:26 -05:00
|
|
|
#define PER_NANO 1000000000
|
|
|
|
|
2006-12-31 10:02:22 -05:00
|
|
|
static void
|
2008-07-16 15:19:36 -04:00
|
|
|
native_sleep(rb_thread_t *th, struct timeval *tv)
|
2006-12-31 10:02:22 -05:00
|
|
|
{
|
|
|
|
struct timespec ts;
|
|
|
|
struct timeval tvn;
|
|
|
|
|
|
|
|
if (tv) {
|
|
|
|
gettimeofday(&tvn, NULL);
|
|
|
|
ts.tv_sec = tvn.tv_sec + tv->tv_sec;
|
|
|
|
ts.tv_nsec = (tvn.tv_usec + tv->tv_usec) * 1000;
|
2008-11-06 08:21:26 -05:00
|
|
|
if (ts.tv_nsec >= PER_NANO){
|
2007-01-04 22:33:40 -05:00
|
|
|
ts.tv_sec += 1;
|
2008-11-06 08:21:26 -05:00
|
|
|
ts.tv_nsec -= PER_NANO;
|
|
|
|
}
|
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
|
|
|
|
2007-02-19 11:16:12 -05:00
|
|
|
thread_debug("native_sleep %ld\n", tv ? tv->tv_sec : -1);
|
2006-12-31 10:02:22 -05:00
|
|
|
GVL_UNLOCK_BEGIN();
|
|
|
|
{
|
|
|
|
pthread_mutex_lock(&th->interrupt_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 {
|
2008-07-09 09:41:19 -04:00
|
|
|
if (tv == 0 || ts.tv_sec < tvn.tv_sec /* overflow */ ) {
|
2008-05-16 23:37:37 -04:00
|
|
|
int r;
|
2006-12-31 10:02:22 -05:00
|
|
|
thread_debug("native_sleep: pthread_cond_wait start\n");
|
2008-05-16 23:37:37 -04:00
|
|
|
r = pthread_cond_wait(&th->native_thread_data.sleep_cond,
|
2008-11-06 08:21:26 -05:00
|
|
|
&th->interrupt_lock);
|
2009-11-24 06:03:51 -05:00
|
|
|
if (r) rb_bug_errno("pthread_cond_wait", r);
|
2006-12-31 10:02:22 -05:00
|
|
|
thread_debug("native_sleep: pthread_cond_wait end\n");
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
int r;
|
2008-07-09 09:41:19 -04:00
|
|
|
thread_debug("native_sleep: pthread_cond_timedwait start (%ld, %ld)\n",
|
2007-03-07 19:23:14 -05:00
|
|
|
(unsigned long)ts.tv_sec, ts.tv_nsec);
|
2006-12-31 10:02:22 -05:00
|
|
|
r = pthread_cond_timedwait(&th->native_thread_data.sleep_cond,
|
|
|
|
&th->interrupt_lock, &ts);
|
2009-11-24 06:03:51 -05:00
|
|
|
if (r && r != ETIMEDOUT) rb_bug_errno("pthread_cond_timedwait", r);
|
2008-05-16 23:37:37 -04:00
|
|
|
|
2006-12-31 10:02:22 -05:00
|
|
|
thread_debug("native_sleep: pthread_cond_timedwait end (%d)\n", r);
|
|
|
|
}
|
|
|
|
}
|
2008-05-29 21:52:38 -04:00
|
|
|
th->unblock.func = 0;
|
|
|
|
th->unblock.arg = 0;
|
2006-12-31 10:02:22 -05:00
|
|
|
|
2007-12-24 23:16:06 -05:00
|
|
|
pthread_mutex_unlock(&th->interrupt_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");
|
|
|
|
}
|
|
|
|
|
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
|
|
|
};
|
|
|
|
|
2008-04-20 02:14:49 -04:00
|
|
|
#ifndef __CYGWIN__
|
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,
|
|
|
|
};
|
2008-04-20 02:14:49 -04:00
|
|
|
#endif
|
2006-12-31 10:02:22 -05:00
|
|
|
|
|
|
|
#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);
|
|
|
|
while(list){
|
|
|
|
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
|
|
|
|
2008-04-20 02:14:49 -04:00
|
|
|
#ifndef __CYGWIN__
|
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 =
|
|
|
|
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");
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
|
|
|
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;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
2008-04-20 02:14:49 -04:00
|
|
|
#endif
|
2006-12-31 10:02:22 -05:00
|
|
|
|
|
|
|
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;
|
* 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(list); /* ok */
|
2006-12-31 10:02:22 -05:00
|
|
|
});
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
/* */
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static pthread_t timer_thread_id;
|
2008-11-06 14:02:35 -05:00
|
|
|
static pthread_cond_t timer_thread_cond = PTHREAD_COND_INITIALIZER;
|
2008-11-07 11:14:48 -05:00
|
|
|
static pthread_mutex_t timer_thread_lock = PTHREAD_MUTEX_INITIALIZER;
|
2008-11-06 14:02:35 -05:00
|
|
|
|
|
|
|
static struct timespec *
|
|
|
|
get_ts(struct timespec *ts, unsigned long nsec)
|
|
|
|
{
|
|
|
|
struct timeval tv;
|
|
|
|
gettimeofday(&tv, 0);
|
|
|
|
ts->tv_sec = tv.tv_sec;
|
|
|
|
ts->tv_nsec = tv.tv_usec * 1000 + nsec;
|
|
|
|
if (ts->tv_nsec >= PER_NANO) {
|
|
|
|
ts->tv_sec++;
|
|
|
|
ts->tv_nsec -= PER_NANO;
|
|
|
|
}
|
|
|
|
return ts;
|
|
|
|
}
|
|
|
|
|
2006-12-31 10:02:22 -05:00
|
|
|
static void *
|
|
|
|
thread_timer(void *dummy)
|
|
|
|
{
|
2008-11-06 14:02:35 -05:00
|
|
|
struct timespec ts;
|
2008-11-07 11:14:48 -05:00
|
|
|
|
|
|
|
native_mutex_lock(&timer_thread_lock);
|
2008-11-08 10:31:05 -05:00
|
|
|
native_cond_broadcast(&timer_thread_cond);
|
2008-11-07 11:42:31 -05:00
|
|
|
#define WAIT_FOR_10MS() native_cond_timedwait(&timer_thread_cond, &timer_thread_lock, get_ts(&ts, PER_NANO/100))
|
2009-01-01 16:50:01 -05:00
|
|
|
while (system_working > 0) {
|
|
|
|
int err = WAIT_FOR_10MS();
|
|
|
|
if (err == ETIMEDOUT);
|
|
|
|
else if (err == 0 || err == EINTR) {
|
|
|
|
if (rb_signal_buff_size() == 0) break;
|
2008-11-07 11:14:48 -05:00
|
|
|
}
|
2009-11-24 06:03:51 -05:00
|
|
|
else rb_bug_errno("thread_timer/timedwait", err);
|
2009-01-01 16:50:01 -05:00
|
|
|
|
2009-02-06 08:35:15 -05:00
|
|
|
#if !defined(__CYGWIN__) && !defined(__SYMBIAN32__)
|
2006-12-31 10:02:22 -05:00
|
|
|
if (signal_thread_list_anchor.next) {
|
|
|
|
FGLOCK(&signal_thread_list_lock, {
|
2007-02-08 06:51:40 -05:00
|
|
|
struct signal_thread_list *list;
|
2006-12-31 10:02:22 -05:00
|
|
|
list = signal_thread_list_anchor.next;
|
|
|
|
while (list) {
|
2007-02-08 06:51:40 -05:00
|
|
|
ubf_select_each(list->th);
|
2006-12-31 10:02:22 -05:00
|
|
|
list = list->next;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
2007-02-08 06:51:40 -05:00
|
|
|
#endif
|
2008-07-05 09:22:29 -04:00
|
|
|
timer_thread_function(dummy);
|
2006-12-31 10:02:22 -05:00
|
|
|
}
|
2008-11-07 11:14:48 -05:00
|
|
|
native_mutex_unlock(&timer_thread_lock);
|
2006-12-31 10:02:22 -05:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
rb_thread_create_timer_thread(void)
|
|
|
|
{
|
|
|
|
rb_enable_interrupt();
|
|
|
|
|
|
|
|
if (!timer_thread_id) {
|
|
|
|
pthread_attr_t attr;
|
|
|
|
int err;
|
|
|
|
|
|
|
|
pthread_attr_init(&attr);
|
2007-01-03 20:38:01 -05:00
|
|
|
#ifdef PTHREAD_STACK_MIN
|
2008-07-09 05:17:09 -04:00
|
|
|
pthread_attr_setstacksize(&attr,
|
|
|
|
PTHREAD_STACK_MIN + (THREAD_DEBUG ? BUFSIZ : 0));
|
2007-01-03 20:38:01 -05:00
|
|
|
#endif
|
2008-11-07 11:14:48 -05:00
|
|
|
native_mutex_lock(&timer_thread_lock);
|
2008-11-06 08:21:26 -05:00
|
|
|
err = pthread_create(&timer_thread_id, &attr, thread_timer, 0);
|
2006-12-31 10:02:22 -05:00
|
|
|
if (err != 0) {
|
2008-11-07 11:48:22 -05:00
|
|
|
native_mutex_unlock(&timer_thread_lock);
|
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
|
|
|
}
|
2008-11-07 11:48:22 -05:00
|
|
|
native_cond_wait(&timer_thread_cond, &timer_thread_lock);
|
|
|
|
native_mutex_unlock(&timer_thread_lock);
|
2006-12-31 10:02:22 -05:00
|
|
|
}
|
|
|
|
rb_disable_interrupt(); /* only timer thread recieve signal */
|
|
|
|
}
|
|
|
|
|
2008-11-08 10:31:05 -05:00
|
|
|
static int
|
2008-11-07 11:14:48 -05:00
|
|
|
native_stop_timer_thread(void)
|
|
|
|
{
|
2008-11-08 10:31:05 -05:00
|
|
|
int stopped;
|
2008-11-07 11:14:48 -05:00
|
|
|
native_mutex_lock(&timer_thread_lock);
|
2008-11-08 10:31:05 -05:00
|
|
|
stopped = --system_working <= 0;
|
|
|
|
if (stopped) {
|
|
|
|
native_cond_signal(&timer_thread_cond);
|
|
|
|
}
|
2008-11-07 11:14:48 -05:00
|
|
|
native_mutex_unlock(&timer_thread_lock);
|
2009-11-02 00:45:35 -05:00
|
|
|
if (stopped) {
|
|
|
|
native_thread_join(timer_thread_id);
|
|
|
|
}
|
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)
|
|
|
|
{
|
|
|
|
timer_thread_id = 0;
|
|
|
|
}
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
size /= 5;
|
|
|
|
if (size > water_mark) size = water_mark;
|
|
|
|
if (STACK_DIR_UPPER(1, 0)) {
|
|
|
|
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
|
|
|
|
|
2006-12-31 10:02:22 -05:00
|
|
|
#endif /* THREAD_SYSTEM_DEPENDENT_IMPLEMENTATION */
|