* configure.in: always check existence of the pthread library

* ruby.h: define macros for ruby's native thread check
* eval.c: add ruby's native thread check
* gc.c: ditto


git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@4989 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
This commit is contained in:
nagai 2003-11-20 03:50:32 +00:00
parent 531022ace3
commit 340a511fdf
5 changed files with 93 additions and 83 deletions

View File

@ -1,3 +1,13 @@
Thu Nov 20 12:42:47 2003 Hidetoshi NAGAI <nagai@ai.kyutech.ac.jp>
* configure.in: always check existence of the pthread library
* ruby.h: define macros for ruby's native thread check
* eval.c: add ruby's native thread check
* gc.c: ditto
Wed Nov 19 14:45:18 2003 Minero Aoki <aamine@loveruby.net> Wed Nov 19 14:45:18 2003 Minero Aoki <aamine@loveruby.net>
* lib/net/http.rb (to_ary): print more friendly warning message. * lib/net/http.rb (to_ary): print more friendly warning message.

View File

@ -662,35 +662,36 @@ int main()
], rb_cv_stack_grow_dir=-1, rb_cv_stack_grow_dir=+1, rb_cv_stack_grow_dir=0)]) ], 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) AC_DEFINE_UNQUOTED(STACK_GROW_DIRECTION, $rb_cv_stack_grow_dir)
dnl default value for $KANJI AC_CHECK_LIB(pthread, pthread_mutex_init,
DEFAULT_KCODE="KCODE_NONE" rb_with_pthread=yes, rb_with_pthread=no)
if test "$rb_with_pthread" = "no"; then
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, AC_CHECK_LIB(pthread, __pthread_mutex_init,
rb_use_pthread_ext=yes, rb_use_pthread_ext=no) rb_with_pthread=yes, rb_with_pthread=no)
fi fi
if test "$rb_use_pthread_ext" = "yes"; then if test "$rb_with_pthread" = "yes"; then
LIBS="-lpthread $LIBS" LIBS="-lpthread $LIBS"
AC_DEFINE(_REENTRANT) AC_DEFINE(_REENTRANT)
AC_DEFINE(_THREAD_SAFE) AC_DEFINE(_THREAD_SAFE)
AC_DEFINE(HAVE_LIBPTHREAD) AC_DEFINE(HAVE_LIBPTHREAD)
else else
AC_CHECK_LIB(pthreads, pthread_mutex_init, AC_CHECK_LIB(pthreads, pthread_mutex_init,
rb_use_pthread_ext=yes, rb_use_pthread_ext=no) rb_with_pthread=yes, rb_with_pthread=no)
if test "$rb_use_pthread_ext" = "yes"; then if test "$rb_with_pthread" = "yes"; then
LIBS="-lpthreads $LIBS" LIBS="-lpthreads $LIBS"
AC_DEFINE(_REENTRANT) AC_DEFINE(_REENTRANT)
AC_DEFINE(_THREAD_SAFE) AC_DEFINE(_THREAD_SAFE)
AC_DEFINE(HAVE_LIBPTHREAD) 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 else
AC_CHECK_LIB(c_r, pthread_mutex_init, AC_CHECK_LIB(c_r, pthread_mutex_init,
rb_use_pthread_ext=yes, rb_use_pthread_ext=no) rb_with_pthread=yes, rb_with_pthread=no)
if test "$rb_use_pthread_ext" = "yes"; then if test "$rb_with_pthread" = "yes"; then
if test "$with_libc_r" = "yes"; then if test "$with_libc_r" = "yes"; then
if test "$rb_cv_supplementary_lib_c_r" = "yes"; then if test "$rb_cv_supplementary_lib_c_r" = "yes"; then
AC_DEFINE(_REENTRANT) AC_DEFINE(_REENTRANT)
@ -704,10 +705,15 @@ AC_ARG_WITH(pthread-ext,
AC_DEFINE(_THREAD_SAFE) AC_DEFINE(_THREAD_SAFE)
AC_DEFINE(HAVE_LIBPTHREAD) AC_DEFINE(HAVE_LIBPTHREAD)
fi fi
else
AC_MSG_WARN("Don't know how to find pthread library on your system -- thread support disabled")
fi fi
fi fi
fi fi
]) fi
dnl default value for $KANJI
DEFAULT_KCODE="KCODE_NONE"
AC_ARG_WITH(default-kcode, AC_ARG_WITH(default-kcode,
[ --with-default-kcode=CODE specify default value for \$KCODE (utf8|euc|sjis|none)], [ --with-default-kcode=CODE specify default value for \$KCODE (utf8|euc|sjis|none)],

58
eval.c
View File

@ -33,10 +33,6 @@
#include "st.h" #include "st.h"
#include "dln.h" #include "dln.h"
#if defined(HAVE_LIBPTHREAD) && defined(USE_PTHREAD_EXTLIB)
#include <pthread.h>
#endif
#ifdef __APPLE__ #ifdef __APPLE__
#include <crt_externs.h> #include <crt_externs.h>
#endif #endif
@ -1161,6 +1157,14 @@ void Init_stack _((void*));
void Init_heap _((void)); void Init_heap _((void));
void Init_ext _((void)); void Init_ext _((void));
#ifdef HAVE_NATIVETHREAD
static rb_nativethread_t ruby_thid;
int
is_ruby_native_thread() {
return NATIVETHREAD_EQUAL(ruby_thid, NATIVETHREAD_CURRENT());
}
#endif
void void
ruby_init() ruby_init()
{ {
@ -1172,6 +1176,9 @@ ruby_init()
if (initialized) if (initialized)
return; return;
initialized = 1; initialized = 1;
#ifdef HAVE_NATIVETHREAD
ruby_thid = NATIVETHREAD_CURRENT();
#endif
ruby_frame = top_frame = &frame; ruby_frame = top_frame = &frame;
ruby_iter = &iter; ruby_iter = &iter;
@ -8647,11 +8654,6 @@ find_bad_fds(dst, src, max)
return test; return test;
} }
#if defined(HAVE_LIBPTHREAD) && defined(USE_PTHREAD_EXTLIB)
static pthread_t thid;
static pthread_mutex_t mtx = PTHREAD_MUTEX_INITIALIZER;
#endif
void void
rb_thread_schedule() rb_thread_schedule()
{ {
@ -8668,10 +8670,12 @@ rb_thread_schedule()
int n, max; int n, max;
int need_select = 0; int need_select = 0;
int select_timeout = 0; int select_timeout = 0;
#if defined(HAVE_LIBPTHREAD) && defined(USE_PTHREAD_EXTLIB)
int st;
#endif
#ifdef HAVE_NATIVETHREAD
if (!is_ruby_native_thread()) {
rb_bug("cross-thread violation on rb_thread_schedule()");
}
#endif
rb_thread_pending = 0; rb_thread_pending = 0;
if (curr_thread == curr_thread->next if (curr_thread == curr_thread->next
&& curr_thread->status == THREAD_RUNNABLE) && curr_thread->status == THREAD_RUNNABLE)
@ -8680,16 +8684,6 @@ rb_thread_schedule()
next = 0; next = 0;
curr = curr_thread; /* starting thread */ 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) { while (curr->status == THREAD_KILLED) {
curr = curr->prev; curr = curr->prev;
} }
@ -8887,22 +8881,12 @@ rb_thread_schedule()
} }
next->wait_for = 0; next->wait_for = 0;
if (next->status == THREAD_RUNNABLE && next == curr_thread) { 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; return;
} }
/* context switch */ /* context switch */
if (curr == curr_thread) { if (curr == curr_thread) {
if (THREAD_SAVE_CONTEXT(curr)) { if (THREAD_SAVE_CONTEXT(curr)) {
#if defined(HAVE_LIBPTHREAD) && defined(USE_PTHREAD_EXTLIB)
if (st != EBUSY) {
pthread_mutex_unlock(& mtx);
}
#endif
return; return;
} }
} }
@ -8912,19 +8896,9 @@ rb_thread_schedule()
if (!(next->flags & THREAD_TERMINATING)) { if (!(next->flags & THREAD_TERMINATING)) {
next->flags |= THREAD_TERMINATING; next->flags |= THREAD_TERMINATING;
/* terminate; execute ensure-clause if any */ /* 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); 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); rb_thread_restore_context(next, RESTORE_NORMAL);
} }

5
gc.c
View File

@ -1216,6 +1216,11 @@ rb_gc()
jmp_buf save_regs_gc_mark; jmp_buf save_regs_gc_mark;
SET_STACK_END; SET_STACK_END;
#ifdef HAVE_NATIVETHREAD
if (!is_ruby_native_thread()) {
rb_bug("cross-thread violation on rb_gc()");
}
#endif
if (dont_gc || during_gc) { if (dont_gc || during_gc) {
if (!freelist) { if (!freelist) {
add_heap(); add_heap();

15
ruby.h
View File

@ -671,6 +671,21 @@ rb_special_const_p(obj)
static char *dln_libs_to_be_linked[] = { EXTLIB, 0 }; static char *dln_libs_to_be_linked[] = { EXTLIB, 0 };
#endif #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 int is_ruby_native_thread();
#endif
#if defined(__cplusplus) #if defined(__cplusplus)
} /* extern "C" { */ } /* extern "C" { */
#endif #endif