From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.1 (2015-04-28) on dcvr.yhbt.net X-Spam-Level: X-Spam-ASN: X-Spam-Status: No, score=-3.9 required=3.0 tests=ALL_TRUSTED,AWL,BAYES_00 shortcircuit=no autolearn=ham autolearn_force=no version=3.4.1 Received: from localhost (dcvr.yhbt.net [127.0.0.1]) by dcvr.yhbt.net (Postfix) with ESMTP id 630EF1F85A for ; Tue, 26 Jun 2018 09:38:19 +0000 (UTC) From: Eric Wong To: spew@80x24.org Subject: [PATCH 09/14] pgrp list Date: Tue, 26 Jun 2018 09:38:12 +0000 Message-Id: <20180626093817.1533-10-e@80x24.org> In-Reply-To: <20180626093817.1533-1-e@80x24.org> References: <20180626093817.1533-1-e@80x24.org> List-Id: From: Eric Wong --- mjit.c | 7 +++--- process.c | 31 +++++++++++++++++--------- spec/ruby/core/process/waitall_spec.rb | 1 + vm_core.h | 4 +++- 4 files changed, 28 insertions(+), 15 deletions(-) diff --git a/mjit.c b/mjit.c index 2e11395851..603b192c27 100644 --- a/mjit.c +++ b/mjit.c @@ -1566,9 +1566,10 @@ wait_pch(void *ignored) static void ubf_pch(void *ignored) { - rb_native_mutex_lock(&mjit_engine_mutex); - rb_native_cond_signal(&mjit_pch_wakeup); - rb_native_mutex_unlock(&mjit_engine_mutex); + if (pthread_mutex_trylock(&mjit_engine_mutex) == 0) { + rb_native_cond_signal(&mjit_pch_wakeup); + rb_native_mutex_unlock(&mjit_engine_mutex); + } } /* Finish the threads processing units and creating PCH, finalize diff --git a/process.c b/process.c index ce1445fbaa..12a6d9b7b7 100644 --- a/process.c +++ b/process.c @@ -924,13 +924,13 @@ waitpid_notify(struct waitpid_state *w, pid_t ret) } /* called by both timer thread and main thread */ -void -ruby_waitpid_all(rb_vm_t *vm) + +static void +waitpid_each(struct list_head *head) { struct waitpid_state *w = 0, *next; - rb_native_mutex_lock(&vm->waitpid_lock); - list_for_each_safe(&vm->waiting_pids, w, next, wnode) { + list_for_each_safe(head, w, next, wnode) { pid_t ret = do_waitpid(w->pid, &w->status, w->options | WNOHANG); if (!ret) continue; @@ -947,6 +947,16 @@ ruby_waitpid_all(rb_vm_t *vm) waitpid_notify(w, ret); } } +} + +void +ruby_waitpid_all(rb_vm_t *vm) +{ + rb_native_mutex_lock(&vm->waitpid_lock); + waitpid_each(&vm->waiting_pids); + if (list_empty(&vm->waiting_pids)) { + waitpid_each(&vm->waiting_grps); + } rb_native_mutex_unlock(&vm->waitpid_lock); } @@ -970,14 +980,15 @@ ruby_waitpid_locked(rb_vm_t *vm, rb_pid_t pid, int *status, int options, assert(!ruby_thread_has_gvl_p() && "must not have GVL"); waitpid_state_init(&w, pid, options); - w.ret = do_waitpid(w.pid, &w.status, w.options | WNOHANG); + if (w.pid > 0 || list_empty(&vm->waiting_pids)) + w.ret = do_waitpid(w.pid, &w.status, w.options | WNOHANG); if (w.ret) { if (w.ret == -1) w.errnum = errno; } else { w.cond = cond; w.ec = 0; - list_add(&vm->waiting_pids, &w.wnode); + list_add(w.pid > 0 ? &vm->waiting_pids : &vm->waiting_grps, &w.wnode); do { rb_native_cond_wait(w.cond, &vm->waitpid_lock); } while (!w.ret); @@ -1055,7 +1066,8 @@ waitpid_wait(struct waitpid_state *w) */ rb_native_mutex_lock(&vm->waitpid_lock); - w->ret = do_waitpid(w->pid, &w->status, w->options | WNOHANG); + if (w->pid > 0 || list_empty(&vm->waiting_pids)) + w->ret = do_waitpid(w->pid, &w->status, w->options | WNOHANG); if (w->ret || (w->options & WNOHANG)) { w->cond = 0; if (w->ret == -1) w->errnum = errno; @@ -1063,10 +1075,7 @@ waitpid_wait(struct waitpid_state *w) else { w->cond = rb_sleep_cond_get(w->ec); /* order matters, favor specified PIDs rather than -1 or 0 */ - if (w->pid > 0) - list_add(&vm->waiting_pids, &w->wnode); - else - list_add_tail(&vm->waiting_pids, &w->wnode); + list_add(w->pid > 0 ? &vm->waiting_pids : &vm->waiting_grps, &w->wnode); } rb_native_mutex_unlock(&vm->waitpid_lock); diff --git a/spec/ruby/core/process/waitall_spec.rb b/spec/ruby/core/process/waitall_spec.rb index bdc1ea7490..f269eccc7d 100644 --- a/spec/ruby/core/process/waitall_spec.rb +++ b/spec/ruby/core/process/waitall_spec.rb @@ -35,6 +35,7 @@ pids << Process.fork { Process.exit! 0 } a = Process.waitall a.should be_kind_of(Array) + a.map(&:first).sort.should == pids.sort a.size.should == 3 pids.each { |pid| pid_status = a.assoc(pid) diff --git a/vm_core.h b/vm_core.h index 936b22d439..bdbe87287b 100644 --- a/vm_core.h +++ b/vm_core.h @@ -562,7 +562,8 @@ typedef struct rb_vm_struct { rb_serial_t fork_gen; rb_nativethread_lock_t waitpid_lock; - struct list_head waiting_pids; /* <=> struct waitpid_state */ + struct list_head waiting_pids; /* PID > 0: <=> struct waitpid_state */ + struct list_head waiting_grps; /* PID <= 0: <=> struct waitpid_state */ struct list_head waiting_fds; /* <=> struct waiting_fd */ struct list_head living_threads; VALUE thgroup_default; @@ -1572,6 +1573,7 @@ rb_vm_living_threads_init(rb_vm_t *vm) { list_head_init(&vm->waiting_fds); list_head_init(&vm->waiting_pids); + list_head_init(&vm->waiting_grps); list_head_init(&vm->living_threads); vm->living_thread_num = 0; } -- EW