dumping ground for random patches and texts
 help / color / mirror / Atom feed
From: Eric Wong <e@80x24.org>
To: spew@80x24.org
Subject: [PATCH 09/14] pgrp list
Date: Tue, 26 Jun 2018 09:38:12 +0000	[thread overview]
Message-ID: <20180626093817.1533-10-e@80x24.org> (raw)
In-Reply-To: <20180626093817.1533-1-e@80x24.org>

From: Eric Wong <normalperson@yhbt.net>

---
 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


  parent reply	other threads:[~2018-06-26  9:38 UTC|newest]

Thread overview: 16+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2018-06-26  9:38 [PATCH 00/14] SIGCHLD hijack for waitpid Eric Wong
2018-06-26  9:38 ` [PATCH 01/14] hijack SIGCHLD handler for internal use Eric Wong
2018-06-26  9:38 ` [PATCH 02/14] fix SIGCHLD hijacking race conditions Eric Wong
2018-06-26  9:38 ` [PATCH 03/14] mjit.c: allow working on platforms without SIGCHLD Eric Wong
2018-06-26  9:38 ` [PATCH 04/14] cleanups Eric Wong
2018-06-26  9:38 ` [PATCH 05/14] handle SIGCHLD in both the timer-thread and main thread Eric Wong
2018-06-26  9:38 ` [PATCH 06/14] Revert "test_process.rb: skip tests for Bug 14867" Eric Wong
2018-06-26  9:38 ` [PATCH 07/14] Revert "spec: skip Process wait specs on MJIT" Eric Wong
2018-06-26  9:38 ` [PATCH 08/14] wip testing Eric Wong
2018-06-26  9:38 ` Eric Wong [this message]
2018-06-26  9:38 ` [PATCH 10/14] grantpt Eric Wong
2018-06-26  9:38 ` [PATCH 11/14] blocking mjit_finish Eric Wong
2018-06-26  9:38 ` [PATCH 12/14] reinitialize waitpid_lock Eric Wong
2018-06-26  9:38 ` [PATCH 13/14] signal.c: prevent spurious wakeup of main thread for SIGCHLD Eric Wong
2018-06-26  9:38 ` [PATCH 14/14] process.c (rb_waitpid): reimplement non-SIGCHLD code path Eric Wong
2018-06-26 10:51 ` [PATCH 15/14] force SIGCHLD to the timer-thread Eric Wong

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20180626093817.1533-10-e@80x24.org \
    --to=e@80x24.org \
    --cc=spew@80x24.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).