2006-12-31 18:02:22 +03:00
|
|
|
/**********************************************************************
|
|
|
|
|
|
|
|
thread.c -
|
|
|
|
|
|
|
|
$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-14 01:13:04 +03:00
|
|
|
Copyright (C) 2004-2007 Koichi Sasada
|
2006-12-31 18:02:22 +03:00
|
|
|
|
|
|
|
**********************************************************************/
|
|
|
|
|
|
|
|
/*
|
2009-11-03 20:46:28 +03:00
|
|
|
YARV Thread Design
|
2006-12-31 18:02:22 +03:00
|
|
|
|
|
|
|
model 1: Userlevel Thread
|
|
|
|
Same as traditional ruby thread.
|
|
|
|
|
2009-01-12 04:43:23 +03:00
|
|
|
model 2: Native Thread with Global VM lock
|
2006-12-31 18:02:22 +03:00
|
|
|
Using pthread (or Windows thread) and Ruby threads run concurrent.
|
|
|
|
|
|
|
|
model 3: Native Thread with fine grain lock
|
|
|
|
Using pthread and Ruby threads run concurrent or parallel.
|
|
|
|
|
|
|
|
------------------------------------------------------------------------
|
|
|
|
|
|
|
|
model 2:
|
2009-01-12 04:43:23 +03:00
|
|
|
A thread has mutex (GVL: Global VM Lock or Giant VM Lock) can run.
|
|
|
|
When thread scheduling, running thread release GVL. If running thread
|
2006-12-31 18:02:22 +03:00
|
|
|
try blocking operation, this thread must release GVL and another
|
|
|
|
thread can continue this flow. After blocking operation, thread
|
* 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 22:00:03 +03:00
|
|
|
must check interrupt (RUBY_VM_CHECK_INTS).
|
2006-12-31 18:02:22 +03:00
|
|
|
|
|
|
|
Every VM can run parallel.
|
|
|
|
|
|
|
|
Ruby threads are scheduled by OS thread scheduler.
|
|
|
|
|
|
|
|
------------------------------------------------------------------------
|
|
|
|
|
|
|
|
model 3:
|
|
|
|
Every threads run concurrent or parallel and to access shared object
|
|
|
|
exclusive access control is needed. For example, to access String
|
|
|
|
object or Array object, fine grain lock must be locked every time.
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
|
|
/* for model 2 */
|
|
|
|
|
|
|
|
#include "eval_intern.h"
|
2007-02-05 15:21:01 +03:00
|
|
|
#include "gc.h"
|
2011-06-09 19:02:46 +04:00
|
|
|
#include "internal.h"
|
2011-05-04 04:59:57 +04:00
|
|
|
#include "ruby/io.h"
|
2012-07-10 17:57:11 +04:00
|
|
|
#include "ruby/thread.h"
|
2006-12-31 18:02:22 +03:00
|
|
|
|
2008-08-13 11:53:35 +04:00
|
|
|
#ifndef USE_NATIVE_THREAD_PRIORITY
|
|
|
|
#define USE_NATIVE_THREAD_PRIORITY 0
|
|
|
|
#define RUBY_THREAD_PRIORITY_MAX 3
|
|
|
|
#define RUBY_THREAD_PRIORITY_MIN -3
|
|
|
|
#endif
|
|
|
|
|
2007-02-24 12:39:25 +03:00
|
|
|
#ifndef THREAD_DEBUG
|
2006-12-31 18:02:22 +03:00
|
|
|
#define THREAD_DEBUG 0
|
2007-02-24 12:39:25 +03:00
|
|
|
#endif
|
2006-12-31 18:02:22 +03:00
|
|
|
|
2007-05-03 17:19:11 +04:00
|
|
|
VALUE rb_cMutex;
|
2012-07-05 11:00:29 +04:00
|
|
|
VALUE rb_cThreadShield;
|
2007-05-03 17:19:11 +04:00
|
|
|
|
2012-11-28 16:34:15 +04:00
|
|
|
static void sleep_timeval(rb_thread_t *th, struct timeval time, int spurious_check);
|
|
|
|
static void sleep_wait_for_interrupt(rb_thread_t *th, double sleepsec, int spurious_check);
|
|
|
|
static void sleep_forever(rb_thread_t *th, int nodeadlock, int spurious_check);
|
2007-05-03 17:04:42 +04:00
|
|
|
static double timeofday(void);
|
2009-06-08 20:14:06 +04:00
|
|
|
static int rb_threadptr_dead(rb_thread_t *th);
|
2008-06-12 17:01:38 +04:00
|
|
|
static void rb_check_deadlock(rb_vm_t *vm);
|
|
|
|
|
2011-05-18 17:36:46 +04:00
|
|
|
#define eKillSignal INT2FIX(0)
|
|
|
|
#define eTerminateSignal INT2FIX(1)
|
2008-11-08 18:31:05 +03:00
|
|
|
static volatile int system_working = 1;
|
2006-12-31 18:02:22 +03:00
|
|
|
|
2011-02-12 08:44:23 +03:00
|
|
|
#define closed_stream_error GET_VM()->special_exceptions[ruby_error_closed_stream]
|
|
|
|
|
2006-12-31 18:02:22 +03:00
|
|
|
inline static void
|
2008-05-08 01:27:34 +04:00
|
|
|
st_delete_wrap(st_table *table, st_data_t key)
|
2006-12-31 18:02:22 +03:00
|
|
|
{
|
2008-05-08 01:27:34 +04:00
|
|
|
st_delete(table, &key, 0);
|
2006-12-31 18:02:22 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/********************************************************************************/
|
|
|
|
|
|
|
|
#define THREAD_SYSTEM_DEPENDENT_IMPLEMENTATION
|
|
|
|
|
2008-09-26 04:47:18 +04:00
|
|
|
struct rb_blocking_region_buffer {
|
|
|
|
enum rb_thread_status prev_status;
|
|
|
|
struct rb_unblock_callback oldubf;
|
|
|
|
};
|
|
|
|
|
2012-11-28 17:01:25 +04:00
|
|
|
static int set_unblock_function(rb_thread_t *th, rb_unblock_function_t *func, void *arg,
|
|
|
|
struct rb_unblock_callback *old, int fail_if_interrupted);
|
2008-05-30 05:52:38 +04:00
|
|
|
static void reset_unblock_function(rb_thread_t *th, const struct rb_unblock_callback *old);
|
2006-12-31 18:02:22 +03:00
|
|
|
|
2012-11-28 17:01:25 +04:00
|
|
|
static inline int blocking_region_begin(rb_thread_t *th, struct rb_blocking_region_buffer *region,
|
|
|
|
rb_unblock_function_t *ubf, void *arg, int fail_if_interrupted);
|
2009-02-12 10:21:42 +03:00
|
|
|
static inline void blocking_region_end(rb_thread_t *th, struct rb_blocking_region_buffer *region);
|
2008-09-26 04:47:18 +04:00
|
|
|
|
2009-11-09 14:18:17 +03:00
|
|
|
#define RB_GC_SAVE_MACHINE_CONTEXT(th) \
|
|
|
|
do { \
|
|
|
|
rb_gc_save_machine_context(th); \
|
|
|
|
SET_MACHINE_STACK_END(&(th)->machine_stack_end); \
|
|
|
|
} while (0)
|
|
|
|
|
2007-02-08 14:51:40 +03:00
|
|
|
#define GVL_UNLOCK_BEGIN() do { \
|
|
|
|
rb_thread_t *_th_stored = GET_THREAD(); \
|
2009-11-09 14:18:17 +03:00
|
|
|
RB_GC_SAVE_MACHINE_CONTEXT(_th_stored); \
|
2010-11-27 23:15:59 +03:00
|
|
|
gvl_release(_th_stored->vm);
|
2007-02-08 14:51:40 +03:00
|
|
|
|
|
|
|
#define GVL_UNLOCK_END() \
|
2010-11-27 23:15:59 +03:00
|
|
|
gvl_acquire(_th_stored->vm, _th_stored); \
|
2007-02-08 14:51:40 +03:00
|
|
|
rb_thread_set_current(_th_stored); \
|
|
|
|
} while(0)
|
|
|
|
|
2012-11-28 17:01:25 +04:00
|
|
|
#define BLOCKING_REGION(exec, ubf, ubfarg, fail_if_interrupted) do { \
|
2012-11-20 05:21:19 +04:00
|
|
|
rb_thread_t *__th = GET_THREAD(); \
|
2008-09-26 04:47:18 +04:00
|
|
|
struct rb_blocking_region_buffer __region; \
|
2012-11-28 17:01:25 +04:00
|
|
|
if (blocking_region_begin(__th, &__region, (ubf), (ubfarg), fail_if_interrupted)) { \
|
|
|
|
exec; \
|
|
|
|
blocking_region_end(__th, &__region); \
|
|
|
|
}; \
|
2006-12-31 18:02:22 +03:00
|
|
|
} while(0)
|
|
|
|
|
|
|
|
#if THREAD_DEBUG
|
2008-07-09 15:18:52 +04:00
|
|
|
#ifdef HAVE_VA_ARGS_MACRO
|
|
|
|
void rb_thread_debug(const char *file, int line, const char *fmt, ...);
|
|
|
|
#define thread_debug(fmt, ...) rb_thread_debug(__FILE__, __LINE__, fmt, ##__VA_ARGS__)
|
|
|
|
#define POSITION_FORMAT "%s:%d:"
|
|
|
|
#define POSITION_ARGS ,file, line
|
|
|
|
#else
|
2007-02-24 12:39:25 +03:00
|
|
|
void rb_thread_debug(const char *fmt, ...);
|
2008-07-09 15:18:52 +04:00
|
|
|
#define thread_debug rb_thread_debug
|
|
|
|
#define POSITION_FORMAT
|
|
|
|
#define POSITION_ARGS
|
|
|
|
#endif
|
2007-02-24 12:39:25 +03:00
|
|
|
|
|
|
|
# if THREAD_DEBUG < 0
|
|
|
|
static int rb_thread_debug_enabled;
|
|
|
|
|
2009-09-17 08:51:33 +04:00
|
|
|
/*
|
|
|
|
* call-seq:
|
2010-05-18 01:07:33 +04:00
|
|
|
* Thread.DEBUG -> num
|
2009-09-17 08:51:33 +04:00
|
|
|
*
|
|
|
|
* Returns the thread debug level. Available only if compiled with
|
|
|
|
* THREAD_DEBUG=-1.
|
|
|
|
*/
|
|
|
|
|
2007-02-24 12:39:25 +03:00
|
|
|
static VALUE
|
|
|
|
rb_thread_s_debug(void)
|
|
|
|
{
|
|
|
|
return INT2NUM(rb_thread_debug_enabled);
|
|
|
|
}
|
|
|
|
|
2009-09-17 08:51:33 +04:00
|
|
|
/*
|
|
|
|
* call-seq:
|
|
|
|
* Thread.DEBUG = num
|
|
|
|
*
|
|
|
|
* Sets the thread debug level. Available only if compiled with
|
|
|
|
* THREAD_DEBUG=-1.
|
|
|
|
*/
|
|
|
|
|
2007-02-24 12:39:25 +03:00
|
|
|
static VALUE
|
|
|
|
rb_thread_s_debug_set(VALUE self, VALUE val)
|
|
|
|
{
|
2009-09-17 08:50:52 +04:00
|
|
|
rb_thread_debug_enabled = RTEST(val) ? NUM2INT(val) : 0;
|
2007-02-24 12:39:25 +03:00
|
|
|
return val;
|
|
|
|
}
|
|
|
|
# else
|
|
|
|
# define rb_thread_debug_enabled THREAD_DEBUG
|
|
|
|
# endif
|
2006-12-31 18:02:22 +03:00
|
|
|
#else
|
|
|
|
#define thread_debug if(0)printf
|
|
|
|
#endif
|
|
|
|
|
2007-07-12 05:19:18 +04:00
|
|
|
#ifndef __ia64
|
|
|
|
#define thread_start_func_2(th, st, rst) thread_start_func_2(th, st)
|
|
|
|
#endif
|
|
|
|
NOINLINE(static int thread_start_func_2(rb_thread_t *th, VALUE *stack_start,
|
|
|
|
VALUE *register_stack_start));
|
2008-07-05 17:22:29 +04:00
|
|
|
static void timer_thread_function(void *);
|
2007-07-12 05:19:18 +04:00
|
|
|
|
2007-02-08 14:51:40 +03:00
|
|
|
#if defined(_WIN32)
|
2007-12-20 12:29:46 +03:00
|
|
|
#include "thread_win32.c"
|
2006-12-31 18:02:22 +03:00
|
|
|
|
|
|
|
#define DEBUG_OUT() \
|
|
|
|
WaitForSingleObject(&debug_mutex, INFINITE); \
|
2008-07-09 15:18:52 +04:00
|
|
|
printf(POSITION_FORMAT"%p - %s" POSITION_ARGS, GetCurrentThreadId(), buf); \
|
2007-02-24 12:39:25 +03:00
|
|
|
fflush(stdout); \
|
2006-12-31 18:02:22 +03:00
|
|
|
ReleaseMutex(&debug_mutex);
|
|
|
|
|
|
|
|
#elif defined(HAVE_PTHREAD_H)
|
2007-12-20 12:29:46 +03:00
|
|
|
#include "thread_pthread.c"
|
2006-12-31 18:02:22 +03:00
|
|
|
|
|
|
|
#define DEBUG_OUT() \
|
|
|
|
pthread_mutex_lock(&debug_mutex); \
|
2008-07-09 15:18:52 +04:00
|
|
|
printf(POSITION_FORMAT"%#"PRIxVALUE" - %s" POSITION_ARGS, (VALUE)pthread_self(), buf); \
|
2007-02-24 12:39:25 +03:00
|
|
|
fflush(stdout); \
|
2006-12-31 18:02:22 +03:00
|
|
|
pthread_mutex_unlock(&debug_mutex);
|
|
|
|
|
|
|
|
#else
|
|
|
|
#error "unsupported thread type"
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#if THREAD_DEBUG
|
|
|
|
static int debug_mutex_initialized = 1;
|
* 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 22:00:03 +03:00
|
|
|
static rb_thread_lock_t debug_mutex;
|
2006-12-31 18:02:22 +03:00
|
|
|
|
|
|
|
void
|
2008-07-09 15:18:52 +04:00
|
|
|
rb_thread_debug(
|
|
|
|
#ifdef HAVE_VA_ARGS_MACRO
|
|
|
|
const char *file, int line,
|
|
|
|
#endif
|
|
|
|
const char *fmt, ...)
|
2006-12-31 18:02:22 +03:00
|
|
|
{
|
|
|
|
va_list args;
|
|
|
|
char buf[BUFSIZ];
|
|
|
|
|
2007-02-24 12:39:25 +03:00
|
|
|
if (!rb_thread_debug_enabled) return;
|
|
|
|
|
2006-12-31 18:02:22 +03:00
|
|
|
if (debug_mutex_initialized == 1) {
|
|
|
|
debug_mutex_initialized = 0;
|
|
|
|
native_mutex_initialize(&debug_mutex);
|
|
|
|
}
|
|
|
|
|
|
|
|
va_start(args, fmt);
|
|
|
|
vsnprintf(buf, BUFSIZ, fmt, args);
|
|
|
|
va_end(args);
|
|
|
|
|
|
|
|
DEBUG_OUT();
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2010-11-27 23:15:59 +03:00
|
|
|
void
|
|
|
|
rb_vm_gvl_destroy(rb_vm_t *vm)
|
|
|
|
{
|
|
|
|
gvl_release(vm);
|
|
|
|
gvl_destroy(vm);
|
|
|
|
}
|
|
|
|
|
2010-01-23 23:18:36 +03:00
|
|
|
void
|
|
|
|
rb_thread_lock_unlock(rb_thread_lock_t *lock)
|
|
|
|
{
|
|
|
|
native_mutex_unlock(lock);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
rb_thread_lock_destroy(rb_thread_lock_t *lock)
|
|
|
|
{
|
|
|
|
native_mutex_destroy(lock);
|
|
|
|
}
|
2006-12-31 18:02:22 +03:00
|
|
|
|
2012-11-28 17:01:25 +04:00
|
|
|
static int
|
2012-11-20 05:21:19 +04:00
|
|
|
set_unblock_function(rb_thread_t *th, rb_unblock_function_t *func, void *arg,
|
2012-11-28 17:01:25 +04:00
|
|
|
struct rb_unblock_callback *old, int fail_if_interrupted)
|
2006-12-31 18:02:22 +03:00
|
|
|
{
|
|
|
|
check_ints:
|
2012-11-28 17:01:25 +04:00
|
|
|
if (fail_if_interrupted) {
|
|
|
|
if (RUBY_VM_INTERRUPTED_ANY(th)) {
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
RUBY_VM_CHECK_INTS(th);
|
|
|
|
}
|
|
|
|
|
2012-11-20 05:21:19 +04:00
|
|
|
native_mutex_lock(&th->interrupt_lock);
|
2012-11-26 14:57:39 +04:00
|
|
|
if (RUBY_VM_INTERRUPTED_ANY(th)) {
|
2012-11-20 05:21:19 +04:00
|
|
|
native_mutex_unlock(&th->interrupt_lock);
|
2007-02-24 12:41:41 +03:00
|
|
|
goto check_ints;
|
|
|
|
}
|
2006-12-31 18:02:22 +03:00
|
|
|
else {
|
2012-11-20 05:21:19 +04:00
|
|
|
if (old) *old = th->unblock;
|
|
|
|
th->unblock.func = func;
|
|
|
|
th->unblock.arg = arg;
|
2006-12-31 18:02:22 +03:00
|
|
|
}
|
2012-11-20 05:21:19 +04:00
|
|
|
native_mutex_unlock(&th->interrupt_lock);
|
2012-11-28 17:01:25 +04:00
|
|
|
|
|
|
|
return TRUE;
|
2006-12-31 18:02:22 +03:00
|
|
|
}
|
|
|
|
|
2008-05-30 05:52:38 +04:00
|
|
|
static void
|
|
|
|
reset_unblock_function(rb_thread_t *th, const struct rb_unblock_callback *old)
|
|
|
|
{
|
|
|
|
native_mutex_lock(&th->interrupt_lock);
|
|
|
|
th->unblock = *old;
|
|
|
|
native_mutex_unlock(&th->interrupt_lock);
|
|
|
|
}
|
|
|
|
|
2012-11-26 13:22:01 +04:00
|
|
|
static void
|
|
|
|
rb_threadptr_interrupt_common(rb_thread_t *th, int trap)
|
2006-12-31 18:02:22 +03:00
|
|
|
{
|
|
|
|
native_mutex_lock(&th->interrupt_lock);
|
2012-11-26 13:22:01 +04:00
|
|
|
if (trap)
|
|
|
|
RUBY_VM_SET_TRAP_INTERRUPT(th);
|
|
|
|
else
|
|
|
|
RUBY_VM_SET_INTERRUPT(th);
|
2008-05-30 05:52:38 +04:00
|
|
|
if (th->unblock.func) {
|
|
|
|
(th->unblock.func)(th->unblock.arg);
|
2006-12-31 18:02:22 +03:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
/* none */
|
|
|
|
}
|
|
|
|
native_mutex_unlock(&th->interrupt_lock);
|
|
|
|
}
|
|
|
|
|
2012-11-26 13:22:01 +04:00
|
|
|
void
|
|
|
|
rb_threadptr_interrupt(rb_thread_t *th)
|
|
|
|
{
|
|
|
|
rb_threadptr_interrupt_common(th, 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
rb_threadptr_trap_interrupt(rb_thread_t *th)
|
|
|
|
{
|
|
|
|
rb_threadptr_interrupt_common(th, 1);
|
|
|
|
}
|
2006-12-31 18:02:22 +03: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 09:37:46 +03:00
|
|
|
terminate_i(st_data_t key, st_data_t val, rb_thread_t *main_thread)
|
2006-12-31 18:02:22 +03:00
|
|
|
{
|
|
|
|
VALUE thval = key;
|
* 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 09:37:46 +03:00
|
|
|
rb_thread_t *th;
|
2006-12-31 18:02:22 +03:00
|
|
|
GetThreadPtr(thval, th);
|
|
|
|
|
|
|
|
if (th != main_thread) {
|
* 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-09 07:33:55 +03:00
|
|
|
thread_debug("terminate_i: %p\n", (void *)th);
|
2012-07-18 09:46:40 +04:00
|
|
|
rb_threadptr_async_errinfo_enque(th, eTerminateSignal);
|
|
|
|
rb_threadptr_interrupt(th);
|
2006-12-31 18:02:22 +03:00
|
|
|
}
|
|
|
|
else {
|
* 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-09 07:33:55 +03:00
|
|
|
thread_debug("terminate_i: main thread (%p)\n", (void *)th);
|
2006-12-31 18:02:22 +03:00
|
|
|
}
|
|
|
|
return ST_CONTINUE;
|
|
|
|
}
|
|
|
|
|
2008-07-28 16:27:43 +04:00
|
|
|
typedef struct rb_mutex_struct
|
|
|
|
{
|
|
|
|
rb_thread_lock_t lock;
|
|
|
|
rb_thread_cond_t cond;
|
|
|
|
struct rb_thread_struct volatile *th;
|
2011-06-12 10:55:12 +04:00
|
|
|
int cond_waiting;
|
2008-07-28 16:27:43 +04:00
|
|
|
struct rb_mutex_struct *next_mutex;
|
2012-11-28 12:30:51 +04:00
|
|
|
int allow_trap;
|
2011-06-16 04:12:55 +04:00
|
|
|
} rb_mutex_t;
|
2008-07-28 16:27:43 +04:00
|
|
|
|
2011-06-16 04:12:55 +04:00
|
|
|
static void rb_mutex_abandon_all(rb_mutex_t *mutexes);
|
2011-07-08 08:50:25 +04:00
|
|
|
static const char* rb_mutex_unlock_th(rb_mutex_t *mutex, rb_thread_t volatile *th);
|
|
|
|
|
|
|
|
void
|
|
|
|
rb_threadptr_unlock_all_locking_mutexes(rb_thread_t *th)
|
|
|
|
{
|
|
|
|
const char *err;
|
|
|
|
rb_mutex_t *mutex;
|
|
|
|
rb_mutex_t *mutexes = th->keeping_mutexes;
|
|
|
|
|
|
|
|
while (mutexes) {
|
|
|
|
mutex = mutexes;
|
|
|
|
/* rb_warn("mutex #<%p> remains to be locked by terminated thread",
|
|
|
|
mutexes); */
|
|
|
|
mutexes = mutex->next_mutex;
|
|
|
|
err = rb_mutex_unlock_th(mutex, th);
|
|
|
|
if (err) rb_bug("invalid keeping_mutexes: %s", err);
|
|
|
|
}
|
|
|
|
}
|
2008-07-28 16:27:43 +04:00
|
|
|
|
2006-12-31 18:02:22 +03:00
|
|
|
void
|
|
|
|
rb_thread_terminate_all(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 09:37:46 +03:00
|
|
|
rb_thread_t *th = GET_THREAD(); /* main thread */
|
* 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 22:00:03 +03:00
|
|
|
rb_vm_t *vm = th->vm;
|
2009-08-20 04:42:16 +04:00
|
|
|
|
2006-12-31 18:02:22 +03:00
|
|
|
if (vm->main_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-09 07:33:55 +03:00
|
|
|
rb_bug("rb_thread_terminate_all: called by child thread (%p, %p)",
|
|
|
|
(void *)vm->main_thread, (void *)th);
|
2006-12-31 18:02:22 +03:00
|
|
|
}
|
|
|
|
|
2008-06-12 17:01:38 +04:00
|
|
|
/* unlock all locking mutexes */
|
2011-07-08 08:50:25 +04:00
|
|
|
rb_threadptr_unlock_all_locking_mutexes(th);
|
2008-06-12 17:01:38 +04:00
|
|
|
|
2012-11-27 07:18:29 +04:00
|
|
|
retry:
|
|
|
|
thread_debug("rb_thread_terminate_all (main thread: %p)\n", (void *)th);
|
|
|
|
st_foreach(vm->living_threads, terminate_i, (st_data_t)th);
|
|
|
|
|
2006-12-31 18:02:22 +03:00
|
|
|
while (!rb_thread_alone()) {
|
2012-11-27 03:08:36 +04:00
|
|
|
int state;
|
|
|
|
|
2012-11-27 07:04:36 +04:00
|
|
|
TH_PUSH_TAG(th);
|
|
|
|
if ((state = TH_EXEC_TAG()) == 0) {
|
2012-11-26 17:47:23 +04:00
|
|
|
native_sleep(th, 0);
|
2012-11-27 02:49:36 +04:00
|
|
|
RUBY_VM_CHECK_INTS_BLOCKING(th);
|
2007-08-17 11:24:03 +04:00
|
|
|
}
|
2012-11-27 07:04:36 +04:00
|
|
|
TH_POP_TAG();
|
2012-11-27 07:18:29 +04:00
|
|
|
|
|
|
|
if (state) {
|
|
|
|
goto retry;
|
|
|
|
}
|
2006-12-31 18:02:22 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2008-05-11 08:15:29 +04:00
|
|
|
thread_cleanup_func_before_exec(void *th_ptr)
|
2006-12-31 18:02:22 +03:00
|
|
|
{
|
* 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 09:37:46 +03:00
|
|
|
rb_thread_t *th = th_ptr;
|
2006-12-31 18:02:22 +03:00
|
|
|
th->status = THREAD_KILLED;
|
|
|
|
th->machine_stack_start = th->machine_stack_end = 0;
|
2007-06-14 12:35:20 +04:00
|
|
|
#ifdef __ia64
|
|
|
|
th->machine_register_stack_start = th->machine_register_stack_end = 0;
|
|
|
|
#endif
|
2008-05-11 08:15:29 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2010-12-20 18:16:41 +03:00
|
|
|
thread_cleanup_func(void *th_ptr, int atfork)
|
2008-05-11 08:15:29 +04:00
|
|
|
{
|
|
|
|
rb_thread_t *th = th_ptr;
|
2009-02-23 19:20:06 +03:00
|
|
|
|
2009-11-15 18:10:49 +03:00
|
|
|
th->locking_mutex = Qfalse;
|
2008-05-11 08:15:29 +04:00
|
|
|
thread_cleanup_func_before_exec(th_ptr);
|
2010-12-20 18:16:41 +03:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Unfortunately, we can't release native threading resource at fork
|
|
|
|
* because libc may have unstable locking state therefore touching
|
|
|
|
* a threading resource may cause a deadlock.
|
|
|
|
*/
|
|
|
|
if (atfork)
|
|
|
|
return;
|
|
|
|
|
2010-12-20 18:20:17 +03:00
|
|
|
native_mutex_destroy(&th->interrupt_lock);
|
2007-02-08 23:24:55 +03:00
|
|
|
native_thread_destroy(th);
|
2006-12-31 18:02:22 +03:00
|
|
|
}
|
|
|
|
|
2009-06-08 20:14:06 +04:00
|
|
|
static VALUE rb_threadptr_raise(rb_thread_t *, int, VALUE *);
|
2007-08-10 00:12:21 +04:00
|
|
|
|
2008-06-14 06:59:19 +04:00
|
|
|
void
|
|
|
|
ruby_thread_init_stack(rb_thread_t *th)
|
|
|
|
{
|
|
|
|
native_thread_init_stack(th);
|
|
|
|
}
|
|
|
|
|
2006-12-31 18:02:22 +03:00
|
|
|
static int
|
2007-07-12 05:19:18 +04:00
|
|
|
thread_start_func_2(rb_thread_t *th, VALUE *stack_start, VALUE *register_stack_start)
|
2006-12-31 18:02:22 +03:00
|
|
|
{
|
|
|
|
int state;
|
|
|
|
VALUE args = th->first_args;
|
* 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 22:00:03 +03:00
|
|
|
rb_proc_t *proc;
|
2012-11-28 16:34:15 +04:00
|
|
|
rb_thread_list_t *join_list;
|
2007-08-10 19:01:14 +04:00
|
|
|
rb_thread_t *main_th;
|
2007-08-10 00:12:21 +04:00
|
|
|
VALUE errinfo = Qnil;
|
2010-05-13 20:20:26 +04:00
|
|
|
# ifdef USE_SIGALTSTACK
|
|
|
|
void rb_register_sigaltstack(rb_thread_t *th);
|
|
|
|
|
|
|
|
rb_register_sigaltstack(th);
|
|
|
|
# endif
|
2007-08-10 00:12:21 +04:00
|
|
|
|
2012-11-29 01:55:14 +04:00
|
|
|
if (th == th->vm->main_thread)
|
|
|
|
rb_bug("thread_start_func_2 must not used for main thread");
|
|
|
|
|
2008-12-24 19:52:38 +03:00
|
|
|
ruby_thread_set_native(th);
|
|
|
|
|
2006-12-31 18:02:22 +03:00
|
|
|
th->machine_stack_start = stack_start;
|
2007-06-14 12:35:20 +04:00
|
|
|
#ifdef __ia64
|
|
|
|
th->machine_register_stack_start = register_stack_start;
|
|
|
|
#endif
|
* 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-09 07:33:55 +03:00
|
|
|
thread_debug("thread start: %p\n", (void *)th);
|
2006-12-31 18:02:22 +03:00
|
|
|
|
2010-11-27 23:15:59 +03:00
|
|
|
gvl_acquire(th->vm, th);
|
2006-12-31 18:02:22 +03:00
|
|
|
{
|
* 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-09 07:33:55 +03:00
|
|
|
thread_debug("thread start (get lock): %p\n", (void *)th);
|
* 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 22:00:03 +03:00
|
|
|
rb_thread_set_current(th);
|
2006-12-31 18:02:22 +03:00
|
|
|
|
|
|
|
TH_PUSH_TAG(th);
|
|
|
|
if ((state = EXEC_TAG()) == 0) {
|
* cont.c: support Fiber. Check test/ruby/test_fiber.rb for detail.
Fiber is known as "Micro Thread", "Coroutine", and other terms.
At this time, only Fiber#pass is supported to change context.
I want to know more suitable method name/API for Fiber (... do you
know more suitable class name instead of Fiber?) as "suspend/resume",
"call", "yield", "start/kick/stop/restart", ....
* eval.c, eval_intern.h, thread.c, yarvcore.c, yarvcore.h: ditto.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@12395 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2007-05-27 23:12:43 +04:00
|
|
|
SAVE_ROOT_JMPBUF(th, {
|
2009-11-13 10:22:04 +03:00
|
|
|
if (!th->first_func) {
|
* cont.c: support Fiber. Check test/ruby/test_fiber.rb for detail.
Fiber is known as "Micro Thread", "Coroutine", and other terms.
At this time, only Fiber#pass is supported to change context.
I want to know more suitable method name/API for Fiber (... do you
know more suitable class name instead of Fiber?) as "suspend/resume",
"call", "yield", "start/kick/stop/restart", ....
* eval.c, eval_intern.h, thread.c, yarvcore.c, yarvcore.h: ditto.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@12395 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2007-05-27 23:12:43 +04:00
|
|
|
GetProcPtr(th->first_proc, proc);
|
|
|
|
th->errinfo = Qnil;
|
2012-06-11 07:14:59 +04:00
|
|
|
th->root_lep = rb_vm_ep_local_ep(proc->block.ep);
|
|
|
|
th->root_svar = Qnil;
|
2012-08-20 15:36:34 +04:00
|
|
|
th->value = rb_vm_invoke_proc(th, proc,
|
2009-05-09 07:25:16 +04:00
|
|
|
(int)RARRAY_LEN(args), RARRAY_PTR(args), 0);
|
* cont.c: support Fiber. Check test/ruby/test_fiber.rb for detail.
Fiber is known as "Micro Thread", "Coroutine", and other terms.
At this time, only Fiber#pass is supported to change context.
I want to know more suitable method name/API for Fiber (... do you
know more suitable class name instead of Fiber?) as "suspend/resume",
"call", "yield", "start/kick/stop/restart", ....
* eval.c, eval_intern.h, thread.c, yarvcore.c, yarvcore.h: ditto.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@12395 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2007-05-27 23:12:43 +04:00
|
|
|
}
|
|
|
|
else {
|
2009-11-13 10:22:04 +03:00
|
|
|
th->value = (*th->first_func)((void *)args);
|
* cont.c: support Fiber. Check test/ruby/test_fiber.rb for detail.
Fiber is known as "Micro Thread", "Coroutine", and other terms.
At this time, only Fiber#pass is supported to change context.
I want to know more suitable method name/API for Fiber (... do you
know more suitable class name instead of Fiber?) as "suspend/resume",
"call", "yield", "start/kick/stop/restart", ....
* eval.c, eval_intern.h, thread.c, yarvcore.c, yarvcore.h: ditto.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@12395 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2007-05-27 23:12:43 +04:00
|
|
|
}
|
|
|
|
});
|
2006-12-31 18:02:22 +03:00
|
|
|
}
|
|
|
|
else {
|
2008-08-06 01:55:23 +04:00
|
|
|
errinfo = th->errinfo;
|
|
|
|
if (state == TAG_FATAL) {
|
|
|
|
/* fatal error within this thread, need to stop whole script */
|
|
|
|
}
|
2012-11-29 01:54:52 +04:00
|
|
|
else if (th->vm->thread_abort_on_exception ||
|
|
|
|
th->abort_on_exception || RTEST(ruby_debug)) {
|
2008-08-06 01:55:23 +04:00
|
|
|
/* exit on main_thread */
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
errinfo = Qnil;
|
2007-08-10 00:12:21 +04:00
|
|
|
}
|
2006-12-31 18:02:22 +03:00
|
|
|
th->value = Qnil;
|
|
|
|
}
|
2012-11-29 01:55:26 +04:00
|
|
|
TH_POP_TAG();
|
2006-12-31 18:02:22 +03:00
|
|
|
|
|
|
|
th->status = THREAD_KILLED;
|
* 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-09 07:33:55 +03:00
|
|
|
thread_debug("thread end: %p\n", (void *)th);
|
2006-12-31 18:02:22 +03:00
|
|
|
|
2007-08-10 19:01:14 +04:00
|
|
|
main_th = th->vm->main_thread;
|
2012-11-29 01:55:14 +04:00
|
|
|
if (RB_TYPE_P(errinfo, T_OBJECT)) {
|
|
|
|
/* treat with normal error object */
|
|
|
|
rb_threadptr_raise(main_th, 1, &errinfo);
|
2007-08-17 11:24:03 +04:00
|
|
|
}
|
2008-06-12 17:01:38 +04:00
|
|
|
|
2009-02-18 04:29:13 +03:00
|
|
|
/* delete self other than main thread from living_threads */
|
2012-11-29 01:55:14 +04:00
|
|
|
st_delete_wrap(th->vm->living_threads, th->self);
|
|
|
|
if (rb_thread_alone()) {
|
|
|
|
/* I'm last thread. wake up main thread from rb_thread_terminate_all */
|
|
|
|
rb_threadptr_interrupt(main_th);
|
2009-02-18 04:29:13 +03:00
|
|
|
}
|
2007-08-10 19:01:14 +04:00
|
|
|
|
2012-11-29 01:55:26 +04:00
|
|
|
/* locking_mutex must be Qfalse */
|
|
|
|
if (th->locking_mutex != Qfalse) {
|
|
|
|
rb_bug("thread_start_func_2: locking_mutex must not be set (%p:%"PRIxVALUE")",
|
|
|
|
(void *)th, th->locking_mutex);
|
|
|
|
}
|
|
|
|
rb_threadptr_unlock_all_locking_mutexes(th);
|
|
|
|
rb_check_deadlock(th->vm);
|
|
|
|
|
2009-11-03 20:46:28 +03:00
|
|
|
/* wake up joining threads */
|
2012-11-28 16:34:15 +04:00
|
|
|
join_list = th->join_list;
|
|
|
|
while (join_list) {
|
|
|
|
rb_threadptr_interrupt(join_list->th);
|
|
|
|
switch (join_list->th->status) {
|
2008-07-16 23:19:36 +04:00
|
|
|
case THREAD_STOPPED: case THREAD_STOPPED_FOREVER:
|
2012-11-28 16:34:15 +04:00
|
|
|
join_list->th->status = THREAD_RUNNABLE;
|
2008-07-16 23:19:36 +04:00
|
|
|
default: break;
|
|
|
|
}
|
2012-11-28 16:34:15 +04:00
|
|
|
join_list = join_list->next;
|
2006-12-31 18:02:22 +03:00
|
|
|
}
|
2007-11-21 12:06:06 +03:00
|
|
|
|
|
|
|
if (!th->root_fiber) {
|
|
|
|
rb_thread_recycle_stack_release(th->stack);
|
|
|
|
th->stack = 0;
|
|
|
|
}
|
2006-12-31 18:02:22 +03:00
|
|
|
}
|
2012-11-29 01:55:14 +04:00
|
|
|
thread_cleanup_func(th, FALSE);
|
|
|
|
gvl_release(th->vm);
|
2007-08-10 00:12:21 +04:00
|
|
|
|
2006-12-31 18:02:22 +03:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static VALUE
|
2007-12-05 10:18:52 +03:00
|
|
|
thread_create_core(VALUE thval, VALUE args, VALUE (*fn)(ANYARGS))
|
2006-12-31 18:02:22 +03:00
|
|
|
{
|
2012-07-19 18:19:40 +04:00
|
|
|
rb_thread_t *th, *current_th = GET_THREAD();
|
2009-11-12 07:57:39 +03:00
|
|
|
int err;
|
2006-12-31 18:02:22 +03:00
|
|
|
|
2008-01-18 21:48:12 +03:00
|
|
|
if (OBJ_FROZEN(GET_THREAD()->thgroup)) {
|
|
|
|
rb_raise(rb_eThreadError,
|
|
|
|
"can't start a new thread (frozen ThreadGroup)");
|
|
|
|
}
|
2006-12-31 18:02:22 +03:00
|
|
|
GetThreadPtr(thval, th);
|
2007-02-05 15:21:01 +03:00
|
|
|
|
2006-12-31 18:02:22 +03:00
|
|
|
/* setup thread environment */
|
2007-02-05 15:21:01 +03:00
|
|
|
th->first_func = fn;
|
2008-07-10 19:58:07 +04:00
|
|
|
th->first_proc = fn ? Qfalse : rb_block_proc();
|
|
|
|
th->first_args = args; /* GC: shouldn't put before above line */
|
2007-02-05 15:21:01 +03:00
|
|
|
|
2012-07-19 18:19:40 +04:00
|
|
|
th->priority = current_th->priority;
|
|
|
|
th->thgroup = current_th->thgroup;
|
2007-08-10 18:54:50 +04:00
|
|
|
|
2012-07-25 20:40:04 +04:00
|
|
|
th->async_errinfo_queue = rb_ary_tmp_new(0);
|
2012-07-18 09:46:40 +04:00
|
|
|
th->async_errinfo_queue_checked = 0;
|
2012-07-19 18:19:40 +04:00
|
|
|
th->async_errinfo_mask_stack = rb_ary_dup(current_th->async_errinfo_mask_stack);
|
2012-07-29 20:00:36 +04:00
|
|
|
RBASIC(th->async_errinfo_mask_stack)->klass = 0;
|
2012-07-18 09:46:40 +04:00
|
|
|
|
2012-11-26 14:57:39 +04:00
|
|
|
th->interrupt_mask = 0;
|
2012-11-26 12:05:49 +04:00
|
|
|
|
2006-12-31 18:02:22 +03:00
|
|
|
native_mutex_initialize(&th->interrupt_lock);
|
2010-03-24 17:47:00 +03:00
|
|
|
|
2006-12-31 18:02:22 +03:00
|
|
|
/* kick thread */
|
2009-11-12 07:57:39 +03:00
|
|
|
err = native_thread_create(th);
|
|
|
|
if (err) {
|
|
|
|
th->status = THREAD_KILLED;
|
|
|
|
rb_raise(rb_eThreadError, "can't create Thread (%d)", err);
|
|
|
|
}
|
2012-11-24 06:00:10 +04:00
|
|
|
st_insert(th->vm->living_threads, thval, (st_data_t) th->thread_id);
|
2006-12-31 18:02:22 +03:00
|
|
|
return thval;
|
|
|
|
}
|
|
|
|
|
2009-09-17 08:51:33 +04:00
|
|
|
/* :nodoc: */
|
2007-02-05 15:21:01 +03:00
|
|
|
static VALUE
|
2007-12-05 10:18:52 +03:00
|
|
|
thread_s_new(int argc, VALUE *argv, VALUE klass)
|
2007-02-05 15:21:01 +03:00
|
|
|
{
|
2007-12-05 10:18:52 +03:00
|
|
|
rb_thread_t *th;
|
|
|
|
VALUE thread = rb_thread_alloc(klass);
|
2011-07-10 11:46:00 +04:00
|
|
|
|
2012-11-28 08:43:15 +04:00
|
|
|
if (GET_VM()->main_thread->status == THREAD_KILLED)
|
2011-07-10 11:46:00 +04:00
|
|
|
rb_raise(rb_eThreadError, "can't alloc thread");
|
|
|
|
|
2007-12-05 10:18:52 +03:00
|
|
|
rb_obj_call_init(thread, argc, argv);
|
|
|
|
GetThreadPtr(thread, th);
|
|
|
|
if (!th->first_args) {
|
|
|
|
rb_raise(rb_eThreadError, "uninitialized thread - check `%s#initialize'",
|
|
|
|
rb_class2name(klass));
|
|
|
|
}
|
|
|
|
return thread;
|
|
|
|
}
|
|
|
|
|
2007-12-24 10:52:39 +03:00
|
|
|
/*
|
|
|
|
* call-seq:
|
2010-05-18 01:07:33 +04:00
|
|
|
* Thread.start([args]*) {|args| block } -> thread
|
|
|
|
* Thread.fork([args]*) {|args| block } -> thread
|
2007-12-24 10:52:39 +03:00
|
|
|
*
|
|
|
|
* Basically the same as <code>Thread::new</code>. However, if class
|
|
|
|
* <code>Thread</code> is subclassed, then calling <code>start</code> in that
|
|
|
|
* subclass will not invoke the subclass's <code>initialize</code> method.
|
|
|
|
*/
|
|
|
|
|
2007-12-05 10:18:52 +03:00
|
|
|
static VALUE
|
|
|
|
thread_start(VALUE klass, VALUE args)
|
|
|
|
{
|
|
|
|
return thread_create_core(rb_thread_alloc(klass), args, 0);
|
|
|
|
}
|
|
|
|
|
2009-09-17 08:51:33 +04:00
|
|
|
/* :nodoc: */
|
2007-12-05 10:18:52 +03:00
|
|
|
static VALUE
|
|
|
|
thread_initialize(VALUE thread, VALUE args)
|
|
|
|
{
|
|
|
|
rb_thread_t *th;
|
|
|
|
if (!rb_block_given_p()) {
|
|
|
|
rb_raise(rb_eThreadError, "must be called with a block");
|
|
|
|
}
|
|
|
|
GetThreadPtr(thread, th);
|
|
|
|
if (th->first_args) {
|
|
|
|
VALUE proc = th->first_proc, line, loc;
|
|
|
|
const char *file;
|
|
|
|
if (!proc || !RTEST(loc = rb_proc_location(proc))) {
|
|
|
|
rb_raise(rb_eThreadError, "already initialized thread");
|
|
|
|
}
|
2007-12-05 10:43:08 +03:00
|
|
|
file = RSTRING_PTR(RARRAY_PTR(loc)[0]);
|
2007-12-05 10:18:52 +03:00
|
|
|
if (NIL_P(line = RARRAY_PTR(loc)[1])) {
|
|
|
|
rb_raise(rb_eThreadError, "already initialized thread - %s",
|
|
|
|
file);
|
|
|
|
}
|
2008-07-05 17:22:29 +04:00
|
|
|
rb_raise(rb_eThreadError, "already initialized thread - %s:%d",
|
2007-12-05 10:18:52 +03:00
|
|
|
file, NUM2INT(line));
|
|
|
|
}
|
|
|
|
return thread_create_core(thread, args, 0);
|
2007-02-05 15:21:01 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
VALUE
|
|
|
|
rb_thread_create(VALUE (*fn)(ANYARGS), void *arg)
|
|
|
|
{
|
2007-12-05 10:18:52 +03:00
|
|
|
return thread_create_core(rb_thread_alloc(rb_cThread), (VALUE)arg, fn);
|
2007-02-05 15:21:01 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2006-12-31 18:02:22 +03:00
|
|
|
/* +infty, for this purpose */
|
|
|
|
#define DELAY_INFTY 1E30
|
|
|
|
|
2008-04-22 08:13:01 +04:00
|
|
|
struct join_arg {
|
|
|
|
rb_thread_t *target, *waiting;
|
|
|
|
double limit;
|
|
|
|
int forever;
|
|
|
|
};
|
|
|
|
|
2006-12-31 18:02:22 +03:00
|
|
|
static VALUE
|
2008-04-22 08:13:01 +04:00
|
|
|
remove_from_join_list(VALUE arg)
|
2006-12-31 18:02:22 +03:00
|
|
|
{
|
2008-04-22 08:13:01 +04:00
|
|
|
struct join_arg *p = (struct join_arg *)arg;
|
|
|
|
rb_thread_t *target_th = p->target, *th = p->waiting;
|
2006-12-31 18:02:22 +03:00
|
|
|
|
|
|
|
if (target_th->status != THREAD_KILLED) {
|
2012-11-28 16:34:15 +04:00
|
|
|
rb_thread_list_t **p = &target_th->join_list;
|
2008-04-22 08:13:01 +04:00
|
|
|
|
2012-11-28 16:34:15 +04:00
|
|
|
while (*p) {
|
|
|
|
if ((*p)->th == th) {
|
|
|
|
*p = (*p)->next;
|
2008-04-22 08:13:01 +04:00
|
|
|
break;
|
|
|
|
}
|
2012-11-28 16:34:15 +04:00
|
|
|
p = &(*p)->next;
|
2008-04-22 08:13:01 +04:00
|
|
|
}
|
2006-12-31 18:02:22 +03:00
|
|
|
}
|
2008-04-22 08:13:01 +04:00
|
|
|
|
|
|
|
return Qnil;
|
|
|
|
}
|
|
|
|
|
|
|
|
static VALUE
|
|
|
|
thread_join_sleep(VALUE arg)
|
|
|
|
{
|
|
|
|
struct join_arg *p = (struct join_arg *)arg;
|
|
|
|
rb_thread_t *target_th = p->target, *th = p->waiting;
|
|
|
|
double now, limit = p->limit;
|
|
|
|
|
2006-12-31 18:02:22 +03:00
|
|
|
while (target_th->status != THREAD_KILLED) {
|
2008-04-22 08:13:01 +04:00
|
|
|
if (p->forever) {
|
2012-11-28 16:34:15 +04:00
|
|
|
sleep_forever(th, 1, 0);
|
2006-12-31 18:02:22 +03:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
now = timeofday();
|
|
|
|
if (now > limit) {
|
2007-02-05 15:21:01 +03:00
|
|
|
thread_debug("thread_join: timeout (thid: %p)\n",
|
* 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 08:25:46 +04:00
|
|
|
(void *)target_th->thread_id);
|
2008-04-22 08:13:01 +04:00
|
|
|
return Qfalse;
|
2006-12-31 18:02:22 +03:00
|
|
|
}
|
2012-11-28 16:34:15 +04:00
|
|
|
sleep_wait_for_interrupt(th, limit - now, 0);
|
2006-12-31 18:02:22 +03:00
|
|
|
}
|
2007-02-05 15:21:01 +03:00
|
|
|
thread_debug("thread_join: interrupted (thid: %p)\n",
|
* 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 08:25:46 +04:00
|
|
|
(void *)target_th->thread_id);
|
2006-12-31 18:02:22 +03:00
|
|
|
}
|
2008-04-22 08:13:01 +04:00
|
|
|
return Qtrue;
|
|
|
|
}
|
|
|
|
|
|
|
|
static VALUE
|
|
|
|
thread_join(rb_thread_t *target_th, double delay)
|
|
|
|
{
|
|
|
|
rb_thread_t *th = GET_THREAD();
|
|
|
|
struct join_arg arg;
|
|
|
|
|
2012-11-27 06:00:09 +04:00
|
|
|
if (th == target_th) {
|
|
|
|
rb_raise(rb_eThreadError, "Target thread must not be current thread");
|
|
|
|
}
|
2012-11-27 06:00:19 +04:00
|
|
|
if (GET_VM()->main_thread == target_th) {
|
|
|
|
rb_raise(rb_eThreadError, "Target thread must not be main thread");
|
|
|
|
}
|
2012-11-27 06:00:09 +04:00
|
|
|
|
2008-04-22 08:13:01 +04:00
|
|
|
arg.target = target_th;
|
|
|
|
arg.waiting = th;
|
|
|
|
arg.limit = timeofday() + delay;
|
|
|
|
arg.forever = delay == DELAY_INFTY;
|
|
|
|
|
|
|
|
thread_debug("thread_join (thid: %p)\n", (void *)target_th->thread_id);
|
|
|
|
|
|
|
|
if (target_th->status != THREAD_KILLED) {
|
2012-11-28 16:34:15 +04:00
|
|
|
rb_thread_list_t list;
|
|
|
|
list.next = target_th->join_list;
|
|
|
|
list.th = th;
|
|
|
|
target_th->join_list = &list;
|
2008-04-22 08:13:01 +04:00
|
|
|
if (!rb_ensure(thread_join_sleep, (VALUE)&arg,
|
|
|
|
remove_from_join_list, (VALUE)&arg)) {
|
|
|
|
return Qnil;
|
|
|
|
}
|
|
|
|
}
|
2006-12-31 18:02:22 +03:00
|
|
|
|
2007-02-05 15:21:01 +03:00
|
|
|
thread_debug("thread_join: success (thid: %p)\n",
|
* 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 08:25:46 +04:00
|
|
|
(void *)target_th->thread_id);
|
2006-12-31 18:02:22 +03:00
|
|
|
|
|
|
|
if (target_th->errinfo != Qnil) {
|
|
|
|
VALUE err = target_th->errinfo;
|
|
|
|
|
|
|
|
if (FIXNUM_P(err)) {
|
|
|
|
/* */
|
|
|
|
}
|
2012-05-23 11:13:21 +04:00
|
|
|
else if (RB_TYPE_P(target_th->errinfo, T_NODE)) {
|
* vm.c: add a prefix "rb_" to exposed functions
vm_get_ruby_level_next_cfp(), rb_vm_make_env_object(),
vm_stack_to_heap(), vm_make_proc(), vm_invoke_proc(),
vm_get_sourceline(), vm_cref(), vm_localjump_error(),
vm_make_jump_tag_but_local_jump(), vm_jump_tag_but_local_jump().
This changes may affect only core because most of renamed functions
require a pointer of not-exposed struct such as rb_thread_t or NODE.
In short, they are core functions.
* cont.c, eval.c, eval_intern.h, load.c, proc.c, thread.c,
vm_core.h, vm_dump.c, vm_eval.c, vm_exec.c, vm_insnhelper.c:
ditto.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@21659 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2009-01-19 05:38:11 +03:00
|
|
|
rb_exc_raise(rb_vm_make_jump_tag_but_local_jump(
|
2006-12-31 18:02:22 +03:00
|
|
|
GET_THROWOBJ_STATE(err), GET_THROWOBJ_VAL(err)));
|
|
|
|
}
|
|
|
|
else {
|
2007-02-14 05:42:11 +03:00
|
|
|
/* normal exception */
|
2006-12-31 18:02:22 +03:00
|
|
|
rb_exc_raise(err);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return target_th->self;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* call-seq:
|
2010-05-18 01:07:33 +04:00
|
|
|
* thr.join -> thr
|
|
|
|
* thr.join(limit) -> thr
|
* 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 08:25:10 +04:00
|
|
|
*
|
2006-12-31 18:02:22 +03:00
|
|
|
* The calling thread will suspend execution and run <i>thr</i>. Does not
|
|
|
|
* return until <i>thr</i> exits or until <i>limit</i> seconds have passed. If
|
|
|
|
* the time limit expires, <code>nil</code> will be returned, otherwise
|
|
|
|
* <i>thr</i> is returned.
|
* 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 08:25:10 +04:00
|
|
|
*
|
2006-12-31 18:02:22 +03:00
|
|
|
* Any threads not joined will be killed when the main program exits. If
|
|
|
|
* <i>thr</i> had previously raised an exception and the
|
|
|
|
* <code>abort_on_exception</code> and <code>$DEBUG</code> flags are not set
|
|
|
|
* (so the exception has not yet been processed) it will be processed at this
|
|
|
|
* time.
|
* 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 08:25:10 +04:00
|
|
|
*
|
2006-12-31 18:02:22 +03:00
|
|
|
* a = Thread.new { print "a"; sleep(10); print "b"; print "c" }
|
|
|
|
* x = Thread.new { print "x"; Thread.pass; print "y"; print "z" }
|
|
|
|
* x.join # Let x thread finish, a will be killed on exit.
|
* 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 08:25:10 +04:00
|
|
|
*
|
2006-12-31 18:02:22 +03:00
|
|
|
* <em>produces:</em>
|
* 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 08:25:10 +04:00
|
|
|
*
|
2006-12-31 18:02:22 +03:00
|
|
|
* axyz
|
* 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 08:25:10 +04:00
|
|
|
*
|
2006-12-31 18:02:22 +03:00
|
|
|
* The following example illustrates the <i>limit</i> parameter.
|
* 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 08:25:10 +04:00
|
|
|
*
|
2006-12-31 18:02:22 +03:00
|
|
|
* y = Thread.new { 4.times { sleep 0.1; puts 'tick... ' }}
|
|
|
|
* puts "Waiting" until y.join(0.15)
|
* 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 08:25:10 +04:00
|
|
|
*
|
2006-12-31 18:02:22 +03:00
|
|
|
* <em>produces:</em>
|
* 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 08:25:10 +04:00
|
|
|
*
|
2006-12-31 18:02:22 +03:00
|
|
|
* tick...
|
|
|
|
* Waiting
|
|
|
|
* tick...
|
|
|
|
* Waitingtick...
|
* 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 08:25:10 +04:00
|
|
|
*
|
|
|
|
*
|
2006-12-31 18:02:22 +03:00
|
|
|
* tick...
|
|
|
|
*/
|
|
|
|
|
|
|
|
static VALUE
|
2007-02-05 15:21:01 +03:00
|
|
|
thread_join_m(int argc, VALUE *argv, VALUE self)
|
2006-12-31 18:02:22 +03:00
|
|
|
{
|
* 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 09:37:46 +03:00
|
|
|
rb_thread_t *target_th;
|
2006-12-31 18:02:22 +03:00
|
|
|
double delay = DELAY_INFTY;
|
|
|
|
VALUE limit;
|
* 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 08:25:10 +04:00
|
|
|
|
2006-12-31 18:02:22 +03:00
|
|
|
GetThreadPtr(self, target_th);
|
|
|
|
|
|
|
|
rb_scan_args(argc, argv, "01", &limit);
|
|
|
|
if (!NIL_P(limit)) {
|
|
|
|
delay = rb_num2dbl(limit);
|
|
|
|
}
|
2007-12-25 07:16:06 +03:00
|
|
|
|
2007-02-05 15:21:01 +03:00
|
|
|
return thread_join(target_th, delay);
|
2006-12-31 18:02:22 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* call-seq:
|
2010-05-18 01:07:33 +04:00
|
|
|
* thr.value -> obj
|
* 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 08:25:10 +04:00
|
|
|
*
|
2006-12-31 18:02:22 +03:00
|
|
|
* Waits for <i>thr</i> to complete (via <code>Thread#join</code>) and returns
|
|
|
|
* its value.
|
* 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 08:25:10 +04:00
|
|
|
*
|
2006-12-31 18:02:22 +03:00
|
|
|
* a = Thread.new { 2 + 2 }
|
|
|
|
* a.value #=> 4
|
|
|
|
*/
|
|
|
|
|
|
|
|
static VALUE
|
2007-02-05 15:21:01 +03:00
|
|
|
thread_value(VALUE self)
|
2006-12-31 18:02:22 +03:00
|
|
|
{
|
* 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 09:37:46 +03:00
|
|
|
rb_thread_t *th;
|
2006-12-31 18:02:22 +03:00
|
|
|
GetThreadPtr(self, th);
|
2007-02-05 15:21:01 +03:00
|
|
|
thread_join(th, DELAY_INFTY);
|
2006-12-31 18:02:22 +03:00
|
|
|
return th->value;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Thread Scheduling
|
|
|
|
*/
|
|
|
|
|
|
|
|
static struct timeval
|
|
|
|
double2timeval(double d)
|
|
|
|
{
|
|
|
|
struct timeval time;
|
|
|
|
|
|
|
|
time.tv_sec = (int)d;
|
|
|
|
time.tv_usec = (int)((d - (int)d) * 1e6);
|
|
|
|
if (time.tv_usec < 0) {
|
2009-05-09 07:25:16 +04:00
|
|
|
time.tv_usec += (int)1e6;
|
2006-12-31 18:02:22 +03:00
|
|
|
time.tv_sec -= 1;
|
|
|
|
}
|
|
|
|
return time;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2012-11-28 16:34:15 +04:00
|
|
|
sleep_forever(rb_thread_t *th, int deadlockable,int spurious_check)
|
2006-12-31 18:02:22 +03:00
|
|
|
{
|
2012-11-20 05:21:19 +04:00
|
|
|
enum rb_thread_status prev_status = th->status;
|
2011-06-25 18:28:56 +04:00
|
|
|
enum rb_thread_status status = deadlockable ? THREAD_STOPPED_FOREVER : THREAD_STOPPED;
|
2008-07-16 23:19:36 +04:00
|
|
|
|
2012-11-20 05:21:19 +04:00
|
|
|
th->status = status;
|
2008-07-16 23:19:36 +04:00
|
|
|
do {
|
|
|
|
if (deadlockable) {
|
2012-11-20 05:21:19 +04:00
|
|
|
th->vm->sleeper++;
|
|
|
|
rb_check_deadlock(th->vm);
|
2008-07-16 23:19:36 +04:00
|
|
|
}
|
2012-11-20 05:21:19 +04:00
|
|
|
native_sleep(th, 0);
|
2008-07-16 23:19:36 +04:00
|
|
|
if (deadlockable) {
|
2012-11-20 05:21:19 +04:00
|
|
|
th->vm->sleeper--;
|
2008-07-16 23:19:36 +04:00
|
|
|
}
|
2012-11-20 05:21:19 +04:00
|
|
|
RUBY_VM_CHECK_INTS_BLOCKING(th);
|
2012-11-28 16:34:15 +04:00
|
|
|
} while (spurious_check && th->status == status);
|
2012-11-20 05:21:19 +04:00
|
|
|
th->status = prev_status;
|
2006-12-31 18:02:22 +03:00
|
|
|
}
|
|
|
|
|
2008-07-09 17:41:19 +04:00
|
|
|
static void
|
|
|
|
getclockofday(struct timeval *tp)
|
|
|
|
{
|
|
|
|
#if defined(HAVE_CLOCK_GETTIME) && defined(CLOCK_MONOTONIC)
|
|
|
|
struct timespec ts;
|
|
|
|
|
|
|
|
if (clock_gettime(CLOCK_MONOTONIC, &ts) == 0) {
|
|
|
|
tp->tv_sec = ts.tv_sec;
|
|
|
|
tp->tv_usec = ts.tv_nsec / 1000;
|
|
|
|
} else
|
|
|
|
#endif
|
|
|
|
{
|
|
|
|
gettimeofday(tp, NULL);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2006-12-31 18:02:22 +03:00
|
|
|
static void
|
2012-11-28 16:34:15 +04:00
|
|
|
sleep_timeval(rb_thread_t *th, struct timeval tv,int spurious_check)
|
2006-12-31 18:02:22 +03:00
|
|
|
{
|
2008-07-16 23:19:36 +04:00
|
|
|
struct timeval to, tvn;
|
2012-11-20 05:21:19 +04:00
|
|
|
enum rb_thread_status prev_status = th->status;
|
2008-07-16 23:19:36 +04:00
|
|
|
|
|
|
|
getclockofday(&to);
|
|
|
|
to.tv_sec += tv.tv_sec;
|
|
|
|
if ((to.tv_usec += tv.tv_usec) >= 1000000) {
|
|
|
|
to.tv_sec++;
|
|
|
|
to.tv_usec -= 1000000;
|
|
|
|
}
|
|
|
|
|
2012-11-20 05:21:19 +04:00
|
|
|
th->status = THREAD_STOPPED;
|
2008-07-16 23:19:36 +04:00
|
|
|
do {
|
2012-11-20 05:21:19 +04:00
|
|
|
native_sleep(th, &tv);
|
|
|
|
RUBY_VM_CHECK_INTS_BLOCKING(th);
|
2008-07-16 23:19:36 +04:00
|
|
|
getclockofday(&tvn);
|
|
|
|
if (to.tv_sec < tvn.tv_sec) break;
|
|
|
|
if (to.tv_sec == tvn.tv_sec && to.tv_usec <= tvn.tv_usec) break;
|
|
|
|
thread_debug("sleep_timeval: %ld.%.6ld > %ld.%.6ld\n",
|
2008-12-15 17:49:31 +03:00
|
|
|
(long)to.tv_sec, (long)to.tv_usec,
|
|
|
|
(long)tvn.tv_sec, (long)tvn.tv_usec);
|
2008-07-16 23:19:36 +04:00
|
|
|
tv.tv_sec = to.tv_sec - tvn.tv_sec;
|
|
|
|
if ((tv.tv_usec = to.tv_usec - tvn.tv_usec) < 0) {
|
|
|
|
--tv.tv_sec;
|
|
|
|
tv.tv_usec += 1000000;
|
|
|
|
}
|
2012-11-28 16:34:15 +04:00
|
|
|
} while (spurious_check && th->status == THREAD_STOPPED);
|
2012-11-20 05:21:19 +04:00
|
|
|
th->status = prev_status;
|
2006-12-31 18:02:22 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
* cont.c (rb_fiber_current), dln.c (dln_print_undef, dln_undefined),
eval.c (rb_iterator_p, rb_need_block), load.c: (Init_load), ruby.c
(uscore_get, rb_f_chop), st.c (stat_col), signal.c
(rb_signal_buff_size, ruby_sig_finalize), thread.c
(rb_thread_sleep_forever, rb_thread_sleep_deadly, rb_thread_alone):
protoized.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@21929 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2009-02-01 15:51:44 +03:00
|
|
|
rb_thread_sleep_forever(void)
|
2006-12-31 18:02:22 +03:00
|
|
|
{
|
|
|
|
thread_debug("rb_thread_sleep_forever\n");
|
2012-11-28 16:34:15 +04:00
|
|
|
sleep_forever(GET_THREAD(), 0, 1);
|
2008-06-12 17:01:38 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
* cont.c (rb_fiber_current), dln.c (dln_print_undef, dln_undefined),
eval.c (rb_iterator_p, rb_need_block), load.c: (Init_load), ruby.c
(uscore_get, rb_f_chop), st.c (stat_col), signal.c
(rb_signal_buff_size, ruby_sig_finalize), thread.c
(rb_thread_sleep_forever, rb_thread_sleep_deadly, rb_thread_alone):
protoized.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@21929 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2009-02-01 15:51:44 +03:00
|
|
|
rb_thread_sleep_deadly(void)
|
2008-06-12 17:01:38 +04:00
|
|
|
{
|
|
|
|
thread_debug("rb_thread_sleep_deadly\n");
|
2012-11-28 16:34:15 +04:00
|
|
|
sleep_forever(GET_THREAD(), 1, 1);
|
2006-12-31 18:02:22 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
static double
|
|
|
|
timeofday(void)
|
|
|
|
{
|
2008-05-09 14:17:15 +04:00
|
|
|
#if defined(HAVE_CLOCK_GETTIME) && defined(CLOCK_MONOTONIC)
|
|
|
|
struct timespec tp;
|
|
|
|
|
|
|
|
if (clock_gettime(CLOCK_MONOTONIC, &tp) == 0) {
|
|
|
|
return (double)tp.tv_sec + (double)tp.tv_nsec * 1e-9;
|
|
|
|
} else
|
|
|
|
#endif
|
|
|
|
{
|
|
|
|
struct timeval tv;
|
|
|
|
gettimeofday(&tv, NULL);
|
|
|
|
return (double)tv.tv_sec + (double)tv.tv_usec * 1e-6;
|
|
|
|
}
|
2006-12-31 18:02:22 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2012-11-28 16:34:15 +04:00
|
|
|
sleep_wait_for_interrupt(rb_thread_t *th, double sleepsec,int spurious_check)
|
2006-12-31 18:02:22 +03:00
|
|
|
{
|
2012-11-28 16:34:15 +04:00
|
|
|
sleep_timeval(th, double2timeval(sleepsec),spurious_check);
|
2006-12-31 18:02:22 +03: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 09:37:46 +03:00
|
|
|
sleep_for_polling(rb_thread_t *th)
|
2006-12-31 18:02:22 +03:00
|
|
|
{
|
|
|
|
struct timeval time;
|
|
|
|
time.tv_sec = 0;
|
|
|
|
time.tv_usec = 100 * 1000; /* 0.1 sec */
|
2012-11-28 16:34:15 +04:00
|
|
|
sleep_timeval(th, time, 1);
|
2006-12-31 18:02:22 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
rb_thread_wait_for(struct timeval time)
|
|
|
|
{
|
* 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 09:37:46 +03:00
|
|
|
rb_thread_t *th = GET_THREAD();
|
2012-11-28 16:34:15 +04:00
|
|
|
sleep_timeval(th, time, 1);
|
2006-12-31 18:02:22 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
rb_thread_polling(void)
|
|
|
|
{
|
|
|
|
if (!rb_thread_alone()) {
|
2012-11-20 05:21:19 +04:00
|
|
|
rb_thread_t *th = GET_THREAD();
|
|
|
|
RUBY_VM_CHECK_INTS_BLOCKING(th);
|
|
|
|
sleep_for_polling(th);
|
2006-12-31 18:02:22 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-08-13 12:21:24 +04:00
|
|
|
/*
|
|
|
|
* CAUTION: This function causes thread switching.
|
|
|
|
* rb_thread_check_ints() check ruby's interrupts.
|
|
|
|
* some interrupt needs thread switching/invoke handlers,
|
|
|
|
* and so on.
|
|
|
|
*/
|
2012-11-20 05:21:19 +04:00
|
|
|
|
2008-08-13 12:21:24 +04:00
|
|
|
void
|
|
|
|
rb_thread_check_ints(void)
|
|
|
|
{
|
2012-07-19 18:19:40 +04:00
|
|
|
RUBY_VM_CHECK_INTS_BLOCKING(GET_THREAD());
|
2008-08-13 12:21:24 +04:00
|
|
|
}
|
|
|
|
|
2008-09-04 16:00:24 +04:00
|
|
|
/*
|
|
|
|
* Hidden API for tcl/tk wrapper.
|
|
|
|
* There is no guarantee to perpetuate it.
|
|
|
|
*/
|
|
|
|
int
|
|
|
|
rb_thread_check_trap_pending(void)
|
|
|
|
{
|
2011-07-01 02:29:34 +04:00
|
|
|
return rb_signal_buff_size() != 0;
|
2008-09-04 16:00:24 +04:00
|
|
|
}
|
|
|
|
|
2008-09-23 12:17:17 +04:00
|
|
|
/* This function can be called in blocking region. */
|
|
|
|
int
|
|
|
|
rb_thread_interrupted(VALUE thval)
|
|
|
|
{
|
|
|
|
rb_thread_t *th;
|
|
|
|
GetThreadPtr(thval, th);
|
|
|
|
return RUBY_VM_INTERRUPTED(th);
|
|
|
|
}
|
|
|
|
|
2006-12-31 18:02:22 +03:00
|
|
|
void
|
|
|
|
rb_thread_sleep(int sec)
|
|
|
|
{
|
|
|
|
rb_thread_wait_for(rb_time_timeval(INT2FIX(sec)));
|
|
|
|
}
|
|
|
|
|
2009-08-22 20:19:18 +04:00
|
|
|
static void
|
2011-06-29 21:36:00 +04:00
|
|
|
rb_thread_schedule_limits(unsigned long limits_us)
|
2006-12-31 18:02:22 +03:00
|
|
|
{
|
|
|
|
thread_debug("rb_thread_schedule\n");
|
|
|
|
if (!rb_thread_alone()) {
|
* 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 09:37:46 +03:00
|
|
|
rb_thread_t *th = GET_THREAD();
|
2006-12-31 18:02:22 +03:00
|
|
|
|
2011-06-29 21:43:58 +04:00
|
|
|
if (th->running_time_us >= limits_us) {
|
|
|
|
thread_debug("rb_thread_schedule/switch start\n");
|
2012-04-28 13:59:44 +04:00
|
|
|
th->yielding = 1;
|
2011-06-29 21:43:58 +04:00
|
|
|
RB_GC_SAVE_MACHINE_CONTEXT(th);
|
2011-06-13 19:06:30 +04:00
|
|
|
gvl_yield(th->vm, th);
|
2012-04-28 13:59:44 +04:00
|
|
|
th->yielding = 0;
|
2011-06-29 21:43:58 +04:00
|
|
|
rb_thread_set_current(th);
|
|
|
|
thread_debug("rb_thread_schedule/switch done\n");
|
|
|
|
}
|
2006-12-31 18:02:22 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-08-22 20:19:18 +04:00
|
|
|
void
|
|
|
|
rb_thread_schedule(void)
|
|
|
|
{
|
2012-11-26 14:57:39 +04:00
|
|
|
rb_thread_t *cur_th = GET_THREAD();
|
2011-06-29 21:36:00 +04:00
|
|
|
rb_thread_schedule_limits(0);
|
|
|
|
|
2012-11-26 14:57:39 +04:00
|
|
|
if (UNLIKELY(RUBY_VM_INTERRUPTED_ANY(cur_th))) {
|
|
|
|
rb_threadptr_execute_interrupts(cur_th, 0);
|
2011-06-29 21:36:00 +04:00
|
|
|
}
|
2009-08-22 20:19:18 +04:00
|
|
|
}
|
|
|
|
|
2008-09-26 04:47:18 +04:00
|
|
|
/* blocking region */
|
|
|
|
|
2012-11-28 17:01:25 +04:00
|
|
|
static inline int
|
|
|
|
blocking_region_begin(rb_thread_t *th, struct rb_blocking_region_buffer *region,
|
|
|
|
rb_unblock_function_t *ubf, void *arg, int fail_if_interrupted)
|
|
|
|
{
|
|
|
|
region->prev_status = th->status;
|
|
|
|
if (set_unblock_function(th, ubf, arg, ®ion->oldubf, fail_if_interrupted)) {
|
|
|
|
th->blocking_region_buffer = region;
|
|
|
|
th->status = THREAD_STOPPED;
|
|
|
|
thread_debug("enter blocking region (%p)\n", (void *)th);
|
|
|
|
RB_GC_SAVE_MACHINE_CONTEXT(th);
|
|
|
|
gvl_release(th->vm);
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-09-26 04:47:18 +04:00
|
|
|
static inline void
|
|
|
|
blocking_region_end(rb_thread_t *th, struct rb_blocking_region_buffer *region)
|
|
|
|
{
|
2010-11-27 23:15:59 +03:00
|
|
|
gvl_acquire(th->vm, th);
|
2008-09-26 04:47:18 +04:00
|
|
|
rb_thread_set_current(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-09 07:33:55 +03:00
|
|
|
thread_debug("leave blocking region (%p)\n", (void *)th);
|
2008-09-26 04:47:18 +04:00
|
|
|
remove_signal_thread_list(th);
|
2009-01-12 04:43:23 +03:00
|
|
|
th->blocking_region_buffer = 0;
|
2008-09-26 04:47:18 +04:00
|
|
|
reset_unblock_function(th, ®ion->oldubf);
|
|
|
|
if (th->status == THREAD_STOPPED) {
|
|
|
|
th->status = region->prev_status;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
struct rb_blocking_region_buffer *
|
|
|
|
rb_thread_blocking_region_begin(void)
|
|
|
|
{
|
2012-11-20 05:21:19 +04:00
|
|
|
rb_thread_t *th = GET_THREAD();
|
2008-09-26 04:47:18 +04:00
|
|
|
struct rb_blocking_region_buffer *region = ALLOC(struct rb_blocking_region_buffer);
|
2012-11-28 17:01:25 +04:00
|
|
|
blocking_region_begin(th, region, ubf_select, th, FALSE);
|
2008-09-26 04:47:18 +04:00
|
|
|
return region;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
rb_thread_blocking_region_end(struct rb_blocking_region_buffer *region)
|
|
|
|
{
|
2010-04-19 19:34:04 +04:00
|
|
|
int saved_errno = errno;
|
2012-11-20 05:21:19 +04:00
|
|
|
rb_thread_t *th = ruby_thread_from_native();
|
|
|
|
blocking_region_end(th, region);
|
2008-09-26 04:47:18 +04:00
|
|
|
xfree(region);
|
2012-11-20 05:21:19 +04:00
|
|
|
RUBY_VM_CHECK_INTS_BLOCKING(th);
|
2010-04-19 19:34:04 +04:00
|
|
|
errno = saved_errno;
|
2008-09-26 04:47:18 +04:00
|
|
|
}
|
|
|
|
|
2012-11-28 17:01:25 +04:00
|
|
|
static void *
|
|
|
|
call_without_gvl(void *(*func)(void *), void *data1,
|
|
|
|
rb_unblock_function_t *ubf, void *data2, int fail_if_interrupted)
|
|
|
|
{
|
|
|
|
void *val = 0;
|
|
|
|
|
|
|
|
rb_thread_t *th = GET_THREAD();
|
|
|
|
int saved_errno = 0;
|
|
|
|
|
|
|
|
th->waiting_fd = -1;
|
|
|
|
if (ubf == RUBY_UBF_IO || ubf == RUBY_UBF_PROCESS) {
|
|
|
|
ubf = ubf_select;
|
|
|
|
data2 = th;
|
|
|
|
}
|
|
|
|
|
|
|
|
BLOCKING_REGION({
|
|
|
|
val = func(data1);
|
|
|
|
saved_errno = errno;
|
|
|
|
}, ubf, data2, fail_if_interrupted);
|
|
|
|
|
|
|
|
if (!fail_if_interrupted) {
|
|
|
|
RUBY_VM_CHECK_INTS_BLOCKING(th);
|
|
|
|
}
|
|
|
|
|
|
|
|
errno = saved_errno;
|
|
|
|
|
|
|
|
return val;
|
|
|
|
}
|
|
|
|
|
2008-09-23 12:51:33 +04:00
|
|
|
/*
|
2012-07-18 10:47:43 +04:00
|
|
|
* rb_thread_call_without_gvl - permit concurrent/parallel execution.
|
2012-11-28 17:01:25 +04:00
|
|
|
* rb_thread_call_without_gvl2 - permit concurrent/parallel execution
|
|
|
|
* without interrupt proceess.
|
2008-09-23 12:51:33 +04:00
|
|
|
*
|
2012-07-18 10:47:43 +04:00
|
|
|
* rb_thread_call_without_gvl() does:
|
2012-11-28 17:01:25 +04:00
|
|
|
* (1) Check interrupts.
|
|
|
|
* (2) release GVL.
|
2008-09-23 12:51:33 +04:00
|
|
|
* Other Ruby threads may run in parallel.
|
2012-11-28 17:01:25 +04:00
|
|
|
* (3) call func with data1
|
|
|
|
* (4) acquire GVL.
|
2008-09-23 12:51:33 +04:00
|
|
|
* Other Ruby threads can not run in parallel any more.
|
2012-11-28 17:01:25 +04:00
|
|
|
* (5) Check interrupts.
|
2012-07-18 10:47:43 +04:00
|
|
|
*
|
|
|
|
* rb_thread_call_without_gvl2() does:
|
2012-11-28 17:01:25 +04:00
|
|
|
* (1) Check interrupt and return if interrupted.
|
|
|
|
* (2) release GVL.
|
|
|
|
* (3) call func with data1 and a pointer to the flags.
|
|
|
|
* (4) acquire GVL.
|
2008-09-23 12:51:33 +04:00
|
|
|
*
|
2012-10-31 04:07:17 +04:00
|
|
|
* If another thread interrupts this thread (Thread#kill, signal delivery,
|
|
|
|
* VM-shutdown request, and so on), `ubf()' is called (`ubf()' means
|
2012-10-31 04:29:27 +04:00
|
|
|
* "un-blocking function"). `ubf()' should interrupt `func()' execution by
|
|
|
|
* toggling a cancellation flag, canceling the invocation of a call inside
|
|
|
|
* `func()' or similar. Note that `ubf()' may not be called with the GVL.
|
2008-09-23 12:58:13 +04:00
|
|
|
*
|
2012-10-31 04:07:17 +04:00
|
|
|
* There are built-in ubfs and you can specify these ubfs:
|
2008-09-23 12:51:33 +04:00
|
|
|
*
|
2012-10-31 04:07:17 +04:00
|
|
|
* * RUBY_UBF_IO: ubf for IO operation
|
|
|
|
* * RUBY_UBF_PROCESS: ubf for process operation
|
2008-09-23 12:51:33 +04:00
|
|
|
*
|
2012-10-31 04:07:17 +04:00
|
|
|
* However, we can not guarantee our built-in ubfs interrupt your `func()'
|
|
|
|
* correctly. Be careful to use rb_thread_call_without_gvl(). If you don't
|
|
|
|
* provide proper ubf(), your program will not stop for Control+C or other
|
|
|
|
* shutdown events.
|
2012-07-18 10:47:43 +04:00
|
|
|
*
|
2012-11-28 17:01:25 +04:00
|
|
|
* "Check interrupts" on above list means that check asynchronous
|
2012-10-31 04:07:17 +04:00
|
|
|
* interrupt events (such as Thread#kill, signal delivery, VM-shutdown
|
|
|
|
* request, and so on) and call corresponding procedures
|
|
|
|
* (such as `trap' for signals, raise an exception for Thread#raise).
|
|
|
|
* If `func()' finished and receive interrupts, you may skip interrupt
|
|
|
|
* checking. For example, assume the following func() it read data from file.
|
2012-07-18 10:47:43 +04:00
|
|
|
*
|
2012-10-31 04:07:17 +04:00
|
|
|
* read_func(...) {
|
|
|
|
* // (a) before read
|
|
|
|
* read(buffer); // (b) reading
|
|
|
|
* // (c) after read
|
|
|
|
* }
|
2012-07-18 10:47:43 +04:00
|
|
|
*
|
2012-10-31 04:07:17 +04:00
|
|
|
* If an interrupt occurs at (a) or (b), then `ubf()' cancels this
|
|
|
|
* `read_func()' and interrupts are checked. However, if an interrupt occurs
|
|
|
|
* at (c), after *read* operation is completed, check intterrupts is harmful
|
|
|
|
* because it causes irrevocable side-effect, the read data will vanish. To
|
2012-11-28 17:01:25 +04:00
|
|
|
* avoid such problem, the `read_func()' should be used with
|
|
|
|
* `rb_thread_call_without_gvl2()'.
|
2012-07-18 10:47:43 +04:00
|
|
|
*
|
2012-11-28 17:01:25 +04:00
|
|
|
* If `rb_thread_call_without_gvl2()' detects interrupt, return its execution
|
|
|
|
* immediately. This function does not show when the execution was interrupted.
|
|
|
|
* For example, there are 4 possible timing (a), (b), (c) and before calling
|
|
|
|
* read_func(). You need to record progress of a read_func() and check
|
|
|
|
* the progress after `rb_thread_call_without_gvl2()'. You may need to call
|
|
|
|
* `rb_thread_check_ints()' correctly or your program can not process proper
|
|
|
|
* process such as `trap' and so on.
|
2012-07-18 10:47:43 +04:00
|
|
|
*
|
2012-10-31 04:07:17 +04:00
|
|
|
* NOTE: You can not execute most of Ruby C API and touch Ruby
|
|
|
|
* objects in `func()' and `ubf()', including raising an
|
|
|
|
* exception, because current thread doesn't acquire GVL
|
2012-10-31 04:29:27 +04:00
|
|
|
* (it causes synchronization problems). If you need to
|
|
|
|
* call ruby functions either use rb_thread_call_with_gvl()
|
|
|
|
* or read source code of C APIs and confirm safety by
|
|
|
|
* yourself.
|
2008-09-23 12:51:33 +04:00
|
|
|
*
|
2012-10-31 04:07:17 +04:00
|
|
|
* NOTE: In short, this API is difficult to use safely. I recommend you
|
|
|
|
* use other ways if you have. We lack experiences to use this API.
|
|
|
|
* Please report your problem related on it.
|
2008-12-30 10:57:53 +03:00
|
|
|
*
|
2012-10-31 04:07:17 +04:00
|
|
|
* NOTE: Releasing GVL and re-acquiring GVL may be expensive operations
|
2012-10-31 04:29:27 +04:00
|
|
|
* for a short running `func()'. Be sure to benchmark and use this
|
2012-10-31 04:07:17 +04:00
|
|
|
* mechanism when `func()' consumes enough time.
|
2012-07-18 10:47:43 +04:00
|
|
|
*
|
2012-10-31 04:07:17 +04:00
|
|
|
* Safe C API:
|
|
|
|
* * rb_thread_interrupted() - check interrupt flag
|
|
|
|
* * ruby_xmalloc(), ruby_xrealloc(), ruby_xfree() -
|
|
|
|
* they will work without GVL, and may acquire GVL when GC is needed.
|
2008-09-23 12:51:33 +04:00
|
|
|
*/
|
2012-07-10 17:57:11 +04:00
|
|
|
void *
|
2012-11-28 17:01:25 +04:00
|
|
|
rb_thread_call_without_gvl2(void *(*func)(void *), void *data1,
|
2012-07-18 10:47:43 +04:00
|
|
|
rb_unblock_function_t *ubf, void *data2)
|
2006-12-31 18:02:22 +03:00
|
|
|
{
|
2012-11-28 17:01:25 +04:00
|
|
|
return call_without_gvl(func, data1, ubf, data2, TRUE);
|
2012-07-18 10:47:43 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
void *
|
|
|
|
rb_thread_call_without_gvl(void *(*func)(void *data), void *data1,
|
|
|
|
rb_unblock_function_t *ubf, void *data2)
|
|
|
|
{
|
2012-11-28 17:01:25 +04:00
|
|
|
return call_without_gvl(func, data1, ubf, data2, FALSE);
|
2012-07-18 10:47:43 +04:00
|
|
|
}
|
|
|
|
|
2011-02-12 08:44:23 +03:00
|
|
|
VALUE
|
|
|
|
rb_thread_io_blocking_region(rb_blocking_function_t *func, void *data1, int fd)
|
|
|
|
{
|
2012-11-01 23:48:34 +04:00
|
|
|
VALUE val = Qundef; /* shouldn't be used */
|
2012-11-20 05:21:19 +04:00
|
|
|
rb_thread_t *th = GET_THREAD();
|
2011-02-12 08:44:23 +03:00
|
|
|
int saved_errno = 0;
|
2012-07-18 09:46:40 +04:00
|
|
|
int state;
|
2011-02-12 08:44:23 +03:00
|
|
|
|
2012-11-20 05:21:19 +04:00
|
|
|
th->waiting_fd = fd;
|
2012-07-18 09:46:40 +04:00
|
|
|
|
2012-11-20 05:21:19 +04:00
|
|
|
TH_PUSH_TAG(th);
|
2012-07-18 09:46:40 +04:00
|
|
|
if ((state = EXEC_TAG()) == 0) {
|
|
|
|
BLOCKING_REGION({
|
|
|
|
val = func(data1);
|
|
|
|
saved_errno = errno;
|
2012-11-28 17:01:25 +04:00
|
|
|
}, ubf_select, th, FALSE);
|
2012-07-18 09:46:40 +04:00
|
|
|
}
|
|
|
|
TH_POP_TAG();
|
|
|
|
|
|
|
|
/* clear waitinf_fd anytime */
|
2012-11-20 05:21:19 +04:00
|
|
|
th->waiting_fd = -1;
|
2012-07-18 09:46:40 +04:00
|
|
|
|
|
|
|
if (state) {
|
|
|
|
JUMP_TAG(state);
|
|
|
|
}
|
|
|
|
/* TODO: check func() */
|
2012-11-20 05:21:19 +04:00
|
|
|
RUBY_VM_CHECK_INTS_BLOCKING(th);
|
2012-07-18 09:46:40 +04:00
|
|
|
|
2011-02-12 08:44:23 +03:00
|
|
|
errno = saved_errno;
|
|
|
|
|
|
|
|
return val;
|
|
|
|
}
|
|
|
|
|
2008-12-30 10:57:53 +03:00
|
|
|
VALUE
|
2012-07-10 17:57:11 +04:00
|
|
|
rb_thread_blocking_region(
|
2008-12-30 10:57:53 +03:00
|
|
|
rb_blocking_function_t *func, void *data1,
|
|
|
|
rb_unblock_function_t *ubf, void *data2)
|
|
|
|
{
|
2012-07-10 17:57:11 +04:00
|
|
|
void *(*f)(void*) = (void *(*)(void*))func;
|
|
|
|
return (VALUE)rb_thread_call_without_gvl(f, data1, ubf, data2);
|
2008-12-30 10:57:53 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2012-10-31 04:07:17 +04:00
|
|
|
* rb_thread_call_with_gvl - re-enter the Ruby world after GVL release.
|
2008-12-30 10:57:53 +03:00
|
|
|
*
|
2012-10-31 04:07:17 +04:00
|
|
|
* After releasing GVL using rb_thread_blocking_region() or
|
|
|
|
* rb_thread_call_without_gvl() you can not access Ruby values or invoke
|
|
|
|
* methods. If you need to access Ruby you must use this function
|
|
|
|
* rb_thread_call_with_gvl().
|
2008-12-30 10:57:53 +03:00
|
|
|
*
|
|
|
|
* This function rb_thread_call_with_gvl() does:
|
|
|
|
* (1) acquire GVL.
|
|
|
|
* (2) call passed function `func'.
|
|
|
|
* (3) release GVL.
|
|
|
|
* (4) return a value which is returned at (2).
|
|
|
|
*
|
|
|
|
* NOTE: You should not return Ruby object at (2) because such Object
|
|
|
|
* will not marked.
|
|
|
|
*
|
2012-10-31 04:07:17 +04:00
|
|
|
* NOTE: If an exception is raised in `func', this function DOES NOT
|
2008-12-30 10:57:53 +03:00
|
|
|
* protect (catch) the exception. If you have any resources
|
|
|
|
* which should free before throwing exception, you need use
|
|
|
|
* rb_protect() in `func' and return a value which represents
|
|
|
|
* exception is raised.
|
|
|
|
*
|
2012-10-31 04:07:17 +04:00
|
|
|
* NOTE: This function should not be called by a thread which was not
|
|
|
|
* created as Ruby thread (created by Thread.new or so). In other
|
|
|
|
* words, this function *DOES NOT* associate or convert a NON-Ruby
|
|
|
|
* thread to a Ruby thread.
|
2008-12-30 10:57:53 +03:00
|
|
|
*/
|
|
|
|
void *
|
|
|
|
rb_thread_call_with_gvl(void *(*func)(void *), void *data1)
|
|
|
|
{
|
2012-11-20 05:21:19 +04:00
|
|
|
rb_thread_t *th = ruby_thread_from_native();
|
2008-12-30 10:57:53 +03:00
|
|
|
struct rb_blocking_region_buffer *brb;
|
|
|
|
struct rb_unblock_callback prev_unblock;
|
|
|
|
void *r;
|
|
|
|
|
2012-11-20 05:21:19 +04:00
|
|
|
if (th == 0) {
|
2008-12-30 10:57:53 +03:00
|
|
|
/* Error is occurred, but we can't use rb_bug()
|
|
|
|
* because this thread is not Ruby's thread.
|
|
|
|
* What should we do?
|
|
|
|
*/
|
|
|
|
|
|
|
|
fprintf(stderr, "[BUG] rb_thread_call_with_gvl() is called by non-ruby thread\n");
|
2011-07-10 12:29:46 +04:00
|
|
|
exit(EXIT_FAILURE);
|
2008-12-30 10:57:53 +03:00
|
|
|
}
|
|
|
|
|
2012-11-20 05:21:19 +04:00
|
|
|
brb = (struct rb_blocking_region_buffer *)th->blocking_region_buffer;
|
|
|
|
prev_unblock = th->unblock;
|
2008-12-30 10:57:53 +03:00
|
|
|
|
2009-01-12 04:43:23 +03:00
|
|
|
if (brb == 0) {
|
|
|
|
rb_bug("rb_thread_call_with_gvl: called by a thread which has GVL.");
|
|
|
|
}
|
|
|
|
|
2012-11-20 05:21:19 +04:00
|
|
|
blocking_region_end(th, brb);
|
2008-12-30 10:57:53 +03:00
|
|
|
/* enter to Ruby world: You can access Ruby values, methods and so on. */
|
|
|
|
r = (*func)(data1);
|
2009-11-03 20:46:28 +03:00
|
|
|
/* leave from Ruby world: You can not access Ruby values, etc. */
|
2012-11-28 17:01:25 +04:00
|
|
|
blocking_region_begin(th, brb, prev_unblock.func, prev_unblock.arg, FALSE);
|
2008-12-30 10:57:53 +03:00
|
|
|
return r;
|
|
|
|
}
|
|
|
|
|
2009-01-12 04:43:23 +03:00
|
|
|
/*
|
|
|
|
* ruby_thread_has_gvl_p - check if current native thread has GVL.
|
|
|
|
*
|
|
|
|
***
|
|
|
|
*** This API is EXPERIMENTAL!
|
|
|
|
*** We do not guarantee that this API remains in ruby 1.9.2 or later.
|
|
|
|
***
|
|
|
|
*/
|
|
|
|
|
|
|
|
int
|
|
|
|
ruby_thread_has_gvl_p(void)
|
|
|
|
{
|
|
|
|
rb_thread_t *th = ruby_thread_from_native();
|
|
|
|
|
|
|
|
if (th && th->blocking_region_buffer == 0) {
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2006-12-31 18:02:22 +03:00
|
|
|
/*
|
2011-06-30 01:23:36 +04:00
|
|
|
* call-seq:
|
|
|
|
* Thread.pass -> nil
|
* 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 08:25:10 +04:00
|
|
|
*
|
2011-06-30 01:23:36 +04:00
|
|
|
* Give the thread scheduler a hint to pass execution to another thread.
|
|
|
|
* A running thread may or may not switch, it depends on OS and processor.
|
2006-12-31 18:02:22 +03:00
|
|
|
*/
|
|
|
|
|
|
|
|
static VALUE
|
2007-02-05 15:21:01 +03:00
|
|
|
thread_s_pass(VALUE klass)
|
2006-12-31 18:02:22 +03:00
|
|
|
{
|
|
|
|
rb_thread_schedule();
|
|
|
|
return Qnil;
|
|
|
|
}
|
|
|
|
|
2012-07-19 18:19:40 +04:00
|
|
|
/*****************************************************/
|
|
|
|
|
2006-12-31 18:02:22 +03:00
|
|
|
/*
|
2012-07-19 18:19:40 +04:00
|
|
|
* rb_threadptr_async_errinfo_* - manage async errors queue
|
|
|
|
*
|
|
|
|
* Async events such as an exception throwed by Thread#raise,
|
|
|
|
* Thread#kill and thread termination (after main thread termination)
|
|
|
|
* will be queued to th->async_errinfo_queue.
|
|
|
|
* - clear: clear the queue.
|
|
|
|
* - enque: enque err object into queue.
|
|
|
|
* - deque: deque err object from queue.
|
|
|
|
* - active_p: return 1 if the queue should be checked.
|
2006-12-31 18:02:22 +03:00
|
|
|
*
|
2012-07-19 18:19:40 +04:00
|
|
|
* All rb_threadptr_async_errinfo_* functions are called by
|
|
|
|
* a GVL acquired thread, of course.
|
|
|
|
* Note that all "rb_" prefix APIs need GVL to call.
|
2006-12-31 18:02:22 +03:00
|
|
|
*/
|
|
|
|
|
2012-07-19 18:19:40 +04:00
|
|
|
void
|
|
|
|
rb_threadptr_async_errinfo_clear(rb_thread_t *th)
|
|
|
|
{
|
|
|
|
rb_ary_clear(th->async_errinfo_queue);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
rb_threadptr_async_errinfo_enque(rb_thread_t *th, VALUE v)
|
|
|
|
{
|
|
|
|
rb_ary_push(th->async_errinfo_queue, v);
|
|
|
|
th->async_errinfo_queue_checked = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
enum interrupt_timing {
|
|
|
|
INTERRUPT_NONE,
|
|
|
|
INTERRUPT_IMMEDIATE,
|
|
|
|
INTERRUPT_ON_BLOCKING,
|
|
|
|
INTERRUPT_NEVER
|
|
|
|
};
|
|
|
|
|
|
|
|
static enum interrupt_timing
|
|
|
|
rb_threadptr_async_errinfo_check_mask(rb_thread_t *th, VALUE err)
|
|
|
|
{
|
|
|
|
VALUE mask;
|
|
|
|
long mask_stack_len = RARRAY_LEN(th->async_errinfo_mask_stack);
|
|
|
|
VALUE *mask_stack = RARRAY_PTR(th->async_errinfo_mask_stack);
|
|
|
|
VALUE ancestors = rb_mod_ancestors(err); /* TODO: GC guard */
|
|
|
|
long ancestors_len = RARRAY_LEN(ancestors);
|
|
|
|
VALUE *ancestors_ptr = RARRAY_PTR(ancestors);
|
|
|
|
int i, j;
|
|
|
|
|
|
|
|
for (i=0; i<mask_stack_len; i++) {
|
|
|
|
mask = mask_stack[mask_stack_len-(i+1)];
|
|
|
|
|
|
|
|
for (j=0; j<ancestors_len; j++) {
|
|
|
|
VALUE klass = ancestors_ptr[j];
|
|
|
|
VALUE sym;
|
|
|
|
|
|
|
|
/* TODO: remove rb_intern() */
|
|
|
|
if ((sym = rb_hash_aref(mask, klass)) != Qnil) {
|
|
|
|
if (sym == ID2SYM(rb_intern("immediate"))) {
|
|
|
|
return INTERRUPT_IMMEDIATE;
|
|
|
|
}
|
|
|
|
else if (sym == ID2SYM(rb_intern("on_blocking"))) {
|
|
|
|
return INTERRUPT_ON_BLOCKING;
|
|
|
|
}
|
|
|
|
else if (sym == ID2SYM(rb_intern("never"))) {
|
|
|
|
return INTERRUPT_NEVER;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
rb_raise(rb_eThreadError, "unknown mask signature");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
/* try to next mask */
|
|
|
|
}
|
|
|
|
return INTERRUPT_NONE;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
rb_threadptr_async_errinfo_empty_p(rb_thread_t *th)
|
|
|
|
{
|
|
|
|
return RARRAY_LEN(th->async_errinfo_queue) == 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static VALUE
|
|
|
|
rb_threadptr_async_errinfo_deque(rb_thread_t *th, enum interrupt_timing timing)
|
|
|
|
{
|
|
|
|
#if 1 /* 1 to enable Thread#control_interrupt, 0 to ignore it */
|
|
|
|
int i;
|
|
|
|
|
|
|
|
for (i=0; i<RARRAY_LEN(th->async_errinfo_queue); i++) {
|
|
|
|
VALUE err = RARRAY_PTR(th->async_errinfo_queue)[i];
|
|
|
|
|
|
|
|
enum interrupt_timing mask_timing = rb_threadptr_async_errinfo_check_mask(th, CLASS_OF(err));
|
|
|
|
|
|
|
|
switch (mask_timing) {
|
|
|
|
case INTERRUPT_ON_BLOCKING:
|
|
|
|
if (timing != INTERRUPT_ON_BLOCKING) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
/* fall through */
|
|
|
|
case INTERRUPT_NONE: /* default: IMMEDIATE */
|
|
|
|
case INTERRUPT_IMMEDIATE:
|
|
|
|
rb_ary_delete_at(th->async_errinfo_queue, i);
|
|
|
|
return err;
|
|
|
|
case INTERRUPT_NEVER:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
th->async_errinfo_queue_checked = 1;
|
|
|
|
return Qundef;
|
|
|
|
#else
|
|
|
|
VALUE err = rb_ary_shift(th->async_errinfo_queue);
|
|
|
|
if (rb_threadptr_async_errinfo_empty_p(th)) {
|
|
|
|
th->async_errinfo_queue_checked = 1;
|
|
|
|
}
|
|
|
|
return err;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
rb_threadptr_async_errinfo_active_p(rb_thread_t *th)
|
|
|
|
{
|
2012-11-19 15:13:40 +04:00
|
|
|
/*
|
|
|
|
* For optimization, we don't check async errinfo queue
|
|
|
|
* if it nor a thread interrupt mask were not changed
|
|
|
|
* since last check.
|
|
|
|
*/
|
|
|
|
if (th->async_errinfo_queue_checked) {
|
2012-07-19 18:19:40 +04:00
|
|
|
return 0;
|
|
|
|
}
|
2012-11-19 15:13:40 +04:00
|
|
|
|
|
|
|
if (rb_threadptr_async_errinfo_empty_p(th)) {
|
|
|
|
return 0;
|
2012-07-19 18:19:40 +04:00
|
|
|
}
|
2012-11-19 15:13:40 +04:00
|
|
|
|
|
|
|
return 1;
|
2012-07-19 18:19:40 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
static VALUE
|
2012-11-20 05:21:19 +04:00
|
|
|
rb_threadptr_interrupt_mask(rb_thread_t *th, VALUE mask, VALUE (*func)(rb_thread_t *th))
|
2012-07-19 18:19:40 +04:00
|
|
|
{
|
|
|
|
VALUE r = Qnil;
|
|
|
|
int state;
|
|
|
|
|
2012-11-20 05:21:19 +04:00
|
|
|
rb_ary_push(th->async_errinfo_mask_stack, mask);
|
|
|
|
if (!rb_threadptr_async_errinfo_empty_p(th)) {
|
|
|
|
th->async_errinfo_queue_checked = 0;
|
|
|
|
RUBY_VM_SET_INTERRUPT(th);
|
2012-07-19 18:19:40 +04:00
|
|
|
}
|
|
|
|
|
2012-11-20 05:21:19 +04:00
|
|
|
TH_PUSH_TAG(th);
|
2012-07-19 18:19:40 +04:00
|
|
|
if ((state = EXEC_TAG()) == 0) {
|
2012-11-20 05:21:19 +04:00
|
|
|
r = func(th);
|
2012-07-19 18:19:40 +04:00
|
|
|
}
|
|
|
|
TH_POP_TAG();
|
|
|
|
|
2012-11-20 05:21:19 +04:00
|
|
|
rb_ary_pop(th->async_errinfo_mask_stack);
|
|
|
|
if (!rb_threadptr_async_errinfo_empty_p(th)) {
|
|
|
|
th->async_errinfo_queue_checked = 0;
|
|
|
|
RUBY_VM_SET_INTERRUPT(th);
|
2012-07-19 18:19:40 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
if (state) {
|
|
|
|
JUMP_TAG(state);
|
|
|
|
}
|
|
|
|
|
2012-11-20 05:21:19 +04:00
|
|
|
RUBY_VM_CHECK_INTS(th);
|
2012-10-17 03:03:54 +04:00
|
|
|
|
2012-07-19 18:19:40 +04:00
|
|
|
return r;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* call-seq:
|
|
|
|
* Thread.control_interrupt(hash) { ... } -> result of the block
|
|
|
|
*
|
|
|
|
* Thread.control_interrupt controls interrupt timing.
|
|
|
|
*
|
|
|
|
* _interrupt_ means asynchronous event and corresponding procedure
|
|
|
|
* by Thread#raise, Thread#kill, signal trap (not supported yet)
|
|
|
|
* and main thread termination (if main thread terminates, then all
|
|
|
|
* other thread will be killed).
|
|
|
|
*
|
|
|
|
* _hash_ has pairs of ExceptionClass and TimingSymbol. TimingSymbol
|
|
|
|
* is one of them:
|
|
|
|
* - :immediate Invoke interrupt immediately.
|
|
|
|
* - :on_blocking Invoke interrupt while _BlockingOperation_.
|
|
|
|
* - :never Never invoke interrupt.
|
|
|
|
*
|
|
|
|
* _BlockingOperation_ means that the operation will block the calling thread,
|
|
|
|
* such as read and write. On CRuby implementation, _BlockingOperation_ is
|
|
|
|
* operation executed without GVL.
|
|
|
|
*
|
|
|
|
* Masked interrupts are delayed until they are enabled.
|
|
|
|
* This method is similar to sigprocmask(3).
|
|
|
|
*
|
|
|
|
* TODO (DOC): control_interrupt is stacked.
|
|
|
|
* TODO (DOC): check ancestors.
|
|
|
|
* TODO (DOC): to prevent all interrupt, {Object => :never} works.
|
|
|
|
*
|
|
|
|
* NOTE: Asynchronous interrupts are difficult to use.
|
|
|
|
* If you need to communicate between threads,
|
|
|
|
* please consider to use another way such as Queue.
|
|
|
|
* Or use them with deep understanding about this method.
|
|
|
|
*
|
|
|
|
*
|
|
|
|
* # example: Guard from Thread#raise
|
|
|
|
* th = Thread.new do
|
|
|
|
* Thead.control_interrupt(RuntimeError => :never) {
|
|
|
|
* begin
|
|
|
|
* # Thread#raise doesn't interrupt here.
|
|
|
|
* # You can write resource allocation code safely.
|
|
|
|
* Thread.control_interrupt(RuntimeError => :immediate) {
|
|
|
|
* # ...
|
|
|
|
* # It is possible to be interrupted by Thread#raise.
|
|
|
|
* }
|
|
|
|
* ensure
|
|
|
|
* # Thread#raise doesn't interrupt here.
|
|
|
|
* # You can write resource dealocation code safely.
|
|
|
|
* end
|
|
|
|
* }
|
|
|
|
* end
|
|
|
|
* Thread.pass
|
|
|
|
* # ...
|
|
|
|
* th.raise "stop"
|
|
|
|
*
|
|
|
|
* # example: Guard from TimeoutError
|
|
|
|
* require 'timeout'
|
|
|
|
* Thread.control_interrupt(TimeoutError => :never) {
|
|
|
|
* timeout(10){
|
|
|
|
* # TimeoutError doesn't occur here
|
|
|
|
* Thread.control_interrupt(TimeoutError => :on_blocking) {
|
|
|
|
* # possible to be killed by TimeoutError
|
|
|
|
* # while blocking operation
|
|
|
|
* }
|
|
|
|
* # TimeoutError doesn't occur here
|
|
|
|
* }
|
|
|
|
* }
|
|
|
|
*
|
|
|
|
* # example: Stack control settings
|
|
|
|
* Thread.control_interrupt(FooError => :never) {
|
|
|
|
* Thread.control_interrupt(BarError => :never) {
|
|
|
|
* # FooError and BarError are prohibited.
|
|
|
|
* }
|
|
|
|
* }
|
|
|
|
*
|
|
|
|
* # example: check ancestors
|
|
|
|
* Thread.control_interrupt(Exception => :never) {
|
|
|
|
* # all exceptions inherited from Exception are prohibited.
|
|
|
|
* }
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
static VALUE
|
|
|
|
control_interrupt_func(rb_thread_t *th)
|
|
|
|
{
|
|
|
|
return rb_yield(Qnil);
|
|
|
|
}
|
|
|
|
|
|
|
|
static VALUE
|
|
|
|
rb_thread_s_control_interrupt(VALUE self, VALUE mask_arg)
|
|
|
|
{
|
|
|
|
if (!rb_block_given_p()) {
|
|
|
|
rb_raise(rb_eArgError, "block is needed.");
|
|
|
|
}
|
|
|
|
|
|
|
|
return rb_threadptr_interrupt_mask(GET_THREAD(),
|
|
|
|
rb_convert_type(mask_arg, T_HASH, "Hash", "to_hash"),
|
|
|
|
control_interrupt_func);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* call-seq:
|
|
|
|
* Thread.check_interrupt() -> nil
|
|
|
|
*
|
|
|
|
* Check queued interrupts.
|
|
|
|
*
|
|
|
|
* If there are queued interrupts, process respective procedures.
|
|
|
|
*
|
|
|
|
* This method can be defined as the following Ruby code:
|
|
|
|
*
|
|
|
|
* def Thread.check_interrupt
|
|
|
|
* Thread.control_interrupt(Object => :immediate) {
|
|
|
|
* Thread.pass
|
|
|
|
* }
|
|
|
|
* end
|
|
|
|
*
|
|
|
|
* Examples:
|
|
|
|
*
|
|
|
|
* th = Thread.new{
|
|
|
|
* Thread.control_interrupt(RuntimeError => :on_blocking){
|
|
|
|
* while true
|
|
|
|
* ...
|
|
|
|
* # reach safe point to invoke interrupt
|
|
|
|
* Thread.check_interrupt
|
|
|
|
* ...
|
|
|
|
* end
|
|
|
|
* }
|
|
|
|
* }
|
|
|
|
* ...
|
|
|
|
* th.raise # stop thread
|
|
|
|
*
|
|
|
|
* NOTE: This example can be described by the another code.
|
|
|
|
* You need to keep to avoid asynchronous interrupts.
|
|
|
|
*
|
|
|
|
* flag = true
|
|
|
|
* th = Thread.new{
|
|
|
|
* Thread.control_interrupt(RuntimeError => :on_blocking){
|
|
|
|
* while true
|
|
|
|
* ...
|
|
|
|
* # reach safe point to invoke interrupt
|
|
|
|
* break if flag == false
|
|
|
|
* ...
|
|
|
|
* end
|
|
|
|
* }
|
|
|
|
* }
|
|
|
|
* ...
|
|
|
|
* flag = false # stop thread
|
|
|
|
*/
|
2012-11-20 05:21:19 +04:00
|
|
|
|
2012-07-19 18:19:40 +04:00
|
|
|
static VALUE
|
2012-11-20 05:21:19 +04:00
|
|
|
check_interrupt_func(rb_thread_t *th)
|
2012-07-19 18:19:40 +04:00
|
|
|
{
|
2012-11-20 05:21:19 +04:00
|
|
|
RUBY_VM_CHECK_INTS(th);
|
2012-07-19 18:19:40 +04:00
|
|
|
return Qnil;
|
|
|
|
}
|
|
|
|
|
|
|
|
static VALUE
|
|
|
|
rb_thread_s_check_interrupt(VALUE self)
|
|
|
|
{
|
2012-11-19 15:15:07 +04:00
|
|
|
rb_thread_t *cur_th = GET_THREAD();
|
2012-07-19 18:19:40 +04:00
|
|
|
|
2012-11-19 15:15:07 +04:00
|
|
|
if (!rb_threadptr_async_errinfo_empty_p(cur_th)) {
|
2012-07-19 18:19:40 +04:00
|
|
|
VALUE mask = rb_hash_new();
|
|
|
|
rb_hash_aset(mask, rb_cObject, ID2SYM(rb_intern("immediate")));
|
2012-11-19 15:15:07 +04:00
|
|
|
rb_threadptr_interrupt_mask(cur_th, mask, check_interrupt_func);
|
2012-07-19 18:19:40 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
return Qnil;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
rb_threadptr_to_kill(rb_thread_t *th)
|
|
|
|
{
|
|
|
|
rb_threadptr_async_errinfo_clear(th);
|
2012-11-28 12:31:03 +04:00
|
|
|
th->status = THREAD_RUNNABLE;
|
|
|
|
th->to_kill = 1;
|
2012-07-19 18:19:40 +04:00
|
|
|
th->errinfo = INT2FIX(TAG_FATAL);
|
|
|
|
TH_JUMP_TAG(th, TAG_FATAL);
|
|
|
|
}
|
|
|
|
|
2012-11-19 15:04:39 +04:00
|
|
|
void
|
2012-11-20 05:21:19 +04:00
|
|
|
rb_threadptr_execute_interrupts(rb_thread_t *th, int blocking_timing)
|
2006-12-31 18:02:22 +03:00
|
|
|
{
|
2012-11-20 05:21:19 +04:00
|
|
|
if (th->raised_flag) return;
|
2008-07-27 09:59:32 +04:00
|
|
|
|
2012-11-26 14:57:39 +04:00
|
|
|
while (1) {
|
|
|
|
rb_atomic_t interrupt;
|
|
|
|
rb_atomic_t old;
|
2011-07-01 02:29:34 +04:00
|
|
|
int sig;
|
2012-11-26 14:57:39 +04:00
|
|
|
int timer_interrupt;
|
|
|
|
int async_errinfo_interrupt;
|
|
|
|
int finalizer_interrupt;
|
|
|
|
int trap_interrupt;
|
|
|
|
|
|
|
|
do {
|
|
|
|
interrupt = th->interrupt_flag;
|
|
|
|
old = ATOMIC_CAS(th->interrupt_flag, interrupt, interrupt & th->interrupt_mask);
|
|
|
|
} while (old != interrupt);
|
|
|
|
|
|
|
|
interrupt &= ~th->interrupt_mask;
|
|
|
|
if (!interrupt)
|
|
|
|
return;
|
|
|
|
|
2012-11-26 16:17:10 +04:00
|
|
|
timer_interrupt = interrupt & TIMER_INTERRUPT_MASK;
|
|
|
|
async_errinfo_interrupt = interrupt & ASYNC_ERRINFO_INTERRUPT_MASK;
|
|
|
|
finalizer_interrupt = interrupt & FINALIZER_INTERRUPT_MASK;
|
|
|
|
trap_interrupt = interrupt & TRAP_INTERRUPT_MASK;
|
2008-07-27 09:59:32 +04:00
|
|
|
|
2012-11-28 12:31:03 +04:00
|
|
|
|
2006-12-31 18:02:22 +03:00
|
|
|
|
|
|
|
/* signal handling */
|
2012-11-26 13:22:01 +04:00
|
|
|
if (trap_interrupt && (th == th->vm->main_thread)) {
|
2012-11-28 12:31:03 +04:00
|
|
|
enum rb_thread_status prev_status = th->status;
|
|
|
|
th->status = THREAD_RUNNABLE;
|
2011-07-01 02:29:34 +04:00
|
|
|
while ((sig = rb_get_next_signal()) != 0) {
|
2012-11-20 05:21:19 +04:00
|
|
|
rb_signal_exec(th, sig);
|
2011-07-01 02:29:34 +04:00
|
|
|
}
|
2012-11-28 12:31:03 +04:00
|
|
|
th->status = prev_status;
|
2006-12-31 18:02:22 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/* exception from another thread */
|
2012-11-26 13:22:01 +04:00
|
|
|
if (async_errinfo_interrupt && rb_threadptr_async_errinfo_active_p(th)) {
|
2012-11-20 05:21:19 +04:00
|
|
|
VALUE err = rb_threadptr_async_errinfo_deque(th, blocking_timing ? INTERRUPT_ON_BLOCKING : INTERRUPT_NONE);
|
2010-10-12 19:03:51 +04:00
|
|
|
thread_debug("rb_thread_execute_interrupts: %"PRIdVALUE"\n", err);
|
2006-12-31 18:02:22 +03:00
|
|
|
|
2012-07-19 18:19:40 +04:00
|
|
|
if (err == Qundef) {
|
|
|
|
/* no error */
|
|
|
|
}
|
|
|
|
else if (err == eKillSignal /* Thread#kill receieved */ ||
|
|
|
|
err == eTerminateSignal /* Terminate thread */ ) {
|
2012-11-20 05:21:19 +04:00
|
|
|
rb_threadptr_to_kill(th);
|
2006-12-31 18:02:22 +03:00
|
|
|
}
|
|
|
|
else {
|
2012-11-28 12:31:03 +04:00
|
|
|
/* set runnable if th was slept. */
|
|
|
|
if (th->status == THREAD_STOPPED ||
|
|
|
|
th->status == THREAD_STOPPED_FOREVER)
|
|
|
|
th->status = THREAD_RUNNABLE;
|
2006-12-31 18:02:22 +03:00
|
|
|
rb_exc_raise(err);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-07-27 09:59:32 +04:00
|
|
|
if (finalizer_interrupt) {
|
|
|
|
rb_gc_finalize_deferred();
|
|
|
|
}
|
|
|
|
|
2011-06-29 21:36:00 +04:00
|
|
|
if (timer_interrupt) {
|
2012-03-29 05:15:37 +04:00
|
|
|
unsigned long limits_us = TIME_QUANTUM_USEC;
|
2011-06-13 18:14:53 +04:00
|
|
|
|
2012-11-20 05:21:19 +04:00
|
|
|
if (th->priority > 0)
|
|
|
|
limits_us <<= th->priority;
|
2011-06-13 18:14:53 +04:00
|
|
|
else
|
2012-11-20 05:21:19 +04:00
|
|
|
limits_us >>= -th->priority;
|
2011-06-13 18:14:53 +04:00
|
|
|
|
2012-11-28 12:31:03 +04:00
|
|
|
if (th->status == THREAD_RUNNABLE)
|
2012-11-20 05:21:19 +04:00
|
|
|
th->running_time_us += TIME_QUANTUM_USEC;
|
2011-06-13 18:14:53 +04:00
|
|
|
|
2012-11-20 13:48:24 +04:00
|
|
|
EXEC_EVENT_HOOK(th, RUBY_EVENT_SWITCH, th->cfp->self, 0, 0, Qundef);
|
2008-08-14 01:26:49 +04:00
|
|
|
|
2011-06-29 21:36:00 +04:00
|
|
|
rb_thread_schedule_limits(limits_us);
|
2008-07-27 09:59:32 +04:00
|
|
|
}
|
2006-12-31 18:02:22 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-06-09 19:02:46 +04:00
|
|
|
void
|
2011-06-11 05:17:11 +04:00
|
|
|
rb_thread_execute_interrupts(VALUE thval)
|
2011-06-09 19:02:46 +04:00
|
|
|
{
|
2012-11-20 05:21:19 +04:00
|
|
|
rb_thread_t *th;
|
|
|
|
GetThreadPtr(thval, th);
|
|
|
|
rb_threadptr_execute_interrupts(th, 1);
|
2012-07-18 09:46:40 +04:00
|
|
|
}
|
|
|
|
|
2006-12-31 18:02:22 +03:00
|
|
|
static void
|
2009-06-08 20:14:06 +04:00
|
|
|
rb_threadptr_ready(rb_thread_t *th)
|
2006-12-31 18:02:22 +03:00
|
|
|
{
|
2009-06-08 20:14:06 +04:00
|
|
|
rb_threadptr_interrupt(th);
|
2006-12-31 18:02:22 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
static VALUE
|
2009-06-08 20:14:06 +04:00
|
|
|
rb_threadptr_raise(rb_thread_t *th, int argc, VALUE *argv)
|
2006-12-31 18:02:22 +03:00
|
|
|
{
|
|
|
|
VALUE exc;
|
|
|
|
|
2009-06-08 20:14:06 +04:00
|
|
|
if (rb_threadptr_dead(th)) {
|
2006-12-31 18:02:22 +03:00
|
|
|
return Qnil;
|
|
|
|
}
|
|
|
|
|
2012-07-19 18:19:40 +04:00
|
|
|
if (argc == 0) {
|
|
|
|
exc = rb_exc_new(rb_eRuntimeError, 0, 0);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
exc = rb_make_exception(argc, argv);
|
|
|
|
}
|
2012-07-18 09:46:40 +04:00
|
|
|
rb_threadptr_async_errinfo_enque(th, exc);
|
|
|
|
rb_threadptr_interrupt(th);
|
2006-12-31 18:02:22 +03:00
|
|
|
return Qnil;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2009-06-08 20:14:06 +04:00
|
|
|
rb_threadptr_signal_raise(rb_thread_t *th, int sig)
|
2006-12-31 18:02:22 +03:00
|
|
|
{
|
2007-04-19 21:37:03 +04:00
|
|
|
VALUE argv[2];
|
|
|
|
|
|
|
|
argv[0] = rb_eSignal;
|
|
|
|
argv[1] = INT2FIX(sig);
|
2009-06-08 20:14:06 +04:00
|
|
|
rb_threadptr_raise(th->vm->main_thread, 2, argv);
|
2006-12-31 18:02:22 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2009-06-08 20:14:06 +04:00
|
|
|
rb_threadptr_signal_exit(rb_thread_t *th)
|
2006-12-31 18:02:22 +03:00
|
|
|
{
|
2007-04-19 21:37:03 +04:00
|
|
|
VALUE argv[2];
|
|
|
|
|
|
|
|
argv[0] = rb_eSystemExit;
|
|
|
|
argv[1] = rb_str_new2("exit");
|
2009-06-08 20:14:06 +04:00
|
|
|
rb_threadptr_raise(th->vm->main_thread, 2, argv);
|
2006-12-31 18:02:22 +03:00
|
|
|
}
|
|
|
|
|
2009-06-17 07:56:29 +04:00
|
|
|
#if defined(POSIX_SIGNAL) && defined(SIGSEGV) && defined(HAVE_SIGALTSTACK)
|
|
|
|
#define USE_SIGALTSTACK
|
|
|
|
#endif
|
|
|
|
|
2008-11-27 09:05:07 +03:00
|
|
|
void
|
|
|
|
ruby_thread_stack_overflow(rb_thread_t *th)
|
|
|
|
{
|
|
|
|
th->raised_flag = 0;
|
2009-06-17 07:56:29 +04:00
|
|
|
#ifdef USE_SIGALTSTACK
|
|
|
|
rb_exc_raise(sysstack_error);
|
|
|
|
#else
|
|
|
|
th->errinfo = sysstack_error;
|
2008-11-27 09:05:07 +03:00
|
|
|
TH_JUMP_TAG(th, TAG_RAISE);
|
2009-06-17 07:56:29 +04:00
|
|
|
#endif
|
2008-11-27 09:05:07 +03:00
|
|
|
}
|
|
|
|
|
2007-02-05 15:21:01 +03:00
|
|
|
int
|
2009-06-08 20:14:06 +04:00
|
|
|
rb_threadptr_set_raised(rb_thread_t *th)
|
2007-02-05 15:21:01 +03:00
|
|
|
{
|
2008-02-28 07:52:01 +03:00
|
|
|
if (th->raised_flag & RAISED_EXCEPTION) {
|
2007-02-05 15:21:01 +03:00
|
|
|
return 1;
|
|
|
|
}
|
2008-02-28 07:52:01 +03:00
|
|
|
th->raised_flag |= RAISED_EXCEPTION;
|
2007-02-05 15:21:01 +03:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
2009-06-08 20:14:06 +04:00
|
|
|
rb_threadptr_reset_raised(rb_thread_t *th)
|
2007-02-05 15:21:01 +03:00
|
|
|
{
|
2008-02-28 07:52:01 +03:00
|
|
|
if (!(th->raised_flag & RAISED_EXCEPTION)) {
|
2007-02-05 15:21:01 +03:00
|
|
|
return 0;
|
|
|
|
}
|
2008-02-28 07:52:01 +03:00
|
|
|
th->raised_flag &= ~RAISED_EXCEPTION;
|
2007-02-05 15:21:01 +03:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2011-02-12 08:44:23 +03:00
|
|
|
static int
|
|
|
|
thread_fd_close_i(st_data_t key, st_data_t val, st_data_t data)
|
|
|
|
{
|
|
|
|
int fd = (int)data;
|
|
|
|
rb_thread_t *th;
|
|
|
|
GetThreadPtr((VALUE)key, th);
|
|
|
|
|
2012-07-18 09:46:40 +04:00
|
|
|
if (th->waiting_fd == fd) {
|
|
|
|
VALUE err = th->vm->special_exceptions[ruby_error_closed_stream];
|
|
|
|
rb_threadptr_async_errinfo_enque(th, err);
|
|
|
|
rb_threadptr_interrupt(th);
|
2011-02-12 08:44:23 +03:00
|
|
|
}
|
|
|
|
return ST_CONTINUE;
|
|
|
|
}
|
|
|
|
|
2007-02-05 15:21:01 +03:00
|
|
|
void
|
|
|
|
rb_thread_fd_close(int fd)
|
|
|
|
{
|
2011-02-12 08:44:23 +03:00
|
|
|
st_foreach(GET_THREAD()->vm->living_threads, thread_fd_close_i, (st_index_t)fd);
|
2007-02-05 15:21:01 +03:00
|
|
|
}
|
2006-12-31 18:02:22 +03:00
|
|
|
|
|
|
|
/*
|
|
|
|
* call-seq:
|
2009-10-12 02:08:51 +04:00
|
|
|
* thr.raise
|
|
|
|
* thr.raise(string)
|
|
|
|
* thr.raise(exception [, string [, array]])
|
* 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 08:25:10 +04:00
|
|
|
*
|
2006-12-31 18:02:22 +03:00
|
|
|
* Raises an exception (see <code>Kernel::raise</code>) from <i>thr</i>. The
|
|
|
|
* caller does not have to be <i>thr</i>.
|
* 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 08:25:10 +04:00
|
|
|
*
|
2006-12-31 18:02:22 +03:00
|
|
|
* Thread.abort_on_exception = true
|
|
|
|
* a = Thread.new { sleep(200) }
|
|
|
|
* a.raise("Gotcha")
|
* 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 08:25:10 +04:00
|
|
|
*
|
2006-12-31 18:02:22 +03:00
|
|
|
* <em>produces:</em>
|
* 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 08:25:10 +04:00
|
|
|
*
|
2006-12-31 18:02:22 +03:00
|
|
|
* prog.rb:3: Gotcha (RuntimeError)
|
|
|
|
* from prog.rb:2:in `initialize'
|
|
|
|
* from prog.rb:2:in `new'
|
|
|
|
* from prog.rb:2
|
|
|
|
*/
|
|
|
|
|
|
|
|
static VALUE
|
2007-02-05 15:21:01 +03:00
|
|
|
thread_raise_m(int argc, VALUE *argv, VALUE self)
|
2006-12-31 18:02:22 +03:00
|
|
|
{
|
2012-10-23 17:37:45 +04:00
|
|
|
rb_thread_t *target_th;
|
2012-11-20 05:21:19 +04:00
|
|
|
rb_thread_t *th = GET_THREAD();
|
2012-10-23 17:37:45 +04:00
|
|
|
GetThreadPtr(self, target_th);
|
|
|
|
rb_threadptr_raise(target_th, argc, argv);
|
|
|
|
|
|
|
|
/* To perform Thread.current.raise as Kernel.raise */
|
2012-11-20 05:21:19 +04:00
|
|
|
if (th == target_th) {
|
|
|
|
RUBY_VM_CHECK_INTS(th);
|
2012-10-23 17:37:45 +04:00
|
|
|
}
|
2006-12-31 18:02:22 +03:00
|
|
|
return Qnil;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* call-seq:
|
2010-05-18 01:07:33 +04:00
|
|
|
* thr.exit -> thr or nil
|
|
|
|
* thr.kill -> thr or nil
|
|
|
|
* thr.terminate -> thr or nil
|
* 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 08:25:10 +04:00
|
|
|
*
|
2006-12-31 18:02:22 +03:00
|
|
|
* Terminates <i>thr</i> and schedules another thread to be run. If this thread
|
|
|
|
* is already marked to be killed, <code>exit</code> returns the
|
|
|
|
* <code>Thread</code>. If this is the main thread, or the last thread, exits
|
|
|
|
* the process.
|
|
|
|
*/
|
|
|
|
|
|
|
|
VALUE
|
|
|
|
rb_thread_kill(VALUE 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 09:37:46 +03:00
|
|
|
rb_thread_t *th;
|
2006-12-31 18:02:22 +03:00
|
|
|
|
|
|
|
GetThreadPtr(thread, th);
|
|
|
|
|
|
|
|
if (th != GET_THREAD() && th->safe_level < 4) {
|
|
|
|
rb_secure(4);
|
|
|
|
}
|
2012-11-28 12:31:03 +04:00
|
|
|
if (th->to_kill || th->status == THREAD_KILLED) {
|
2006-12-31 18:02:22 +03:00
|
|
|
return thread;
|
|
|
|
}
|
|
|
|
if (th == th->vm->main_thread) {
|
|
|
|
rb_exit(EXIT_SUCCESS);
|
|
|
|
}
|
|
|
|
|
* 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-09 07:33:55 +03:00
|
|
|
thread_debug("rb_thread_kill: %p (%p)\n", (void *)th, (void *)th->thread_id);
|
2006-12-31 18:02:22 +03:00
|
|
|
|
2012-07-19 18:19:40 +04:00
|
|
|
if (th == GET_THREAD()) {
|
|
|
|
/* kill myself immediately */
|
|
|
|
rb_threadptr_to_kill(th);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
rb_threadptr_async_errinfo_enque(th, eKillSignal);
|
|
|
|
rb_threadptr_interrupt(th);
|
|
|
|
}
|
2006-12-31 18:02:22 +03:00
|
|
|
return thread;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* call-seq:
|
2010-05-18 01:07:33 +04:00
|
|
|
* Thread.kill(thread) -> thread
|
* 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 08:25:10 +04:00
|
|
|
*
|
2006-12-31 18:02:22 +03:00
|
|
|
* Causes the given <em>thread</em> to exit (see <code>Thread::exit</code>).
|
* 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 08:25:10 +04:00
|
|
|
*
|
2006-12-31 18:02:22 +03:00
|
|
|
* count = 0
|
|
|
|
* a = Thread.new { loop { count += 1 } }
|
|
|
|
* sleep(0.1) #=> 0
|
|
|
|
* Thread.kill(a) #=> #<Thread:0x401b3d30 dead>
|
|
|
|
* count #=> 93947
|
|
|
|
* a.alive? #=> false
|
|
|
|
*/
|
|
|
|
|
|
|
|
static VALUE
|
|
|
|
rb_thread_s_kill(VALUE obj, VALUE th)
|
|
|
|
{
|
|
|
|
return rb_thread_kill(th);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* call-seq:
|
2010-05-18 01:07:33 +04:00
|
|
|
* Thread.exit -> thread
|
* 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 08:25:10 +04:00
|
|
|
*
|
2006-12-31 18:02:22 +03:00
|
|
|
* Terminates the currently running thread and schedules another thread to be
|
|
|
|
* run. If this thread is already marked to be killed, <code>exit</code>
|
|
|
|
* returns the <code>Thread</code>. If this is the main thread, or the last
|
|
|
|
* thread, exit the process.
|
|
|
|
*/
|
|
|
|
|
|
|
|
static VALUE
|
2007-03-19 06:58:57 +03:00
|
|
|
rb_thread_exit(void)
|
2006-12-31 18:02:22 +03:00
|
|
|
{
|
2012-07-19 18:19:40 +04:00
|
|
|
rb_thread_t *th = GET_THREAD();
|
|
|
|
return rb_thread_kill(th->self);
|
2006-12-31 18:02:22 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* call-seq:
|
2010-05-18 01:07:33 +04:00
|
|
|
* thr.wakeup -> thr
|
* 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 08:25:10 +04:00
|
|
|
*
|
2006-12-31 18:02:22 +03:00
|
|
|
* Marks <i>thr</i> as eligible for scheduling (it may still remain blocked on
|
|
|
|
* I/O, however). Does not invoke the scheduler (see <code>Thread#run</code>).
|
* 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 08:25:10 +04:00
|
|
|
*
|
2006-12-31 18:02:22 +03:00
|
|
|
* c = Thread.new { Thread.stop; puts "hey!" }
|
2011-06-29 19:51:54 +04:00
|
|
|
* sleep 0.1 while c.status!='sleep'
|
2006-12-31 18:02:22 +03:00
|
|
|
* c.wakeup
|
2011-06-29 19:51:54 +04:00
|
|
|
* c.join
|
* 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 08:25:10 +04:00
|
|
|
*
|
2006-12-31 18:02:22 +03:00
|
|
|
* <em>produces:</em>
|
* 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 08:25:10 +04:00
|
|
|
*
|
2006-12-31 18:02:22 +03:00
|
|
|
* hey!
|
|
|
|
*/
|
|
|
|
|
|
|
|
VALUE
|
|
|
|
rb_thread_wakeup(VALUE thread)
|
2010-07-17 08:04:51 +04:00
|
|
|
{
|
|
|
|
if (!RTEST(rb_thread_wakeup_alive(thread))) {
|
|
|
|
rb_raise(rb_eThreadError, "killed thread");
|
|
|
|
}
|
|
|
|
return thread;
|
|
|
|
}
|
|
|
|
|
|
|
|
VALUE
|
|
|
|
rb_thread_wakeup_alive(VALUE thread)
|
2006-12-31 18:02:22 +03:00
|
|
|
{
|
* 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 09:37:46 +03:00
|
|
|
rb_thread_t *th;
|
2006-12-31 18:02:22 +03:00
|
|
|
GetThreadPtr(thread, th);
|
|
|
|
|
|
|
|
if (th->status == THREAD_KILLED) {
|
2010-07-17 08:04:51 +04:00
|
|
|
return Qnil;
|
2006-12-31 18:02:22 +03:00
|
|
|
}
|
2009-06-08 20:14:06 +04:00
|
|
|
rb_threadptr_ready(th);
|
2012-11-28 12:31:03 +04:00
|
|
|
if (th->status == THREAD_STOPPED || th->status == THREAD_STOPPED_FOREVER)
|
2008-07-16 23:19:36 +04:00
|
|
|
th->status = THREAD_RUNNABLE;
|
2006-12-31 18:02:22 +03:00
|
|
|
return thread;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* call-seq:
|
2010-05-18 01:07:33 +04:00
|
|
|
* thr.run -> thr
|
* 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 08:25:10 +04:00
|
|
|
*
|
2007-12-25 16:14:23 +03:00
|
|
|
* Wakes up <i>thr</i>, making it eligible for scheduling.
|
* 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 08:25:10 +04:00
|
|
|
*
|
2006-12-31 18:02:22 +03:00
|
|
|
* a = Thread.new { puts "a"; Thread.stop; puts "c" }
|
2011-06-29 19:47:24 +04:00
|
|
|
* sleep 0.1 while a.status!='sleep'
|
2006-12-31 18:02:22 +03:00
|
|
|
* puts "Got here"
|
|
|
|
* a.run
|
|
|
|
* a.join
|
* 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 08:25:10 +04:00
|
|
|
*
|
2006-12-31 18:02:22 +03:00
|
|
|
* <em>produces:</em>
|
* 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 08:25:10 +04:00
|
|
|
*
|
2006-12-31 18:02:22 +03:00
|
|
|
* a
|
|
|
|
* Got here
|
|
|
|
* c
|
|
|
|
*/
|
|
|
|
|
|
|
|
VALUE
|
2007-02-25 05:36:58 +03:00
|
|
|
rb_thread_run(VALUE thread)
|
2006-12-31 18:02:22 +03:00
|
|
|
{
|
|
|
|
rb_thread_wakeup(thread);
|
|
|
|
rb_thread_schedule();
|
|
|
|
return thread;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* call-seq:
|
2010-05-18 01:07:33 +04:00
|
|
|
* Thread.stop -> nil
|
* 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 08:25:10 +04:00
|
|
|
*
|
2006-12-31 18:02:22 +03:00
|
|
|
* Stops execution of the current thread, putting it into a ``sleep'' state,
|
2007-12-25 16:14:23 +03:00
|
|
|
* and schedules execution of another thread.
|
* 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 08:25:10 +04:00
|
|
|
*
|
2006-12-31 18:02:22 +03:00
|
|
|
* a = Thread.new { print "a"; Thread.stop; print "c" }
|
2011-06-29 19:55:44 +04:00
|
|
|
* sleep 0.1 while a.status!='sleep'
|
2006-12-31 18:02:22 +03:00
|
|
|
* print "b"
|
|
|
|
* a.run
|
|
|
|
* a.join
|
* 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 08:25:10 +04:00
|
|
|
*
|
2006-12-31 18:02:22 +03:00
|
|
|
* <em>produces:</em>
|
* 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 08:25:10 +04:00
|
|
|
*
|
2006-12-31 18:02:22 +03:00
|
|
|
* abc
|
|
|
|
*/
|
|
|
|
|
|
|
|
VALUE
|
|
|
|
rb_thread_stop(void)
|
|
|
|
{
|
|
|
|
if (rb_thread_alone()) {
|
|
|
|
rb_raise(rb_eThreadError,
|
|
|
|
"stopping only thread\n\tnote: use sleep to stop forever");
|
|
|
|
}
|
2008-06-12 17:01:38 +04:00
|
|
|
rb_thread_sleep_deadly();
|
2006-12-31 18:02:22 +03:00
|
|
|
return Qnil;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
thread_list_i(st_data_t key, st_data_t val, void *data)
|
|
|
|
{
|
|
|
|
VALUE ary = (VALUE)data;
|
* 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 09:37:46 +03:00
|
|
|
rb_thread_t *th;
|
2006-12-31 18:02:22 +03:00
|
|
|
GetThreadPtr((VALUE)key, th);
|
|
|
|
|
|
|
|
switch (th->status) {
|
* 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 08:25:10 +04:00
|
|
|
case THREAD_RUNNABLE:
|
|
|
|
case THREAD_STOPPED:
|
2008-06-12 17:01:38 +04:00
|
|
|
case THREAD_STOPPED_FOREVER:
|
2006-12-31 18:02:22 +03:00
|
|
|
rb_ary_push(ary, th->self);
|
* 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 08:25:10 +04:00
|
|
|
default:
|
2006-12-31 18:02:22 +03:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
return ST_CONTINUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
/********************************************************************/
|
|
|
|
|
|
|
|
/*
|
|
|
|
* call-seq:
|
2010-05-18 01:07:33 +04:00
|
|
|
* Thread.list -> array
|
* 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 08:25:10 +04:00
|
|
|
*
|
2006-12-31 18:02:22 +03:00
|
|
|
* Returns an array of <code>Thread</code> objects for all threads that are
|
|
|
|
* either runnable or stopped.
|
* 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 08:25:10 +04:00
|
|
|
*
|
2006-12-31 18:02:22 +03:00
|
|
|
* Thread.new { sleep(200) }
|
|
|
|
* Thread.new { 1000000.times {|i| i*i } }
|
|
|
|
* Thread.new { Thread.stop }
|
|
|
|
* Thread.list.each {|t| p t}
|
* 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 08:25:10 +04:00
|
|
|
*
|
2006-12-31 18:02:22 +03:00
|
|
|
* <em>produces:</em>
|
* 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 08:25:10 +04:00
|
|
|
*
|
2006-12-31 18:02:22 +03:00
|
|
|
* #<Thread:0x401b3e84 sleep>
|
|
|
|
* #<Thread:0x401b3f38 run>
|
|
|
|
* #<Thread:0x401b3fb0 sleep>
|
|
|
|
* #<Thread:0x401bdf4c run>
|
|
|
|
*/
|
|
|
|
|
|
|
|
VALUE
|
|
|
|
rb_thread_list(void)
|
|
|
|
{
|
|
|
|
VALUE ary = rb_ary_new();
|
|
|
|
st_foreach(GET_THREAD()->vm->living_threads, thread_list_i, ary);
|
|
|
|
return ary;
|
|
|
|
}
|
|
|
|
|
2007-02-05 15:21:01 +03:00
|
|
|
VALUE
|
|
|
|
rb_thread_current(void)
|
|
|
|
{
|
|
|
|
return GET_THREAD()->self;
|
|
|
|
}
|
|
|
|
|
2006-12-31 18:02:22 +03:00
|
|
|
/*
|
|
|
|
* call-seq:
|
2010-05-18 01:07:33 +04:00
|
|
|
* Thread.current -> thread
|
* 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 08:25:10 +04:00
|
|
|
*
|
2006-12-31 18:02:22 +03:00
|
|
|
* Returns the currently executing thread.
|
* 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 08:25:10 +04:00
|
|
|
*
|
2006-12-31 18:02:22 +03:00
|
|
|
* Thread.current #=> #<Thread:0x401bdf4c run>
|
|
|
|
*/
|
|
|
|
|
|
|
|
static VALUE
|
2007-02-05 15:21:01 +03:00
|
|
|
thread_s_current(VALUE klass)
|
2006-12-31 18:02:22 +03:00
|
|
|
{
|
2007-02-05 15:21:01 +03:00
|
|
|
return rb_thread_current();
|
2006-12-31 18:02:22 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
VALUE
|
|
|
|
rb_thread_main(void)
|
|
|
|
{
|
|
|
|
return GET_THREAD()->vm->main_thread->self;
|
|
|
|
}
|
|
|
|
|
2009-09-17 08:51:33 +04:00
|
|
|
/*
|
|
|
|
* call-seq:
|
2010-05-18 01:07:33 +04:00
|
|
|
* Thread.main -> thread
|
2009-09-17 08:51:33 +04:00
|
|
|
*
|
|
|
|
* Returns the main thread.
|
|
|
|
*/
|
|
|
|
|
2006-12-31 18:02:22 +03:00
|
|
|
static VALUE
|
|
|
|
rb_thread_s_main(VALUE klass)
|
|
|
|
{
|
|
|
|
return rb_thread_main();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* call-seq:
|
2010-05-18 01:07:33 +04:00
|
|
|
* Thread.abort_on_exception -> true or false
|
* 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 08:25:10 +04:00
|
|
|
*
|
2006-12-31 18:02:22 +03:00
|
|
|
* Returns the status of the global ``abort on exception'' condition. The
|
|
|
|
* default is <code>false</code>. When set to <code>true</code>, or if the
|
|
|
|
* global <code>$DEBUG</code> flag is <code>true</code> (perhaps because the
|
|
|
|
* command line option <code>-d</code> was specified) all threads will abort
|
|
|
|
* (the process will <code>exit(0)</code>) if an exception is raised in any
|
|
|
|
* thread. See also <code>Thread::abort_on_exception=</code>.
|
|
|
|
*/
|
|
|
|
|
|
|
|
static VALUE
|
2007-03-19 06:58:57 +03:00
|
|
|
rb_thread_s_abort_exc(void)
|
2006-12-31 18:02:22 +03:00
|
|
|
{
|
|
|
|
return GET_THREAD()->vm->thread_abort_on_exception ? Qtrue : Qfalse;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* call-seq:
|
2010-05-18 01:07:33 +04:00
|
|
|
* Thread.abort_on_exception= boolean -> true or false
|
* 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 08:25:10 +04:00
|
|
|
*
|
2006-12-31 18:02:22 +03:00
|
|
|
* When set to <code>true</code>, all threads will abort if an exception is
|
|
|
|
* raised. Returns the new state.
|
* 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 08:25:10 +04:00
|
|
|
*
|
2006-12-31 18:02:22 +03:00
|
|
|
* Thread.abort_on_exception = true
|
|
|
|
* t1 = Thread.new do
|
|
|
|
* puts "In new thread"
|
|
|
|
* raise "Exception from thread"
|
|
|
|
* end
|
|
|
|
* sleep(1)
|
|
|
|
* puts "not reached"
|
* 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 08:25:10 +04:00
|
|
|
*
|
2006-12-31 18:02:22 +03:00
|
|
|
* <em>produces:</em>
|
* 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 08:25:10 +04:00
|
|
|
*
|
2006-12-31 18:02:22 +03:00
|
|
|
* In new thread
|
|
|
|
* prog.rb:4: Exception from thread (RuntimeError)
|
|
|
|
* from prog.rb:2:in `initialize'
|
|
|
|
* from prog.rb:2:in `new'
|
|
|
|
* from prog.rb:2
|
|
|
|
*/
|
|
|
|
|
|
|
|
static VALUE
|
|
|
|
rb_thread_s_abort_exc_set(VALUE self, VALUE val)
|
|
|
|
{
|
|
|
|
rb_secure(4);
|
|
|
|
GET_THREAD()->vm->thread_abort_on_exception = RTEST(val);
|
|
|
|
return val;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* call-seq:
|
2010-05-18 01:07:33 +04:00
|
|
|
* thr.abort_on_exception -> true or false
|
* 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 08:25:10 +04:00
|
|
|
*
|
2006-12-31 18:02:22 +03:00
|
|
|
* Returns the status of the thread-local ``abort on exception'' condition for
|
|
|
|
* <i>thr</i>. The default is <code>false</code>. See also
|
|
|
|
* <code>Thread::abort_on_exception=</code>.
|
|
|
|
*/
|
|
|
|
|
|
|
|
static VALUE
|
|
|
|
rb_thread_abort_exc(VALUE 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 09:37:46 +03:00
|
|
|
rb_thread_t *th;
|
2006-12-31 18:02:22 +03:00
|
|
|
GetThreadPtr(thread, th);
|
|
|
|
return th->abort_on_exception ? Qtrue : Qfalse;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* call-seq:
|
2010-05-18 01:07:33 +04:00
|
|
|
* thr.abort_on_exception= boolean -> true or false
|
* 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 08:25:10 +04:00
|
|
|
*
|
2006-12-31 18:02:22 +03:00
|
|
|
* When set to <code>true</code>, causes all threads (including the main
|
|
|
|
* program) to abort if an exception is raised in <i>thr</i>. The process will
|
|
|
|
* effectively <code>exit(0)</code>.
|
|
|
|
*/
|
|
|
|
|
|
|
|
static VALUE
|
|
|
|
rb_thread_abort_exc_set(VALUE thread, VALUE val)
|
|
|
|
{
|
* 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 09:37:46 +03:00
|
|
|
rb_thread_t *th;
|
2006-12-31 18:02:22 +03:00
|
|
|
rb_secure(4);
|
|
|
|
|
|
|
|
GetThreadPtr(thread, th);
|
|
|
|
th->abort_on_exception = RTEST(val);
|
|
|
|
return val;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* call-seq:
|
2010-05-18 01:07:33 +04:00
|
|
|
* thr.group -> thgrp or nil
|
* 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 08:25:10 +04:00
|
|
|
*
|
2006-12-31 18:02:22 +03:00
|
|
|
* Returns the <code>ThreadGroup</code> which contains <i>thr</i>, or nil if
|
|
|
|
* the thread is not a member of any group.
|
* 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 08:25:10 +04:00
|
|
|
*
|
2006-12-31 18:02:22 +03:00
|
|
|
* Thread.main.group #=> #<ThreadGroup:0x4029d914>
|
|
|
|
*/
|
|
|
|
|
|
|
|
VALUE
|
|
|
|
rb_thread_group(VALUE 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 09:37:46 +03:00
|
|
|
rb_thread_t *th;
|
2006-12-31 18:02:22 +03:00
|
|
|
VALUE group;
|
|
|
|
GetThreadPtr(thread, th);
|
|
|
|
group = th->thgroup;
|
|
|
|
|
|
|
|
if (!group) {
|
|
|
|
group = Qnil;
|
|
|
|
}
|
|
|
|
return group;
|
|
|
|
}
|
|
|
|
|
|
|
|
static const char *
|
2012-11-28 12:31:03 +04:00
|
|
|
thread_status_name(rb_thread_t *th)
|
2006-12-31 18:02:22 +03:00
|
|
|
{
|
2012-11-28 12:31:03 +04:00
|
|
|
switch (th->status) {
|
* 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 08:25:10 +04:00
|
|
|
case THREAD_RUNNABLE:
|
2012-11-28 12:31:03 +04:00
|
|
|
if (th->to_kill)
|
|
|
|
return "aborting";
|
|
|
|
else
|
|
|
|
return "run";
|
* 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 08:25:10 +04:00
|
|
|
case THREAD_STOPPED:
|
2008-06-12 17:01:38 +04:00
|
|
|
case THREAD_STOPPED_FOREVER:
|
2006-12-31 18:02:22 +03:00
|
|
|
return "sleep";
|
* 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 08:25:10 +04:00
|
|
|
case THREAD_KILLED:
|
2006-12-31 18:02:22 +03:00
|
|
|
return "dead";
|
* 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 08:25:10 +04:00
|
|
|
default:
|
2006-12-31 18:02:22 +03:00
|
|
|
return "unknown";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
2009-06-08 20:14:06 +04:00
|
|
|
rb_threadptr_dead(rb_thread_t *th)
|
2006-12-31 18:02:22 +03:00
|
|
|
{
|
|
|
|
return th->status == THREAD_KILLED;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* call-seq:
|
2010-05-18 01:07:33 +04:00
|
|
|
* thr.status -> string, false or nil
|
* 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 08:25:10 +04:00
|
|
|
*
|
2006-12-31 18:02:22 +03:00
|
|
|
* Returns the status of <i>thr</i>: ``<code>sleep</code>'' if <i>thr</i> is
|
|
|
|
* sleeping or waiting on I/O, ``<code>run</code>'' if <i>thr</i> is executing,
|
|
|
|
* ``<code>aborting</code>'' if <i>thr</i> is aborting, <code>false</code> if
|
|
|
|
* <i>thr</i> terminated normally, and <code>nil</code> if <i>thr</i>
|
|
|
|
* terminated with an exception.
|
* 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 08:25:10 +04:00
|
|
|
*
|
2006-12-31 18:02:22 +03:00
|
|
|
* a = Thread.new { raise("die now") }
|
|
|
|
* b = Thread.new { Thread.stop }
|
|
|
|
* c = Thread.new { Thread.exit }
|
|
|
|
* d = Thread.new { sleep }
|
|
|
|
* d.kill #=> #<Thread:0x401b3678 aborting>
|
|
|
|
* a.status #=> nil
|
|
|
|
* b.status #=> "sleep"
|
|
|
|
* c.status #=> false
|
|
|
|
* d.status #=> "aborting"
|
|
|
|
* Thread.current.status #=> "run"
|
|
|
|
*/
|
|
|
|
|
|
|
|
static VALUE
|
|
|
|
rb_thread_status(VALUE 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 09:37:46 +03:00
|
|
|
rb_thread_t *th;
|
2006-12-31 18:02:22 +03:00
|
|
|
GetThreadPtr(thread, th);
|
|
|
|
|
2009-06-08 20:14:06 +04:00
|
|
|
if (rb_threadptr_dead(th)) {
|
2006-12-31 18:02:22 +03:00
|
|
|
if (!NIL_P(th->errinfo) && !FIXNUM_P(th->errinfo)
|
|
|
|
/* TODO */ ) {
|
|
|
|
return Qnil;
|
|
|
|
}
|
|
|
|
return Qfalse;
|
|
|
|
}
|
2012-11-28 12:31:03 +04:00
|
|
|
return rb_str_new2(thread_status_name(th));
|
2006-12-31 18:02:22 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* call-seq:
|
2010-05-18 01:07:33 +04:00
|
|
|
* thr.alive? -> true or false
|
* 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 08:25:10 +04:00
|
|
|
*
|
2006-12-31 18:02:22 +03:00
|
|
|
* Returns <code>true</code> if <i>thr</i> is running or sleeping.
|
* 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 08:25:10 +04:00
|
|
|
*
|
2006-12-31 18:02:22 +03:00
|
|
|
* thr = Thread.new { }
|
|
|
|
* thr.join #=> #<Thread:0x401b3fb0 dead>
|
|
|
|
* Thread.current.alive? #=> true
|
|
|
|
* thr.alive? #=> false
|
|
|
|
*/
|
|
|
|
|
|
|
|
static VALUE
|
|
|
|
rb_thread_alive_p(VALUE 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 09:37:46 +03:00
|
|
|
rb_thread_t *th;
|
2006-12-31 18:02:22 +03:00
|
|
|
GetThreadPtr(thread, th);
|
|
|
|
|
2009-06-08 20:14:06 +04:00
|
|
|
if (rb_threadptr_dead(th))
|
2006-12-31 18:02:22 +03:00
|
|
|
return Qfalse;
|
|
|
|
return Qtrue;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* call-seq:
|
2010-05-18 01:07:33 +04:00
|
|
|
* thr.stop? -> true or false
|
* 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 08:25:10 +04:00
|
|
|
*
|
2006-12-31 18:02:22 +03:00
|
|
|
* Returns <code>true</code> if <i>thr</i> is dead or sleeping.
|
* 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 08:25:10 +04:00
|
|
|
*
|
2006-12-31 18:02:22 +03:00
|
|
|
* a = Thread.new { Thread.stop }
|
|
|
|
* b = Thread.current
|
|
|
|
* a.stop? #=> true
|
|
|
|
* b.stop? #=> false
|
|
|
|
*/
|
|
|
|
|
|
|
|
static VALUE
|
|
|
|
rb_thread_stop_p(VALUE 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 09:37:46 +03:00
|
|
|
rb_thread_t *th;
|
2006-12-31 18:02:22 +03:00
|
|
|
GetThreadPtr(thread, th);
|
|
|
|
|
2009-06-08 20:14:06 +04:00
|
|
|
if (rb_threadptr_dead(th))
|
2006-12-31 18:02:22 +03:00
|
|
|
return Qtrue;
|
2008-06-12 17:01:38 +04:00
|
|
|
if (th->status == THREAD_STOPPED || th->status == THREAD_STOPPED_FOREVER)
|
2006-12-31 18:02:22 +03:00
|
|
|
return Qtrue;
|
|
|
|
return Qfalse;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* call-seq:
|
2010-05-18 01:07:33 +04:00
|
|
|
* thr.safe_level -> integer
|
* 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 08:25:10 +04:00
|
|
|
*
|
2006-12-31 18:02:22 +03:00
|
|
|
* Returns the safe level in effect for <i>thr</i>. Setting thread-local safe
|
|
|
|
* levels can help when implementing sandboxes which run insecure code.
|
* 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 08:25:10 +04:00
|
|
|
*
|
2006-12-31 18:02:22 +03:00
|
|
|
* thr = Thread.new { $SAFE = 3; sleep }
|
|
|
|
* Thread.current.safe_level #=> 0
|
|
|
|
* thr.safe_level #=> 3
|
|
|
|
*/
|
|
|
|
|
|
|
|
static VALUE
|
|
|
|
rb_thread_safe_level(VALUE 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 09:37:46 +03:00
|
|
|
rb_thread_t *th;
|
2006-12-31 18:02:22 +03:00
|
|
|
GetThreadPtr(thread, th);
|
|
|
|
|
|
|
|
return INT2NUM(th->safe_level);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* call-seq:
|
2010-05-18 01:07:33 +04:00
|
|
|
* thr.inspect -> string
|
2006-12-31 18:02:22 +03:00
|
|
|
*
|
|
|
|
* Dump the name, id, and status of _thr_ to a string.
|
|
|
|
*/
|
|
|
|
|
|
|
|
static VALUE
|
|
|
|
rb_thread_inspect(VALUE thread)
|
|
|
|
{
|
2008-05-31 13:28:20 +04:00
|
|
|
const char *cname = rb_obj_classname(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 09:37:46 +03:00
|
|
|
rb_thread_t *th;
|
2006-12-31 18:02:22 +03:00
|
|
|
const char *status;
|
|
|
|
VALUE str;
|
|
|
|
|
|
|
|
GetThreadPtr(thread, th);
|
2012-11-28 12:31:03 +04:00
|
|
|
status = thread_status_name(th);
|
2006-12-31 18:02:22 +03:00
|
|
|
str = rb_sprintf("#<%s:%p %s>", cname, (void *)thread, status);
|
|
|
|
OBJ_INFECT(str, thread);
|
|
|
|
|
|
|
|
return str;
|
|
|
|
}
|
|
|
|
|
|
|
|
VALUE
|
|
|
|
rb_thread_local_aref(VALUE thread, ID id)
|
|
|
|
{
|
* 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 09:37:46 +03:00
|
|
|
rb_thread_t *th;
|
* compile.c (iseq_build_body), error.c (set_syserr, get_syserr),
(syserr_initialize), gc.c (define_final, rb_gc_copy_finalizer),
(run_final), hash.c (rb_hash_aref, rb_hash_lookup2),
(rb_hash_fetch_m, rb_hash_clear, rb_hash_aset, eql_i),
iseq.c (iseq_load, iseq_data_to_ary), marshal.c (r_symlink),
thread.c (rb_thread_local_aref),
variable.c (generic_ivar_remove, ivar_get, rb_const_get_0),
(rb_cvar_get), vm.c (rb_vm_check_redefinition_opt_method),
vm_insnhelper.c (vm_get_ev_const), vm_method.c (remove_method),
ext/iconv/iconv.c (map_charset): use st_data_t.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@29462 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2010-10-12 18:47:23 +04:00
|
|
|
st_data_t val;
|
2006-12-31 18:02:22 +03:00
|
|
|
|
|
|
|
GetThreadPtr(thread, th);
|
|
|
|
if (rb_safe_level() >= 4 && th != GET_THREAD()) {
|
|
|
|
rb_raise(rb_eSecurityError, "Insecure: thread locals");
|
|
|
|
}
|
|
|
|
if (!th->local_storage) {
|
|
|
|
return Qnil;
|
|
|
|
}
|
|
|
|
if (st_lookup(th->local_storage, id, &val)) {
|
* compile.c (iseq_build_body), error.c (set_syserr, get_syserr),
(syserr_initialize), gc.c (define_final, rb_gc_copy_finalizer),
(run_final), hash.c (rb_hash_aref, rb_hash_lookup2),
(rb_hash_fetch_m, rb_hash_clear, rb_hash_aset, eql_i),
iseq.c (iseq_load, iseq_data_to_ary), marshal.c (r_symlink),
thread.c (rb_thread_local_aref),
variable.c (generic_ivar_remove, ivar_get, rb_const_get_0),
(rb_cvar_get), vm.c (rb_vm_check_redefinition_opt_method),
vm_insnhelper.c (vm_get_ev_const), vm_method.c (remove_method),
ext/iconv/iconv.c (map_charset): use st_data_t.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@29462 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2010-10-12 18:47:23 +04:00
|
|
|
return (VALUE)val;
|
2006-12-31 18:02:22 +03:00
|
|
|
}
|
|
|
|
return Qnil;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* call-seq:
|
2010-05-18 01:07:33 +04:00
|
|
|
* thr[sym] -> obj or nil
|
* 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 08:25:10 +04:00
|
|
|
*
|
2012-01-03 11:01:15 +04:00
|
|
|
* Attribute Reference---Returns the value of a fiber-local variable (current thread's root fiber
|
|
|
|
* if not explicitely inside a Fiber), using either a symbol or a string name.
|
|
|
|
* If the specified variable does not exist, returns <code>nil</code>.
|
* 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 08:25:10 +04:00
|
|
|
*
|
2011-06-28 00:09:59 +04:00
|
|
|
* [
|
|
|
|
* Thread.new { Thread.current["name"] = "A" },
|
|
|
|
* Thread.new { Thread.current[:name] = "B" },
|
|
|
|
* Thread.new { Thread.current["name"] = "C" }
|
|
|
|
* ].each do |th|
|
|
|
|
* th.join
|
|
|
|
* puts "#{th.inspect}: #{th[:name]}"
|
|
|
|
* end
|
* 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 08:25:10 +04:00
|
|
|
*
|
2006-12-31 18:02:22 +03:00
|
|
|
* <em>produces:</em>
|
* 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 08:25:10 +04:00
|
|
|
*
|
2011-06-28 00:09:59 +04:00
|
|
|
* #<Thread:0x00000002a54220 dead>: A
|
|
|
|
* #<Thread:0x00000002a541a8 dead>: B
|
|
|
|
* #<Thread:0x00000002a54130 dead>: C
|
2012-07-02 17:15:29 +04:00
|
|
|
*
|
|
|
|
* Thread#[] and Thread#[]= are not thread-local but fiber-local.
|
2012-10-06 23:51:40 +04:00
|
|
|
* This confusion did not exist in Ruby 1.8 because
|
|
|
|
* fibers were only available since Ruby 1.9.
|
2012-07-02 17:15:29 +04:00
|
|
|
* Ruby 1.9 chooses that the methods behaves fiber-local to save
|
|
|
|
* following idiom for dynamic scope.
|
|
|
|
*
|
|
|
|
* def meth(newvalue)
|
|
|
|
* begin
|
|
|
|
* oldvalue = Thread.current[:name]
|
|
|
|
* Thread.current[:name] = newvalue
|
|
|
|
* yield
|
|
|
|
* ensure
|
|
|
|
* Thread.current[:name] = oldvalue
|
|
|
|
* end
|
|
|
|
* end
|
|
|
|
*
|
|
|
|
* The idiom may not work as dynamic scope if the methods are thread-local
|
|
|
|
* and a given block switches fiber.
|
|
|
|
*
|
|
|
|
* f = Fiber.new {
|
|
|
|
* meth(1) {
|
|
|
|
* Fiber.yield
|
|
|
|
* }
|
|
|
|
* }
|
|
|
|
* meth(2) {
|
|
|
|
* f.resume
|
|
|
|
* }
|
|
|
|
* f.resume
|
|
|
|
* p Thread.current[:name]
|
|
|
|
* #=> nil if fiber-local
|
|
|
|
* #=> 2 if thread-local (The value 2 is leaked to outside of meth method.)
|
|
|
|
*
|
2012-10-29 21:22:36 +04:00
|
|
|
* For thread-local variables, please see <code>Thread#thread_local_get</code>
|
|
|
|
* and <code>Thread#thread_local_set</code>.
|
|
|
|
*
|
2006-12-31 18:02:22 +03:00
|
|
|
*/
|
|
|
|
|
|
|
|
static VALUE
|
|
|
|
rb_thread_aref(VALUE thread, VALUE id)
|
|
|
|
{
|
|
|
|
return rb_thread_local_aref(thread, rb_to_id(id));
|
|
|
|
}
|
|
|
|
|
|
|
|
VALUE
|
|
|
|
rb_thread_local_aset(VALUE thread, ID id, VALUE val)
|
|
|
|
{
|
* 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 09:37:46 +03:00
|
|
|
rb_thread_t *th;
|
2006-12-31 18:02:22 +03:00
|
|
|
GetThreadPtr(thread, th);
|
|
|
|
|
|
|
|
if (rb_safe_level() >= 4 && th != GET_THREAD()) {
|
|
|
|
rb_raise(rb_eSecurityError, "Insecure: can't modify thread locals");
|
|
|
|
}
|
|
|
|
if (OBJ_FROZEN(thread)) {
|
|
|
|
rb_error_frozen("thread locals");
|
|
|
|
}
|
|
|
|
if (!th->local_storage) {
|
|
|
|
th->local_storage = st_init_numtable();
|
|
|
|
}
|
|
|
|
if (NIL_P(val)) {
|
2008-05-08 01:27:34 +04:00
|
|
|
st_delete_wrap(th->local_storage, id);
|
2006-12-31 18:02:22 +03:00
|
|
|
return Qnil;
|
|
|
|
}
|
|
|
|
st_insert(th->local_storage, id, val);
|
|
|
|
return val;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* call-seq:
|
2010-05-18 01:07:33 +04:00
|
|
|
* thr[sym] = obj -> obj
|
* 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 08:25:10 +04:00
|
|
|
*
|
2012-01-03 11:01:15 +04:00
|
|
|
* Attribute Assignment---Sets or creates the value of a fiber-local variable,
|
2012-10-29 21:22:36 +04:00
|
|
|
* using either a symbol or a string. See also <code>Thread#[]</code>. For
|
|
|
|
* thread-local variables, please see <code>Thread#thread_variable_set</code>
|
|
|
|
* and <code>Thread#thread_variable_get</code>.
|
2006-12-31 18:02:22 +03:00
|
|
|
*/
|
|
|
|
|
|
|
|
static VALUE
|
2010-05-08 19:27:48 +04:00
|
|
|
rb_thread_aset(VALUE self, VALUE id, VALUE val)
|
2006-12-31 18:02:22 +03:00
|
|
|
{
|
|
|
|
return rb_thread_local_aset(self, rb_to_id(id), val);
|
|
|
|
}
|
|
|
|
|
2012-10-29 21:22:36 +04:00
|
|
|
/*
|
|
|
|
* call-seq:
|
|
|
|
* thr.thread_variable_get(key) -> obj or nil
|
|
|
|
*
|
|
|
|
* Returns the value of a thread local variable that has been set. Note that
|
|
|
|
* these are different than fiber local values. For fiber local values,
|
|
|
|
* please see Thread#[] and Thread#[]=.
|
|
|
|
*
|
|
|
|
* Thread local values are carried along with threads, and do not respect
|
|
|
|
* fibers. For example:
|
|
|
|
*
|
|
|
|
* Thread.new {
|
|
|
|
* Thread.current.thread_variable_set("foo", "bar") # set a thread local
|
|
|
|
* Thread.current["foo"] = "bar" # set a fiber local
|
|
|
|
*
|
|
|
|
* Fiber.new {
|
|
|
|
* Fiber.yield [
|
|
|
|
* Thread.current.thread_variable_get("foo"), # get the thread local
|
|
|
|
* Thread.current["foo"], # get the fiber local
|
|
|
|
* ]
|
|
|
|
* }.resume
|
|
|
|
* }.join.value # => ['bar', nil]
|
|
|
|
*
|
|
|
|
* The value "bar" is returned for the thread local, where nil is returned
|
|
|
|
* for the fiber local. The fiber is executed in the same thread, so the
|
|
|
|
* thread local values are available.
|
|
|
|
*
|
|
|
|
* See also Thread#[]
|
|
|
|
*/
|
|
|
|
|
|
|
|
static VALUE
|
|
|
|
rb_thread_variable_get(VALUE thread, VALUE id)
|
|
|
|
{
|
|
|
|
VALUE locals;
|
|
|
|
rb_thread_t *th;
|
|
|
|
|
|
|
|
GetThreadPtr(thread, th);
|
|
|
|
|
|
|
|
if (rb_safe_level() >= 4 && th != GET_THREAD()) {
|
|
|
|
rb_raise(rb_eSecurityError, "Insecure: can't modify thread locals");
|
|
|
|
}
|
|
|
|
|
|
|
|
locals = rb_iv_get(thread, "locals");
|
|
|
|
return rb_hash_aref(locals, ID2SYM(rb_to_id(id)));
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* call-seq:
|
|
|
|
* thr.thread_variable_set(key, value)
|
|
|
|
*
|
|
|
|
* Sets a thread local with +key+ to +value+. Note that these are local to
|
|
|
|
* threads, and not to fibers. Please see Thread#thread_variable_get and
|
|
|
|
* Thread#[] for more information.
|
|
|
|
*/
|
|
|
|
|
|
|
|
static VALUE
|
|
|
|
rb_thread_variable_set(VALUE thread, VALUE id, VALUE val)
|
|
|
|
{
|
|
|
|
VALUE locals;
|
|
|
|
rb_thread_t *th;
|
|
|
|
|
|
|
|
GetThreadPtr(thread, th);
|
|
|
|
|
|
|
|
if (rb_safe_level() >= 4 && th != GET_THREAD()) {
|
|
|
|
rb_raise(rb_eSecurityError, "Insecure: can't modify thread locals");
|
|
|
|
}
|
|
|
|
if (OBJ_FROZEN(thread)) {
|
|
|
|
rb_error_frozen("thread locals");
|
|
|
|
}
|
|
|
|
|
|
|
|
locals = rb_iv_get(thread, "locals");
|
|
|
|
return rb_hash_aset(locals, ID2SYM(rb_to_id(id)), val);
|
|
|
|
}
|
|
|
|
|
2006-12-31 18:02:22 +03:00
|
|
|
/*
|
|
|
|
* call-seq:
|
2010-05-18 01:07:33 +04:00
|
|
|
* thr.key?(sym) -> true or false
|
* 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 08:25:10 +04:00
|
|
|
*
|
2006-12-31 18:02:22 +03:00
|
|
|
* Returns <code>true</code> if the given string (or symbol) exists as a
|
2012-01-03 11:01:15 +04:00
|
|
|
* fiber-local variable.
|
* 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 08:25:10 +04:00
|
|
|
*
|
2006-12-31 18:02:22 +03:00
|
|
|
* me = Thread.current
|
|
|
|
* me[:oliver] = "a"
|
|
|
|
* me.key?(:oliver) #=> true
|
|
|
|
* me.key?(:stanley) #=> false
|
|
|
|
*/
|
|
|
|
|
|
|
|
static VALUE
|
2008-05-07 17:24:03 +04:00
|
|
|
rb_thread_key_p(VALUE self, VALUE key)
|
2006-12-31 18:02:22 +03:00
|
|
|
{
|
* 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 09:37:46 +03:00
|
|
|
rb_thread_t *th;
|
2008-05-07 17:24:03 +04:00
|
|
|
ID id = rb_to_id(key);
|
|
|
|
|
2006-12-31 18:02:22 +03:00
|
|
|
GetThreadPtr(self, th);
|
|
|
|
|
|
|
|
if (!th->local_storage) {
|
|
|
|
return Qfalse;
|
|
|
|
}
|
2008-05-08 01:27:34 +04:00
|
|
|
if (st_lookup(th->local_storage, id, 0)) {
|
2006-12-31 18:02:22 +03:00
|
|
|
return Qtrue;
|
|
|
|
}
|
|
|
|
return Qfalse;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
thread_keys_i(ID key, VALUE value, VALUE ary)
|
|
|
|
{
|
|
|
|
rb_ary_push(ary, ID2SYM(key));
|
|
|
|
return ST_CONTINUE;
|
|
|
|
}
|
|
|
|
|
2008-06-19 18:18:46 +04:00
|
|
|
static int
|
|
|
|
vm_living_thread_num(rb_vm_t *vm)
|
|
|
|
{
|
2012-06-03 14:47:45 +04:00
|
|
|
return (int)vm->living_threads->num_entries;
|
2008-06-19 18:18:46 +04:00
|
|
|
}
|
|
|
|
|
2006-12-31 18:02:22 +03:00
|
|
|
int
|
* cont.c (rb_fiber_current), dln.c (dln_print_undef, dln_undefined),
eval.c (rb_iterator_p, rb_need_block), load.c: (Init_load), ruby.c
(uscore_get, rb_f_chop), st.c (stat_col), signal.c
(rb_signal_buff_size, ruby_sig_finalize), thread.c
(rb_thread_sleep_forever, rb_thread_sleep_deadly, rb_thread_alone):
protoized.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@21929 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2009-02-01 15:51:44 +03:00
|
|
|
rb_thread_alone(void)
|
2006-12-31 18:02:22 +03:00
|
|
|
{
|
|
|
|
int num = 1;
|
|
|
|
if (GET_THREAD()->vm->living_threads) {
|
2008-06-19 18:18:46 +04:00
|
|
|
num = vm_living_thread_num(GET_THREAD()->vm);
|
2006-12-31 18:02:22 +03:00
|
|
|
thread_debug("rb_thread_alone: %d\n", num);
|
|
|
|
}
|
|
|
|
return num == 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* call-seq:
|
2010-05-18 01:07:33 +04:00
|
|
|
* thr.keys -> array
|
* 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 08:25:10 +04:00
|
|
|
*
|
2012-01-03 11:01:15 +04:00
|
|
|
* Returns an an array of the names of the fiber-local variables (as Symbols).
|
* 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 08:25:10 +04:00
|
|
|
*
|
2006-12-31 18:02:22 +03:00
|
|
|
* thr = Thread.new do
|
|
|
|
* Thread.current[:cat] = 'meow'
|
|
|
|
* Thread.current["dog"] = 'woof'
|
|
|
|
* end
|
|
|
|
* thr.join #=> #<Thread:0x401b3f10 dead>
|
|
|
|
* thr.keys #=> [:dog, :cat]
|
|
|
|
*/
|
|
|
|
|
|
|
|
static VALUE
|
|
|
|
rb_thread_keys(VALUE self)
|
|
|
|
{
|
* 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 09:37:46 +03:00
|
|
|
rb_thread_t *th;
|
2006-12-31 18:02:22 +03:00
|
|
|
VALUE ary = rb_ary_new();
|
|
|
|
GetThreadPtr(self, th);
|
|
|
|
|
|
|
|
if (th->local_storage) {
|
|
|
|
st_foreach(th->local_storage, thread_keys_i, ary);
|
|
|
|
}
|
|
|
|
return ary;
|
|
|
|
}
|
|
|
|
|
2012-10-29 21:22:36 +04:00
|
|
|
static int
|
|
|
|
keys_i(VALUE key, VALUE value, VALUE ary)
|
|
|
|
{
|
|
|
|
rb_ary_push(ary, key);
|
|
|
|
return ST_CONTINUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* call-seq:
|
|
|
|
* thr.thread_variables -> array
|
|
|
|
*
|
|
|
|
* Returns an an array of the names of the thread-local variables (as Symbols).
|
|
|
|
*
|
|
|
|
* thr = Thread.new do
|
|
|
|
* Thread.current.thread_variable_set(:cat, 'meow')
|
|
|
|
* Thread.current.thread_variable_set("dog", 'woof')
|
|
|
|
* end
|
|
|
|
* thr.join #=> #<Thread:0x401b3f10 dead>
|
|
|
|
* thr.thread_variables #=> [:dog, :cat]
|
|
|
|
*
|
|
|
|
* Note that these are not fiber local variables. Please see Thread#[] and
|
|
|
|
* Thread#thread_variable_get for more details.
|
|
|
|
*/
|
|
|
|
|
|
|
|
static VALUE
|
|
|
|
rb_thread_variables(VALUE thread)
|
|
|
|
{
|
|
|
|
VALUE locals;
|
|
|
|
VALUE ary;
|
|
|
|
|
|
|
|
locals = rb_iv_get(thread, "locals");
|
|
|
|
ary = rb_ary_new();
|
|
|
|
rb_hash_foreach(locals, keys_i, ary);
|
|
|
|
|
|
|
|
return ary;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* call-seq:
|
|
|
|
* thr.thread_variable?(key) -> true or false
|
|
|
|
*
|
|
|
|
* Returns <code>true</code> if the given string (or symbol) exists as a
|
|
|
|
* thread-local variable.
|
|
|
|
*
|
|
|
|
* me = Thread.current
|
|
|
|
* me.thread_variable_set(:oliver, "a")
|
|
|
|
* me.thread_variable?(:oliver) #=> true
|
|
|
|
* me.thread_variable?(:stanley) #=> false
|
|
|
|
*
|
|
|
|
* Note that these are not fiber local variables. Please see Thread#[] and
|
|
|
|
* Thread#thread_variable_get for more details.
|
|
|
|
*/
|
|
|
|
|
|
|
|
static VALUE
|
|
|
|
rb_thread_variable_p(VALUE thread, VALUE key)
|
|
|
|
{
|
|
|
|
VALUE locals;
|
|
|
|
|
|
|
|
locals = rb_iv_get(thread, "locals");
|
|
|
|
|
|
|
|
if (!RHASH(locals)->ntbl)
|
|
|
|
return Qfalse;
|
|
|
|
|
|
|
|
if (st_lookup(RHASH(locals)->ntbl, ID2SYM(rb_to_id(key)), 0)) {
|
|
|
|
return Qtrue;
|
|
|
|
}
|
|
|
|
|
|
|
|
return Qfalse;
|
|
|
|
}
|
|
|
|
|
2006-12-31 18:02:22 +03:00
|
|
|
/*
|
|
|
|
* call-seq:
|
2010-05-18 01:07:33 +04:00
|
|
|
* thr.priority -> integer
|
2007-05-10 05:12:10 +04:00
|
|
|
*
|
|
|
|
* Returns the priority of <i>thr</i>. Default is inherited from the
|
|
|
|
* current thread which creating the new thread, or zero for the
|
2010-04-15 17:44:52 +04:00
|
|
|
* initial main thread; higher-priority thread will run more frequently
|
|
|
|
* than lower-priority threads (but lower-priority threads can also run).
|
|
|
|
*
|
|
|
|
* This is just hint for Ruby thread scheduler. It may be ignored on some
|
|
|
|
* platform.
|
2007-05-10 05:12:10 +04:00
|
|
|
*
|
2006-12-31 18:02:22 +03:00
|
|
|
* Thread.current.priority #=> 0
|
|
|
|
*/
|
|
|
|
|
|
|
|
static VALUE
|
|
|
|
rb_thread_priority(VALUE 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 09:37:46 +03:00
|
|
|
rb_thread_t *th;
|
2006-12-31 18:02:22 +03:00
|
|
|
GetThreadPtr(thread, th);
|
|
|
|
return INT2NUM(th->priority);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* call-seq:
|
2010-05-18 01:07:33 +04:00
|
|
|
* thr.priority= integer -> thr
|
* 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 08:25:10 +04:00
|
|
|
*
|
2006-12-31 18:02:22 +03:00
|
|
|
* Sets the priority of <i>thr</i> to <i>integer</i>. Higher-priority threads
|
2010-04-15 17:44:52 +04:00
|
|
|
* will run more frequently than lower-priority threads (but lower-priority
|
|
|
|
* threads can also run).
|
|
|
|
*
|
|
|
|
* This is just hint for Ruby thread scheduler. It may be ignored on some
|
|
|
|
* platform.
|
* 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 08:25:10 +04:00
|
|
|
*
|
2006-12-31 18:02:22 +03:00
|
|
|
* count1 = count2 = 0
|
|
|
|
* a = Thread.new do
|
|
|
|
* loop { count1 += 1 }
|
|
|
|
* end
|
|
|
|
* a.priority = -1
|
* 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 08:25:10 +04:00
|
|
|
*
|
2006-12-31 18:02:22 +03:00
|
|
|
* b = Thread.new do
|
|
|
|
* loop { count2 += 1 }
|
|
|
|
* end
|
|
|
|
* b.priority = -2
|
|
|
|
* sleep 1 #=> 1
|
|
|
|
* count1 #=> 622504
|
|
|
|
* count2 #=> 5832
|
|
|
|
*/
|
|
|
|
|
|
|
|
static VALUE
|
|
|
|
rb_thread_priority_set(VALUE thread, VALUE prio)
|
|
|
|
{
|
* 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 09:37:46 +03:00
|
|
|
rb_thread_t *th;
|
2008-08-13 11:53:35 +04:00
|
|
|
int priority;
|
2008-08-14 09:56:51 +04:00
|
|
|
GetThreadPtr(thread, th);
|
2006-12-31 18:02:22 +03:00
|
|
|
|
|
|
|
rb_secure(4);
|
|
|
|
|
2008-08-13 11:53:35 +04:00
|
|
|
#if USE_NATIVE_THREAD_PRIORITY
|
2006-12-31 18:02:22 +03:00
|
|
|
th->priority = NUM2INT(prio);
|
|
|
|
native_thread_apply_priority(th);
|
2008-08-13 11:53:35 +04:00
|
|
|
#else
|
|
|
|
priority = NUM2INT(prio);
|
|
|
|
if (priority > RUBY_THREAD_PRIORITY_MAX) {
|
|
|
|
priority = RUBY_THREAD_PRIORITY_MAX;
|
|
|
|
}
|
|
|
|
else if (priority < RUBY_THREAD_PRIORITY_MIN) {
|
|
|
|
priority = RUBY_THREAD_PRIORITY_MIN;
|
|
|
|
}
|
|
|
|
th->priority = priority;
|
|
|
|
#endif
|
|
|
|
return INT2NUM(th->priority);
|
2006-12-31 18:02:22 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/* for IO */
|
|
|
|
|
2007-02-05 15:21:01 +03:00
|
|
|
#if defined(NFDBITS) && defined(HAVE_RB_FD_INIT)
|
2008-12-02 13:00:10 +03:00
|
|
|
|
|
|
|
/*
|
2009-01-05 09:55:14 +03:00
|
|
|
* several Unix platforms support file descriptors bigger than FD_SETSIZE
|
2008-12-02 13:00:10 +03:00
|
|
|
* in select(2) system call.
|
|
|
|
*
|
|
|
|
* - Linux 2.2.12 (?)
|
|
|
|
* - NetBSD 1.2 (src/sys/kern/sys_generic.c:1.25)
|
|
|
|
* select(2) documents how to allocate fd_set dynamically.
|
|
|
|
* http://netbsd.gw.com/cgi-bin/man-cgi?select++NetBSD-4.0
|
|
|
|
* - FreeBSD 2.2 (src/sys/kern/sys_generic.c:1.19)
|
|
|
|
* - OpenBSD 2.0 (src/sys/kern/sys_generic.c:1.4)
|
|
|
|
* select(2) documents how to allocate fd_set dynamically.
|
|
|
|
* http://www.openbsd.org/cgi-bin/man.cgi?query=select&manpath=OpenBSD+4.4
|
2009-02-18 04:29:13 +03:00
|
|
|
* - HP-UX documents how to allocate fd_set dynamically.
|
2008-12-02 13:00:10 +03:00
|
|
|
* http://docs.hp.com/en/B2355-60105/select.2.html
|
|
|
|
* - Solaris 8 has select_large_fdset
|
2011-08-19 08:17:04 +04:00
|
|
|
* - Mac OS X 10.7 (Lion)
|
|
|
|
* select(2) returns EINVAL if nfds is greater than FD_SET_SIZE and
|
|
|
|
* _DARWIN_UNLIMITED_SELECT (or _DARWIN_C_SOURCE) isn't defined.
|
|
|
|
* http://developer.apple.com/library/mac/#releasenotes/Darwin/SymbolVariantsRelNotes/_index.html
|
2008-12-02 13:00:10 +03:00
|
|
|
*
|
|
|
|
* When fd_set is not big enough to hold big file descriptors,
|
|
|
|
* it should be allocated dynamically.
|
|
|
|
* Note that this assumes fd_set is structured as bitmap.
|
|
|
|
*
|
|
|
|
* rb_fd_init allocates the memory.
|
|
|
|
* rb_fd_term free the memory.
|
|
|
|
* rb_fd_set may re-allocates bitmap.
|
|
|
|
*
|
|
|
|
* So rb_fd_set doesn't reject file descriptors bigger than FD_SETSIZE.
|
|
|
|
*/
|
|
|
|
|
2006-12-31 18:02:22 +03:00
|
|
|
void
|
2011-05-07 17:40:56 +04:00
|
|
|
rb_fd_init(rb_fdset_t *fds)
|
2006-12-31 18:02:22 +03:00
|
|
|
{
|
2007-02-05 15:21:01 +03:00
|
|
|
fds->maxfd = 0;
|
|
|
|
fds->fdset = ALLOC(fd_set);
|
|
|
|
FD_ZERO(fds->fdset);
|
|
|
|
}
|
2006-12-31 18:02:22 +03:00
|
|
|
|
2011-05-07 17:36:08 +04:00
|
|
|
void
|
|
|
|
rb_fd_init_copy(rb_fdset_t *dst, rb_fdset_t *src)
|
|
|
|
{
|
|
|
|
size_t size = howmany(rb_fd_max(src), NFDBITS) * sizeof(fd_mask);
|
|
|
|
|
|
|
|
if (size < sizeof(fd_set))
|
|
|
|
size = sizeof(fd_set);
|
|
|
|
dst->maxfd = src->maxfd;
|
|
|
|
dst->fdset = xmalloc(size);
|
|
|
|
memcpy(dst->fdset, src->fdset, size);
|
|
|
|
}
|
|
|
|
|
2007-02-05 15:21:01 +03:00
|
|
|
void
|
|
|
|
rb_fd_term(rb_fdset_t *fds)
|
|
|
|
{
|
* 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 14:01:40 +04:00
|
|
|
if (fds->fdset) xfree(fds->fdset);
|
2007-02-05 15:21:01 +03:00
|
|
|
fds->maxfd = 0;
|
|
|
|
fds->fdset = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
rb_fd_zero(rb_fdset_t *fds)
|
|
|
|
{
|
2011-05-07 17:44:27 +04:00
|
|
|
if (fds->fdset)
|
2007-02-05 15:21:01 +03:00
|
|
|
MEMZERO(fds->fdset, fd_mask, howmany(fds->maxfd, NFDBITS));
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
rb_fd_resize(int n, rb_fdset_t *fds)
|
|
|
|
{
|
2009-05-09 07:25:16 +04:00
|
|
|
size_t m = howmany(n + 1, NFDBITS) * sizeof(fd_mask);
|
|
|
|
size_t o = howmany(fds->maxfd, NFDBITS) * sizeof(fd_mask);
|
2007-02-05 15:21:01 +03:00
|
|
|
|
|
|
|
if (m < sizeof(fd_set)) m = sizeof(fd_set);
|
|
|
|
if (o < sizeof(fd_set)) o = sizeof(fd_set);
|
|
|
|
|
|
|
|
if (m > o) {
|
2010-06-28 16:58:03 +04:00
|
|
|
fds->fdset = xrealloc(fds->fdset, m);
|
2007-02-05 15:21:01 +03:00
|
|
|
memset((char *)fds->fdset + o, 0, m - o);
|
|
|
|
}
|
|
|
|
if (n >= fds->maxfd) fds->maxfd = n + 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
rb_fd_set(int n, rb_fdset_t *fds)
|
|
|
|
{
|
|
|
|
rb_fd_resize(n, fds);
|
|
|
|
FD_SET(n, fds->fdset);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
rb_fd_clr(int n, rb_fdset_t *fds)
|
|
|
|
{
|
|
|
|
if (n >= fds->maxfd) return;
|
|
|
|
FD_CLR(n, fds->fdset);
|
2006-12-31 18:02:22 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
int
|
2007-02-05 15:21:01 +03:00
|
|
|
rb_fd_isset(int n, const rb_fdset_t *fds)
|
2006-12-31 18:02:22 +03:00
|
|
|
{
|
2007-02-05 15:21:01 +03:00
|
|
|
if (n >= fds->maxfd) return 0;
|
|
|
|
return FD_ISSET(n, fds->fdset) != 0; /* "!= 0" avoids FreeBSD PR 91421 */
|
|
|
|
}
|
|
|
|
|
2011-05-15 18:57:35 +04:00
|
|
|
void
|
|
|
|
rb_fd_copy(rb_fdset_t *dst, const fd_set *src, int max)
|
|
|
|
{
|
|
|
|
size_t size = howmany(max, NFDBITS) * sizeof(fd_mask);
|
|
|
|
|
|
|
|
if (size < sizeof(fd_set)) size = sizeof(fd_set);
|
|
|
|
dst->maxfd = max;
|
|
|
|
dst->fdset = xrealloc(dst->fdset, size);
|
|
|
|
memcpy(dst->fdset, src, size);
|
|
|
|
}
|
|
|
|
|
2011-09-12 15:36:06 +04:00
|
|
|
static void
|
|
|
|
rb_fd_rcopy(fd_set *dst, rb_fdset_t *src)
|
|
|
|
{
|
|
|
|
size_t size = howmany(rb_fd_max(src), NFDBITS) * sizeof(fd_mask);
|
|
|
|
|
|
|
|
if (size > sizeof(fd_set)) {
|
|
|
|
rb_raise(rb_eArgError, "too large fdsets");
|
|
|
|
}
|
|
|
|
memcpy(dst, rb_fd_ptr(src), sizeof(fd_set));
|
|
|
|
}
|
|
|
|
|
2007-02-05 15:21:01 +03:00
|
|
|
void
|
2011-05-15 18:51:09 +04:00
|
|
|
rb_fd_dup(rb_fdset_t *dst, const rb_fdset_t *src)
|
2007-02-05 15:21:01 +03:00
|
|
|
{
|
2011-04-30 15:18:14 +04:00
|
|
|
size_t size = howmany(rb_fd_max(src), NFDBITS) * sizeof(fd_mask);
|
2007-02-05 15:21:01 +03:00
|
|
|
|
2011-04-30 15:18:14 +04:00
|
|
|
if (size < sizeof(fd_set))
|
|
|
|
size = sizeof(fd_set);
|
|
|
|
dst->maxfd = src->maxfd;
|
2010-06-28 16:58:03 +04:00
|
|
|
dst->fdset = xrealloc(dst->fdset, size);
|
2011-05-04 02:37:07 +04:00
|
|
|
memcpy(dst->fdset, src->fdset, size);
|
2007-02-05 15:21:01 +03:00
|
|
|
}
|
|
|
|
|
2012-05-17 06:48:59 +04:00
|
|
|
#ifdef __native_client__
|
|
|
|
int select(int nfds, fd_set *readfds, fd_set *writefds,
|
|
|
|
fd_set *exceptfds, struct timeval *timeout);
|
|
|
|
#endif
|
|
|
|
|
2008-03-30 10:38:05 +04:00
|
|
|
int
|
|
|
|
rb_fd_select(int n, rb_fdset_t *readfds, rb_fdset_t *writefds, rb_fdset_t *exceptfds, struct timeval *timeout)
|
|
|
|
{
|
|
|
|
fd_set *r = NULL, *w = NULL, *e = NULL;
|
|
|
|
if (readfds) {
|
|
|
|
rb_fd_resize(n - 1, readfds);
|
|
|
|
r = rb_fd_ptr(readfds);
|
|
|
|
}
|
|
|
|
if (writefds) {
|
|
|
|
rb_fd_resize(n - 1, writefds);
|
|
|
|
w = rb_fd_ptr(writefds);
|
|
|
|
}
|
|
|
|
if (exceptfds) {
|
|
|
|
rb_fd_resize(n - 1, exceptfds);
|
|
|
|
e = rb_fd_ptr(exceptfds);
|
|
|
|
}
|
|
|
|
return select(n, r, w, e, timeout);
|
|
|
|
}
|
|
|
|
|
2007-02-05 15:21:01 +03:00
|
|
|
#undef FD_ZERO
|
|
|
|
#undef FD_SET
|
|
|
|
#undef FD_CLR
|
|
|
|
#undef FD_ISSET
|
|
|
|
|
|
|
|
#define FD_ZERO(f) rb_fd_zero(f)
|
2011-01-08 13:29:15 +03:00
|
|
|
#define FD_SET(i, f) rb_fd_set((i), (f))
|
|
|
|
#define FD_CLR(i, f) rb_fd_clr((i), (f))
|
|
|
|
#define FD_ISSET(i, f) rb_fd_isset((i), (f))
|
2007-02-05 15:21:01 +03:00
|
|
|
|
* include/ruby/intern.h, thread.c, win32/Makefile.sub (rb_fdset_t,
rb_fd_init, rb_fd_term, rb_fd_zero, rb_fd_set, rb_fd_clr, rb_fd_isset,
rb_fd_select, rb_fd_ptr, rb_fd_max, HAVE_RB_FD_INIT): new type,
functions, and macros for Windows.
* win32/win32.c (extract_fd, rb_w32_select): use rb_fdset_t to expand
fd_array if needed. [ruby-core:19946]
* win32/win32.c (copy_fd): new funcion for rb_w32_select().
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@21487 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2009-01-13 10:52:22 +03:00
|
|
|
#elif defined(_WIN32)
|
|
|
|
|
|
|
|
void
|
2011-05-07 17:40:56 +04:00
|
|
|
rb_fd_init(rb_fdset_t *set)
|
* include/ruby/intern.h, thread.c, win32/Makefile.sub (rb_fdset_t,
rb_fd_init, rb_fd_term, rb_fd_zero, rb_fd_set, rb_fd_clr, rb_fd_isset,
rb_fd_select, rb_fd_ptr, rb_fd_max, HAVE_RB_FD_INIT): new type,
functions, and macros for Windows.
* win32/win32.c (extract_fd, rb_w32_select): use rb_fdset_t to expand
fd_array if needed. [ruby-core:19946]
* win32/win32.c (copy_fd): new funcion for rb_w32_select().
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@21487 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2009-01-13 10:52:22 +03:00
|
|
|
{
|
|
|
|
set->capa = FD_SETSIZE;
|
|
|
|
set->fdset = ALLOC(fd_set);
|
|
|
|
FD_ZERO(set->fdset);
|
|
|
|
}
|
|
|
|
|
2011-05-07 17:36:08 +04:00
|
|
|
void
|
|
|
|
rb_fd_init_copy(rb_fdset_t *dst, rb_fdset_t *src)
|
|
|
|
{
|
|
|
|
rb_fd_init(dst);
|
2011-05-15 18:51:09 +04:00
|
|
|
rb_fd_dup(dst, src);
|
2011-05-07 17:36:08 +04:00
|
|
|
}
|
|
|
|
|
2011-09-12 15:36:06 +04:00
|
|
|
static void
|
|
|
|
rb_fd_rcopy(fd_set *dst, rb_fdset_t *src)
|
|
|
|
{
|
|
|
|
int max = rb_fd_max(src);
|
|
|
|
|
2011-09-14 06:44:57 +04:00
|
|
|
/* we assume src is the result of select() with dst, so dst should be
|
|
|
|
* larger or equal than src. */
|
2011-11-03 17:50:55 +04:00
|
|
|
if (max > FD_SETSIZE || (UINT)max > dst->fd_count) {
|
2011-09-12 15:36:06 +04:00
|
|
|
rb_raise(rb_eArgError, "too large fdsets");
|
|
|
|
}
|
|
|
|
|
|
|
|
memcpy(dst->fd_array, src->fdset->fd_array, max);
|
|
|
|
dst->fd_count = max;
|
|
|
|
}
|
|
|
|
|
* include/ruby/intern.h, thread.c, win32/Makefile.sub (rb_fdset_t,
rb_fd_init, rb_fd_term, rb_fd_zero, rb_fd_set, rb_fd_clr, rb_fd_isset,
rb_fd_select, rb_fd_ptr, rb_fd_max, HAVE_RB_FD_INIT): new type,
functions, and macros for Windows.
* win32/win32.c (extract_fd, rb_w32_select): use rb_fdset_t to expand
fd_array if needed. [ruby-core:19946]
* win32/win32.c (copy_fd): new funcion for rb_w32_select().
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@21487 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2009-01-13 10:52:22 +03:00
|
|
|
void
|
|
|
|
rb_fd_term(rb_fdset_t *set)
|
|
|
|
{
|
|
|
|
xfree(set->fdset);
|
|
|
|
set->fdset = NULL;
|
|
|
|
set->capa = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
rb_fd_set(int fd, rb_fdset_t *set)
|
|
|
|
{
|
|
|
|
unsigned int i;
|
|
|
|
SOCKET s = rb_w32_get_osfhandle(fd);
|
|
|
|
|
|
|
|
for (i = 0; i < set->fdset->fd_count; i++) {
|
|
|
|
if (set->fdset->fd_array[i] == s) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
* array.c, bignum.c, dln.c, error.c, gc.c, io.c, marshal.c,
numeric.c, pack.c, strftime.c, string.c, thread.c, transcode.c,
transcode_data.h, util.c, variable.c, vm_dump.c,
include/ruby/encoding.h, missing/crypt.c, missing/vsnprintf.c:
suppress VC type warnings. [ruby-core:22726]
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@22914 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2009-03-12 12:16:15 +03:00
|
|
|
if (set->fdset->fd_count >= (unsigned)set->capa) {
|
* include/ruby/intern.h, thread.c, win32/Makefile.sub (rb_fdset_t,
rb_fd_init, rb_fd_term, rb_fd_zero, rb_fd_set, rb_fd_clr, rb_fd_isset,
rb_fd_select, rb_fd_ptr, rb_fd_max, HAVE_RB_FD_INIT): new type,
functions, and macros for Windows.
* win32/win32.c (extract_fd, rb_w32_select): use rb_fdset_t to expand
fd_array if needed. [ruby-core:19946]
* win32/win32.c (copy_fd): new funcion for rb_w32_select().
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@21487 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2009-01-13 10:52:22 +03:00
|
|
|
set->capa = (set->fdset->fd_count / FD_SETSIZE + 1) * FD_SETSIZE;
|
|
|
|
set->fdset = xrealloc(set->fdset, sizeof(unsigned int) + sizeof(SOCKET) * set->capa);
|
|
|
|
}
|
|
|
|
set->fdset->fd_array[set->fdset->fd_count++] = s;
|
|
|
|
}
|
|
|
|
|
|
|
|
#undef FD_ZERO
|
|
|
|
#undef FD_SET
|
|
|
|
#undef FD_CLR
|
|
|
|
#undef FD_ISSET
|
|
|
|
|
|
|
|
#define FD_ZERO(f) rb_fd_zero(f)
|
2011-01-08 13:29:15 +03:00
|
|
|
#define FD_SET(i, f) rb_fd_set((i), (f))
|
|
|
|
#define FD_CLR(i, f) rb_fd_clr((i), (f))
|
|
|
|
#define FD_ISSET(i, f) rb_fd_isset((i), (f))
|
* include/ruby/intern.h, thread.c, win32/Makefile.sub (rb_fdset_t,
rb_fd_init, rb_fd_term, rb_fd_zero, rb_fd_set, rb_fd_clr, rb_fd_isset,
rb_fd_select, rb_fd_ptr, rb_fd_max, HAVE_RB_FD_INIT): new type,
functions, and macros for Windows.
* win32/win32.c (extract_fd, rb_w32_select): use rb_fdset_t to expand
fd_array if needed. [ruby-core:19946]
* win32/win32.c (copy_fd): new funcion for rb_w32_select().
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@21487 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2009-01-13 10:52:22 +03:00
|
|
|
|
2011-09-12 15:36:06 +04:00
|
|
|
#else
|
|
|
|
#define rb_fd_rcopy(d, s) (*(d) = *(s))
|
2007-02-05 15:21:01 +03:00
|
|
|
#endif
|
2006-12-31 18:02:22 +03:00
|
|
|
|
2007-02-08 14:51:40 +03:00
|
|
|
static int
|
2011-04-30 15:23:08 +04:00
|
|
|
do_select(int n, rb_fdset_t *read, rb_fdset_t *write, rb_fdset_t *except,
|
2007-02-08 14:51:40 +03:00
|
|
|
struct timeval *timeout)
|
|
|
|
{
|
2012-11-29 04:52:51 +04:00
|
|
|
int UNINITIALIZED_VAR(result);
|
|
|
|
int lerrno;
|
2011-04-30 15:23:08 +04:00
|
|
|
rb_fdset_t UNINITIALIZED_VAR(orig_read);
|
|
|
|
rb_fdset_t UNINITIALIZED_VAR(orig_write);
|
|
|
|
rb_fdset_t UNINITIALIZED_VAR(orig_except);
|
2008-04-26 13:36:35 +04:00
|
|
|
double limit = 0;
|
2007-02-14 07:02:12 +03:00
|
|
|
struct timeval wait_rest;
|
2012-11-20 05:21:19 +04:00
|
|
|
rb_thread_t *th = GET_THREAD();
|
2007-02-14 07:02:12 +03:00
|
|
|
|
|
|
|
if (timeout) {
|
2008-07-30 12:48:31 +04:00
|
|
|
limit = timeofday();
|
|
|
|
limit += (double)timeout->tv_sec+(double)timeout->tv_usec*1e-6;
|
2007-02-14 07:02:12 +03:00
|
|
|
wait_rest = *timeout;
|
|
|
|
timeout = &wait_rest;
|
|
|
|
}
|
|
|
|
|
2011-05-07 17:36:08 +04:00
|
|
|
if (read)
|
|
|
|
rb_fd_init_copy(&orig_read, read);
|
|
|
|
if (write)
|
|
|
|
rb_fd_init_copy(&orig_write, write);
|
|
|
|
if (except)
|
|
|
|
rb_fd_init_copy(&orig_except, except);
|
2007-02-14 07:02:12 +03:00
|
|
|
|
|
|
|
retry:
|
|
|
|
lerrno = 0;
|
|
|
|
|
2011-09-27 04:59:04 +04:00
|
|
|
BLOCKING_REGION({
|
2012-11-20 05:21:19 +04:00
|
|
|
result = native_fd_select(n, read, write, except, timeout, th);
|
2011-06-15 22:04:13 +04:00
|
|
|
if (result < 0) lerrno = errno;
|
2012-11-28 17:01:25 +04:00
|
|
|
}, ubf_select, th, FALSE);
|
2012-07-18 09:46:40 +04:00
|
|
|
|
2012-11-20 05:21:19 +04:00
|
|
|
RUBY_VM_CHECK_INTS_BLOCKING(th);
|
2012-07-18 09:46:40 +04:00
|
|
|
|
2007-02-08 14:51:40 +03:00
|
|
|
errno = lerrno;
|
2007-02-14 07:02:12 +03:00
|
|
|
|
|
|
|
if (result < 0) {
|
2008-04-26 13:36:35 +04:00
|
|
|
switch (errno) {
|
|
|
|
case EINTR:
|
2007-02-14 07:02:12 +03:00
|
|
|
#ifdef ERESTART
|
2008-04-26 13:36:35 +04:00
|
|
|
case ERESTART:
|
2007-02-14 07:02:12 +03:00
|
|
|
#endif
|
2011-04-30 15:23:08 +04:00
|
|
|
if (read)
|
2011-05-15 18:51:09 +04:00
|
|
|
rb_fd_dup(read, &orig_read);
|
2011-04-30 15:23:08 +04:00
|
|
|
if (write)
|
2011-05-15 18:51:09 +04:00
|
|
|
rb_fd_dup(write, &orig_write);
|
2011-04-30 15:23:08 +04:00
|
|
|
if (except)
|
2011-05-15 18:51:09 +04:00
|
|
|
rb_fd_dup(except, &orig_except);
|
2011-04-30 15:08:39 +04:00
|
|
|
|
2007-02-14 07:02:12 +03:00
|
|
|
if (timeout) {
|
|
|
|
double d = limit - timeofday();
|
|
|
|
|
2011-11-09 06:06:49 +04:00
|
|
|
wait_rest.tv_sec = (time_t)d;
|
2009-05-09 07:25:16 +04:00
|
|
|
wait_rest.tv_usec = (int)((d-(double)wait_rest.tv_sec)*1e6);
|
2007-02-14 07:02:12 +03:00
|
|
|
if (wait_rest.tv_sec < 0) wait_rest.tv_sec = 0;
|
|
|
|
if (wait_rest.tv_usec < 0) wait_rest.tv_usec = 0;
|
|
|
|
}
|
2011-04-30 15:08:39 +04:00
|
|
|
|
2007-02-14 07:02:12 +03:00
|
|
|
goto retry;
|
2008-04-26 13:36:35 +04:00
|
|
|
default:
|
|
|
|
break;
|
2007-02-14 07:02:12 +03:00
|
|
|
}
|
|
|
|
}
|
2011-07-01 06:08:48 +04:00
|
|
|
|
|
|
|
if (read)
|
|
|
|
rb_fd_term(&orig_read);
|
|
|
|
if (write)
|
|
|
|
rb_fd_term(&orig_write);
|
|
|
|
if (except)
|
|
|
|
rb_fd_term(&orig_except);
|
|
|
|
|
2007-02-08 14:51:40 +03:00
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2007-02-05 15:21:01 +03:00
|
|
|
static void
|
2007-02-14 07:02:12 +03:00
|
|
|
rb_thread_wait_fd_rw(int fd, int read)
|
2007-02-05 15:21:01 +03:00
|
|
|
{
|
|
|
|
int result = 0;
|
2011-05-04 04:59:57 +04:00
|
|
|
int events = read ? RB_WAITFD_IN : RB_WAITFD_OUT;
|
|
|
|
|
2007-02-14 07:02:12 +03:00
|
|
|
thread_debug("rb_thread_wait_fd_rw(%d, %s)\n", fd, read ? "read" : "write");
|
2007-02-05 15:21:01 +03:00
|
|
|
|
2008-07-11 15:51:39 +04:00
|
|
|
if (fd < 0) {
|
|
|
|
rb_raise(rb_eIOError, "closed stream");
|
|
|
|
}
|
2008-10-09 03:49:22 +04:00
|
|
|
if (rb_thread_alone()) return;
|
2006-12-31 18:02:22 +03:00
|
|
|
while (result <= 0) {
|
2011-05-04 04:59:57 +04:00
|
|
|
result = rb_wait_for_single_fd(fd, events, NULL);
|
2007-06-26 11:30:01 +04:00
|
|
|
|
2007-12-25 07:47:47 +03:00
|
|
|
if (result < 0) {
|
2007-02-20 09:07:05 +03:00
|
|
|
rb_sys_fail(0);
|
|
|
|
}
|
2006-12-31 18:02:22 +03:00
|
|
|
}
|
2007-02-05 15:21:01 +03:00
|
|
|
|
2007-02-14 07:02:12 +03:00
|
|
|
thread_debug("rb_thread_wait_fd_rw(%d, %s): done\n", fd, read ? "read" : "write");
|
2007-02-05 15:21:01 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
rb_thread_wait_fd(int fd)
|
|
|
|
{
|
2007-02-14 07:02:12 +03:00
|
|
|
rb_thread_wait_fd_rw(fd, 1);
|
2007-02-05 15:21:01 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
rb_thread_fd_writable(int fd)
|
|
|
|
{
|
2007-02-14 07:02:12 +03:00
|
|
|
rb_thread_wait_fd_rw(fd, 0);
|
* compile.c, cont.c, gc.c, insns.def, iseq.c, iseq.h, process.c,
thread.c, vm.c, vm_core.h, vm_dump.c, vm_eval.c,
vm_insnhelper.c, vm_method.c, template/insns_info.inc.tmpl,
tool/instruction.rb: fixed types.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@25030 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2009-09-22 00:58:26 +04:00
|
|
|
return TRUE;
|
2006-12-31 18:02:22 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
rb_thread_select(int max, fd_set * read, fd_set * write, fd_set * except,
|
|
|
|
struct timeval *timeout)
|
|
|
|
{
|
2011-08-30 19:04:11 +04:00
|
|
|
rb_fdset_t fdsets[3];
|
2011-08-30 04:33:05 +04:00
|
|
|
rb_fdset_t *rfds = NULL;
|
|
|
|
rb_fdset_t *wfds = NULL;
|
|
|
|
rb_fdset_t *efds = NULL;
|
|
|
|
int retval;
|
|
|
|
|
|
|
|
if (read) {
|
|
|
|
rfds = &fdsets[0];
|
2011-08-30 19:04:11 +04:00
|
|
|
rb_fd_init(rfds);
|
2011-08-30 04:33:05 +04:00
|
|
|
rb_fd_copy(rfds, read, max);
|
|
|
|
}
|
|
|
|
if (write) {
|
|
|
|
wfds = &fdsets[1];
|
2011-08-30 19:04:11 +04:00
|
|
|
rb_fd_init(wfds);
|
2011-08-30 04:33:05 +04:00
|
|
|
rb_fd_copy(wfds, write, max);
|
|
|
|
}
|
|
|
|
if (except) {
|
|
|
|
efds = &fdsets[2];
|
2011-09-09 05:22:06 +04:00
|
|
|
rb_fd_init(efds);
|
2011-08-30 04:33:05 +04:00
|
|
|
rb_fd_copy(efds, except, max);
|
2006-12-31 18:02:22 +03:00
|
|
|
}
|
2011-04-30 20:08:20 +04:00
|
|
|
|
2011-08-30 19:49:55 +04:00
|
|
|
retval = rb_thread_fd_select(max, rfds, wfds, efds, timeout);
|
2011-04-30 20:08:20 +04:00
|
|
|
|
2011-09-12 15:36:06 +04:00
|
|
|
if (rfds) {
|
|
|
|
rb_fd_rcopy(read, rfds);
|
2011-08-30 04:33:05 +04:00
|
|
|
rb_fd_term(rfds);
|
2011-09-12 15:36:06 +04:00
|
|
|
}
|
|
|
|
if (wfds) {
|
|
|
|
rb_fd_rcopy(write, wfds);
|
2011-08-30 04:33:05 +04:00
|
|
|
rb_fd_term(wfds);
|
2011-09-12 15:36:06 +04:00
|
|
|
}
|
|
|
|
if (efds) {
|
|
|
|
rb_fd_rcopy(except, efds);
|
2011-08-30 04:33:05 +04:00
|
|
|
rb_fd_term(efds);
|
2011-09-12 15:36:06 +04:00
|
|
|
}
|
2006-12-31 18:02:22 +03:00
|
|
|
|
2011-08-30 04:33:05 +04:00
|
|
|
return retval;
|
|
|
|
}
|
2006-12-31 18:02:22 +03:00
|
|
|
|
2009-04-01 08:56:51 +04:00
|
|
|
int
|
|
|
|
rb_thread_fd_select(int max, rb_fdset_t * read, rb_fdset_t * write, rb_fdset_t * except,
|
|
|
|
struct timeval *timeout)
|
|
|
|
{
|
|
|
|
if (!read && !write && !except) {
|
|
|
|
if (!timeout) {
|
|
|
|
rb_thread_sleep_forever();
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
rb_thread_wait_for(*timeout);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (read) {
|
2011-04-30 15:23:08 +04:00
|
|
|
rb_fd_resize(max - 1, read);
|
2009-04-01 08:56:51 +04:00
|
|
|
}
|
|
|
|
if (write) {
|
2011-04-30 15:23:08 +04:00
|
|
|
rb_fd_resize(max - 1, write);
|
2009-04-01 08:56:51 +04:00
|
|
|
}
|
|
|
|
if (except) {
|
2011-04-30 15:23:08 +04:00
|
|
|
rb_fd_resize(max - 1, except);
|
2009-04-01 08:56:51 +04:00
|
|
|
}
|
2011-04-30 15:23:08 +04:00
|
|
|
return do_select(max, read, write, except, timeout);
|
2009-04-01 08:56:51 +04:00
|
|
|
}
|
|
|
|
|
2011-05-04 05:07:03 +04:00
|
|
|
/*
|
|
|
|
* poll() is supported by many OSes, but so far Linux is the only
|
|
|
|
* one we know of that supports using poll() in all places select()
|
|
|
|
* would work.
|
|
|
|
*/
|
2011-11-15 15:09:47 +04:00
|
|
|
#if defined(HAVE_POLL) && defined(__linux__)
|
2011-05-04 05:07:03 +04:00
|
|
|
# define USE_POLL
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifdef USE_POLL
|
2011-05-04 14:02:06 +04:00
|
|
|
|
|
|
|
/* The same with linux kernel. TODO: make platform independent definition. */
|
|
|
|
#define POLLIN_SET (POLLRDNORM | POLLRDBAND | POLLIN | POLLHUP | POLLERR)
|
|
|
|
#define POLLOUT_SET (POLLWRBAND | POLLWRNORM | POLLOUT | POLLERR)
|
|
|
|
#define POLLEX_SET (POLLPRI)
|
|
|
|
|
2011-05-06 18:58:38 +04:00
|
|
|
#define TIMET_MAX (~(time_t)0 <= 0 ? (time_t)((~(unsigned_time_t)0) >> 1) : (time_t)(~(unsigned_time_t)0))
|
|
|
|
#define TIMET_MIN (~(time_t)0 <= 0 ? (time_t)(((unsigned_time_t)1) << (sizeof(time_t) * CHAR_BIT - 1)) : (time_t)0)
|
|
|
|
|
|
|
|
#ifndef HAVE_PPOLL
|
|
|
|
/* TODO: don't ignore sigmask */
|
2012-06-27 09:37:15 +04:00
|
|
|
int
|
|
|
|
ppoll(struct pollfd *fds, nfds_t nfds,
|
|
|
|
const struct timespec *ts, const sigset_t *sigmask)
|
2011-05-06 18:58:38 +04:00
|
|
|
{
|
|
|
|
int timeout_ms;
|
|
|
|
|
|
|
|
if (ts) {
|
|
|
|
int tmp, tmp2;
|
|
|
|
|
|
|
|
if (ts->tv_sec > TIMET_MAX/1000)
|
|
|
|
timeout_ms = -1;
|
|
|
|
else {
|
|
|
|
tmp = ts->tv_sec * 1000;
|
2011-05-27 03:48:31 +04:00
|
|
|
tmp2 = ts->tv_nsec / (1000 * 1000);
|
2011-05-06 18:58:38 +04:00
|
|
|
if (TIMET_MAX - tmp < tmp2)
|
|
|
|
timeout_ms = -1;
|
|
|
|
else
|
|
|
|
timeout_ms = tmp + tmp2;
|
|
|
|
}
|
2012-06-27 09:37:15 +04:00
|
|
|
}
|
|
|
|
else
|
2011-05-27 03:48:31 +04:00
|
|
|
timeout_ms = -1;
|
2011-05-06 18:58:38 +04:00
|
|
|
|
|
|
|
return poll(fds, nfds, timeout_ms);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2011-05-04 05:07:03 +04:00
|
|
|
/*
|
|
|
|
* returns a mask of events
|
|
|
|
*/
|
|
|
|
int
|
|
|
|
rb_wait_for_single_fd(int fd, int events, struct timeval *tv)
|
|
|
|
{
|
|
|
|
struct pollfd fds;
|
|
|
|
int result, lerrno;
|
2011-05-06 18:58:38 +04:00
|
|
|
double limit = 0;
|
|
|
|
struct timespec ts;
|
|
|
|
struct timespec *timeout = NULL;
|
2012-11-20 05:21:19 +04:00
|
|
|
rb_thread_t *th = GET_THREAD();
|
2011-05-06 18:58:38 +04:00
|
|
|
|
|
|
|
if (tv) {
|
|
|
|
ts.tv_sec = tv->tv_sec;
|
|
|
|
ts.tv_nsec = tv->tv_usec * 1000;
|
|
|
|
limit = timeofday();
|
|
|
|
limit += (double)tv->tv_sec + (double)tv->tv_usec * 1e-6;
|
|
|
|
timeout = &ts;
|
|
|
|
}
|
2011-05-04 05:07:03 +04:00
|
|
|
|
|
|
|
fds.fd = fd;
|
|
|
|
fds.events = (short)events;
|
|
|
|
|
|
|
|
retry:
|
|
|
|
lerrno = 0;
|
|
|
|
BLOCKING_REGION({
|
2011-05-06 18:58:38 +04:00
|
|
|
result = ppoll(&fds, 1, timeout, NULL);
|
2011-05-04 05:07:03 +04:00
|
|
|
if (result < 0) lerrno = errno;
|
2012-11-28 17:01:25 +04:00
|
|
|
}, ubf_select, th, FALSE);
|
2011-05-04 05:07:03 +04:00
|
|
|
|
2012-11-20 05:21:19 +04:00
|
|
|
RUBY_VM_CHECK_INTS_BLOCKING(th);
|
2012-07-18 09:46:40 +04:00
|
|
|
|
2011-05-04 05:07:03 +04:00
|
|
|
if (result < 0) {
|
|
|
|
errno = lerrno;
|
|
|
|
switch (errno) {
|
|
|
|
case EINTR:
|
|
|
|
#ifdef ERESTART
|
|
|
|
case ERESTART:
|
|
|
|
#endif
|
2011-05-06 18:58:38 +04:00
|
|
|
if (timeout) {
|
|
|
|
double d = limit - timeofday();
|
|
|
|
|
|
|
|
ts.tv_sec = (long)d;
|
|
|
|
ts.tv_nsec = (long)((d - (double)ts.tv_sec) * 1e9);
|
|
|
|
if (ts.tv_sec < 0)
|
|
|
|
ts.tv_sec = 0;
|
|
|
|
if (ts.tv_nsec < 0)
|
|
|
|
ts.tv_nsec = 0;
|
2011-05-04 05:07:03 +04:00
|
|
|
}
|
|
|
|
goto retry;
|
|
|
|
}
|
2011-05-04 14:02:06 +04:00
|
|
|
return -1;
|
2011-05-04 05:07:03 +04:00
|
|
|
}
|
|
|
|
|
2011-05-04 14:02:06 +04:00
|
|
|
if (fds.revents & POLLNVAL) {
|
|
|
|
errno = EBADF;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* POLLIN, POLLOUT have a different meanings from select(2)'s read/write bit.
|
|
|
|
* Therefore we need fix it up.
|
|
|
|
*/
|
|
|
|
result = 0;
|
|
|
|
if (fds.revents & POLLIN_SET)
|
|
|
|
result |= RB_WAITFD_IN;
|
|
|
|
if (fds.revents & POLLOUT_SET)
|
|
|
|
result |= RB_WAITFD_OUT;
|
|
|
|
if (fds.revents & POLLEX_SET)
|
|
|
|
result |= RB_WAITFD_PRI;
|
|
|
|
|
2011-05-04 05:07:03 +04:00
|
|
|
return result;
|
|
|
|
}
|
|
|
|
#else /* ! USE_POLL - implement rb_io_poll_fd() using select() */
|
2012-06-27 09:37:15 +04:00
|
|
|
static rb_fdset_t *
|
|
|
|
init_set_fd(int fd, rb_fdset_t *fds)
|
2011-05-04 04:59:57 +04:00
|
|
|
{
|
|
|
|
rb_fd_init(fds);
|
|
|
|
rb_fd_set(fd, fds);
|
|
|
|
|
|
|
|
return fds;
|
|
|
|
}
|
|
|
|
|
|
|
|
struct select_args {
|
|
|
|
union {
|
|
|
|
int fd;
|
|
|
|
int error;
|
|
|
|
} as;
|
|
|
|
rb_fdset_t *read;
|
|
|
|
rb_fdset_t *write;
|
|
|
|
rb_fdset_t *except;
|
|
|
|
struct timeval *tv;
|
|
|
|
};
|
|
|
|
|
|
|
|
static VALUE
|
|
|
|
select_single(VALUE ptr)
|
|
|
|
{
|
|
|
|
struct select_args *args = (struct select_args *)ptr;
|
|
|
|
int r;
|
|
|
|
|
|
|
|
r = rb_thread_fd_select(args->as.fd + 1,
|
|
|
|
args->read, args->write, args->except, args->tv);
|
|
|
|
if (r == -1)
|
|
|
|
args->as.error = errno;
|
|
|
|
if (r > 0) {
|
|
|
|
r = 0;
|
|
|
|
if (args->read && rb_fd_isset(args->as.fd, args->read))
|
|
|
|
r |= RB_WAITFD_IN;
|
|
|
|
if (args->write && rb_fd_isset(args->as.fd, args->write))
|
|
|
|
r |= RB_WAITFD_OUT;
|
|
|
|
if (args->except && rb_fd_isset(args->as.fd, args->except))
|
|
|
|
r |= RB_WAITFD_PRI;
|
|
|
|
}
|
|
|
|
return (VALUE)r;
|
|
|
|
}
|
|
|
|
|
|
|
|
static VALUE
|
|
|
|
select_single_cleanup(VALUE ptr)
|
|
|
|
{
|
|
|
|
struct select_args *args = (struct select_args *)ptr;
|
|
|
|
|
|
|
|
if (args->read) rb_fd_term(args->read);
|
|
|
|
if (args->write) rb_fd_term(args->write);
|
|
|
|
if (args->except) rb_fd_term(args->except);
|
|
|
|
|
|
|
|
return (VALUE)-1;
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
rb_wait_for_single_fd(int fd, int events, struct timeval *tv)
|
|
|
|
{
|
|
|
|
rb_fdset_t rfds, wfds, efds;
|
|
|
|
struct select_args args;
|
|
|
|
int r;
|
|
|
|
VALUE ptr = (VALUE)&args;
|
|
|
|
|
|
|
|
args.as.fd = fd;
|
|
|
|
args.read = (events & RB_WAITFD_IN) ? init_set_fd(fd, &rfds) : NULL;
|
|
|
|
args.write = (events & RB_WAITFD_OUT) ? init_set_fd(fd, &wfds) : NULL;
|
|
|
|
args.except = (events & RB_WAITFD_PRI) ? init_set_fd(fd, &efds) : NULL;
|
|
|
|
args.tv = tv;
|
|
|
|
|
|
|
|
r = (int)rb_ensure(select_single, ptr, select_single_cleanup, ptr);
|
|
|
|
if (r == -1)
|
|
|
|
errno = args.as.error;
|
|
|
|
|
|
|
|
return r;
|
|
|
|
}
|
2011-05-04 05:07:03 +04:00
|
|
|
#endif /* ! USE_POLL */
|
2009-04-01 08:56:51 +04:00
|
|
|
|
2006-12-31 18:02:22 +03:00
|
|
|
/*
|
|
|
|
* for GC
|
|
|
|
*/
|
|
|
|
|
2007-07-14 11:19:59 +04:00
|
|
|
#ifdef USE_CONSERVATIVE_STACK_END
|
2006-12-31 18:02:22 +03:00
|
|
|
void
|
2007-02-05 15:21:01 +03:00
|
|
|
rb_gc_set_stack_end(VALUE **stack_end_p)
|
2006-12-31 18:02:22 +03:00
|
|
|
{
|
|
|
|
VALUE stack_end;
|
|
|
|
*stack_end_p = &stack_end;
|
|
|
|
}
|
2007-07-14 11:19:59 +04:00
|
|
|
#endif
|
2006-12-31 18:02:22 +03:00
|
|
|
|
|
|
|
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 09:37:46 +03:00
|
|
|
rb_gc_save_machine_context(rb_thread_t *th)
|
2006-12-31 18:02:22 +03:00
|
|
|
{
|
2008-05-08 10:59:03 +04:00
|
|
|
FLUSH_REGISTER_WINDOWS;
|
2007-06-14 12:35:20 +04:00
|
|
|
#ifdef __ia64
|
|
|
|
th->machine_register_stack_end = rb_ia64_bsp();
|
|
|
|
#endif
|
2006-12-31 18:02:22 +03:00
|
|
|
setjmp(th->machine_regs);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
2010-04-27 16:27:13 +04:00
|
|
|
void
|
|
|
|
rb_threadptr_check_signal(rb_thread_t *mth)
|
2006-12-31 18:02:22 +03:00
|
|
|
{
|
2010-04-27 16:27:13 +04:00
|
|
|
/* mth must be main_thread */
|
2011-07-01 02:29:34 +04:00
|
|
|
if (rb_signal_buff_size() > 0) {
|
|
|
|
/* wakeup main thread */
|
2012-11-26 13:22:01 +04:00
|
|
|
rb_threadptr_trap_interrupt(mth);
|
2006-12-31 18:02:22 +03:00
|
|
|
}
|
2010-04-27 16:27:13 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
timer_thread_function(void *arg)
|
|
|
|
{
|
|
|
|
rb_vm_t *vm = GET_VM(); /* TODO: fix me for Multi-VM */
|
|
|
|
|
|
|
|
/* for time slice */
|
2012-04-28 13:59:44 +04:00
|
|
|
if (!vm->running_thread->yielding) {
|
|
|
|
RUBY_VM_SET_TIMER_INTERRUPT(vm->running_thread);
|
|
|
|
}
|
2010-04-27 16:27:13 +04:00
|
|
|
|
|
|
|
/* check signal */
|
|
|
|
rb_threadptr_check_signal(vm->main_thread);
|
2007-04-25 07:50:00 +04:00
|
|
|
|
|
|
|
#if 0
|
|
|
|
/* prove profiler */
|
|
|
|
if (vm->prove_profile.enable) {
|
|
|
|
rb_thread_t *th = vm->running_thread;
|
|
|
|
|
|
|
|
if (vm->during_gc) {
|
|
|
|
/* GC prove profiling */
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
2006-12-31 18:02:22 +03:00
|
|
|
}
|
|
|
|
|
2007-01-07 12:47:52 +03:00
|
|
|
void
|
2011-06-27 04:30:41 +04:00
|
|
|
rb_thread_stop_timer_thread(int close_anyway)
|
2007-01-07 12:47:52 +03:00
|
|
|
{
|
2011-06-27 04:30:41 +04:00
|
|
|
if (timer_thread_id && native_stop_timer_thread(close_anyway)) {
|
2009-11-02 06:58:25 +03:00
|
|
|
native_reset_timer_thread();
|
2007-01-07 12:47:52 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
rb_thread_reset_timer_thread(void)
|
|
|
|
{
|
2009-11-02 06:58:25 +03:00
|
|
|
native_reset_timer_thread();
|
2007-01-07 12:47:52 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
rb_thread_start_timer_thread(void)
|
|
|
|
{
|
2008-11-24 18:08:24 +03:00
|
|
|
system_working = 1;
|
2007-01-07 12:47:52 +03:00
|
|
|
rb_thread_create_timer_thread();
|
|
|
|
}
|
|
|
|
|
2008-07-01 20:55:30 +04:00
|
|
|
static int
|
|
|
|
clear_coverage_i(st_data_t key, st_data_t val, st_data_t dummy)
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
VALUE lines = (VALUE)val;
|
|
|
|
|
|
|
|
for (i = 0; i < RARRAY_LEN(lines); i++) {
|
|
|
|
if (RARRAY_PTR(lines)[i] != Qnil) {
|
|
|
|
RARRAY_PTR(lines)[i] = INT2FIX(0);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return ST_CONTINUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
clear_coverage(void)
|
|
|
|
{
|
2008-07-08 19:13:22 +04:00
|
|
|
VALUE coverages = rb_get_coverages();
|
2008-07-03 16:55:12 +04:00
|
|
|
if (RTEST(coverages)) {
|
|
|
|
st_foreach(RHASH_TBL(coverages), clear_coverage_i, 0);
|
2008-07-01 20:55:30 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-12-27 13:30:04 +03:00
|
|
|
static void
|
|
|
|
rb_thread_atfork_internal(int (*atfork)(st_data_t, st_data_t, st_data_t))
|
|
|
|
{
|
|
|
|
rb_thread_t *th = GET_THREAD();
|
|
|
|
rb_vm_t *vm = th->vm;
|
|
|
|
VALUE thval = th->self;
|
|
|
|
vm->main_thread = th;
|
|
|
|
|
2010-11-27 23:15:59 +03:00
|
|
|
gvl_atfork(th->vm);
|
2008-12-27 13:30:04 +03:00
|
|
|
st_foreach(vm->living_threads, atfork, (st_data_t)th);
|
|
|
|
st_clear(vm->living_threads);
|
|
|
|
st_insert(vm->living_threads, thval, (st_data_t)th->thread_id);
|
|
|
|
vm->sleeper = 0;
|
|
|
|
clear_coverage();
|
|
|
|
}
|
|
|
|
|
2008-01-14 13:02:51 +03:00
|
|
|
static int
|
2008-12-27 13:30:04 +03:00
|
|
|
terminate_atfork_i(st_data_t key, st_data_t val, st_data_t current_th)
|
2008-01-14 13:02:51 +03:00
|
|
|
{
|
|
|
|
VALUE thval = key;
|
|
|
|
rb_thread_t *th;
|
|
|
|
GetThreadPtr(thval, th);
|
|
|
|
|
2008-12-27 13:30:04 +03:00
|
|
|
if (th != (rb_thread_t *)current_th) {
|
2009-11-18 11:48:24 +03:00
|
|
|
if (th->keeping_mutexes) {
|
|
|
|
rb_mutex_abandon_all(th->keeping_mutexes);
|
|
|
|
}
|
|
|
|
th->keeping_mutexes = NULL;
|
2010-12-20 18:16:41 +03:00
|
|
|
thread_cleanup_func(th, TRUE);
|
2008-01-14 13:02:51 +03:00
|
|
|
}
|
|
|
|
return ST_CONTINUE;
|
|
|
|
}
|
2006-12-31 18:02:22 +03:00
|
|
|
|
|
|
|
void
|
|
|
|
rb_thread_atfork(void)
|
|
|
|
{
|
2008-12-27 13:30:04 +03:00
|
|
|
rb_thread_atfork_internal(terminate_atfork_i);
|
2012-11-28 16:34:15 +04:00
|
|
|
GET_THREAD()->join_list = NULL;
|
2010-12-25 11:50:53 +03:00
|
|
|
|
|
|
|
/* We don't want reproduce CVE-2003-0900. */
|
2008-06-20 06:46:17 +04:00
|
|
|
rb_reset_random_seed();
|
2008-05-11 08:15:29 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
2008-12-27 13:30:04 +03:00
|
|
|
terminate_atfork_before_exec_i(st_data_t key, st_data_t val, st_data_t current_th)
|
2008-05-11 08:15:29 +04:00
|
|
|
{
|
|
|
|
VALUE thval = key;
|
|
|
|
rb_thread_t *th;
|
|
|
|
GetThreadPtr(thval, th);
|
|
|
|
|
2008-12-27 13:30:04 +03:00
|
|
|
if (th != (rb_thread_t *)current_th) {
|
2008-05-11 08:15:29 +04:00
|
|
|
thread_cleanup_func_before_exec(th);
|
|
|
|
}
|
|
|
|
return ST_CONTINUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
rb_thread_atfork_before_exec(void)
|
|
|
|
{
|
2008-12-27 13:30:04 +03:00
|
|
|
rb_thread_atfork_internal(terminate_atfork_before_exec_i);
|
2006-12-31 18:02:22 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
struct thgroup {
|
|
|
|
int enclosed;
|
|
|
|
VALUE group;
|
|
|
|
};
|
|
|
|
|
2009-09-09 07:21:14 +04:00
|
|
|
static size_t
|
|
|
|
thgroup_memsize(const void *ptr)
|
|
|
|
{
|
|
|
|
return ptr ? sizeof(struct thgroup) : 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static const rb_data_type_t thgroup_data_type = {
|
|
|
|
"thgroup",
|
2010-07-18 11:31:54 +04:00
|
|
|
{NULL, RUBY_TYPED_DEFAULT_FREE, thgroup_memsize,},
|
2009-09-09 07:21:14 +04:00
|
|
|
};
|
|
|
|
|
2006-12-31 18:02:22 +03:00
|
|
|
/*
|
|
|
|
* Document-class: ThreadGroup
|
|
|
|
*
|
|
|
|
* <code>ThreadGroup</code> provides a means of keeping track of a number of
|
|
|
|
* threads as a group. A <code>Thread</code> can belong to only one
|
|
|
|
* <code>ThreadGroup</code> at a time; adding a thread to a new group will
|
|
|
|
* remove it from any previous group.
|
* 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 08:25:10 +04:00
|
|
|
*
|
2006-12-31 18:02:22 +03:00
|
|
|
* Newly created threads belong to the same group as the thread from which they
|
|
|
|
* were created.
|
|
|
|
*/
|
|
|
|
|
|
|
|
static VALUE
|
|
|
|
thgroup_s_alloc(VALUE klass)
|
|
|
|
{
|
|
|
|
VALUE group;
|
|
|
|
struct thgroup *data;
|
|
|
|
|
2009-09-09 07:21:14 +04:00
|
|
|
group = TypedData_Make_Struct(klass, struct thgroup, &thgroup_data_type, data);
|
2006-12-31 18:02:22 +03:00
|
|
|
data->enclosed = 0;
|
|
|
|
data->group = group;
|
|
|
|
|
|
|
|
return group;
|
|
|
|
}
|
|
|
|
|
|
|
|
struct thgroup_list_params {
|
|
|
|
VALUE ary;
|
|
|
|
VALUE group;
|
|
|
|
};
|
|
|
|
|
|
|
|
static int
|
|
|
|
thgroup_list_i(st_data_t key, st_data_t val, st_data_t data)
|
|
|
|
{
|
|
|
|
VALUE thread = (VALUE)key;
|
|
|
|
VALUE ary = ((struct thgroup_list_params *)data)->ary;
|
|
|
|
VALUE group = ((struct thgroup_list_params *)data)->group;
|
* 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 09:37:46 +03:00
|
|
|
rb_thread_t *th;
|
2006-12-31 18:02:22 +03:00
|
|
|
GetThreadPtr(thread, th);
|
|
|
|
|
|
|
|
if (th->thgroup == group) {
|
|
|
|
rb_ary_push(ary, thread);
|
|
|
|
}
|
|
|
|
return ST_CONTINUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* call-seq:
|
2010-05-18 01:07:33 +04:00
|
|
|
* thgrp.list -> array
|
* 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 08:25:10 +04:00
|
|
|
*
|
2006-12-31 18:02:22 +03:00
|
|
|
* Returns an array of all existing <code>Thread</code> objects that belong to
|
|
|
|
* this group.
|
* 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 08:25:10 +04:00
|
|
|
*
|
2006-12-31 18:02:22 +03:00
|
|
|
* ThreadGroup::Default.list #=> [#<Thread:0x401bdf4c run>]
|
|
|
|
*/
|
|
|
|
|
|
|
|
static VALUE
|
|
|
|
thgroup_list(VALUE group)
|
|
|
|
{
|
|
|
|
VALUE ary = rb_ary_new();
|
2007-07-20 11:11:35 +04:00
|
|
|
struct thgroup_list_params param;
|
2009-02-18 04:29:13 +03:00
|
|
|
|
2007-07-20 11:11:35 +04:00
|
|
|
param.ary = ary;
|
|
|
|
param.group = group;
|
2006-12-31 18:02:22 +03:00
|
|
|
st_foreach(GET_THREAD()->vm->living_threads, thgroup_list_i, (st_data_t) & param);
|
|
|
|
return ary;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* call-seq:
|
2010-05-18 01:07:33 +04:00
|
|
|
* thgrp.enclose -> thgrp
|
* 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 08:25:10 +04:00
|
|
|
*
|
2006-12-31 18:02:22 +03:00
|
|
|
* Prevents threads from being added to or removed from the receiving
|
|
|
|
* <code>ThreadGroup</code>. New threads can still be started in an enclosed
|
|
|
|
* <code>ThreadGroup</code>.
|
* 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 08:25:10 +04:00
|
|
|
*
|
2006-12-31 18:02:22 +03:00
|
|
|
* ThreadGroup::Default.enclose #=> #<ThreadGroup:0x4029d914>
|
|
|
|
* thr = Thread::new { Thread.stop } #=> #<Thread:0x402a7210 sleep>
|
|
|
|
* tg = ThreadGroup::new #=> #<ThreadGroup:0x402752d4>
|
|
|
|
* tg.add thr
|
|
|
|
*
|
|
|
|
* <em>produces:</em>
|
|
|
|
*
|
|
|
|
* ThreadError: can't move from the enclosed thread group
|
|
|
|
*/
|
|
|
|
|
2008-09-04 22:23:27 +04:00
|
|
|
static VALUE
|
2007-03-19 06:58:57 +03:00
|
|
|
thgroup_enclose(VALUE group)
|
2006-12-31 18:02:22 +03:00
|
|
|
{
|
|
|
|
struct thgroup *data;
|
|
|
|
|
2009-09-09 07:21:14 +04:00
|
|
|
TypedData_Get_Struct(group, struct thgroup, &thgroup_data_type, data);
|
2006-12-31 18:02:22 +03:00
|
|
|
data->enclosed = 1;
|
|
|
|
|
|
|
|
return group;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* call-seq:
|
2010-05-18 01:07:33 +04:00
|
|
|
* thgrp.enclosed? -> true or false
|
* 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 08:25:10 +04:00
|
|
|
*
|
2006-12-31 18:02:22 +03:00
|
|
|
* Returns <code>true</code> if <em>thgrp</em> is enclosed. See also
|
|
|
|
* ThreadGroup#enclose.
|
|
|
|
*/
|
|
|
|
|
|
|
|
static VALUE
|
|
|
|
thgroup_enclosed_p(VALUE group)
|
|
|
|
{
|
|
|
|
struct thgroup *data;
|
|
|
|
|
2009-09-09 07:21:14 +04:00
|
|
|
TypedData_Get_Struct(group, struct thgroup, &thgroup_data_type, data);
|
2006-12-31 18:02:22 +03:00
|
|
|
if (data->enclosed)
|
|
|
|
return Qtrue;
|
|
|
|
return Qfalse;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* call-seq:
|
2010-05-18 01:07:33 +04:00
|
|
|
* thgrp.add(thread) -> thgrp
|
* 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 08:25:10 +04:00
|
|
|
*
|
2006-12-31 18:02:22 +03:00
|
|
|
* Adds the given <em>thread</em> to this group, removing it from any other
|
|
|
|
* group to which it may have previously belonged.
|
* 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 08:25:10 +04:00
|
|
|
*
|
2006-12-31 18:02:22 +03:00
|
|
|
* puts "Initial group is #{ThreadGroup::Default.list}"
|
|
|
|
* tg = ThreadGroup.new
|
|
|
|
* t1 = Thread.new { sleep }
|
|
|
|
* t2 = Thread.new { sleep }
|
|
|
|
* puts "t1 is #{t1}"
|
|
|
|
* puts "t2 is #{t2}"
|
|
|
|
* tg.add(t1)
|
|
|
|
* puts "Initial group now #{ThreadGroup::Default.list}"
|
|
|
|
* puts "tg group now #{tg.list}"
|
* 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 08:25:10 +04:00
|
|
|
*
|
2006-12-31 18:02:22 +03:00
|
|
|
* <em>produces:</em>
|
* 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 08:25:10 +04:00
|
|
|
*
|
2006-12-31 18:02:22 +03:00
|
|
|
* Initial group is #<Thread:0x401bdf4c>
|
|
|
|
* t1 is #<Thread:0x401b3c90>
|
|
|
|
* t2 is #<Thread:0x401b3c18>
|
|
|
|
* Initial group now #<Thread:0x401b3c18>#<Thread:0x401bdf4c>
|
|
|
|
* tg group now #<Thread:0x401b3c90>
|
|
|
|
*/
|
|
|
|
|
|
|
|
static VALUE
|
|
|
|
thgroup_add(VALUE group, VALUE 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 09:37:46 +03:00
|
|
|
rb_thread_t *th;
|
2006-12-31 18:02:22 +03:00
|
|
|
struct thgroup *data;
|
|
|
|
|
|
|
|
rb_secure(4);
|
|
|
|
GetThreadPtr(thread, th);
|
|
|
|
|
|
|
|
if (OBJ_FROZEN(group)) {
|
|
|
|
rb_raise(rb_eThreadError, "can't move to the frozen thread group");
|
|
|
|
}
|
2009-09-09 07:21:14 +04:00
|
|
|
TypedData_Get_Struct(group, struct thgroup, &thgroup_data_type, data);
|
2006-12-31 18:02:22 +03:00
|
|
|
if (data->enclosed) {
|
|
|
|
rb_raise(rb_eThreadError, "can't move to the enclosed thread group");
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!th->thgroup) {
|
|
|
|
return Qnil;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (OBJ_FROZEN(th->thgroup)) {
|
|
|
|
rb_raise(rb_eThreadError, "can't move from the frozen thread group");
|
|
|
|
}
|
2009-09-09 07:21:14 +04:00
|
|
|
TypedData_Get_Struct(th->thgroup, struct thgroup, &thgroup_data_type, data);
|
2006-12-31 18:02:22 +03:00
|
|
|
if (data->enclosed) {
|
|
|
|
rb_raise(rb_eThreadError,
|
|
|
|
"can't move from the enclosed thread group");
|
|
|
|
}
|
|
|
|
|
|
|
|
th->thgroup = group;
|
|
|
|
return group;
|
|
|
|
}
|
|
|
|
|
2007-05-03 17:04:42 +04:00
|
|
|
|
2006-12-31 18:02:22 +03:00
|
|
|
/*
|
2007-05-03 17:04:42 +04:00
|
|
|
* Document-class: Mutex
|
|
|
|
*
|
|
|
|
* Mutex implements a simple semaphore that can be used to coordinate access to
|
|
|
|
* shared data from multiple concurrent threads.
|
|
|
|
*
|
|
|
|
* Example:
|
|
|
|
*
|
|
|
|
* require 'thread'
|
|
|
|
* semaphore = Mutex.new
|
|
|
|
*
|
|
|
|
* a = Thread.new {
|
|
|
|
* semaphore.synchronize {
|
|
|
|
* # access shared resource
|
|
|
|
* }
|
|
|
|
* }
|
|
|
|
*
|
|
|
|
* b = Thread.new {
|
|
|
|
* semaphore.synchronize {
|
|
|
|
* # access shared resource
|
|
|
|
* }
|
|
|
|
* }
|
|
|
|
*
|
2006-12-31 18:02:22 +03:00
|
|
|
*/
|
|
|
|
|
2007-08-27 20:48:14 +04:00
|
|
|
#define GetMutexPtr(obj, tobj) \
|
2011-06-16 04:12:55 +04:00
|
|
|
TypedData_Get_Struct((obj), rb_mutex_t, &mutex_data_type, (tobj))
|
2006-12-31 18:02:22 +03:00
|
|
|
|
2011-06-16 04:12:55 +04:00
|
|
|
static const char *rb_mutex_unlock_th(rb_mutex_t *mutex, rb_thread_t volatile *th);
|
2008-06-12 17:01:38 +04:00
|
|
|
|
2009-09-09 07:21:14 +04:00
|
|
|
#define mutex_mark NULL
|
|
|
|
|
2006-12-31 18:02:22 +03:00
|
|
|
static void
|
|
|
|
mutex_free(void *ptr)
|
|
|
|
{
|
|
|
|
if (ptr) {
|
2011-06-16 04:12:55 +04:00
|
|
|
rb_mutex_t *mutex = ptr;
|
2008-06-12 17:01:38 +04:00
|
|
|
if (mutex->th) {
|
|
|
|
/* rb_warn("free locked mutex"); */
|
2011-06-16 04:12:55 +04:00
|
|
|
const char *err = rb_mutex_unlock_th(mutex, mutex->th);
|
2008-12-28 04:39:18 +03:00
|
|
|
if (err) rb_bug("%s", err);
|
2008-06-12 17:01:38 +04:00
|
|
|
}
|
2007-02-08 23:24:55 +03:00
|
|
|
native_mutex_destroy(&mutex->lock);
|
2007-08-27 20:48:14 +04:00
|
|
|
native_cond_destroy(&mutex->cond);
|
2006-12-31 18:02:22 +03:00
|
|
|
}
|
|
|
|
ruby_xfree(ptr);
|
|
|
|
}
|
|
|
|
|
2009-09-09 07:21:14 +04:00
|
|
|
static size_t
|
|
|
|
mutex_memsize(const void *ptr)
|
|
|
|
{
|
2011-06-16 04:12:55 +04:00
|
|
|
return ptr ? sizeof(rb_mutex_t) : 0;
|
2009-09-09 07:21:14 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
static const rb_data_type_t mutex_data_type = {
|
|
|
|
"mutex",
|
2010-07-18 11:31:54 +04:00
|
|
|
{mutex_mark, mutex_free, mutex_memsize,},
|
2009-09-09 07:21:14 +04:00
|
|
|
};
|
|
|
|
|
2011-06-09 18:45:56 +04:00
|
|
|
VALUE
|
|
|
|
rb_obj_is_mutex(VALUE obj)
|
|
|
|
{
|
|
|
|
if (rb_typeddata_is_kind_of(obj, &mutex_data_type)) {
|
|
|
|
return Qtrue;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
return Qfalse;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2006-12-31 18:02:22 +03:00
|
|
|
static VALUE
|
|
|
|
mutex_alloc(VALUE klass)
|
|
|
|
{
|
|
|
|
VALUE volatile obj;
|
2011-06-16 04:12:55 +04:00
|
|
|
rb_mutex_t *mutex;
|
2006-12-31 18:02:22 +03:00
|
|
|
|
2011-06-16 04:12:55 +04:00
|
|
|
obj = TypedData_Make_Struct(klass, rb_mutex_t, &mutex_data_type, mutex);
|
2006-12-31 18:02:22 +03:00
|
|
|
native_mutex_initialize(&mutex->lock);
|
2011-05-06 21:39:32 +04:00
|
|
|
native_cond_initialize(&mutex->cond, RB_CONDATTR_CLOCK_MONOTONIC);
|
2006-12-31 18:02:22 +03:00
|
|
|
return obj;
|
|
|
|
}
|
|
|
|
|
2007-05-03 17:04:42 +04:00
|
|
|
/*
|
|
|
|
* call-seq:
|
2010-05-18 01:07:33 +04:00
|
|
|
* Mutex.new -> mutex
|
2007-05-03 17:04:42 +04:00
|
|
|
*
|
|
|
|
* Creates a new Mutex
|
|
|
|
*/
|
2006-12-31 18:02:22 +03:00
|
|
|
static VALUE
|
|
|
|
mutex_initialize(VALUE self)
|
|
|
|
{
|
|
|
|
return self;
|
|
|
|
}
|
|
|
|
|
2007-05-03 17:04:42 +04:00
|
|
|
VALUE
|
|
|
|
rb_mutex_new(void)
|
|
|
|
{
|
|
|
|
return mutex_alloc(rb_cMutex);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* call-seq:
|
2010-05-18 01:07:33 +04:00
|
|
|
* mutex.locked? -> true or false
|
2007-05-03 17:04:42 +04:00
|
|
|
*
|
|
|
|
* Returns +true+ if this lock is currently held by some thread.
|
|
|
|
*/
|
2007-05-03 17:19:11 +04:00
|
|
|
VALUE
|
|
|
|
rb_mutex_locked_p(VALUE self)
|
2006-12-31 18:02:22 +03:00
|
|
|
{
|
2011-06-16 04:12:55 +04:00
|
|
|
rb_mutex_t *mutex;
|
2007-08-27 20:48:14 +04:00
|
|
|
GetMutexPtr(self, mutex);
|
2006-12-31 18:02:22 +03:00
|
|
|
return mutex->th ? Qtrue : Qfalse;
|
|
|
|
}
|
|
|
|
|
2008-06-12 17:01:38 +04:00
|
|
|
static void
|
|
|
|
mutex_locked(rb_thread_t *th, VALUE self)
|
|
|
|
{
|
2011-06-16 04:12:55 +04:00
|
|
|
rb_mutex_t *mutex;
|
2008-07-27 19:20:01 +04:00
|
|
|
GetMutexPtr(self, mutex);
|
|
|
|
|
2008-06-12 17:01:38 +04:00
|
|
|
if (th->keeping_mutexes) {
|
|
|
|
mutex->next_mutex = th->keeping_mutexes;
|
|
|
|
}
|
2008-07-27 19:20:01 +04:00
|
|
|
th->keeping_mutexes = mutex;
|
2008-06-12 17:01:38 +04:00
|
|
|
}
|
|
|
|
|
2007-05-03 17:04:42 +04:00
|
|
|
/*
|
|
|
|
* call-seq:
|
2010-05-18 01:07:33 +04:00
|
|
|
* mutex.try_lock -> true or false
|
2007-05-03 17:04:42 +04:00
|
|
|
*
|
|
|
|
* Attempts to obtain the lock and returns immediately. Returns +true+ if the
|
|
|
|
* lock was granted.
|
|
|
|
*/
|
2007-05-03 17:19:11 +04:00
|
|
|
VALUE
|
2007-08-27 20:48:14 +04:00
|
|
|
rb_mutex_trylock(VALUE self)
|
2006-12-31 18:02:22 +03:00
|
|
|
{
|
2011-06-16 04:12:55 +04:00
|
|
|
rb_mutex_t *mutex;
|
2007-08-27 20:48:14 +04:00
|
|
|
VALUE locked = Qfalse;
|
|
|
|
GetMutexPtr(self, mutex);
|
2006-12-31 18:02:22 +03:00
|
|
|
|
2007-08-27 20:48:14 +04:00
|
|
|
native_mutex_lock(&mutex->lock);
|
|
|
|
if (mutex->th == 0) {
|
2007-04-12 12:14:54 +04:00
|
|
|
mutex->th = GET_THREAD();
|
2007-08-27 20:48:14 +04:00
|
|
|
locked = Qtrue;
|
2008-06-12 17:01:38 +04:00
|
|
|
|
|
|
|
mutex_locked(GET_THREAD(), self);
|
2006-12-31 18:02:22 +03:00
|
|
|
}
|
2007-08-27 20:48:14 +04:00
|
|
|
native_mutex_unlock(&mutex->lock);
|
|
|
|
|
|
|
|
return locked;
|
|
|
|
}
|
|
|
|
|
2008-04-13 13:52:29 +04:00
|
|
|
static int
|
2012-11-20 05:21:19 +04:00
|
|
|
lock_func(rb_thread_t *th, rb_mutex_t *mutex, int timeout_ms)
|
2007-08-27 20:48:14 +04:00
|
|
|
{
|
2008-06-12 17:01:38 +04:00
|
|
|
int interrupted = 0;
|
2011-06-12 10:55:12 +04:00
|
|
|
int err = 0;
|
2008-04-13 13:52:29 +04:00
|
|
|
|
2011-06-12 10:55:12 +04:00
|
|
|
mutex->cond_waiting++;
|
2011-04-29 05:18:11 +04:00
|
|
|
for (;;) {
|
|
|
|
if (!mutex->th) {
|
2012-11-20 05:21:19 +04:00
|
|
|
mutex->th = th;
|
2011-04-29 05:18:11 +04:00
|
|
|
break;
|
|
|
|
}
|
2012-11-20 05:21:19 +04:00
|
|
|
if (RUBY_VM_INTERRUPTED(th)) {
|
2011-06-12 10:55:12 +04:00
|
|
|
interrupted = 1;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (err == ETIMEDOUT) {
|
|
|
|
interrupted = 2;
|
|
|
|
break;
|
|
|
|
}
|
2011-04-29 05:18:11 +04:00
|
|
|
|
2011-04-29 05:11:28 +04:00
|
|
|
if (timeout_ms) {
|
2011-05-06 21:39:32 +04:00
|
|
|
struct timespec timeout_rel;
|
|
|
|
struct timespec timeout;
|
|
|
|
|
|
|
|
timeout_rel.tv_sec = 0;
|
|
|
|
timeout_rel.tv_nsec = timeout_ms * 1000 * 1000;
|
|
|
|
timeout = native_cond_timeout(&mutex->cond, timeout_rel);
|
2011-06-12 10:55:12 +04:00
|
|
|
err = native_cond_timedwait(&mutex->cond, &mutex->lock, &timeout);
|
2011-04-29 05:11:28 +04:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
native_cond_wait(&mutex->cond, &mutex->lock);
|
2011-06-12 10:55:12 +04:00
|
|
|
err = 0;
|
2007-11-20 13:47:53 +03:00
|
|
|
}
|
2006-12-31 18:02:22 +03:00
|
|
|
}
|
2011-06-12 10:55:12 +04:00
|
|
|
mutex->cond_waiting--;
|
|
|
|
|
2008-04-13 13:52:29 +04:00
|
|
|
return interrupted;
|
2007-08-27 20:48:14 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2007-11-20 13:47:53 +03:00
|
|
|
lock_interrupt(void *ptr)
|
2007-08-27 20:48:14 +04:00
|
|
|
{
|
2011-06-16 04:12:55 +04:00
|
|
|
rb_mutex_t *mutex = (rb_mutex_t *)ptr;
|
2007-08-27 20:48:14 +04:00
|
|
|
native_mutex_lock(&mutex->lock);
|
2011-06-12 10:55:12 +04:00
|
|
|
if (mutex->cond_waiting > 0)
|
2007-08-27 20:48:14 +04:00
|
|
|
native_cond_broadcast(&mutex->cond);
|
|
|
|
native_mutex_unlock(&mutex->lock);
|
2006-12-31 18:02:22 +03:00
|
|
|
}
|
|
|
|
|
2012-09-09 10:27:02 +04:00
|
|
|
/*
|
|
|
|
* At maximum, only one thread can use cond_timedwait and watch deadlock
|
|
|
|
* periodically. Multiple polling thread (i.e. concurrent deadlock check)
|
|
|
|
* introduces new race conditions. [Bug #6278] [ruby-core:44275]
|
|
|
|
*/
|
2012-09-10 12:21:02 +04:00
|
|
|
static const rb_thread_t *patrol_thread = NULL;
|
2012-09-09 10:27:02 +04:00
|
|
|
|
2007-05-03 17:04:42 +04:00
|
|
|
/*
|
|
|
|
* call-seq:
|
2010-05-18 01:07:33 +04:00
|
|
|
* mutex.lock -> self
|
2007-05-03 17:04:42 +04:00
|
|
|
*
|
|
|
|
* Attempts to grab the lock and waits if it isn't available.
|
|
|
|
* Raises +ThreadError+ if +mutex+ was locked by the current thread.
|
|
|
|
*/
|
2007-05-03 17:19:11 +04:00
|
|
|
VALUE
|
|
|
|
rb_mutex_lock(VALUE self)
|
2006-12-31 18:02:22 +03:00
|
|
|
{
|
2012-11-28 09:40:54 +04:00
|
|
|
rb_thread_t *th = GET_THREAD();
|
2012-11-28 12:30:51 +04:00
|
|
|
rb_mutex_t *mutex;
|
|
|
|
GetMutexPtr(self, mutex);
|
2012-11-28 09:40:54 +04:00
|
|
|
|
|
|
|
/* When running trap handler */
|
2012-11-28 12:30:51 +04:00
|
|
|
if (!mutex->allow_trap && th->interrupt_mask & TRAP_INTERRUPT_MASK) {
|
2012-11-28 09:40:54 +04:00
|
|
|
rb_raise(rb_eThreadError, "can't be called from trap context");
|
|
|
|
}
|
2008-12-29 06:03:09 +03:00
|
|
|
|
2007-08-27 20:48:14 +04:00
|
|
|
if (rb_mutex_trylock(self) == Qfalse) {
|
2012-11-20 05:21:19 +04:00
|
|
|
if (mutex->th == GET_THREAD()) {
|
2008-12-29 06:03:09 +03:00
|
|
|
rb_raise(rb_eThreadError, "deadlock; recursive locking");
|
|
|
|
}
|
|
|
|
|
2012-11-20 05:21:19 +04:00
|
|
|
while (mutex->th != th) {
|
2008-04-13 13:52:29 +04:00
|
|
|
int interrupted;
|
2012-11-20 05:21:19 +04:00
|
|
|
enum rb_thread_status prev_status = th->status;
|
2011-04-29 05:11:28 +04:00
|
|
|
int timeout_ms = 0;
|
2008-06-19 18:18:46 +04:00
|
|
|
struct rb_unblock_callback oldubf;
|
2008-06-12 17:01:38 +04:00
|
|
|
|
2012-11-28 17:01:25 +04:00
|
|
|
set_unblock_function(th, lock_interrupt, mutex, &oldubf, FALSE);
|
2012-11-20 05:21:19 +04:00
|
|
|
th->status = THREAD_STOPPED_FOREVER;
|
|
|
|
th->locking_mutex = self;
|
2011-04-29 05:11:28 +04:00
|
|
|
|
2011-06-12 10:58:15 +04:00
|
|
|
native_mutex_lock(&mutex->lock);
|
2012-11-20 05:21:19 +04:00
|
|
|
th->vm->sleeper++;
|
2011-04-29 05:11:28 +04:00
|
|
|
/*
|
2011-06-12 10:58:15 +04:00
|
|
|
* Carefully! while some contended threads are in lock_func(),
|
2011-04-29 05:11:28 +04:00
|
|
|
* vm->sleepr is unstable value. we have to avoid both deadlock
|
|
|
|
* and busy loop.
|
|
|
|
*/
|
2012-11-20 05:21:19 +04:00
|
|
|
if ((vm_living_thread_num(th->vm) == th->vm->sleeper) &&
|
2012-09-09 10:27:02 +04:00
|
|
|
!patrol_thread) {
|
2011-04-29 05:11:28 +04:00
|
|
|
timeout_ms = 100;
|
2012-11-20 05:21:19 +04:00
|
|
|
patrol_thread = th;
|
2008-06-12 17:01:38 +04:00
|
|
|
}
|
2012-09-09 10:27:02 +04:00
|
|
|
|
2011-06-12 10:58:15 +04:00
|
|
|
GVL_UNLOCK_BEGIN();
|
2012-11-20 05:21:19 +04:00
|
|
|
interrupted = lock_func(th, mutex, timeout_ms);
|
2011-06-12 10:58:15 +04:00
|
|
|
native_mutex_unlock(&mutex->lock);
|
|
|
|
GVL_UNLOCK_END();
|
2008-04-13 13:52:29 +04:00
|
|
|
|
2012-11-20 05:21:19 +04:00
|
|
|
if (patrol_thread == th)
|
2012-09-09 10:27:02 +04:00
|
|
|
patrol_thread = NULL;
|
|
|
|
|
2012-11-20 05:21:19 +04:00
|
|
|
reset_unblock_function(th, &oldubf);
|
2008-06-12 17:01:38 +04:00
|
|
|
|
2012-11-20 05:21:19 +04:00
|
|
|
th->locking_mutex = Qfalse;
|
2008-06-19 18:18:46 +04:00
|
|
|
if (mutex->th && interrupted == 2) {
|
2012-11-20 05:21:19 +04:00
|
|
|
rb_check_deadlock(th->vm);
|
2008-06-12 17:01:38 +04:00
|
|
|
}
|
2012-11-20 05:21:19 +04:00
|
|
|
if (th->status == THREAD_STOPPED_FOREVER) {
|
|
|
|
th->status = prev_status;
|
2008-06-19 18:18:46 +04:00
|
|
|
}
|
2012-11-20 05:21:19 +04:00
|
|
|
th->vm->sleeper--;
|
2008-06-12 17:01:38 +04:00
|
|
|
|
2012-11-20 05:21:19 +04:00
|
|
|
if (mutex->th == th) mutex_locked(th, self);
|
2007-11-20 13:47:53 +03:00
|
|
|
|
2008-04-13 13:52:29 +04:00
|
|
|
if (interrupted) {
|
2012-11-20 05:21:19 +04:00
|
|
|
RUBY_VM_CHECK_INTS_BLOCKING(th);
|
2008-04-13 13:52:29 +04:00
|
|
|
}
|
2007-08-27 20:48:14 +04:00
|
|
|
}
|
2006-12-31 18:02:22 +03:00
|
|
|
}
|
|
|
|
return self;
|
|
|
|
}
|
|
|
|
|
2008-06-12 17:01:38 +04:00
|
|
|
static const char *
|
2011-06-16 04:12:55 +04:00
|
|
|
rb_mutex_unlock_th(rb_mutex_t *mutex, rb_thread_t volatile *th)
|
2006-12-31 18:02:22 +03:00
|
|
|
{
|
2008-05-31 13:28:20 +04:00
|
|
|
const char *err = NULL;
|
2006-12-31 18:02:22 +03:00
|
|
|
|
2007-08-27 20:48:14 +04:00
|
|
|
native_mutex_lock(&mutex->lock);
|
2008-01-10 19:12:32 +03:00
|
|
|
|
|
|
|
if (mutex->th == 0) {
|
|
|
|
err = "Attempt to unlock a mutex which is not locked";
|
2007-08-27 20:48:14 +04:00
|
|
|
}
|
2008-12-28 04:39:18 +03:00
|
|
|
else if (mutex->th != th) {
|
2008-01-10 19:12:32 +03:00
|
|
|
err = "Attempt to unlock a mutex which is locked by another thread";
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
mutex->th = 0;
|
2011-06-12 10:55:12 +04:00
|
|
|
if (mutex->cond_waiting > 0)
|
2008-01-10 19:12:32 +03:00
|
|
|
native_cond_signal(&mutex->cond);
|
|
|
|
}
|
|
|
|
|
2006-12-31 18:02:22 +03:00
|
|
|
native_mutex_unlock(&mutex->lock);
|
2007-08-27 20:48:14 +04:00
|
|
|
|
2008-06-12 17:01:38 +04:00
|
|
|
if (!err) {
|
2012-01-13 13:29:13 +04:00
|
|
|
rb_mutex_t *volatile *th_mutex = &th->keeping_mutexes;
|
|
|
|
while (*th_mutex != mutex) {
|
|
|
|
th_mutex = &(*th_mutex)->next_mutex;
|
2008-06-12 17:01:38 +04:00
|
|
|
}
|
2012-01-13 13:29:13 +04:00
|
|
|
*th_mutex = mutex->next_mutex;
|
2008-07-28 13:59:12 +04:00
|
|
|
mutex->next_mutex = NULL;
|
2008-06-12 17:01:38 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
return err;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* call-seq:
|
2010-05-18 01:07:33 +04:00
|
|
|
* mutex.unlock -> self
|
2008-06-12 17:01:38 +04:00
|
|
|
*
|
|
|
|
* Releases the lock.
|
|
|
|
* Raises +ThreadError+ if +mutex+ wasn't locked by the current thread.
|
|
|
|
*/
|
|
|
|
VALUE
|
|
|
|
rb_mutex_unlock(VALUE self)
|
|
|
|
{
|
|
|
|
const char *err;
|
2011-06-16 04:12:55 +04:00
|
|
|
rb_mutex_t *mutex;
|
2008-06-12 17:01:38 +04:00
|
|
|
GetMutexPtr(self, mutex);
|
|
|
|
|
2012-11-26 19:17:01 +04:00
|
|
|
/* When running trap handler */
|
2012-11-28 12:30:51 +04:00
|
|
|
if (!mutex->allow_trap && GET_THREAD()->interrupt_mask & TRAP_INTERRUPT_MASK) {
|
2012-11-26 19:17:01 +04:00
|
|
|
rb_raise(rb_eThreadError, "can't be called from trap context");
|
|
|
|
}
|
|
|
|
|
2011-06-16 04:12:55 +04:00
|
|
|
err = rb_mutex_unlock_th(mutex, GET_THREAD());
|
2008-12-10 19:30:40 +03:00
|
|
|
if (err) rb_raise(rb_eThreadError, "%s", err);
|
2008-01-10 19:12:32 +03:00
|
|
|
|
2006-12-31 18:02:22 +03:00
|
|
|
return self;
|
|
|
|
}
|
|
|
|
|
2009-11-18 11:48:24 +03:00
|
|
|
static void
|
2011-06-16 04:12:55 +04:00
|
|
|
rb_mutex_abandon_all(rb_mutex_t *mutexes)
|
2009-11-18 11:48:24 +03:00
|
|
|
{
|
2011-06-16 04:12:55 +04:00
|
|
|
rb_mutex_t *mutex;
|
2009-11-18 11:48:24 +03:00
|
|
|
|
|
|
|
while (mutexes) {
|
|
|
|
mutex = mutexes;
|
|
|
|
mutexes = mutex->next_mutex;
|
|
|
|
mutex->th = 0;
|
|
|
|
mutex->next_mutex = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-04-08 16:36:42 +04:00
|
|
|
static VALUE
|
|
|
|
rb_mutex_sleep_forever(VALUE time)
|
|
|
|
{
|
2012-11-28 18:40:00 +04:00
|
|
|
sleep_forever(GET_THREAD(), 1, 0); /* permit spurious check */
|
2008-04-08 16:36:42 +04:00
|
|
|
return Qnil;
|
|
|
|
}
|
|
|
|
|
|
|
|
static VALUE
|
|
|
|
rb_mutex_wait_for(VALUE time)
|
|
|
|
{
|
2012-11-28 18:40:00 +04:00
|
|
|
struct timeval *t = (struct timeval *)time;
|
|
|
|
sleep_timeval(GET_THREAD(), *t, 0); /* permit spurious check */
|
2008-04-08 16:36:42 +04:00
|
|
|
return Qnil;
|
|
|
|
}
|
|
|
|
|
2007-05-03 17:19:11 +04:00
|
|
|
VALUE
|
|
|
|
rb_mutex_sleep(VALUE self, VALUE timeout)
|
|
|
|
{
|
|
|
|
time_t beg, end;
|
|
|
|
struct timeval t;
|
|
|
|
|
|
|
|
if (!NIL_P(timeout)) {
|
|
|
|
t = rb_time_interval(timeout);
|
|
|
|
}
|
|
|
|
rb_mutex_unlock(self);
|
|
|
|
beg = time(0);
|
|
|
|
if (NIL_P(timeout)) {
|
2008-04-08 16:36:42 +04:00
|
|
|
rb_ensure(rb_mutex_sleep_forever, Qnil, rb_mutex_lock, self);
|
2007-05-03 17:19:11 +04:00
|
|
|
}
|
|
|
|
else {
|
2008-04-08 16:36:42 +04:00
|
|
|
rb_ensure(rb_mutex_wait_for, (VALUE)&t, rb_mutex_lock, self);
|
2007-05-03 17:19:11 +04:00
|
|
|
}
|
|
|
|
end = time(0) - beg;
|
|
|
|
return INT2FIX(end);
|
|
|
|
}
|
|
|
|
|
2007-05-03 17:04:42 +04:00
|
|
|
/*
|
|
|
|
* call-seq:
|
2010-05-18 01:07:33 +04:00
|
|
|
* mutex.sleep(timeout = nil) -> number
|
2007-05-03 17:04:42 +04:00
|
|
|
*
|
|
|
|
* Releases the lock and sleeps +timeout+ seconds if it is given and
|
* 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 08:25:10 +04:00
|
|
|
* non-nil or forever. Raises +ThreadError+ if +mutex+ wasn't locked by
|
2007-05-03 17:04:42 +04:00
|
|
|
* the current thread.
|
2012-11-28 18:40:00 +04:00
|
|
|
*
|
|
|
|
* Note that this method can wakeup without explicit Thread#wakeup call.
|
|
|
|
* For example, receiving signal and so on.
|
2007-05-03 17:04:42 +04:00
|
|
|
*/
|
2006-12-31 18:02:22 +03:00
|
|
|
static VALUE
|
|
|
|
mutex_sleep(int argc, VALUE *argv, VALUE self)
|
|
|
|
{
|
2007-05-03 17:19:11 +04:00
|
|
|
VALUE timeout;
|
2006-12-31 18:02:22 +03:00
|
|
|
|
2007-05-03 17:19:11 +04:00
|
|
|
rb_scan_args(argc, argv, "01", &timeout);
|
|
|
|
return rb_mutex_sleep(self, timeout);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* call-seq:
|
2010-05-18 01:07:33 +04:00
|
|
|
* mutex.synchronize { ... } -> result of the block
|
2007-05-03 17:19:11 +04:00
|
|
|
*
|
|
|
|
* Obtains a lock, runs the block, and releases the lock when the block
|
|
|
|
* completes. See the example under +Mutex+.
|
|
|
|
*/
|
|
|
|
|
|
|
|
VALUE
|
2008-11-03 19:56:49 +03:00
|
|
|
rb_mutex_synchronize(VALUE mutex, VALUE (*func)(VALUE arg), VALUE arg)
|
2007-05-03 17:19:11 +04:00
|
|
|
{
|
|
|
|
rb_mutex_lock(mutex);
|
|
|
|
return rb_ensure(func, arg, rb_mutex_unlock, mutex);
|
|
|
|
}
|
|
|
|
|
2012-11-19 14:22:53 +04:00
|
|
|
/*
|
|
|
|
* call-seq:
|
|
|
|
* mutex.synchronize { ... } -> result of the block
|
|
|
|
*
|
|
|
|
* Obtains a lock, runs the block, and releases the lock when the block
|
|
|
|
* completes. See the example under +Mutex+.
|
|
|
|
*/
|
|
|
|
static VALUE
|
|
|
|
rb_mutex_synchronize_m(VALUE self, VALUE args)
|
|
|
|
{
|
|
|
|
if (!rb_block_given_p()) {
|
|
|
|
rb_raise(rb_eThreadError, "must be called with a block");
|
|
|
|
}
|
|
|
|
|
|
|
|
return rb_mutex_synchronize(self, rb_yield, Qnil);
|
|
|
|
}
|
|
|
|
|
2012-11-28 12:30:51 +04:00
|
|
|
void rb_mutex_allow_trap(VALUE self, int val)
|
|
|
|
{
|
|
|
|
rb_mutex_t *m;
|
|
|
|
GetMutexPtr(self, m);
|
|
|
|
|
|
|
|
m->allow_trap = val;
|
|
|
|
}
|
|
|
|
|
2007-05-03 17:19:11 +04:00
|
|
|
/*
|
2012-07-05 11:00:29 +04:00
|
|
|
* Document-class: ThreadShield
|
2007-05-03 17:19:11 +04:00
|
|
|
*/
|
2009-09-09 07:21:14 +04:00
|
|
|
static void
|
2012-07-05 11:00:29 +04:00
|
|
|
thread_shield_mark(void *ptr)
|
2009-09-09 07:21:14 +04:00
|
|
|
{
|
|
|
|
rb_gc_mark((VALUE)ptr);
|
|
|
|
}
|
|
|
|
|
2012-07-05 11:00:29 +04:00
|
|
|
static const rb_data_type_t thread_shield_data_type = {
|
|
|
|
"thread_shield",
|
|
|
|
{thread_shield_mark, 0, 0,},
|
2009-09-09 07:21:14 +04:00
|
|
|
};
|
|
|
|
|
2007-05-03 17:19:11 +04:00
|
|
|
static VALUE
|
2012-07-05 11:00:29 +04:00
|
|
|
thread_shield_alloc(VALUE klass)
|
2007-05-03 17:19:11 +04:00
|
|
|
{
|
2012-07-05 11:00:29 +04:00
|
|
|
return TypedData_Wrap_Struct(klass, &thread_shield_data_type, (void *)mutex_alloc(0));
|
2007-05-03 17:19:11 +04:00
|
|
|
}
|
|
|
|
|
2012-07-05 11:00:29 +04:00
|
|
|
#define GetThreadShieldPtr(obj) ((VALUE)rb_check_typeddata((obj), &thread_shield_data_type))
|
|
|
|
#define THREAD_SHIELD_WAITING_MASK (FL_USER0|FL_USER1|FL_USER2|FL_USER3|FL_USER4|FL_USER5|FL_USER6|FL_USER7|FL_USER8|FL_USER9|FL_USER10|FL_USER11|FL_USER12|FL_USER13|FL_USER14|FL_USER15|FL_USER16|FL_USER17|FL_USER18|FL_USER19)
|
|
|
|
#define THREAD_SHIELD_WAITING_SHIFT (FL_USHIFT)
|
|
|
|
#define rb_thread_shield_waiting(b) (int)((RBASIC(b)->flags&THREAD_SHIELD_WAITING_MASK)>>THREAD_SHIELD_WAITING_SHIFT)
|
2012-07-06 07:24:41 +04:00
|
|
|
|
|
|
|
static inline void
|
|
|
|
rb_thread_shield_waiting_inc(VALUE b)
|
|
|
|
{
|
2012-07-06 07:46:39 +04:00
|
|
|
unsigned int w = rb_thread_shield_waiting(b);
|
2012-07-06 07:24:41 +04:00
|
|
|
w++;
|
2012-07-06 07:46:39 +04:00
|
|
|
if (w > (unsigned int)(THREAD_SHIELD_WAITING_MASK>>THREAD_SHIELD_WAITING_SHIFT))
|
2012-07-06 07:24:41 +04:00
|
|
|
rb_raise(rb_eRuntimeError, "waiting count overflow");
|
|
|
|
RBASIC(b)->flags &= ~THREAD_SHIELD_WAITING_MASK;
|
|
|
|
RBASIC(b)->flags |= ((VALUE)w << THREAD_SHIELD_WAITING_SHIFT);
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline void
|
|
|
|
rb_thread_shield_waiting_dec(VALUE b)
|
|
|
|
{
|
2012-07-06 07:46:39 +04:00
|
|
|
unsigned int w = rb_thread_shield_waiting(b);
|
2012-07-06 07:24:41 +04:00
|
|
|
if (!w) rb_raise(rb_eRuntimeError, "waiting count underflow");
|
|
|
|
w--;
|
|
|
|
RBASIC(b)->flags &= ~THREAD_SHIELD_WAITING_MASK;
|
|
|
|
RBASIC(b)->flags |= ((VALUE)w << THREAD_SHIELD_WAITING_SHIFT);
|
|
|
|
}
|
2009-09-09 07:21:14 +04:00
|
|
|
|
2007-05-03 17:19:11 +04:00
|
|
|
VALUE
|
2012-07-05 11:00:29 +04:00
|
|
|
rb_thread_shield_new(void)
|
2007-05-03 17:19:11 +04:00
|
|
|
{
|
2012-07-05 11:00:29 +04:00
|
|
|
VALUE thread_shield = thread_shield_alloc(rb_cThreadShield);
|
|
|
|
rb_mutex_lock((VALUE)DATA_PTR(thread_shield));
|
|
|
|
return thread_shield;
|
2008-11-11 21:28:47 +03:00
|
|
|
}
|
|
|
|
|
2011-12-14 05:20:11 +04:00
|
|
|
/*
|
2012-07-05 11:00:29 +04:00
|
|
|
* Wait a thread shield.
|
2011-12-14 05:20:11 +04:00
|
|
|
*
|
|
|
|
* Returns
|
2012-07-05 11:00:29 +04:00
|
|
|
* true: acquired the thread shield
|
|
|
|
* false: the thread shield was destroyed and no other threads waiting
|
|
|
|
* nil: the thread shield was destroyed but still in use
|
2011-12-14 05:20:11 +04:00
|
|
|
*/
|
2007-05-03 17:19:11 +04:00
|
|
|
VALUE
|
2012-07-05 11:00:29 +04:00
|
|
|
rb_thread_shield_wait(VALUE self)
|
2007-05-03 17:19:11 +04:00
|
|
|
{
|
2012-07-05 11:00:29 +04:00
|
|
|
VALUE mutex = GetThreadShieldPtr(self);
|
2011-06-16 04:12:55 +04:00
|
|
|
rb_mutex_t *m;
|
2007-05-03 17:19:11 +04:00
|
|
|
|
2008-11-13 00:08:47 +03:00
|
|
|
if (!mutex) return Qfalse;
|
2008-11-13 05:31:16 +03:00
|
|
|
GetMutexPtr(mutex, m);
|
2011-12-14 05:20:11 +04:00
|
|
|
if (m->th == GET_THREAD()) return Qnil;
|
2012-07-05 11:00:29 +04:00
|
|
|
rb_thread_shield_waiting_inc(self);
|
2008-11-13 00:08:47 +03:00
|
|
|
rb_mutex_lock(mutex);
|
2012-07-05 11:00:29 +04:00
|
|
|
rb_thread_shield_waiting_dec(self);
|
2008-11-13 00:08:47 +03:00
|
|
|
if (DATA_PTR(self)) return Qtrue;
|
|
|
|
rb_mutex_unlock(mutex);
|
2012-07-05 11:00:29 +04:00
|
|
|
return rb_thread_shield_waiting(self) > 0 ? Qnil : Qfalse;
|
2008-11-11 21:28:47 +03:00
|
|
|
}
|
|
|
|
|
2011-12-14 07:20:02 +04:00
|
|
|
/*
|
2012-07-05 18:10:48 +04:00
|
|
|
* Release a thread shield, and return true if it has waiting threads.
|
2011-12-14 07:20:02 +04:00
|
|
|
*/
|
2008-11-11 21:28:47 +03:00
|
|
|
VALUE
|
2012-07-05 11:00:29 +04:00
|
|
|
rb_thread_shield_release(VALUE self)
|
2008-11-11 21:28:47 +03:00
|
|
|
{
|
2012-07-05 11:00:29 +04:00
|
|
|
VALUE mutex = GetThreadShieldPtr(self);
|
2011-12-14 07:20:02 +04:00
|
|
|
rb_mutex_unlock(mutex);
|
2012-07-05 11:00:29 +04:00
|
|
|
return rb_thread_shield_waiting(self) > 0 ? Qtrue : Qfalse;
|
2008-11-11 21:28:47 +03:00
|
|
|
}
|
|
|
|
|
2011-12-14 07:20:02 +04:00
|
|
|
/*
|
2012-07-05 18:10:48 +04:00
|
|
|
* Release and destroy a thread shield, and return true if it has waiting threads.
|
2011-12-14 07:20:02 +04:00
|
|
|
*/
|
2008-11-11 21:28:47 +03:00
|
|
|
VALUE
|
2012-07-05 11:00:29 +04:00
|
|
|
rb_thread_shield_destroy(VALUE self)
|
2008-11-11 21:28:47 +03:00
|
|
|
{
|
2012-07-05 11:00:29 +04:00
|
|
|
VALUE mutex = GetThreadShieldPtr(self);
|
2008-11-13 00:08:47 +03:00
|
|
|
DATA_PTR(self) = 0;
|
2011-12-14 07:20:02 +04:00
|
|
|
rb_mutex_unlock(mutex);
|
2012-07-05 11:00:29 +04:00
|
|
|
return rb_thread_shield_waiting(self) > 0 ? Qtrue : Qfalse;
|
2011-12-13 11:13:31 +04:00
|
|
|
}
|
|
|
|
|
2007-02-05 15:21:01 +03:00
|
|
|
/* variables for recursive traversals */
|
|
|
|
static ID recursive_key;
|
|
|
|
|
2009-09-13 08:45:54 +04:00
|
|
|
/*
|
|
|
|
* Returns the current "recursive list" used to detect recursion.
|
|
|
|
* This list is a hash table, unique for the current thread and for
|
|
|
|
* the current __callee__.
|
|
|
|
*/
|
2007-02-05 15:21:01 +03:00
|
|
|
|
2007-12-07 06:22:28 +03:00
|
|
|
static VALUE
|
2009-09-13 09:09:15 +04:00
|
|
|
recursive_list_access(void)
|
|
|
|
{
|
2009-09-13 08:45:54 +04:00
|
|
|
volatile VALUE hash = rb_thread_local_aref(rb_thread_current(), recursive_key);
|
|
|
|
VALUE sym = ID2SYM(rb_frame_this_func());
|
|
|
|
VALUE list;
|
2011-09-29 15:07:45 +04:00
|
|
|
if (NIL_P(hash) || !RB_TYPE_P(hash, T_HASH)) {
|
2007-02-05 15:21:01 +03:00
|
|
|
hash = rb_hash_new();
|
2009-08-04 00:45:52 +04:00
|
|
|
OBJ_UNTRUST(hash);
|
2007-02-05 15:21:01 +03:00
|
|
|
rb_thread_local_aset(rb_thread_current(), recursive_key, hash);
|
|
|
|
list = Qnil;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
list = rb_hash_aref(hash, sym);
|
|
|
|
}
|
2011-09-29 15:07:45 +04:00
|
|
|
if (NIL_P(list) || !RB_TYPE_P(list, T_HASH)) {
|
2007-12-07 06:22:28 +03:00
|
|
|
list = rb_hash_new();
|
2009-08-03 13:15:09 +04:00
|
|
|
OBJ_UNTRUST(list);
|
2007-02-05 15:21:01 +03:00
|
|
|
rb_hash_aset(hash, sym, list);
|
|
|
|
}
|
2009-09-13 08:45:54 +04:00
|
|
|
return list;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Returns Qtrue iff obj_id (or the pair <obj, paired_obj>) is already
|
|
|
|
* in the recursion list.
|
|
|
|
* Assumes the recursion list is valid.
|
|
|
|
*/
|
|
|
|
|
|
|
|
static VALUE
|
|
|
|
recursive_check(VALUE list, VALUE obj_id, VALUE paired_obj_id)
|
|
|
|
{
|
|
|
|
VALUE pair_list = rb_hash_lookup2(list, obj_id, Qundef);
|
|
|
|
if (pair_list == Qundef)
|
|
|
|
return Qfalse;
|
|
|
|
if (paired_obj_id) {
|
2011-09-29 15:07:45 +04:00
|
|
|
if (!RB_TYPE_P(pair_list, T_HASH)) {
|
2009-09-13 08:45:54 +04:00
|
|
|
if (pair_list != paired_obj_id)
|
|
|
|
return Qfalse;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
if (NIL_P(rb_hash_lookup(pair_list, paired_obj_id)))
|
|
|
|
return Qfalse;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return Qtrue;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Pushes obj_id (or the pair <obj_id, paired_obj_id>) in the recursion list.
|
|
|
|
* For a single obj_id, it sets list[obj_id] to Qtrue.
|
|
|
|
* For a pair, it sets list[obj_id] to paired_obj_id if possible,
|
|
|
|
* otherwise list[obj_id] becomes a hash like:
|
|
|
|
* {paired_obj_id_1 => true, paired_obj_id_2 => true, ... }
|
|
|
|
* Assumes the recursion list is valid.
|
|
|
|
*/
|
|
|
|
|
|
|
|
static void
|
|
|
|
recursive_push(VALUE list, VALUE obj, VALUE paired_obj)
|
|
|
|
{
|
|
|
|
VALUE pair_list;
|
|
|
|
|
2009-05-24 17:48:23 +04:00
|
|
|
if (!paired_obj) {
|
|
|
|
rb_hash_aset(list, obj, Qtrue);
|
|
|
|
}
|
|
|
|
else if ((pair_list = rb_hash_lookup2(list, obj, Qundef)) == Qundef) {
|
|
|
|
rb_hash_aset(list, obj, paired_obj);
|
|
|
|
}
|
|
|
|
else {
|
2011-09-29 15:07:45 +04:00
|
|
|
if (!RB_TYPE_P(pair_list, T_HASH)){
|
2009-05-24 17:48:23 +04:00
|
|
|
VALUE other_paired_obj = pair_list;
|
|
|
|
pair_list = rb_hash_new();
|
2009-08-03 13:15:09 +04:00
|
|
|
OBJ_UNTRUST(pair_list);
|
2009-05-24 17:48:23 +04:00
|
|
|
rb_hash_aset(pair_list, other_paired_obj, Qtrue);
|
|
|
|
rb_hash_aset(list, obj, pair_list);
|
|
|
|
}
|
|
|
|
rb_hash_aset(pair_list, paired_obj, Qtrue);
|
|
|
|
}
|
2007-02-05 15:21:01 +03:00
|
|
|
}
|
|
|
|
|
2009-09-13 08:45:54 +04:00
|
|
|
/*
|
|
|
|
* Pops obj_id (or the pair <obj_id, paired_obj_id>) from the recursion list.
|
|
|
|
* For a pair, if list[obj_id] is a hash, then paired_obj_id is
|
|
|
|
* removed from the hash and no attempt is made to simplify
|
|
|
|
* list[obj_id] from {only_one_paired_id => true} to only_one_paired_id
|
|
|
|
* Assumes the recursion list is valid.
|
|
|
|
*/
|
|
|
|
|
2007-02-05 15:21:01 +03:00
|
|
|
static void
|
2009-09-13 08:45:54 +04:00
|
|
|
recursive_pop(VALUE list, VALUE obj, VALUE paired_obj)
|
2007-02-05 15:21:01 +03:00
|
|
|
{
|
2009-05-24 17:48:23 +04:00
|
|
|
if (paired_obj) {
|
2009-09-13 08:45:54 +04:00
|
|
|
VALUE pair_list = rb_hash_lookup2(list, obj, Qundef);
|
2009-05-24 17:48:23 +04:00
|
|
|
if (pair_list == Qundef) {
|
2009-09-13 08:45:54 +04:00
|
|
|
VALUE symname = rb_inspect(ID2SYM(rb_frame_this_func()));
|
|
|
|
VALUE thrname = rb_inspect(rb_thread_current());
|
2009-05-24 17:48:23 +04:00
|
|
|
rb_raise(rb_eTypeError, "invalid inspect_tbl pair_list for %s in %s",
|
|
|
|
StringValuePtr(symname), StringValuePtr(thrname));
|
|
|
|
}
|
2011-09-29 15:07:45 +04:00
|
|
|
if (RB_TYPE_P(pair_list, T_HASH)) {
|
2009-05-24 17:48:23 +04:00
|
|
|
rb_hash_delete(pair_list, paired_obj);
|
|
|
|
if (!RHASH_EMPTY_P(pair_list)) {
|
|
|
|
return; /* keep hash until is empty */
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2007-12-07 06:22:28 +03:00
|
|
|
rb_hash_delete(list, obj);
|
2007-02-05 15:21:01 +03:00
|
|
|
}
|
|
|
|
|
2009-09-16 01:30:50 +04:00
|
|
|
struct exec_recursive_params {
|
|
|
|
VALUE (*func) (VALUE, VALUE, int);
|
|
|
|
VALUE list;
|
|
|
|
VALUE obj;
|
|
|
|
VALUE objid;
|
|
|
|
VALUE pairid;
|
|
|
|
VALUE arg;
|
|
|
|
};
|
|
|
|
|
|
|
|
static VALUE
|
|
|
|
exec_recursive_i(VALUE tag, struct exec_recursive_params *p)
|
|
|
|
{
|
|
|
|
VALUE result = Qundef;
|
|
|
|
int state;
|
|
|
|
|
|
|
|
recursive_push(p->list, p->objid, p->pairid);
|
|
|
|
PUSH_TAG();
|
|
|
|
if ((state = EXEC_TAG()) == 0) {
|
* compile.c, cont.c, gc.c, insns.def, iseq.c, iseq.h, process.c,
thread.c, vm.c, vm_core.h, vm_dump.c, vm_eval.c,
vm_insnhelper.c, vm_method.c, template/insns_info.inc.tmpl,
tool/instruction.rb: fixed types.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@25030 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2009-09-22 00:58:26 +04:00
|
|
|
result = (*p->func)(p->obj, p->arg, FALSE);
|
2009-09-16 01:30:50 +04:00
|
|
|
}
|
|
|
|
POP_TAG();
|
|
|
|
recursive_pop(p->list, p->objid, p->pairid);
|
|
|
|
if (state)
|
|
|
|
JUMP_TAG(state);
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2009-09-13 08:45:54 +04:00
|
|
|
/*
|
|
|
|
* Calls func(obj, arg, recursive), where recursive is non-zero if the
|
|
|
|
* current method is called recursively on obj, or on the pair <obj, pairid>
|
2009-09-16 01:30:50 +04:00
|
|
|
* If outer is 0, then the innermost func will be called with recursive set
|
|
|
|
* to Qtrue, otherwise the outermost func will be called. In the latter case,
|
|
|
|
* all inner func are short-circuited by throw.
|
|
|
|
* Implementation details: the value thrown is the recursive list which is
|
|
|
|
* proper to the current method and unlikely to be catched anywhere else.
|
|
|
|
* list[recursive_key] is used as a flag for the outermost call.
|
2009-09-13 08:45:54 +04:00
|
|
|
*/
|
|
|
|
|
2009-05-24 17:48:23 +04:00
|
|
|
static VALUE
|
2009-09-16 01:30:50 +04:00
|
|
|
exec_recursive(VALUE (*func) (VALUE, VALUE, int), VALUE obj, VALUE pairid, VALUE arg, int outer)
|
2007-02-05 15:21:01 +03:00
|
|
|
{
|
2011-02-20 10:26:48 +03:00
|
|
|
VALUE result = Qundef;
|
2009-09-16 01:30:50 +04:00
|
|
|
struct exec_recursive_params p;
|
|
|
|
int outermost;
|
|
|
|
p.list = recursive_list_access();
|
|
|
|
p.objid = rb_obj_id(obj);
|
2011-02-20 10:26:48 +03:00
|
|
|
p.obj = obj;
|
|
|
|
p.pairid = pairid;
|
|
|
|
p.arg = arg;
|
2009-09-16 01:30:50 +04:00
|
|
|
outermost = outer && !recursive_check(p.list, ID2SYM(recursive_key), 0);
|
2007-12-07 06:22:28 +03:00
|
|
|
|
2009-09-16 01:30:50 +04:00
|
|
|
if (recursive_check(p.list, p.objid, pairid)) {
|
|
|
|
if (outer && !outermost) {
|
|
|
|
rb_throw_obj(p.list, p.list);
|
|
|
|
}
|
* compile.c, cont.c, gc.c, insns.def, iseq.c, iseq.h, process.c,
thread.c, vm.c, vm_core.h, vm_dump.c, vm_eval.c,
vm_insnhelper.c, vm_method.c, template/insns_info.inc.tmpl,
tool/instruction.rb: fixed types.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@25030 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2009-09-22 00:58:26 +04:00
|
|
|
return (*func)(obj, arg, TRUE);
|
2007-02-05 15:21:01 +03:00
|
|
|
}
|
|
|
|
else {
|
2009-09-16 01:30:50 +04:00
|
|
|
p.func = func;
|
|
|
|
|
|
|
|
if (outermost) {
|
|
|
|
recursive_push(p.list, ID2SYM(recursive_key), 0);
|
|
|
|
result = rb_catch_obj(p.list, exec_recursive_i, (VALUE)&p);
|
|
|
|
recursive_pop(p.list, ID2SYM(recursive_key), 0);
|
|
|
|
if (result == p.list) {
|
* compile.c, cont.c, gc.c, insns.def, iseq.c, iseq.h, process.c,
thread.c, vm.c, vm_core.h, vm_dump.c, vm_eval.c,
vm_insnhelper.c, vm_method.c, template/insns_info.inc.tmpl,
tool/instruction.rb: fixed types.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@25030 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2009-09-22 00:58:26 +04:00
|
|
|
result = (*func)(obj, arg, TRUE);
|
2009-09-16 01:30:50 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
result = exec_recursive_i(0, &p);
|
2007-02-05 15:21:01 +03:00
|
|
|
}
|
|
|
|
}
|
2011-02-20 10:26:48 +03:00
|
|
|
*(volatile struct exec_recursive_params *)&p;
|
|
|
|
return result;
|
2007-02-05 15:21:01 +03:00
|
|
|
}
|
|
|
|
|
2009-09-13 08:45:54 +04:00
|
|
|
/*
|
|
|
|
* Calls func(obj, arg, recursive), where recursive is non-zero if the
|
|
|
|
* current method is called recursively on obj
|
|
|
|
*/
|
|
|
|
|
2009-05-24 17:48:23 +04:00
|
|
|
VALUE
|
|
|
|
rb_exec_recursive(VALUE (*func) (VALUE, VALUE, int), VALUE obj, VALUE arg)
|
|
|
|
{
|
2009-09-16 01:30:50 +04:00
|
|
|
return exec_recursive(func, obj, 0, arg, 0);
|
2009-05-24 17:48:23 +04:00
|
|
|
}
|
|
|
|
|
2009-09-13 08:45:54 +04:00
|
|
|
/*
|
|
|
|
* Calls func(obj, arg, recursive), where recursive is non-zero if the
|
2009-09-16 01:30:50 +04:00
|
|
|
* current method is called recursively on the ordered pair <obj, paired_obj>
|
2009-09-13 08:45:54 +04:00
|
|
|
*/
|
|
|
|
|
2009-05-24 17:48:23 +04:00
|
|
|
VALUE
|
|
|
|
rb_exec_recursive_paired(VALUE (*func) (VALUE, VALUE, int), VALUE obj, VALUE paired_obj, VALUE arg)
|
|
|
|
{
|
2009-09-16 01:30:50 +04:00
|
|
|
return exec_recursive(func, obj, rb_obj_id(paired_obj), arg, 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* If recursion is detected on the current method and obj, the outermost
|
|
|
|
* func will be called with (obj, arg, Qtrue). All inner func will be
|
|
|
|
* short-circuited using throw.
|
|
|
|
*/
|
|
|
|
|
|
|
|
VALUE
|
|
|
|
rb_exec_recursive_outer(VALUE (*func) (VALUE, VALUE, int), VALUE obj, VALUE arg)
|
|
|
|
{
|
|
|
|
return exec_recursive(func, obj, 0, arg, 1);
|
2009-05-24 17:48:23 +04:00
|
|
|
}
|
|
|
|
|
2009-09-17 08:51:33 +04:00
|
|
|
/*
|
|
|
|
* call-seq:
|
2010-05-18 01:07:33 +04:00
|
|
|
* thr.backtrace -> array
|
2009-09-17 08:51:33 +04:00
|
|
|
*
|
|
|
|
* Returns the current back trace of the _thr_.
|
|
|
|
*/
|
|
|
|
|
2009-06-14 09:59:23 +04:00
|
|
|
static VALUE
|
2012-11-19 10:07:06 +04:00
|
|
|
rb_thread_backtrace_m(int argc, VALUE *argv, VALUE thval)
|
2009-06-14 09:59:23 +04:00
|
|
|
{
|
2012-11-19 10:07:06 +04:00
|
|
|
return vm_thread_backtrace(argc, argv, thval);
|
|
|
|
}
|
|
|
|
|
|
|
|
static VALUE
|
|
|
|
rb_thread_backtrace_locations_m(int argc, VALUE *argv, VALUE thval)
|
|
|
|
{
|
|
|
|
return vm_thread_backtrace_locations(argc, argv, thval);
|
2009-06-14 09:59:23 +04:00
|
|
|
}
|
|
|
|
|
2010-05-08 08:50:09 +04:00
|
|
|
/*
|
|
|
|
* Document-class: ThreadError
|
|
|
|
*
|
|
|
|
* Raised when an invalid operation is attempted on a thread.
|
|
|
|
*
|
|
|
|
* For example, when no other thread has been started:
|
|
|
|
*
|
|
|
|
* Thread.stop
|
|
|
|
*
|
|
|
|
* <em>raises the exception:</em>
|
|
|
|
*
|
|
|
|
* ThreadError: stopping only thread
|
|
|
|
*/
|
|
|
|
|
2007-02-05 15:21:01 +03:00
|
|
|
/*
|
|
|
|
* +Thread+ encapsulates the behavior of a thread of
|
|
|
|
* execution, including the main thread of the Ruby script.
|
* 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 08:25:10 +04:00
|
|
|
*
|
2007-02-05 15:21:01 +03:00
|
|
|
* In the descriptions of the methods in this class, the parameter _sym_
|
|
|
|
* refers to a symbol, which is either a quoted string or a
|
|
|
|
* +Symbol+ (such as <code>:name</code>).
|
|
|
|
*/
|
|
|
|
|
2006-12-31 18:02:22 +03:00
|
|
|
void
|
2007-02-05 15:21:01 +03:00
|
|
|
Init_Thread(void)
|
2006-12-31 18:02:22 +03:00
|
|
|
{
|
2008-06-09 13:25:32 +04:00
|
|
|
#undef rb_intern
|
2008-08-16 04:20:31 +04:00
|
|
|
#define rb_intern(str) rb_intern_const(str)
|
2008-06-09 13:25:32 +04:00
|
|
|
|
2006-12-31 18:02:22 +03:00
|
|
|
VALUE cThGroup;
|
2010-11-27 23:15:59 +03:00
|
|
|
rb_thread_t *th = GET_THREAD();
|
2006-12-31 18:02:22 +03:00
|
|
|
|
2007-12-05 10:18:52 +03:00
|
|
|
rb_define_singleton_method(rb_cThread, "new", thread_s_new, -1);
|
|
|
|
rb_define_singleton_method(rb_cThread, "start", thread_start, -2);
|
|
|
|
rb_define_singleton_method(rb_cThread, "fork", thread_start, -2);
|
2007-02-05 15:21:01 +03:00
|
|
|
rb_define_singleton_method(rb_cThread, "main", rb_thread_s_main, 0);
|
|
|
|
rb_define_singleton_method(rb_cThread, "current", thread_s_current, 0);
|
|
|
|
rb_define_singleton_method(rb_cThread, "stop", rb_thread_stop, 0);
|
|
|
|
rb_define_singleton_method(rb_cThread, "kill", rb_thread_s_kill, 1);
|
|
|
|
rb_define_singleton_method(rb_cThread, "exit", rb_thread_exit, 0);
|
|
|
|
rb_define_singleton_method(rb_cThread, "pass", thread_s_pass, 0);
|
|
|
|
rb_define_singleton_method(rb_cThread, "list", rb_thread_list, 0);
|
|
|
|
rb_define_singleton_method(rb_cThread, "abort_on_exception", rb_thread_s_abort_exc, 0);
|
|
|
|
rb_define_singleton_method(rb_cThread, "abort_on_exception=", rb_thread_s_abort_exc_set, 1);
|
2007-02-24 12:39:25 +03:00
|
|
|
#if THREAD_DEBUG < 0
|
|
|
|
rb_define_singleton_method(rb_cThread, "DEBUG", rb_thread_s_debug, 0);
|
|
|
|
rb_define_singleton_method(rb_cThread, "DEBUG=", rb_thread_s_debug_set, 1);
|
|
|
|
#endif
|
2012-07-19 18:19:40 +04:00
|
|
|
rb_define_singleton_method(rb_cThread, "control_interrupt", rb_thread_s_control_interrupt, 1);
|
|
|
|
rb_define_singleton_method(rb_cThread, "check_interrupt", rb_thread_s_check_interrupt, 1);
|
2007-02-05 15:21:01 +03:00
|
|
|
|
2007-12-05 10:18:52 +03:00
|
|
|
rb_define_method(rb_cThread, "initialize", thread_initialize, -2);
|
2007-02-05 15:21:01 +03:00
|
|
|
rb_define_method(rb_cThread, "raise", thread_raise_m, -1);
|
|
|
|
rb_define_method(rb_cThread, "join", thread_join_m, -1);
|
|
|
|
rb_define_method(rb_cThread, "value", thread_value, 0);
|
|
|
|
rb_define_method(rb_cThread, "kill", rb_thread_kill, 0);
|
|
|
|
rb_define_method(rb_cThread, "terminate", rb_thread_kill, 0);
|
|
|
|
rb_define_method(rb_cThread, "exit", rb_thread_kill, 0);
|
|
|
|
rb_define_method(rb_cThread, "run", rb_thread_run, 0);
|
|
|
|
rb_define_method(rb_cThread, "wakeup", rb_thread_wakeup, 0);
|
|
|
|
rb_define_method(rb_cThread, "[]", rb_thread_aref, 1);
|
|
|
|
rb_define_method(rb_cThread, "[]=", rb_thread_aset, 2);
|
|
|
|
rb_define_method(rb_cThread, "key?", rb_thread_key_p, 1);
|
|
|
|
rb_define_method(rb_cThread, "keys", rb_thread_keys, 0);
|
|
|
|
rb_define_method(rb_cThread, "priority", rb_thread_priority, 0);
|
|
|
|
rb_define_method(rb_cThread, "priority=", rb_thread_priority_set, 1);
|
|
|
|
rb_define_method(rb_cThread, "status", rb_thread_status, 0);
|
2012-10-29 21:22:36 +04:00
|
|
|
rb_define_method(rb_cThread, "thread_variable_get", rb_thread_variable_get, 1);
|
|
|
|
rb_define_method(rb_cThread, "thread_variable_set", rb_thread_variable_set, 2);
|
|
|
|
rb_define_method(rb_cThread, "thread_variables", rb_thread_variables, 0);
|
|
|
|
rb_define_method(rb_cThread, "thread_variable?", rb_thread_variable_p, 1);
|
2007-02-05 15:21:01 +03:00
|
|
|
rb_define_method(rb_cThread, "alive?", rb_thread_alive_p, 0);
|
|
|
|
rb_define_method(rb_cThread, "stop?", rb_thread_stop_p, 0);
|
|
|
|
rb_define_method(rb_cThread, "abort_on_exception", rb_thread_abort_exc, 0);
|
|
|
|
rb_define_method(rb_cThread, "abort_on_exception=", rb_thread_abort_exc_set, 1);
|
|
|
|
rb_define_method(rb_cThread, "safe_level", rb_thread_safe_level, 0);
|
|
|
|
rb_define_method(rb_cThread, "group", rb_thread_group, 0);
|
2012-11-19 10:07:06 +04:00
|
|
|
rb_define_method(rb_cThread, "backtrace", rb_thread_backtrace_m, -1);
|
|
|
|
rb_define_method(rb_cThread, "backtrace_locations", rb_thread_backtrace_locations_m, -1);
|
2007-02-05 15:21:01 +03:00
|
|
|
|
|
|
|
rb_define_method(rb_cThread, "inspect", rb_thread_inspect, 0);
|
2006-12-31 18:02:22 +03:00
|
|
|
|
2011-02-12 08:44:23 +03:00
|
|
|
closed_stream_error = rb_exc_new2(rb_eIOError, "stream closed");
|
|
|
|
OBJ_TAINT(closed_stream_error);
|
|
|
|
OBJ_FREEZE(closed_stream_error);
|
|
|
|
|
2006-12-31 18:02:22 +03:00
|
|
|
cThGroup = rb_define_class("ThreadGroup", rb_cObject);
|
|
|
|
rb_define_alloc_func(cThGroup, thgroup_s_alloc);
|
|
|
|
rb_define_method(cThGroup, "list", thgroup_list, 0);
|
|
|
|
rb_define_method(cThGroup, "enclose", thgroup_enclose, 0);
|
|
|
|
rb_define_method(cThGroup, "enclosed?", thgroup_enclosed_p, 0);
|
|
|
|
rb_define_method(cThGroup, "add", thgroup_add, 1);
|
2007-02-05 15:21:01 +03:00
|
|
|
|
|
|
|
{
|
|
|
|
th->thgroup = th->vm->thgroup_default = rb_obj_alloc(cThGroup);
|
|
|
|
rb_define_const(cThGroup, "Default", th->thgroup);
|
|
|
|
}
|
2006-12-31 18:02:22 +03:00
|
|
|
|
2007-05-03 17:19:11 +04:00
|
|
|
rb_cMutex = rb_define_class("Mutex", rb_cObject);
|
|
|
|
rb_define_alloc_func(rb_cMutex, mutex_alloc);
|
|
|
|
rb_define_method(rb_cMutex, "initialize", mutex_initialize, 0);
|
|
|
|
rb_define_method(rb_cMutex, "locked?", rb_mutex_locked_p, 0);
|
2007-08-27 20:48:14 +04:00
|
|
|
rb_define_method(rb_cMutex, "try_lock", rb_mutex_trylock, 0);
|
2007-05-03 17:19:11 +04:00
|
|
|
rb_define_method(rb_cMutex, "lock", rb_mutex_lock, 0);
|
|
|
|
rb_define_method(rb_cMutex, "unlock", rb_mutex_unlock, 0);
|
|
|
|
rb_define_method(rb_cMutex, "sleep", mutex_sleep, -1);
|
2012-11-19 14:22:53 +04:00
|
|
|
rb_define_method(rb_cMutex, "synchronize", rb_mutex_synchronize_m, 0);
|
* 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 08:25:46 +04:00
|
|
|
|
2007-02-05 15:21:01 +03:00
|
|
|
recursive_key = rb_intern("__recursive_key__");
|
|
|
|
rb_eThreadError = rb_define_class("ThreadError", rb_eStandardError);
|
2007-02-07 16:44:11 +03:00
|
|
|
|
2007-04-19 14:37:08 +04:00
|
|
|
/* init thread core */
|
2006-12-31 18:02:22 +03:00
|
|
|
{
|
|
|
|
/* main thread setting */
|
|
|
|
{
|
2009-01-12 04:43:23 +03:00
|
|
|
/* acquire global vm lock */
|
2010-11-27 23:15:59 +03:00
|
|
|
gvl_init(th->vm);
|
|
|
|
gvl_acquire(th->vm, th);
|
|
|
|
native_mutex_initialize(&th->interrupt_lock);
|
2012-07-18 09:46:40 +04:00
|
|
|
|
2012-07-25 20:40:04 +04:00
|
|
|
th->async_errinfo_queue = rb_ary_tmp_new(0);
|
2012-07-18 09:46:40 +04:00
|
|
|
th->async_errinfo_queue_checked = 0;
|
2012-07-25 20:40:04 +04:00
|
|
|
th->async_errinfo_mask_stack = rb_ary_tmp_new(0);
|
2012-11-26 12:05:49 +04:00
|
|
|
|
2012-11-26 14:57:39 +04:00
|
|
|
th->interrupt_mask = 0;
|
2006-12-31 18:02:22 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
rb_thread_create_timer_thread();
|
2008-06-01 23:55:25 +04:00
|
|
|
|
2011-05-21 23:12:10 +04:00
|
|
|
/* suppress warnings on cygwin, mingw and mswin.*/
|
2008-06-01 23:55:25 +04:00
|
|
|
(void)native_mutex_trylock;
|
2006-12-31 18:02:22 +03:00
|
|
|
}
|
|
|
|
|
2007-07-22 11:07:12 +04:00
|
|
|
int
|
2007-12-25 10:28:51 +03:00
|
|
|
ruby_native_thread_p(void)
|
2007-02-08 14:51:40 +03:00
|
|
|
{
|
2007-08-18 12:40:13 +04:00
|
|
|
rb_thread_t *th = ruby_thread_from_native();
|
|
|
|
|
* compile.c, cont.c, gc.c, insns.def, iseq.c, iseq.h, process.c,
thread.c, vm.c, vm_core.h, vm_dump.c, vm_eval.c,
vm_insnhelper.c, vm_method.c, template/insns_info.inc.tmpl,
tool/instruction.rb: fixed types.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@25030 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2009-09-22 00:58:26 +04:00
|
|
|
return th != 0;
|
2007-02-08 14:51:40 +03:00
|
|
|
}
|
2008-06-12 17:01:38 +04:00
|
|
|
|
|
|
|
static int
|
|
|
|
check_deadlock_i(st_data_t key, st_data_t val, int *found)
|
|
|
|
{
|
|
|
|
VALUE thval = key;
|
|
|
|
rb_thread_t *th;
|
|
|
|
GetThreadPtr(thval, th);
|
|
|
|
|
2011-06-12 10:58:15 +04:00
|
|
|
if (th->status != THREAD_STOPPED_FOREVER || RUBY_VM_INTERRUPTED(th)) {
|
2008-06-12 17:01:38 +04:00
|
|
|
*found = 1;
|
|
|
|
}
|
|
|
|
else if (th->locking_mutex) {
|
2011-06-16 04:12:55 +04:00
|
|
|
rb_mutex_t *mutex;
|
2008-06-12 17:01:38 +04:00
|
|
|
GetMutexPtr(th->locking_mutex, mutex);
|
|
|
|
|
|
|
|
native_mutex_lock(&mutex->lock);
|
2011-06-12 10:55:12 +04:00
|
|
|
if (mutex->th == th || (!mutex->th && mutex->cond_waiting)) {
|
2008-06-12 17:01:38 +04:00
|
|
|
*found = 1;
|
|
|
|
}
|
|
|
|
native_mutex_unlock(&mutex->lock);
|
|
|
|
}
|
|
|
|
|
|
|
|
return (*found) ? ST_STOP : ST_CONTINUE;
|
|
|
|
}
|
|
|
|
|
2011-06-12 10:55:12 +04:00
|
|
|
#ifdef DEBUG_DEADLOCK_CHECK
|
2008-06-19 18:18:46 +04:00
|
|
|
static int
|
|
|
|
debug_i(st_data_t key, st_data_t val, int *found)
|
|
|
|
{
|
|
|
|
VALUE thval = key;
|
|
|
|
rb_thread_t *th;
|
|
|
|
GetThreadPtr(thval, th);
|
|
|
|
|
2011-06-12 10:58:15 +04:00
|
|
|
printf("th:%p %d %d", th, th->status, th->interrupt_flag);
|
2008-06-19 18:18:46 +04:00
|
|
|
if (th->locking_mutex) {
|
2011-06-16 04:12:55 +04:00
|
|
|
rb_mutex_t *mutex;
|
2008-06-19 18:18:46 +04:00
|
|
|
GetMutexPtr(th->locking_mutex, mutex);
|
|
|
|
|
|
|
|
native_mutex_lock(&mutex->lock);
|
2011-06-12 10:55:12 +04:00
|
|
|
printf(" %p %d\n", mutex->th, mutex->cond_waiting);
|
2008-06-19 18:18:46 +04:00
|
|
|
native_mutex_unlock(&mutex->lock);
|
|
|
|
}
|
2011-06-12 10:55:12 +04:00
|
|
|
else
|
|
|
|
puts("");
|
2008-06-19 18:18:46 +04:00
|
|
|
|
|
|
|
return ST_CONTINUE;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2008-06-12 17:01:38 +04:00
|
|
|
static void
|
|
|
|
rb_check_deadlock(rb_vm_t *vm)
|
|
|
|
{
|
|
|
|
int found = 0;
|
|
|
|
|
2008-06-19 18:18:46 +04:00
|
|
|
if (vm_living_thread_num(vm) > vm->sleeper) return;
|
|
|
|
if (vm_living_thread_num(vm) < vm->sleeper) rb_bug("sleeper must not be more than vm_living_thread_num(vm)");
|
2012-09-09 10:27:02 +04:00
|
|
|
if (patrol_thread && patrol_thread != GET_THREAD()) return;
|
2008-06-12 17:01:38 +04:00
|
|
|
|
|
|
|
st_foreach(vm->living_threads, check_deadlock_i, (st_data_t)&found);
|
|
|
|
|
|
|
|
if (!found) {
|
|
|
|
VALUE argv[2];
|
|
|
|
argv[0] = rb_eFatal;
|
2012-04-23 19:27:00 +04:00
|
|
|
argv[1] = rb_str_new2("No live threads left. Deadlock?");
|
2011-06-12 10:55:12 +04:00
|
|
|
#ifdef DEBUG_DEADLOCK_CHECK
|
2008-06-19 18:18:46 +04:00
|
|
|
printf("%d %d %p %p\n", vm->living_threads->num_entries, vm->sleeper, GET_THREAD(), vm->main_thread);
|
|
|
|
st_foreach(vm->living_threads, debug_i, (st_data_t)0);
|
|
|
|
#endif
|
2009-08-21 19:14:29 +04:00
|
|
|
vm->sleeper--;
|
2009-06-08 20:14:06 +04:00
|
|
|
rb_threadptr_raise(vm->main_thread, 2, argv);
|
2008-06-12 17:01:38 +04:00
|
|
|
}
|
|
|
|
}
|
2008-07-03 16:55:12 +04:00
|
|
|
|
|
|
|
static void
|
|
|
|
update_coverage(rb_event_flag_t event, VALUE proc, VALUE self, ID id, VALUE klass)
|
|
|
|
{
|
2008-07-03 18:27:43 +04:00
|
|
|
VALUE coverage = GET_THREAD()->cfp->iseq->coverage;
|
2008-07-08 17:57:06 +04:00
|
|
|
if (coverage && RBASIC(coverage)->klass == 0) {
|
2008-07-03 18:27:43 +04:00
|
|
|
long line = rb_sourceline() - 1;
|
2008-07-03 16:55:12 +04:00
|
|
|
long count;
|
|
|
|
if (RARRAY_PTR(coverage)[line] == Qnil) {
|
2011-08-23 19:44:26 +04:00
|
|
|
return;
|
2008-07-03 16:55:12 +04:00
|
|
|
}
|
|
|
|
count = FIX2LONG(RARRAY_PTR(coverage)[line]) + 1;
|
|
|
|
if (POSFIXABLE(count)) {
|
|
|
|
RARRAY_PTR(coverage)[line] = LONG2FIX(count);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-07-08 19:13:22 +04:00
|
|
|
VALUE
|
|
|
|
rb_get_coverages(void)
|
2008-07-08 17:57:06 +04:00
|
|
|
{
|
2008-07-08 19:13:22 +04:00
|
|
|
return GET_VM()->coverages;
|
2008-07-08 17:57:06 +04:00
|
|
|
}
|
|
|
|
|
2008-07-08 19:13:22 +04:00
|
|
|
void
|
|
|
|
rb_set_coverages(VALUE coverages)
|
2008-07-08 17:57:06 +04:00
|
|
|
{
|
2008-07-08 19:13:22 +04:00
|
|
|
GET_VM()->coverages = coverages;
|
|
|
|
rb_add_event_hook(update_coverage, RUBY_EVENT_COVERAGE, Qnil);
|
2008-07-08 17:57:06 +04:00
|
|
|
}
|
|
|
|
|
2008-07-03 16:55:12 +04:00
|
|
|
void
|
2008-07-08 19:13:22 +04:00
|
|
|
rb_reset_coverages(void)
|
2008-07-03 16:55:12 +04:00
|
|
|
{
|
2008-07-08 19:13:22 +04:00
|
|
|
GET_VM()->coverages = Qfalse;
|
|
|
|
rb_remove_event_hook(update_coverage);
|
2008-07-03 16:55:12 +04:00
|
|
|
}
|