mirror of
https://github.com/ruby/ruby.git
synced 2022-11-09 12:17:21 -05:00
thread.c: avoid FP for Thread#join
FP arithmetic can lose precision in some cases leading to premature wakeup and wasting CPU cycles. Convert to use timeval_* functions for now. git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@62182 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
This commit is contained in:
parent
712439b530
commit
c915390b95
1 changed files with 38 additions and 22 deletions
60
thread.c
60
thread.c
|
@ -99,6 +99,10 @@ static int rb_threadptr_dead(rb_thread_t *th);
|
||||||
static void rb_check_deadlock(rb_vm_t *vm);
|
static void rb_check_deadlock(rb_vm_t *vm);
|
||||||
static int rb_threadptr_pending_interrupt_empty_p(const rb_thread_t *th);
|
static int rb_threadptr_pending_interrupt_empty_p(const rb_thread_t *th);
|
||||||
static const char *thread_status_name(rb_thread_t *th, int detail);
|
static const char *thread_status_name(rb_thread_t *th, int detail);
|
||||||
|
static void timeval_add(struct timeval *, const struct timeval *);
|
||||||
|
static void timeval_sub(struct timeval *, const struct timeval *);
|
||||||
|
static int timeval_update_expire(struct timeval *, const struct timeval *);
|
||||||
|
static void getclockofday(struct timeval *);
|
||||||
|
|
||||||
#define eKillSignal INT2FIX(0)
|
#define eKillSignal INT2FIX(0)
|
||||||
#define eTerminateSignal INT2FIX(1)
|
#define eTerminateSignal INT2FIX(1)
|
||||||
|
@ -481,8 +485,6 @@ rb_threadptr_unlock_all_locking_mutexes(rb_thread_t *th)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
static struct timeval double2timeval(double d);
|
|
||||||
|
|
||||||
void
|
void
|
||||||
rb_thread_terminate_all(void)
|
rb_thread_terminate_all(void)
|
||||||
{
|
{
|
||||||
|
@ -848,12 +850,9 @@ rb_thread_create(VALUE (*fn)(ANYARGS), void *arg)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/* +infty, for this purpose */
|
|
||||||
#define DELAY_INFTY 1E30
|
|
||||||
|
|
||||||
struct join_arg {
|
struct join_arg {
|
||||||
rb_thread_t *target, *waiting;
|
rb_thread_t *target, *waiting;
|
||||||
double delay;
|
struct timeval *limit;
|
||||||
};
|
};
|
||||||
|
|
||||||
static VALUE
|
static VALUE
|
||||||
|
@ -882,11 +881,15 @@ thread_join_sleep(VALUE arg)
|
||||||
{
|
{
|
||||||
struct join_arg *p = (struct join_arg *)arg;
|
struct join_arg *p = (struct join_arg *)arg;
|
||||||
rb_thread_t *target_th = p->target, *th = p->waiting;
|
rb_thread_t *target_th = p->target, *th = p->waiting;
|
||||||
const int forever = p->delay == DELAY_INFTY;
|
struct timeval to;
|
||||||
const double limit = forever ? 0 : timeofday() + p->delay;
|
|
||||||
|
if (p->limit) {
|
||||||
|
getclockofday(&to);
|
||||||
|
timeval_add(&to, p->limit);
|
||||||
|
}
|
||||||
|
|
||||||
while (target_th->status != THREAD_KILLED) {
|
while (target_th->status != THREAD_KILLED) {
|
||||||
if (forever) {
|
if (!p->limit) {
|
||||||
th->status = THREAD_STOPPED_FOREVER;
|
th->status = THREAD_STOPPED_FOREVER;
|
||||||
th->vm->sleeper++;
|
th->vm->sleeper++;
|
||||||
rb_check_deadlock(th->vm);
|
rb_check_deadlock(th->vm);
|
||||||
|
@ -894,17 +897,13 @@ thread_join_sleep(VALUE arg)
|
||||||
th->vm->sleeper--;
|
th->vm->sleeper--;
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
double now = timeofday();
|
if (timeval_update_expire(p->limit, &to)) {
|
||||||
struct timeval tv;
|
|
||||||
|
|
||||||
if (now > limit) {
|
|
||||||
thread_debug("thread_join: timeout (thid: %"PRI_THREAD_ID")\n",
|
thread_debug("thread_join: timeout (thid: %"PRI_THREAD_ID")\n",
|
||||||
thread_id_str(target_th));
|
thread_id_str(target_th));
|
||||||
return Qfalse;
|
return Qfalse;
|
||||||
}
|
}
|
||||||
tv = double2timeval(limit - now);
|
|
||||||
th->status = THREAD_STOPPED;
|
th->status = THREAD_STOPPED;
|
||||||
native_sleep(th, &tv);
|
native_sleep(th, p->limit);
|
||||||
}
|
}
|
||||||
RUBY_VM_CHECK_INTS_BLOCKING(th->ec);
|
RUBY_VM_CHECK_INTS_BLOCKING(th->ec);
|
||||||
th->status = THREAD_RUNNABLE;
|
th->status = THREAD_RUNNABLE;
|
||||||
|
@ -915,7 +914,7 @@ thread_join_sleep(VALUE arg)
|
||||||
}
|
}
|
||||||
|
|
||||||
static VALUE
|
static VALUE
|
||||||
thread_join(rb_thread_t *target_th, double delay)
|
thread_join(rb_thread_t *target_th, struct timeval *tv)
|
||||||
{
|
{
|
||||||
rb_thread_t *th = GET_THREAD();
|
rb_thread_t *th = GET_THREAD();
|
||||||
struct join_arg arg;
|
struct join_arg arg;
|
||||||
|
@ -929,7 +928,7 @@ thread_join(rb_thread_t *target_th, double delay)
|
||||||
|
|
||||||
arg.target = target_th;
|
arg.target = target_th;
|
||||||
arg.waiting = th;
|
arg.waiting = th;
|
||||||
arg.delay = delay;
|
arg.limit = tv;
|
||||||
|
|
||||||
thread_debug("thread_join (thid: %"PRI_THREAD_ID", status: %s)\n",
|
thread_debug("thread_join (thid: %"PRI_THREAD_ID", status: %s)\n",
|
||||||
thread_id_str(target_th), thread_status_name(target_th, TRUE));
|
thread_id_str(target_th), thread_status_name(target_th, TRUE));
|
||||||
|
@ -974,6 +973,8 @@ thread_join(rb_thread_t *target_th, double delay)
|
||||||
return target_th->self;
|
return target_th->self;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static struct timeval double2timeval(double);
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* call-seq:
|
* call-seq:
|
||||||
* thr.join -> thr
|
* thr.join -> thr
|
||||||
|
@ -1016,15 +1017,30 @@ thread_join(rb_thread_t *target_th, double delay)
|
||||||
static VALUE
|
static VALUE
|
||||||
thread_join_m(int argc, VALUE *argv, VALUE self)
|
thread_join_m(int argc, VALUE *argv, VALUE self)
|
||||||
{
|
{
|
||||||
double delay = DELAY_INFTY;
|
|
||||||
VALUE limit;
|
VALUE limit;
|
||||||
|
struct timeval timeval;
|
||||||
|
struct timeval *tv = 0;
|
||||||
|
|
||||||
rb_scan_args(argc, argv, "01", &limit);
|
rb_scan_args(argc, argv, "01", &limit);
|
||||||
if (!NIL_P(limit)) {
|
|
||||||
delay = rb_num2dbl(limit);
|
/*
|
||||||
|
* This supports INFINITY and negative values, so we can't use
|
||||||
|
* rb_time_interval right now...
|
||||||
|
*/
|
||||||
|
switch (TYPE(limit)) {
|
||||||
|
case T_NIL: break;
|
||||||
|
case T_FIXNUM:
|
||||||
|
case T_BIGNUM:
|
||||||
|
timeval.tv_sec = NUM2TIMET(limit);
|
||||||
|
timeval.tv_usec = 0;
|
||||||
|
tv = &timeval;
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
timeval = double2timeval(rb_num2dbl(limit));
|
||||||
|
tv = &timeval;
|
||||||
}
|
}
|
||||||
|
|
||||||
return thread_join(rb_thread_ptr(self), delay);
|
return thread_join(rb_thread_ptr(self), tv);
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
|
@ -1045,7 +1061,7 @@ static VALUE
|
||||||
thread_value(VALUE self)
|
thread_value(VALUE self)
|
||||||
{
|
{
|
||||||
rb_thread_t *th = rb_thread_ptr(self);
|
rb_thread_t *th = rb_thread_ptr(self);
|
||||||
thread_join(th, DELAY_INFTY);
|
thread_join(th, 0);
|
||||||
return th->value;
|
return th->value;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue