[前][次][番号順一覧][スレッド一覧]

ruby-changes:50071

From: k0kubun <ko1@a...>
Date: Sun, 4 Feb 2018 15:58:16 +0900 (JST)
Subject: [ruby-changes:50071] k0kubun:r62189 (trunk): mjit.c: merge MJIT infrastructure

k0kubun	2018-02-04 15:58:09 +0900 (Sun, 04 Feb 2018)

  New Revision: 62189

  https://svn.ruby-lang.org/cgi-bin/viewvc.cgi?view=revision&revision=62189

  Log:
    mjit.c: merge MJIT infrastructure
    
    that allows to JIT-compile Ruby methods by generating C code and
    using C compiler.  See the first comment of mjit.c to know what this
    file does.
    
    mjit.c is authored by Vladimir Makarov <vmakarov@r...>.
    After he invented great method JIT infrastructure for MRI as MJIT,
    Lars Kanis <lars@g...> sent the patch to support MinGW
    in MJIT. In addition to merging it, I ported pthread to Windows native
    threads. Now this MJIT infrastructure can be compiled on Visual Studio.
    
    This commit simplifies mjit.c to decrease code at initial merge. For
    example, this commit does not provide multiple JIT threads support.
    We can resurrect them later if we really want them, but I wanted to minimize
    diff to make it easier to review this patch.
    
    `/tmp/_mjitXXX` file is renamed to `/tmp/_ruby_mjitXXX` because non-Ruby
    developers may not know the name "mjit" and the file name should make
    sure it's from Ruby and not from some harmful programs.  TODO: it may be
    better to store this to some temporary directory which Ruby is already using
    by Tempfile, if it's not bad for performance.
    
    mjit.h: New. It has `mjit_exec` interface similar to `vm_exec`, which is
    for triggering MJIT. This drops interface for AOT compared to the original
    MJIT.
    
    Makefile.in: define macros to let MJIT know the path of MJIT header.
    Probably we can refactor this to reduce the number of macros (TODO).
    win32/Makefile.sub: ditto.
    
    common.mk: compile mjit.o and mjit_compile.o. Unlike original MJIT, this
    commit separates MJIT infrastructure and JIT compiler code as independent
    object files. As initial patch is NOT going to have ultra-fast JIT compiler,
    it's likely to replace JIT compiler, e.g. original MJIT's compiler or some
    future JIT impelementations which are not public now.
    
    inits.c: define MJIT module. This is added because `MJIT.enabled?` was
    necessary for testing.
    test/lib/zombie_hunter.rb: skip if `MJIT.enabled?`. Obviously this
    wouldn't work with current code when JIT is enabled.
    test/ruby/test_io.rb: skip this too. This would make no sense with MJIT.
    
    ruby.c: define MJIT CLI options. As major difference from original MJIT,
    "-j:l"/"--jit:llvm" are renamed to "--jit-cc" because I want to support
    not only gcc/clang but also cl.exe (Visual Studio) in the future. But it
    takes only "--jit-cc=gcc", "--jit-cc=clang" for now. And only long "--jit"
    options are allowed since some Ruby committers preferred it at Ruby
    developers Meeting on January, and some of options are renamed.
    This file also triggers to initialize MJIT thread and variables.
    eval.c: finalize MJIT worker thread and variables.
    test/ruby/test_rubyoptions.rb: fix number of CLI options for --jit.
    
    thread_pthread.c: change for pthread abstraction in MJIT. Prefix rb_ for
    functions which are used by other files.
    thread_win32.c: ditto, for Windows.  Those pthread porting is one of major
    works that YARV-MJIT created, which is my fork of MJIT, in Feature 14235.
    thread.c: follow rb_ prefix changes
    
    vm.c: trigger MJIT call on VM invocation. Also trigger `mjit_mark` to avoid
    SEGV by race between JIT and GC of ISeq. The improvement was provided by
    wanabe <s.wanabe@g...>.
    In JIT compiler I created and am going to add in my next commit, I found
    that having `mjit_exec` after `vm_loop_start:` is harmful because the
    JIT-ed function doesn't proceed other ISeqs on RESTORE_REGS of leave insn.
    Executing non-FINISH frame is unexpected for my JIT compiler and
    `exception_handler` triggers executions of such ISeqs. So `mjit_exec`
    here should be executed only when it directly comes from `vm_exec` call.
    `RubyVM::MJIT` module and `.enabled?` method is added so that we can skip
    some tests which don't expect JIT threads or compiler file descriptors.
    
    vm_insnhelper.h: trigger MJIT on method calls during VM execution.
    
    vm_core.h: add fields required for mjit.c. `bp` must be `cfp[6]` because
    rb_control_frame_struct is likely to be casted to another struct. The
    last position is the safest place to add the new field.
    vm_insnhelper.c: save initial value of cfp->ep as cfp->bp. This is an
    optimization which are done in both MJIT and YARV-MJIT. So this change
    is added in this commit. Calculating bp from ep is a little heavy work,
    so bp is kind of cache for it.
    
    iseq.c: notify ISeq GC to MJIT. We should know which iseq in MJIT queue
    is GCed to avoid SEGV.  TODO: unload some GCed units in some safe way.
    
    gc.c: add hooks so that MJIT can wait GC, and vice versa. Simultaneous
    JIT and GC executions may cause SEGV and so we should synchronize them.
    
    cont.c: save continuation information in MJIT worker. As MJIT shouldn't
    unload JIT-ed code which is being used, MJIT wants to know full list of
    saved execution contexts for continuation and detect ISeqs in use.
    
    mjit_compile.c: added empty JIT compiler so that you can reuse this commit
    to build your own JIT compiler. This commit tries to compile ISeqs but
    all of them are considered as not supported in this commit. So you can't
    use JIT compiler in this commit yet while we added --jit option now.
    
    Patch author: Vladimir Makarov <vmakarov@r...>.
    
    Contributors:
    Takashi Kokubun <takashikkbn@g...>.
    wanabe <s.wanabe@g...>.
    Lars Kanis <lars@g...>.
    
    Part of Feature 12589 and 14235.

  Added files:
    trunk/mjit.c
    trunk/mjit.h
    trunk/mjit_compile.c
  Modified files:
    trunk/Makefile.in
    trunk/common.mk
    trunk/cont.c
    trunk/eval.c
    trunk/gc.c
    trunk/iseq.c
    trunk/ruby.c
    trunk/test/lib/zombie_hunter.rb
    trunk/test/ruby/test_io.rb
    trunk/test/ruby/test_rubyoptions.rb
    trunk/thread.c
    trunk/thread_pthread.c
    trunk/thread_win32.c
    trunk/vm.c
    trunk/vm_core.h
    trunk/vm_insnhelper.h
    trunk/win32/Makefile.sub
Index: thread_pthread.c
===================================================================
--- thread_pthread.c	(revision 62188)
+++ thread_pthread.c	(revision 62189)
@@ -12,6 +12,7 @@ https://github.com/ruby/ruby/blob/trunk/thread_pthread.c#L12
 #ifdef THREAD_SYSTEM_DEPENDENT_IMPLEMENTATION
 
 #include "gc.h"
+#include "mjit.h"
 
 #ifdef HAVE_SYS_RESOURCE_H
 #include <sys/resource.h>
@@ -34,16 +35,16 @@ https://github.com/ruby/ruby/blob/trunk/thread_pthread.c#L35
 #include <kernel/OS.h>
 #endif
 
-static void native_mutex_lock(rb_nativethread_lock_t *lock);
-static void native_mutex_unlock(rb_nativethread_lock_t *lock);
+void rb_native_mutex_lock(rb_nativethread_lock_t *lock);
+void rb_native_mutex_unlock(rb_nativethread_lock_t *lock);
 static int native_mutex_trylock(rb_nativethread_lock_t *lock);
-static void native_mutex_initialize(rb_nativethread_lock_t *lock);
-static void native_mutex_destroy(rb_nativethread_lock_t *lock);
-static void native_cond_signal(rb_nativethread_cond_t *cond);
-static void native_cond_broadcast(rb_nativethread_cond_t *cond);
-static void native_cond_wait(rb_nativethread_cond_t *cond, rb_nativethread_lock_t *mutex);
-static void native_cond_initialize(rb_nativethread_cond_t *cond, int flags);
-static void native_cond_destroy(rb_nativethread_cond_t *cond);
+void rb_native_mutex_initialize(rb_nativethread_lock_t *lock);
+void rb_native_mutex_destroy(rb_nativethread_lock_t *lock);
+void rb_native_cond_signal(rb_nativethread_cond_t *cond);
+void rb_native_cond_broadcast(rb_nativethread_cond_t *cond);
+void rb_native_cond_wait(rb_nativethread_cond_t *cond, rb_nativethread_lock_t *mutex);
+void rb_native_cond_initialize(rb_nativethread_cond_t *cond, int flags);
+void rb_native_cond_destroy(rb_nativethread_cond_t *cond);
 static void rb_thread_wakeup_timer_thread_low(void);
 static struct {
     pthread_t id;
@@ -84,14 +85,14 @@ gvl_acquire_common(rb_vm_t *vm) https://github.com/ruby/ruby/blob/trunk/thread_pthread.c#L85
 	}
 
 	while (vm->gvl.acquired) {
-	    native_cond_wait(&vm->gvl.cond, &vm->gvl.lock);
+            rb_native_cond_wait(&vm->gvl.cond, &vm->gvl.lock);
 	}
 
 	vm->gvl.waiting--;
 
 	if (vm->gvl.need_yield) {
 	    vm->gvl.need_yield = 0;
-	    native_cond_signal(&vm->gvl.switch_cond);
+            rb_native_cond_signal(&vm->gvl.switch_cond);
 	}
     }
 
@@ -101,9 +102,9 @@ gvl_acquire_common(rb_vm_t *vm) https://github.com/ruby/ruby/blob/trunk/thread_pthread.c#L102
 static void
 gvl_acquire(rb_vm_t *vm, rb_thread_t *th)
 {
-    native_mutex_lock(&vm->gvl.lock);
+    rb_native_mutex_lock(&vm->gvl.lock);
     gvl_acquire_common(vm);
-    native_mutex_unlock(&vm->gvl.lock);
+    rb_native_mutex_unlock(&vm->gvl.lock);
 }
 
 static void
@@ -111,28 +112,28 @@ gvl_release_common(rb_vm_t *vm) https://github.com/ruby/ruby/blob/trunk/thread_pthread.c#L112
 {
     vm->gvl.acquired = 0;
     if (vm->gvl.waiting > 0)
-	native_cond_signal(&vm->gvl.cond);
+        rb_native_cond_signal(&vm->gvl.cond);
 }
 
 static void
 gvl_release(rb_vm_t *vm)
 {
-    native_mutex_lock(&vm->gvl.lock);
+    rb_native_mutex_lock(&vm->gvl.lock);
     gvl_release_common(vm);
-    native_mutex_unlock(&vm->gvl.lock);
+    rb_native_mutex_unlock(&vm->gvl.lock);
 }
 
 static void
 gvl_yield(rb_vm_t *vm, rb_thread_t *th)
 {
-    native_mutex_lock(&vm->gvl.lock);
+    rb_native_mutex_lock(&vm->gvl.lock);
 
     gvl_release_common(vm);
 
     /* An another thread is processing GVL yield. */
     if (UNLIKELY(vm->gvl.wait_yield)) {
 	while (vm->gvl.wait_yield)
-	    native_cond_wait(&vm->gvl.switch_wait_cond, &vm->gvl.lock);
+            rb_native_cond_wait(&vm->gvl.switch_wait_cond, &vm->gvl.lock);
 	goto acquire;
     }
 
@@ -141,28 +142,28 @@ gvl_yield(rb_vm_t *vm, rb_thread_t *th) https://github.com/ruby/ruby/blob/trunk/thread_pthread.c#L142
 	vm->gvl.need_yield = 1;
 	vm->gvl.wait_yield = 1;
 	while (vm->gvl.need_yield)
-	    native_cond_wait(&vm->gvl.switch_cond, &vm->gvl.lock);
+            rb_native_cond_wait(&vm->gvl.switch_cond, &vm->gvl.lock);
 	vm->gvl.wait_yield = 0;
     }
     else {
-	native_mutex_unlock(&vm->gvl.lock);
+	rb_native_mutex_unlock(&vm->gvl.lock);
 	sched_yield();
-	native_mutex_lock(&vm->gvl.lock);
+        rb_native_mutex_lock(&vm->gvl.lock);
     }
 
-    native_cond_broadcast(&vm->gvl.switch_wait_cond);
+    rb_native_cond_broadcast(&vm->gvl.switch_wait_cond);
   acquire:
     gvl_acquire_common(vm);
-    native_mutex_unlock(&vm->gvl.lock);
+    rb_native_mutex_unlock(&vm->gvl.lock);
 }
 
 static void
 gvl_init(rb_vm_t *vm)
 {
-    native_mutex_initialize(&vm->gvl.lock);
-    native_cond_initialize(&vm->gvl.cond, RB_CONDATTR_CLOCK_MONOTONIC);
-    native_cond_initialize(&vm->gvl.switch_cond, RB_CONDATTR_CLOCK_MONOTONIC);
-    native_cond_initialize(&vm->gvl.switch_wait_cond, RB_CONDATTR_CLOCK_MONOTONIC);
+    rb_native_mutex_initialize(&vm->gvl.lock);
+    rb_native_cond_initialize(&vm->gvl.cond, RB_CONDATTR_CLOCK_MONOTONIC);
+    rb_native_cond_initialize(&vm->gvl.switch_cond, RB_CONDATTR_CLOCK_MONOTONIC);
+    rb_native_cond_initialize(&vm->gvl.switch_wait_cond, RB_CONDATTR_CLOCK_MONOTONIC);
     vm->gvl.acquired = 0;
     vm->gvl.waiting = 0;
     vm->gvl.need_yield = 0;
@@ -172,10 +173,10 @@ gvl_init(rb_vm_t *vm) https://github.com/ruby/ruby/blob/trunk/thread_pthread.c#L173
 static void
 gvl_destroy(rb_vm_t *vm)
 {
-    native_cond_destroy(&vm->gvl.switch_wait_cond);
-    native_cond_destroy(&vm->gvl.switch_cond);
-    native_cond_destroy(&vm->gvl.cond);
-    native_mutex_destroy(&vm->gvl.lock);
+    rb_native_cond_destroy(&vm->gvl.switch_wait_cond);
+    rb_native_cond_destroy(&vm->gvl.switch_cond);
+    rb_native_cond_destroy(&vm->gvl.cond);
+    rb_native_mutex_destroy(&vm->gvl.lock);
 }
 
 #if defined(HAVE_WORKING_FORK)
@@ -202,8 +203,8 @@ mutex_debug(const char *msg, void *lock) https://github.com/ruby/ruby/blob/trunk/thread_pthread.c#L203
     }
 }
 
-static void
-native_mutex_lock(pthread_mutex_t *lock)
+void
+rb_native_mutex_lock(pthread_mutex_t *lock)
 {
     int r;
     mutex_debug("lock", lock);
@@ -212,8 +213,8 @@ native_mutex_lock(pthread_mutex_t *lock) https://github.com/ruby/ruby/blob/trunk/thread_pthread.c#L213
     }
 }
 
-static void
-native_mutex_unlock(pthread_mutex_t *lock)
+void
+rb_native_mutex_unlock(pthread_mutex_t *lock)
 {
     int r;
     mutex_debug("unlock", lock);
@@ -238,8 +239,8 @@ native_mutex_trylock(pthread_mutex_t *lo https://github.com/ruby/ruby/blob/trunk/thread_pthread.c#L239
     return 0;
 }
 
-static void
-native_mutex_initialize(pthread_mutex_t *lock)
+void
+rb_native_mutex_initialize(pthread_mutex_t *lock)
 {
     int r = pthread_mutex_init(lock, 0);
     mutex_debug("init", lock);
@@ -248,8 +249,8 @@ native_mutex_initialize(pthread_mutex_t https://github.com/ruby/ruby/blob/trunk/thread_pthread.c#L249
     }
 }
 
-static void
-native_mutex_destroy(pthread_mutex_t *lock)
+void
+rb_native_mutex_destroy(pthread_mutex_t *lock)
 {
     int r = pthread_mutex_destroy(lock);
     mutex_debug("destroy", lock);
@@ -258,8 +259,8 @@ native_mutex_destroy(pthread_mutex_t *lo https://github.com/ruby/ruby/blob/trunk/thread_pthread.c#L259
     }
 }
 
-static void
-native_cond_initialize(rb_nativethread_cond_t *cond, int flags)
+void
+rb_native_cond_initialize(rb_nativethread_cond_t *cond, int flags)
 {
     int r;
 # if USE_MONOTONIC_COND
@@ -287,8 +288,8 @@ native_cond_initialize(rb_nativethread_c https://github.com/ruby/ruby/blob/trunk/thread_pthread.c#L288
     return;
 }
 
-static void
-native_cond_destroy(rb_nativethread_cond_t *cond)
+void
+rb_native_cond_destroy(rb_nativethread_cond_t *cond)
 {
     int r = pthread_cond_destroy(&cond->cond);
     if (r != 0) {
@@ -302,12 +303,12 @@ native_cond_destroy(rb_nativethread_cond https://github.com/ruby/ruby/blob/trunk/thread_pthread.c#L303
  *
  * http://www.opensource.apple.com/source/Libc/Libc-763.11/pthreads/pthread_cond.c
  *
- * The following native_cond_signal and native_cond_broadcast functions
+ * The following rb_native_cond_signal and rb_native_cond_broadcast functions
  * need to retrying until pthread functions don't return EAGAIN.
  */
 
-static void
-native_cond_signal(rb_nativethread_cond_t *cond)
+void
+rb_native_cond_signal(rb_nativethread_cond_t *cond)
 {
     int r;
     do {
@@ -318,20 +319,20 @@ native_cond_signal(rb_nativethread_cond_ https://github.com/ruby/ruby/blob/trunk/thread_pthread.c#L319
     }
 }
 
-static void
-native_cond_broadcast(rb_nativethread_cond_t *cond)
+void
+rb_native_cond_broadcast(rb_nativethread_cond_t *cond)
 {
     int r;
     do {
 	r = pthread_cond_broadcast(&cond->cond);
     } while (r == EAGAIN);
     if (r != 0) {
-	rb_bug_errno("native_cond_broadcast", r);
+        rb_bug_errno("rb_native_cond_broadcast", r);
     }
 }
 
-static void
-native_cond_wait(rb_nativethread_cond_t *cond, pthread_mutex_t *mutex)
+void
+rb_native_cond_wait(rb_nativethread_cond_t *cond, pthread_mutex_t *mutex)
 {
     int r = pthread_cond_wait(&cond->cond, mutex);
     if (r != 0) {
@@ -449,7 +450,7 @@ Init_native_thread(rb_thread_t *th) https://github.com/ruby/ruby/blob/trunk/thread_pthread.c#L450
     fill_thread_id_str(th);
     native_thread_init(th);
 #ifdef USE_UBF_LIST
-    native_mutex_initialize(&ubf_list_lock);
+    rb_native_mutex_initialize(&ubf_list_lock);
 #endif
     posix_signal(SIGVTALRM, null_func);
 }
@@ -462,14 +463,14 @@ native_thread_init(rb_thread_t *th) https://github.com/ruby/ruby/blob/trunk/thread_pthread.c#L463
 #ifdef USE_UBF_LIST
     list_node_init(&nd->ubf_list);
 #endif
-    native_cond_initialize(&nd->sleep_cond, RB_CONDATTR_CLOCK_MONOTONIC);
+    rb_native_cond_initialize(&nd->sleep_cond, RB_CONDATTR_CLOCK_MONOTONIC);
     ruby_thread_set_native(th);
 }
 
 static void
 native_thread_destroy(rb_thread_t *th)
 {
-    native_cond_destroy(&th->native_thread_data.sleep_cond);
+    rb_native_cond_destroy(&th->native_thread_data.sleep_cond);
 }
 
 #ifndef USE_THREAD_CACHE
@@ -917,7 +918,7 @@ register_cached_thread_and_wait(void) https://github.com/ruby/ruby/blob/trunk/thread_pthread.c#L918
     ts.tv_sec = tv.tv_sec + 60;
     ts.tv_nsec = tv.tv_usec * 1000;
 
-    native_mutex_lock(&thread_cache_lock);
+    rb_native_mutex_lock(&thread_cache_lock);
     {
 	entry->th_area = &th_area;
 	entry->cond = &cond;
@@ -939,9 +940,9 @@ register_cached_thread_and_wait(void) https://github.com/ruby/ruby/blob/trunk/thread_pthread.c#L940
 	}
 
 	free(entry); /* ok */
-	native_cond_destroy(&cond);
+        rb_native_cond_destroy(&cond);
     }
-    native_mutex_unlock(&thread_cache_lock);
+    rb_native_mutex_unlock(&thread_cache_lock);
 
     return (rb_thread_t *)th_area;
 }
@@ -955,7 +956,7 @@ use_cached_thread(rb_thread_t *th) https://github.com/ruby/ruby/blob/trunk/thread_pthread.c#L956
     struct cached_thread_entry *entry;
 
     if (cached_thread_root) {
-	native_mutex_lock(&thread_cache_lock);
+        rb_native_mutex_lock(&thread_cache_lock);
 	entry = cached_thread_root;
 	{
 	    if (cached_thread_root) {
@@ -965,9 +966,9 @@ use_cached_thread(rb_thread_t *th) https://github.com/ruby/ruby/blob/trunk/thread_pthread.c#L966
 	    }
 	}
 	if (result) {
-	    native_cond_signal(entry->cond);
+	    rb_native_cond_signal(entry->cond);
 	}
-	native_mutex_unlock(&thread_cache_lock);
+	rb_native_mutex_unlock(&thread_cache_lock);
     }
 #endif
     return result;
@@ -1067,7 +1068,7 @@ ubf_pthread_cond_signal(void *ptr) https://github.com/ruby/ruby/blob/trunk/thread_pthread.c#L1068
 {
     rb_thread_t *th = (rb_thread_t *)ptr;
     thread_debug("ubf_pthread_cond_signal (%p)\n", (void *)th);
-    native_cond_signal(&th->native_thread_data.sleep_cond);
+    rb_native_cond_signal(&th->native_thread_data.sleep_cond);
 }
 
 static void
@@ -1101,7 +1102,7 @@ native_sleep(rb_thread_t *th, struct tim https://github.com/ruby/ruby/blob/trunk/thread_pthread.c#L1102
 
     GVL_UNLOCK_BEGIN();
     {
-	native_mutex_lock(lock);
+        rb_native_mutex_lock(lock);
 	th->unblock.func = ubf_pthread_cond_signal;
 	th->unblock.arg = th;
 
@@ -1111,14 +1112,14 @@ native_sleep(rb_thread_t *th, struct tim https://github.com/ruby/ruby/blob/trunk/thread_pthread.c#L1112
 	}
 	else {
 	    if (!timeout_tv)
-		native_cond_wait(cond, lock);
+                rb_native_cond_wait(cond, lock);
 	    else
 		native_cond_timedwait(cond, lock, &timeout);
 	}
 	th->unblock.func = 0;
 	th->unblock.arg = 0;
 
-	native_mutex_unlock(lock);
+	rb_native_mutex_unlock(lock);
     }
     GVL_UNLOCK_END();
 
@@ -1135,9 +1136,9 @@ register_ubf_list(rb_thread_t *th) https://github.com/ruby/ruby/blob/trunk/thread_pthread.c#L1136
     struct list_node *node = &th->native_thread_data.ubf_list;
 
     if (list_empty((struct list_head*)node)) {
-	native_mutex_lock(&ubf_list_lock);
+        rb_native_mutex_lock(&ubf_list_lock);
 	list_add(&ubf_list_head, node);
-	native_mutex_unlock(&ubf_list_lock);
+        rb_native_mutex_unlock(&ubf_list_lock);
     }
 }
 
@@ -1148,9 +1149,9 @@ unregister_ubf_list(rb_thread_t *th) https://github.com/ruby/ruby/blob/trunk/thread_pthread.c#L1149
     struct list_node *node = &th->native_thread_data.ubf_list;
 
     if (!list_empty((struct list_head*)node)) {
-	native_mutex_lock(&ubf_list_lock);
+        rb_native_mutex_lock(&ubf_list_lock);
 	list_del_init(node);
-	native_mutex_unlock(&ubf_list_lock);
+        rb_native_mutex_unlock(&ubf_list_lock);
     }
 }
 
@@ -1197,12 +1198,12 @@ ubf_wakeup_all_threads(void) https://github.com/ruby/ruby/blob/trunk/thread_pthread.c#L1198
     native_thread_data_t *dat;
 
     if (!ubf_threads_empty()) {
-	native_mutex_lock(&ubf_list_lock);
+        rb_native_mutex_lock(&ubf_list_lock);
 	list_for_each(&ubf_list_head, dat, ubf_list) {
 	    th = container_of(dat, rb_thread_t, native_thread_data);
 	    ubf_wakeup_thread(th);
 	}
-	native_mutex_unlock(&ubf_list_lock);
+        rb_native_mutex_unlock(&ubf_list_lock);
     }
 }
 
@@ -1535,9 +1536,9 @@ thread_timer(void *p) https://github.com/ruby/ruby/blob/trunk/thread_pthread.c#L1536
 #endif
 
 #if !USE_SLEEPY_TIMER_THREAD
-    native_mutex_initialize(&timer_thread_lock);
-    native_cond_initialize(&timer_thread_cond, RB_CONDATTR_CLOCK_MONOTONIC);
-    native_mutex_lock(&timer_thread_lock);
+    rb_native_mutex_initialize(&timer_thread_lock);
+    rb_native_cond_initialize(&timer_thread_cond, RB_CONDATTR_CLOCK_MONOTONIC);
+    rb_native_mutex_lock(&timer_thread_lock);
 #endif
     while (system_working > 0) {
 
@@ -1554,9 +1555,9 @@ thread_timer(void *p) https://github.com/ruby/ruby/blob/trunk/thread_pthread.c#L1555
     CLOSE_INVALIDATE(normal[0]);
     CLOSE_INVALIDATE(low[0]);
 #else
-    native_mutex_unlock(&timer_thread_lock);
-    native_cond_destroy(&timer_thread_cond);
-    native_mutex_destroy(&timer_thread_lock);
+    rb_native_mutex_unlock(&timer_thread_lock);
+    rb_native_cond_destroy(&timer_thread_cond);
+    rb_native_mutex_destroy(&timer_thread_lock);
 #endif
 
     if (TT_DEBUG) WRITE_CONST(2, "finish timer thread\n");
@@ -1772,4 +1773,40 @@ rb_nativethread_self(void) https://github.com/ruby/ruby/blob/trunk/thread_pthread.c#L1773
     return pthread_self();
 }
 
+/* A function that wraps actual worker function, for pthread abstraction. */
+static void *
+mjit_worker(void *arg)
+{
+    void (*worker_func)(void) = arg;
+
+    if (pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, NULL) != 0) {
+        fprintf(stderr, "Cannot enable cancelation in MJIT worker\n");
+    }
+#ifdef SET_CURRENT_THREAD_NAME
+    SET_CURRENT_THREAD_NAME("ruby-mjitworker"); /* 16 byte including NUL */
+#endif
+    worker_func();
+    return NULL;
+}
+
+/* Launch MJIT thread. Returns FALSE if it fails to create thread. */
+int
+rb_thread_create_mjit_thread(void (*child_hook)(void), void (*worker_func)(void))
+{
+    pthread_attr_t attr;
+    pthread_t worker_pid;
+
+    pthread_atfork(NULL, NULL, child_hook);
+    if (pthread_attr_init(&attr) == 0
+        && pthread_attr_setscope(&attr, PTHREAD_SCOPE_SYSTEM) == 0
+        && pthread_create(&worker_pid, &attr, mjit_worker, worker_func) == 0) {
+        /* jit_worker thread is not to be joined */
+        pthread_detach(worker_pid);
+        return TRUE;
+    }
+    else {
+        return FALSE;
+    }
+}
+
 #endif /* THREAD_SYSTEM_DEPENDENT_IMPLEMENTATION */
Index: vm.c
===================================================================
--- vm.c	(revision 62188)
+++ vm.c	(revision 62189)
@@ -298,6 +298,7 @@ static VALUE vm_invoke_proc(rb_execution https://github.com/ruby/ruby/blob/trunk/vm.c#L298
 
 static VALUE rb_block_param_proxy;
 
+#include "mjit.h"
 #include "vm_insnhelper.h"
 #include "vm_exec.h"
 #include "vm_insnhelper.c"
@@ -1786,8 +1787,10 @@ vm_exec(rb_execution_context_t *ec) https://github.com/ruby/ruby/blob/trunk/vm.c#L1787
 
     _tag.retval = Qnil;
     if ((state = EC_EXEC_TAG()) == TAG_NONE) {
+        result = mjit_exec(ec);
       vm_loop_start:
-	result = vm_exec_core(ec, initial);
+	if (result == Qundef)
+	    result = vm_exec_core(ec, initial);
 	VM_ASSERT(ec->tag == &_tag);
 	if ((state = _tag.state) != TAG_NONE) {
 	    err = (struct vm_throw_data *)result;
@@ -1870,6 +1873,7 @@ vm_exec(rb_execution_context_t *ec) https://github.com/ruby/ruby/blob/trunk/vm.c#L1873
 		    *ec->cfp->sp++ = THROW_DATA_VAL(err);
 #endif
 		    ec->errinfo = Qnil;
+                    result = Qundef;
 		    goto vm_loop_start;
 		}
 	    }
@@ -1909,6 +1913,7 @@ vm_exec(rb_execution_context_t *ec) https://github.com/ruby/ruby/blob/trunk/vm.c#L1913
 			if (cfp == escape_cfp) {
 			    cfp->pc = cfp->iseq->body->iseq_encoded + entry->cont;
 			    ec->errinfo = Qnil;
+                            result = Qundef;
 			    goto vm_loop_start;
 			}
 		    }
@@ -1943,6 +1948,7 @@ vm_exec(rb_execution_context_t *ec) https://github.com/ruby/ruby/blob/trunk/vm.c#L1948
 			}
 			ec->errinfo = Qnil;
 			VM_ASSERT(ec->tag->state == TAG_NONE);
+                        result = Qundef;
 			goto vm_loop_start;
 		    }
 		}
@@ -1994,6 +2000,7 @@ vm_exec(rb_execution_context_t *ec) https://github.com/ruby/ruby/blob/trunk/vm.c#L2000
 	    state = 0;
 	    ec->tag->state = TAG_NONE;
 	    ec->errinfo = Qnil;
+            result = Qundef;
 	    goto vm_loop_start;
 	}
 	else {
@@ -2122,6 +2129,8 @@ rb_vm_mark(void *ptr) https://github.com/ruby/ruby/blob/trunk/vm.c#L2129
 	rb_vm_trace_mark_event_hooks(&vm->event_hooks);
 
 	rb_gc_mark_values(RUBY_NSIG, vm->trap_list.cmd);
+
+        mjit_mark();
     }
 
     RUBY_MARK_LEAVE("vm");
@@ -2742,6 +2751,12 @@ core_hash_merge_kwd(int argc, VALUE *arg https://github.com/ruby/ruby/blob/trunk/vm.c#L2751
     return hash;
 }
 
+static VALUE
+mjit_enabled_p(void)
+{
+    return mjit_init_p ? Qtrue : Qfalse;
+}
+
 extern VALUE *rb_gc_stack_start;
 extern size_t rb_gc_stack_maxsize;
 #ifdef __ia64
@@ -2795,6 +2810,7 @@ Init_VM(void) https://github.com/ruby/ruby/blob/trunk/vm.c#L2810
     VALUE opts;
     VALUE klass;
     VALUE fcore;
+    VALUE mjit;
 
     /* ::RubyVM */
     rb_cRubyVM = rb_define_class("RubyVM", rb_cObject);
@@ -2826,6 +2842,10 @@ Init_VM(void) https://github.com/ruby/ruby/blob/trunk/vm.c#L2842
     rb_gc_register_mark_object(fcore);
     rb_mRubyVMFrozenCore = fcore;
 
+    /* RubyVM::MJIT */
+    mjit = rb_define_module_under(rb_cRubyVM, "MJIT");
+    rb_define_singleton_method(mjit, "enabled?", mjit_enabled_p, 0);
+
     /*
      * Document-class: Thread
      *
Index: eval.c
===================================================================
--- eval.c	(revision 62188)
+++ eval.c	(revision 62189)
@@ -17,6 +17,7 @@ https://github.com/ruby/ruby/blob/trunk/eval.c#L17
 #include "gc.h"
 #include "ruby/vm.h"
 #include "vm_core.h"
+#include "mjit.h"
 #include "probes_helper.h"
 
 NORETURN(void rb_raise_jump(VALUE, VALUE));
@@ -218,6 +219,8 @@ ruby_cleanup(volatile int ex) https://github.com/ruby/ruby/blob/trunk/eval.c#L219
 	}
     }
 
+    mjit_finish(); /* We still need ISeqs here. */
+
     ruby_finalize_1();
 
     /* unlock again if finalizer took mutexes. */
Index: mjit.c
===================================== (... truncated)

--
ML: ruby-changes@q...
Info: http://www.atdot.net/~ko1/quickml/

[前][次][番号順一覧][スレッド一覧]