ruby-changes:52334
From: normal <ko1@a...>
Date: Sun, 26 Aug 2018 21:41:26 +0900 (JST)
Subject: [ruby-changes:52334] normal:r64542 (trunk): thread_sync.c: common wakeup_{one, all} implementation
normal 2018-08-26 21:41:16 +0900 (Sun, 26 Aug 2018) New Revision: 64542 https://svn.ruby-lang.org/cgi-bin/viewvc.cgi?view=revision&revision=64542 Log: thread_sync.c: common wakeup_{one,all} implementation This let us avoid looping in rb_szqueue_max_set, saves us some lines of code and reduces binary size slightly (numbers from 32-bit x86): text data bss dec hex filename before: 91272 392 156 91820 166ac thread.o after: 91200 392 156 91748 16664 thread.o Inspiration from this taken from the FUTEX_WAKE op of the Linux futex(2) syscall. Modified files: trunk/thread_sync.c Index: thread_sync.c =================================================================== --- thread_sync.c (revision 64541) +++ thread_sync.c (revision 64542) @@ -12,8 +12,8 @@ struct sync_waiter { https://github.com/ruby/ruby/blob/trunk/thread_sync.c#L12 #define MUTEX_ALLOW_TRAP FL_USER1 -static int -wakeup_one(struct list_head *head) +static void +sync_wakeup(struct list_head *head, long max) { struct sync_waiter *cur = 0, *next; @@ -22,24 +22,21 @@ wakeup_one(struct list_head *head) https://github.com/ruby/ruby/blob/trunk/thread_sync.c#L22 if (cur->th->status != THREAD_KILLED) { rb_threadptr_interrupt(cur->th); cur->th->status = THREAD_RUNNABLE; - return TRUE; + if (--max == 0) return; } } - return FALSE; } static void -wakeup_all(struct list_head *head) +wakeup_one(struct list_head *head) { - struct sync_waiter *cur = 0, *next; + sync_wakeup(head, 1); +} - list_for_each_safe(head, cur, next, node) { - list_del_init(&cur->node); - if (cur->th->status != THREAD_KILLED) { - rb_threadptr_interrupt(cur->th); - cur->th->status = THREAD_RUNNABLE; - } - } +static void +wakeup_all(struct list_head *head) +{ + sync_wakeup(head, LONG_MAX); } /* Mutex */ @@ -1112,9 +1109,7 @@ rb_szqueue_max_set(VALUE self, VALUE vma https://github.com/ruby/ruby/blob/trunk/thread_sync.c#L1109 diff = max - sq->max; } sq->max = max; - while (diff-- > 0 && wakeup_one(szqueue_pushq(sq))) { - /* keep waking more up */ - } + sync_wakeup(szqueue_pushq(sq), diff); return vmax; } -- ML: ruby-changes@q... Info: http://www.atdot.net/~ko1/quickml/