Skip to content

Commit

Permalink
* vm_core.h, thread.c, cont.c: add RUBY_VM_SET_INTERRUPT(),
Browse files Browse the repository at this point in the history
  RUBY_VM_SET_TIMER_INTERRUPT(), RUBY_VM_INTERRUPTED().
* thread.c, thread_pthread.c, thread_win32.c: fix to ignore time slice
  event until sleep.
* bootstraptest/test_thread.rb: add a test for time limited join test.



git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@14654 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
  • Loading branch information
ko1 committed Dec 25, 2007
1 parent 94c942e commit 5f0b8af
Show file tree
Hide file tree
Showing 7 changed files with 50 additions and 15 deletions.
10 changes: 10 additions & 0 deletions ChangeLog
Original file line number Diff line number Diff line change
@@ -1,3 +1,13 @@
Tue Dec 25 13:07:56 2007 Koichi Sasada <[email protected]>

* vm_core.h, thread.c, cont.c: add RUBY_VM_SET_INTERRUPT(),
RUBY_VM_SET_TIMER_INTERRUPT(), RUBY_VM_INTERRUPTED().

* thread.c, thread_pthread.c, thread_win32.c: fix to ignore time slice
event until sleep.

* bootstraptest/test_thread.rb: add a test for time limited join test.

Tue Dec 25 12:42:59 2007 Koichi Sasada <[email protected]>

* vm.c (Init_VM): remove unused code.
Expand Down
12 changes: 11 additions & 1 deletion bootstraptest/test_thread.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,17 @@
Thread.new{
}
}.each{|e|
e.join
e.join()
}
}
}
assert_equal %q{5000}, %q{
5000.times{|e|
(1..2).map{
Thread.new{
}
}.each{|e|
e.join(1000000000)
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion cont.c
Original file line number Diff line number Diff line change
Expand Up @@ -610,7 +610,7 @@ rb_fiber_start(void)
th->thrown_errinfo =
vm_make_jump_tag_but_local_jump(state, th->errinfo);
}
th->interrupt_flag = 1;
RUBY_VM_SET_INTERRUPT(th);
}

rb_fiber_terminate(cont);
Expand Down
6 changes: 3 additions & 3 deletions thread.c
Original file line number Diff line number Diff line change
Expand Up @@ -218,7 +218,7 @@ static void
rb_thread_interrupt(rb_thread_t *th)
{
native_mutex_lock(&th->interrupt_lock);
th->interrupt_flag = 1;
RUBY_VM_SET_INTERRUPT(th);
if (th->unblock_function) {
(th->unblock_function)(th->unblock_function_arg);
}
Expand Down Expand Up @@ -562,6 +562,7 @@ thread_join_m(int argc, VALUE *argv, VALUE self)
if (!NIL_P(limit)) {
delay = rb_num2dbl(limit);
}

return thread_join(target_th, delay);
}

Expand Down Expand Up @@ -607,7 +608,6 @@ static void
sleep_forever(rb_thread_t *th)
{
native_sleep(th, 0);
RUBY_VM_CHECK_INTS();
}

static void
Expand Down Expand Up @@ -1924,7 +1924,7 @@ timer_thread_function(void)
rb_vm_t *vm = GET_VM(); /* TODO: fix me for Multi-VM */

/* for time slice */
vm->running_thread->interrupt_flag = 1;
RUBY_VM_SET_TIMER_INTERRUPT(vm->running_thread);

/* check signal */
if (vm->buffered_signal_size && vm->main_thread->exec_signal == 0) {
Expand Down
16 changes: 9 additions & 7 deletions thread_pthread.c
Original file line number Diff line number Diff line change
Expand Up @@ -392,6 +392,7 @@ native_sleep(rb_thread_t *th, struct timeval *tv)
int prev_status = th->status;
struct timespec ts;
struct timeval tvn;
struct timeval tve;

if (tv) {
gettimeofday(&tvn, NULL);
Expand All @@ -410,15 +411,14 @@ native_sleep(rb_thread_t *th, struct timeval *tv)
GVL_UNLOCK_BEGIN();
{
pthread_mutex_lock(&th->interrupt_lock);
th->unblock_function = ubf_pthread_cond_signal;
th->unblock_function_arg = th;

if (th->interrupt_flag) {
if (RUBY_VM_INTERRUPTED(th)) {
/* interrupted. return immediate */
thread_debug("native_sleep: interrupted before sleep\n");
}
else {
th->unblock_function = ubf_pthread_cond_signal;
th->unblock_function_arg = th;

if (tv == 0) {
thread_debug("native_sleep: pthread_cond_wait start\n");
pthread_cond_wait(&th->native_thread_data.sleep_cond,
Expand All @@ -433,14 +433,16 @@ native_sleep(rb_thread_t *th, struct timeval *tv)
&th->interrupt_lock, &ts);
thread_debug("native_sleep: pthread_cond_timedwait end (%d)\n", r);
}
th->unblock_function = 0;
th->unblock_function_arg = 0;
}
pthread_mutex_unlock(&th->interrupt_lock);
th->unblock_function = 0;
th->unblock_function_arg = 0;

pthread_mutex_unlock(&th->interrupt_lock);
th->status = prev_status;
}
GVL_UNLOCK_END();
RUBY_VM_CHECK_INTS();

thread_debug("native_sleep done\n");
}

Expand Down
15 changes: 12 additions & 3 deletions thread_win32.c
Original file line number Diff line number Diff line change
Expand Up @@ -208,17 +208,26 @@ native_sleep(rb_thread_t *th, struct timeval *tv)
{
DWORD ret;
int status = th->status;

th->status = THREAD_STOPPED;
th->unblock_function = ubf_handle;
th->unblock_function_arg = th;
thread_debug("native_sleep start (%d)\n", (int)msec);
ret = w32_wait_events(0, 0, msec, th);
thread_debug("native_sleep done (%d)\n", ret);

if (RUBY_VM_INTERRUPTED(th)) {
/* interrupted. return immediate */
}
else {
thread_debug("native_sleep start (%d)\n", (int)msec);
ret = w32_wait_events(0, 0, msec, th);
thread_debug("native_sleep done (%d)\n", ret);
}

th->unblock_function = 0;
th->unblock_function_arg = 0;
th->status = status;
}
GVL_UNLOCK_END();
RUBY_VM_CHECK_INTS();
}

int
Expand Down
4 changes: 4 additions & 0 deletions vm_core.h
Original file line number Diff line number Diff line change
Expand Up @@ -634,6 +634,10 @@ extern rb_vm_t *ruby_current_vm;
#error "unsupported thread model"
#endif

#define RUBY_VM_SET_INTERRUPT(th) ((th)->interrupt_flag |= 0x02)
#define RUBY_VM_SET_TIMER_INTERRUPT(th) ((th)->interrupt_flag |= 0x01)
#define RUBY_VM_INTERRUPTED(th) ((th)->interrupt_flag & 0x02)

void rb_thread_execute_interrupts(rb_thread_t *);

#define RUBY_VM_CHECK_INTS_TH(th) do { \
Expand Down

0 comments on commit 5f0b8af

Please sign in to comment.