From f2b90a1bb03f677182c821ce7cd04f582b8fa5dc Mon Sep 17 00:00:00 2001 From: "U-w7\\vsc" Date: Mon, 4 Nov 2013 15:31:26 +0000 Subject: [PATCH] improve thread signal support, still far from good --- C/pl-yap.c | 10 ++++++++++ H/pl-incl.h | 1 + configure | 2 ++ include/SWI-Prolog.h | 8 ++++++++ library/dialect/swi/fli/swi.c | 35 +++++++++++++++++++++++++++++++++++ os/pl-thread.h | 3 +-- 6 files changed, 57 insertions(+), 2 deletions(-) diff --git a/C/pl-yap.c b/C/pl-yap.c index ab8cddfb5..69f18ba06 100755 --- a/C/pl-yap.c +++ b/C/pl-yap.c @@ -1297,6 +1297,16 @@ sysError(const char *fm, ...) PL_fail; } +int +raiseSignal(PL_local_data_t *ld, int sig) +{ if (sig == SIG_THREAD_SIGNAL) { + Yap_signal(YAP_ITI_SIGNAL); + return TRUE; + } + fprintf(stderr, "Unsupported signal %d\n", sig); + return FALSE; +} + #if THREADS diff --git a/H/pl-incl.h b/H/pl-incl.h index 807f8255b..f259cd3b3 100755 --- a/H/pl-incl.h +++ b/H/pl-incl.h @@ -888,6 +888,7 @@ extern atom_t lookupUCSAtom(const pl_wchar_t *s, size_t len); extern int toIntegerNumber(Number n, int flags); extern int get_atom_ptr_text(Atom a, PL_chars_t *text); extern int warning(const char *fm, ...); +extern int raiseSignal(PL_local_data_t *ld, int sig); /**** stuff from pl-files.c ****/ void initFiles(void); diff --git a/configure b/configure index 0cd5f3962..9610bf279 100755 --- a/configure +++ b/configure @@ -9675,6 +9675,7 @@ $as_echo_n "checking for $ac_word... " >&6; } if ${ac_cv_prog_JAVAC+:} false; then : $as_echo_n "(cached) " >&6 else +echo $JAVAC if test -n "$JAVAC"; then ac_cv_prog_JAVAC="$JAVAC" # Let the user override the test. else @@ -9682,6 +9683,7 @@ as_save_IFS=$IFS; IFS=$PATH_SEPARATOR for as_dir in $PATH do IFS=$as_save_IFS +echo $as_dir/$ac_word$ac_exec_ext test -z "$as_dir" && as_dir=. for ac_exec_ext in '' $ac_executable_extensions; do if as_fn_executable_p "$as_dir/$ac_word$ac_exec_ext"; then diff --git a/include/SWI-Prolog.h b/include/SWI-Prolog.h index 82bf8e1a2..e22f9976d 100755 --- a/include/SWI-Prolog.h +++ b/include/SWI-Prolog.h @@ -25,6 +25,7 @@ extern "C" { } #endif + #if USE_GMP #include #endif @@ -57,6 +58,12 @@ extern "C" { #endif #endif +#ifndef __WINDOWS__ +#if defined(_MSC_VER) || defined(__MINGW32__) +#define __WINDOWS__ 1 +#endif +#endif + /******************************* * EXPORT * @@ -577,6 +584,7 @@ extern X_API int PL_unify_thread_id(term_t, int); extern X_API int PL_thread_attach_engine(const PL_thread_attr_t *); extern X_API int PL_thread_destroy_engine(void); extern X_API int PL_thread_at_exit(void (*)(void *), void *, int); +extern X_API int PL_thread_raise(int tid, int sig); extern X_API PL_engine_t PL_create_engine(const PL_thread_attr_t *); extern X_API int PL_destroy_engine(PL_engine_t); extern X_API int PL_set_engine(PL_engine_t,PL_engine_t *); diff --git a/library/dialect/swi/fli/swi.c b/library/dialect/swi/fli/swi.c index ebba54c76..55bc7894e 100755 --- a/library/dialect/swi/fli/swi.c +++ b/library/dialect/swi/fli/swi.c @@ -2479,6 +2479,41 @@ X_API int PL_thread_self(void) #endif } +static int +alertThread(int tid) +{ + return pthread_kill(REMOTE_ThreadHandle(tid).pthread_handle, YAP_ALARM_SIGNAL) == 0; +} + +/* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - +PL_thread_raise() is used for re-routing interrupts in the Windows +version, where the signal handler is running from a different thread as +Prolog. +- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */ + +int +PL_thread_raise(int tid, int sig) +{ + PL_LOCK(L_THREAD); + if ( tid < 1 ) + { error: + PL_UNLOCK(L_THREAD); + return FALSE; + } + if ( !REMOTE_ThreadHandle(tid).in_use ) + goto error; + + if ( !raiseSignal(tid, sig) || + !alertThread(tid) ) + goto error; + + PL_UNLOCK(L_THREAD); + + return TRUE; +} + + + X_API int PL_unify_thread_id(term_t t, int i) { CACHE_REGS diff --git a/os/pl-thread.h b/os/pl-thread.h index 6024226df..c105d2989 100755 --- a/os/pl-thread.h +++ b/os/pl-thread.h @@ -28,7 +28,6 @@ extern int recursiveMutexInit(recursiveMutex *m); #define recursiveMutexTryLock(p) pthread_mutex_trylock(p) #define recursiveMutexUnlock(p) pthread_mutex_unlock(p) -#define IF_MT(id, g) if ( id == L_THREAD || GD->thread.enabled ) g typedef struct counting_mutex { simpleMutex mutex; /* mutex itself */ @@ -82,7 +81,7 @@ We assume id == L_THREAD is optimized away if id is known at compile-time - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */ -#define IF_MT(id, g) if ( id == L_THREAD || GD->thread.enabled ) g +#define IF_MT(id, g) if ( id == L_THREAD ) g #ifdef O_CONTENTION_STATISTICS #define countingMutexLock(cm) \