From f11db2a605d99ef6a0943eba34db355188f8efcb Mon Sep 17 00:00:00 2001 From: normal Date: Sat, 10 May 2014 23:48:51 +0000 Subject: vm*: doubly-linked list from ccan to manage vm->living_threads A doubly-linked list for tracking living threads guarantees constant-time insert/delete performance with no corner cases of a hash table. I chose this ccan implementation of doubly-linked lists over the BSD sys/queue.h implementation since: 1) insertion and removal are both branchless 2) locality is improved if a struct may be a member of multiple lists (0002 patch in Feature 9632 will introduce a secondary list for waiting FDs) This also increases cache locality during iteration: improving performance in a new IO#close benchmark with many sleeping threads while still scanning the same number of threads. vm_thread_close 1.762 * vm_core.h (rb_vm_t): list_head and counter for living_threads (rb_thread_t): vmlt_node for living_threads linkage (rb_vm_living_threads_init): new function wrapper (rb_vm_living_threads_insert): ditto (rb_vm_living_threads_remove): ditto * vm.c (rb_vm_living_threads_foreach): new function wrapper * thread.c (terminate_i, thread_start_func_2, thread_create_core, thread_fd_close_i, thread_fd_close): update to use new APIs * vm.c (vm_mark_each_thread_func, rb_vm_mark, ruby_vm_destruct, vm_memsize, vm_init2, Init_VM): ditto * vm_trace.c (clear_trace_func_i, rb_clear_trace_func): ditto * benchmark/bm_vm_thread_close.rb: added to show improvement * ccan/build_assert/build_assert.h: added as a dependency of list.h * ccan/check_type/check_type.h: ditto * ccan/container_of/container_of.h: ditto * ccan/licenses/BSD-MIT: ditto * ccan/licenses/CC0: ditto * ccan/str/str.h: ditto (stripped of unused macros) * ccan/list/list.h: ditto * common.mk: add CCAN_LIST_INCLUDES [ruby-core:61871][Feature 9632 (part 1)] Apologies for the size of this commit, but I think a good doubly-linked list will be useful for future features, too. This may be used to add ordering to a container_of-based hash table to preserve compatibility if required (e.g. feature 9614). git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@45913 b2dd03c8-39d4-4d8f-98ff-823fe69b080e --- vm_core.h | 28 ++++++++++++++++++++++++++-- 1 file changed, 26 insertions(+), 2 deletions(-) (limited to 'vm_core.h') diff --git a/vm_core.h b/vm_core.h index c436b55b4d..1dacfebadf 100644 --- a/vm_core.h +++ b/vm_core.h @@ -23,8 +23,8 @@ #include "id.h" #include "method.h" #include "ruby_atomic.h" - #include "thread_native.h" +#include "ccan/list/list.h" #ifndef ENABLE_VM_OBJSPACE #ifdef _WIN32 @@ -333,7 +333,8 @@ typedef struct rb_vm_struct { struct rb_thread_struct *main_thread; struct rb_thread_struct *running_thread; - st_table *living_threads; + struct list_head living_threads; + size_t living_thread_num; VALUE thgroup_default; int running; @@ -501,6 +502,7 @@ typedef struct rb_ensure_list { } rb_ensure_list_t; typedef struct rb_thread_struct { + struct list_node vmlt_node; VALUE self; rb_vm_t *vm; @@ -856,6 +858,28 @@ void rb_thread_stop_timer_thread(int); void rb_thread_reset_timer_thread(void); void rb_thread_wakeup_timer_thread(void); +static inline void +rb_vm_living_threads_init(rb_vm_t *vm) +{ + list_head_init(&vm->living_threads); + vm->living_thread_num = 0; +} + +static inline void +rb_vm_living_threads_insert(rb_vm_t *vm, rb_thread_t *th) +{ + list_add(&vm->living_threads, &th->vmlt_node); + vm->living_thread_num++; +} + +static inline void +rb_vm_living_threads_remove(rb_vm_t *vm, rb_thread_t *th) +{ + list_del(&th->vmlt_node); + vm->living_thread_num--; +} + +void rb_vm_living_threads_foreach(rb_vm_t*, int (*)(rb_thread_t*, void*), void*); int ruby_thread_has_gvl_p(void); typedef int rb_backtrace_iter_func(void *, VALUE, int, VALUE); rb_control_frame_t *rb_vm_get_ruby_level_next_cfp(rb_thread_t *th, const rb_control_frame_t *cfp); -- cgit v1.2.3