From: Hidetoshi NAGAI Date: 2003-11-19T15:18:17+09:00 Subject: [ruby-dev:21957] Re: pthread trouble on tcltklib 永井@知能.九工大です. From: matz@ruby-lang.org (Yukihiro Matsumoto) Subject: [ruby-dev:21952] Re: pthread trouble on tcltklib Date: Wed, 19 Nov 2003 00:54:06 +0900 Message-ID: <1069170844.389724.7921.nullmailer@picachu.netlab.jp> > |では,configure.in はその方向でということで. > |# ...って,まつもとさんはそれで OK でしょうか? > OKです。 承知しました. ついでに, From: Hidetoshi NAGAI Subject: [ruby-dev:21931] Re: pthread trouble on tcltklib Date: Mon, 17 Nov 2003 17:12:32 +0900 Message-ID: <20031117.171230.74749656.nagai@ai.kyutech.ac.jp> > > どちらも、せいぜい可能なのは即座にreturnくらいでしょう。 > freelist がなくなって rb_gc() が呼ばれた場合は return されても > 困りますから,何も対処しないなら rb_gc() は return よりも > rb_bug が適切かと思います. を鑑み,rb_gc() で freelist == 0 の場合のみを rb_bug とし, それ以外は即座に return としてみたのが添付の patch です. Index: configure.in =================================================================== RCS file: /src/ruby/configure.in,v retrieving revision 1.191 diff -u -r1.191 configure.in --- configure.in 8 Nov 2003 04:53:14 -0000 1.191 +++ configure.in 19 Nov 2003 06:11:29 -0000 @@ -662,52 +662,58 @@ ], rb_cv_stack_grow_dir=-1, rb_cv_stack_grow_dir=+1, rb_cv_stack_grow_dir=0)]) AC_DEFINE_UNQUOTED(STACK_GROW_DIRECTION, $rb_cv_stack_grow_dir) -dnl default value for $KANJI -DEFAULT_KCODE="KCODE_NONE" - -AC_ARG_WITH(pthread-ext, - [ --with-pthread-ext use pthread library on external modules ], - [AC_DEFINE(USE_PTHREAD_EXTLIB) - AC_CHECK_LIB(pthread, pthread_mutex_init, - rb_use_pthread_ext=yes, rb_use_pthread_ext=no) - if test "$rb_use_pthread_ext" = "no"; then - AC_CHECK_LIB(pthread, __pthread_mutex_init, - rb_use_pthread_ext=yes, rb_use_pthread_ext=no) - fi - if test "$rb_use_pthread_ext" = "yes"; then - LIBS="-lpthread $LIBS" - AC_DEFINE(_REENTRANT) - AC_DEFINE(_THREAD_SAFE) - AC_DEFINE(HAVE_LIBPTHREAD) - else - AC_CHECK_LIB(pthreads, pthread_mutex_init, - rb_use_pthread_ext=yes, rb_use_pthread_ext=no) - if test "$rb_use_pthread_ext" = "yes"; then - LIBS="-lpthreads $LIBS" +AC_CHECK_LIB(pthread, pthread_mutex_init, + rb_with_pthread=yes, rb_with_pthread=no) +if test "$rb_with_pthread" = "no"; then + AC_CHECK_LIB(pthread, __pthread_mutex_init, + rb_with_pthread=yes, rb_with_pthread=no) +fi +if test "$rb_with_pthread" = "yes"; then + LIBS="-lpthread $LIBS" + AC_DEFINE(_REENTRANT) + AC_DEFINE(_THREAD_SAFE) + AC_DEFINE(HAVE_LIBPTHREAD) +else + AC_CHECK_LIB(pthreads, pthread_mutex_init, + rb_with_pthread=yes, rb_with_pthread=no) + if test "$rb_with_pthread" = "yes"; then + LIBS="-lpthreads $LIBS" + AC_DEFINE(_REENTRANT) + AC_DEFINE(_THREAD_SAFE) + AC_DEFINE(HAVE_LIBPTHREAD) + else + AC_CHECK_LIB(c, pthread_mutex_init, + rb_with_pthread=yes, rb_with_pthread=no) + if test "$rb_with_pthread" = "yes"; then + AC_DEFINE(_REENTRANT) + AC_DEFINE(_THREAD_SAFE) + AC_DEFINE(HAVE_LIBPTHREAD) + else + AC_CHECK_LIB(c_r, pthread_mutex_init, + rb_with_pthread=yes, rb_with_pthread=no) + if test "$rb_with_pthread" = "yes"; then + if test "$with_libc_r" = "yes"; then + if test "$rb_cv_supplementary_lib_c_r" = "yes"; then + AC_DEFINE(_REENTRANT) + AC_DEFINE(_THREAD_SAFE) + AC_DEFINE(HAVE_LIBPTHREAD) + MAINLIBS="-pthread $MAINLIBS" + fi + else + MAINLIBS="-pthread $MAINLIBS" AC_DEFINE(_REENTRANT) AC_DEFINE(_THREAD_SAFE) AC_DEFINE(HAVE_LIBPTHREAD) - else - AC_CHECK_LIB(c_r, pthread_mutex_init, - rb_use_pthread_ext=yes, rb_use_pthread_ext=no) - if test "$rb_use_pthread_ext" = "yes"; then - if test "$with_libc_r" = "yes"; then - if test "$rb_cv_supplementary_lib_c_r" = "yes"; then - AC_DEFINE(_REENTRANT) - AC_DEFINE(_THREAD_SAFE) - AC_DEFINE(HAVE_LIBPTHREAD) - MAINLIBS="-pthread $MAINLIBS" - fi - else - MAINLIBS="-pthread $MAINLIBS" - AC_DEFINE(_REENTRANT) - AC_DEFINE(_THREAD_SAFE) - AC_DEFINE(HAVE_LIBPTHREAD) - fi - fi fi + else + AC_MSG_WARN("Don't know how to find pthread library on your system -- thread support disabled") fi - ]) + fi + fi +fi + +dnl default value for $KANJI +DEFAULT_KCODE="KCODE_NONE" AC_ARG_WITH(default-kcode, [ --with-default-kcode=CODE specify default value for \$KCODE (utf8|euc|sjis|none)], Index: ruby.h =================================================================== RCS file: /src/ruby/ruby.h,v retrieving revision 1.90 diff -u -r1.90 ruby.h --- ruby.h 14 Oct 2003 02:53:53 -0000 1.90 +++ ruby.h 19 Nov 2003 06:11:29 -0000 @@ -671,6 +671,21 @@ static char *dln_libs_to_be_linked[] = { EXTLIB, 0 }; #endif +#if defined(HAVE_LIBPTHREAD) +typedef pthread_t rb_nativethread_t; +# define NATIVETHREAD_CURRENT() pthread_self() +# define NATIVETHREAD_EQUAL(t1,t2) pthread_equal((t1),(t2)) +# define HAVE_NATIVETHREAD +#elif defined(_WIN32) || defined(_WIN32_WCE) || defined(__CYGWIN__) +typedef DWORD rb_nativethread_t; +# define NATIVETHREAD_CURRENT() GetCurrentThreadId() +# define NATIVETHREAD_EQUAL(t1,t2) ((t1) == (t2)) +# define HAVE_NATIVETHREAD +#endif +#ifdef HAVE_NATIVETHREAD +RUBY_EXTERN rb_nativethread_t ruby_thid; +#endif + #if defined(__cplusplus) } /* extern "C" { */ #endif Index: eval.c =================================================================== RCS file: /src/ruby/eval.c,v retrieving revision 1.590 diff -u -r1.590 eval.c --- eval.c 16 Nov 2003 09:11:01 -0000 1.590 +++ eval.c 19 Nov 2003 06:11:30 -0000 @@ -33,10 +33,6 @@ #include "st.h" #include "dln.h" -#if defined(HAVE_LIBPTHREAD) && defined(USE_PTHREAD_EXTLIB) -#include -#endif - #ifdef __APPLE__ #include #endif @@ -1161,6 +1157,10 @@ void Init_heap _((void)); void Init_ext _((void)); +#ifdef HAVE_NATIVETHREAD +rb_nativethread_t ruby_thid; +#endif + void ruby_init() { @@ -1172,6 +1172,9 @@ if (initialized) return; initialized = 1; +#ifdef HAVE_NATIVETHREAD + ruby_thid = NATIVETHREAD_CURRENT(); +#endif ruby_frame = top_frame = &frame; ruby_iter = &iter; @@ -8647,11 +8650,6 @@ return test; } -#if defined(HAVE_LIBPTHREAD) && defined(USE_PTHREAD_EXTLIB) -static pthread_t thid; -static pthread_mutex_t mtx = PTHREAD_MUTEX_INITIALIZER; -#endif - void rb_thread_schedule() { @@ -8668,10 +8666,13 @@ int n, max; int need_select = 0; int select_timeout = 0; -#if defined(HAVE_LIBPTHREAD) && defined(USE_PTHREAD_EXTLIB) - int st; -#endif +#ifdef HAVE_NATIVETHREAD + if (!NATIVETHREAD_EQUAL(ruby_thid, NATIVETHREAD_CURRENT())) { + rb_warning("non-ruby native thread calls rb_thread_schedule()"); + return; + } +#endif rb_thread_pending = 0; if (curr_thread == curr_thread->next && curr_thread->status == THREAD_RUNNABLE) @@ -8680,16 +8681,6 @@ next = 0; curr = curr_thread; /* starting thread */ -#if defined(HAVE_LIBPTHREAD) && defined(USE_PTHREAD_EXTLIB) - if ((st = pthread_mutex_trylock(&mtx)) == EBUSY) { - if (pthread_self() != thid) { - return; - } - } else { - thid = pthread_self(); - } -#endif - while (curr->status == THREAD_KILLED) { curr = curr->prev; } @@ -8887,22 +8878,12 @@ } next->wait_for = 0; if (next->status == THREAD_RUNNABLE && next == curr_thread) { -#if defined(HAVE_LIBPTHREAD) && defined(USE_PTHREAD_EXTLIB) - if (st != EBUSY) { - pthread_mutex_unlock(& mtx); - } -#endif return; } /* context switch */ if (curr == curr_thread) { if (THREAD_SAVE_CONTEXT(curr)) { -#if defined(HAVE_LIBPTHREAD) && defined(USE_PTHREAD_EXTLIB) - if (st != EBUSY) { - pthread_mutex_unlock(& mtx); - } -#endif return; } } @@ -8912,19 +8893,9 @@ if (!(next->flags & THREAD_TERMINATING)) { next->flags |= THREAD_TERMINATING; /* terminate; execute ensure-clause if any */ -#if defined(HAVE_LIBPTHREAD) && defined(USE_PTHREAD_EXTLIB) - if (st != EBUSY) { - pthread_mutex_unlock(& mtx); - } -#endif rb_thread_restore_context(next, RESTORE_FATAL); } } -#if defined(HAVE_LIBPTHREAD) && defined(USE_PTHREAD_EXTLIB) - if (st != EBUSY) { - pthread_mutex_unlock(& mtx); - } -#endif rb_thread_restore_context(next, RESTORE_NORMAL); } Index: gc.c =================================================================== RCS file: /src/ruby/gc.c,v retrieving revision 1.160 diff -u -r1.160 gc.c --- gc.c 20 Oct 2003 02:06:39 -0000 1.160 +++ gc.c 19 Nov 2003 06:11:30 -0000 @@ -1216,6 +1216,16 @@ jmp_buf save_regs_gc_mark; SET_STACK_END; +#ifdef HAVE_NATIVETHREAD + if (!NATIVETHREAD_EQUAL(ruby_thid, NATIVETHREAD_CURRENT())) { + if (freelist) { + rb_warning("non-ruby native thread calls rb_gc()"); + return; + } else { + rb_bug("non-ruby native thread calls rb_gc() :: empty freelist"); + } + } +#endif if (dont_gc || during_gc) { if (!freelist) { add_heap(); -- 永井 秀利 (九工大 知能情報) nagai@ai.kyutech.ac.jp