From 5989af937b2e324daf8db67d01550bc3bfd71c79 Mon Sep 17 00:00:00 2001 From: Vitor Santos Costa Date: Fri, 29 Jun 2012 15:38:49 -0500 Subject: [PATCH] MT support in mingw win32. --- H/pl-incl.h | 17 ++++++ Makefile.in | 1 + config.h.in | 1 + include/SWI-Prolog.h | 4 +- library/dialect/swi/fli/swi.c | 2 +- os/SWI-Stream.h | 102 +------------------------------- os/pl-nt.c | 14 +++-- os/pl-thread.h | 107 ++++++++++++++++++++++++++++++++++ 8 files changed, 141 insertions(+), 107 deletions(-) mode change 100644 => 100755 config.h.in mode change 100644 => 100755 include/SWI-Prolog.h mode change 100644 => 100755 os/SWI-Stream.h create mode 100755 os/pl-thread.h diff --git a/H/pl-incl.h b/H/pl-incl.h index 784937ee9..c30e7e0bd 100755 --- a/H/pl-incl.h +++ b/H/pl-incl.h @@ -170,7 +170,9 @@ typedef enum #if __YAP_PROLOG__ #include "pl-yap.h" #if _WIN32 +#ifndef THREADS typedef int pthread_t; +#endif #define __WINDOWS__ 1 #else #include @@ -198,6 +200,21 @@ typedef uintptr_t PL_atomic_t; /* same a word */ #define TRAIL_OVERFLOW (-3) #define ARGUMENT_OVERFLOW (-4) + + /******************************** + * THREADS * + *********************************/ + +#include "pl-thread.h" + +#if O_PLMT + /******************************* + * WINDOWS * + *******************************/ + +#define WM_SIGNALLED (WM_USER+4201) /* how to select a good number!? */ +#endif + /******************************** * UTILITIES * *********************************/ diff --git a/Makefile.in b/Makefile.in index 288434edf..0229b3694 100755 --- a/Makefile.in +++ b/Makefile.in @@ -126,6 +126,7 @@ INTERFACE_HEADERS = \ $(srcdir)/include/YapRegs.h \ $(srcdir)/H/YapTerm.h \ $(srcdir)/library/dialect/bprolog/fli/bprolog.h \ + $(srcdir)/os/pl-thread.h \ $(srcdir)/os/SWI-Stream.h IOLIB_HEADERS=$(srcdir)/os/pl-buffer.h \ diff --git a/config.h.in b/config.h.in old mode 100644 new mode 100755 index 0cce26ee2..346f3580b --- a/config.h.in +++ b/config.h.in @@ -153,6 +153,7 @@ /* Define return type for signal */ #undef RETSIGTYPE +#undef HAVE__CHSIZE_S #undef HAVE__NSGETENVIRON #undef HAVE_ACCESS #undef HAVE_ACOSH diff --git a/include/SWI-Prolog.h b/include/SWI-Prolog.h old mode 100644 new mode 100755 index e81a1c375..439f8dc35 --- a/include/SWI-Prolog.h +++ b/include/SWI-Prolog.h @@ -662,11 +662,13 @@ PL_EXPORT(pl_wchar_t*) PL_atom_generator_w(const pl_wchar_t *pref, * WINDOWS MESSAGES * *******************************/ -#ifdef _WINDOWS_ /* is included */ +#if defined(_MSC_VER) || defined(__MINGW32__) /* is included */ #define PL_MSG_EXCEPTION_RAISED -1 #define PL_MSG_IGNORED 0 #define PL_MSG_HANDLED 1 +#include + PL_EXPORT(LRESULT) PL_win_message_proc(HWND hwnd, UINT message, WPARAM wParam, diff --git a/library/dialect/swi/fli/swi.c b/library/dialect/swi/fli/swi.c index ced763cd9..687bb8175 100755 --- a/library/dialect/swi/fli/swi.c +++ b/library/dialect/swi/fli/swi.c @@ -2595,7 +2595,7 @@ PL_set_engine(PL_engine_t engine, PL_engine_t *old) } pthread_mutex_lock(&(REMOTE_ThreadHandle(nwid).tlock)); - if (REMOTE_ThreadHandle(nwid).pthread_handle) { + if (REMOTE_ThreadHandle(nwid).ref_count) { pthread_mutex_unlock(&(REMOTE_ThreadHandle(nwid).tlock)); if (cwid != nwid) { return PL_ENGINE_INUSE; diff --git a/os/SWI-Stream.h b/os/SWI-Stream.h old mode 100644 new mode 100755 index 658fbfcfb..a0b7169b0 --- a/os/SWI-Stream.h +++ b/os/SWI-Stream.h @@ -94,107 +94,7 @@ typedef int64_t (*Sseek64_function)(void *handle, int64_t pos, int whence); typedef int (*Sclose_function)(void *handle); typedef int (*Scontrol_function)(void *handle, int action, void *arg); -#if defined(THREADS) && !defined(O_PLMT) -#define O_PLMT 1 -#endif - -#if defined(O_PLMT) && defined(PL_KERNEL) -/* Support PL_LOCK in the interface */ -#if THREADS - -#include - -typedef pthread_mutex_t simpleMutex; - -#define simpleMutexInit(p) pthread_mutex_init(p, NULL) -#define simpleMutexDelete(p) pthread_mutex_destroy(p) -#define simpleMutexLock(p) pthread_mutex_lock(p) -#define simpleMutexUnlock(p) pthread_mutex_unlock(p) - -typedef pthread_mutex_t recursiveMutex; - -#define NEED_RECURSIVE_MUTEX_INIT 1 -extern int recursiveMutexInit(recursiveMutex *m); -#define recursiveMutexDelete(p) pthread_mutex_destroy(p) -#define recursiveMutexLock(p) pthread_mutex_lock(p) -#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 */ - const char *name; /* name of the mutex */ - long count; /* # times locked */ - long unlocked; /* # times unlocked */ -#ifdef O_CONTENTION_STATISTICS - long collisions; /* # contentions */ -#endif - struct counting_mutex *next; /* next of allocated chain */ -} counting_mutex; - -extern counting_mutex *allocSimpleMutex(const char *name); -extern void freeSimpleMutex(counting_mutex *m); - -extern counting_mutex _PL_mutexes[]; /* Prolog mutexes */ - -#define L_MISC 0 -#define L_ALLOC 1 -#define L_ATOM 2 -#define L_FLAG 3 -#define L_FUNCTOR 4 -#define L_RECORD 5 -#define L_THREAD 6 -#define L_PREDICATE 7 -#define L_MODULE 8 -#define L_TABLE 9 -#define L_BREAK 10 -#define L_FILE 11 -#define L_PLFLAG 12 -#define L_OP 13 -#define L_INIT 14 -#define L_TERM 15 -#define L_GC 16 -#define L_AGC 17 -#define L_FOREIGN 18 -#define L_OS 19 - -#ifdef O_CONTENTION_STATISTICS -#define countingMutexLock(cm) \ - do \ - { if ( pthread_mutex_trylock(&(cm)->mutex) == EBUSY ) \ - { (cm)->collisions++; \ - pthread_mutex_lock(&(cm)->mutex); \ - } \ - (cm)->count++; \ - } while(0) -#else -#define countingMutexLock(cm) \ - do \ - { simpleMutexLock(&(cm)->mutex); \ - (cm)->count++; \ - } while(0) -#endif -#define countingMutexUnlock(cm) \ - do \ - { (cm)->unlocked++; \ - assert((cm)->unlocked <= (cm)->count); \ - simpleMutexUnlock(&(cm)->mutex); \ - } while(0) - -#define PL_LOCK(id) IF_MT(id, countingMutexLock(&_PL_mutexes[id])) -#define PL_UNLOCK(id) IF_MT(id, countingMutexUnlock(&_PL_mutexes[id])) - -#define IOLOCK recursiveMutex - -#endif - -#else -#define PL_LOCK(X) -#define PL_UNLOCK(X) - -typedef void * IOLOCK; -#endif +#include "pl-thread.h" typedef struct io_functions { Sread_function read; /* fill the buffer */ diff --git a/os/pl-nt.c b/os/pl-nt.c index 3dd8fe66c..6d19a657f 100755 --- a/os/pl-nt.c +++ b/os/pl-nt.c @@ -38,7 +38,7 @@ #include "pl-incl.h" #include "pl-utf8.h" -#include +//#include #include #include "pl-ctype.h" #include @@ -234,10 +234,16 @@ Pause(double t) int ftruncate(int fileno, int64_t length) -{ errno_t e; +{ int e; +#if HAVE__CHSIZE_S + /* not always available in mingw */ if ( (e=_chsize_s(fileno, length)) == 0 ) return 0; +#else + if ( (e=_chsize(fileno, (long)length)) == 0 ) + return 0; +#endif errno = e; return -1; @@ -338,7 +344,7 @@ typedef struct } showtype; static int -get_showCmd(term_t show, int *cmd) +get_showCmd(term_t show, UINT *cmd) { char *s; showtype *st; static showtype types[] = @@ -721,7 +727,7 @@ static int unify_csidl_path(term_t t, int csidl) { wchar_t buf[MAX_PATH]; - if ( SHGetSpecialFolderPathW(0, buf, csidl, FALSE) ) + if ( SHGetFolderPathW(0, csidl, NULL, FALSE, buf) ) { wchar_t *p; for(p=buf; *p; p++) diff --git a/os/pl-thread.h b/os/pl-thread.h new file mode 100755 index 000000000..93b560c9a --- /dev/null +++ b/os/pl-thread.h @@ -0,0 +1,107 @@ +#ifndef PL_THREAD_H + +#define PL_THREAD_H 1 + +#if defined(THREADS) && !defined(O_PLMT) +#define O_PLMT 1 +#endif + +#if defined(O_PLMT) // && defined(PL_KERNEL) +/* Support PL_LOCK in the interface */ +#if THREADS + +#include + +typedef pthread_mutex_t simpleMutex; + +#define simpleMutexInit(p) pthread_mutex_init(p, NULL) +#define simpleMutexDelete(p) pthread_mutex_destroy(p) +#define simpleMutexLock(p) pthread_mutex_lock(p) +#define simpleMutexUnlock(p) pthread_mutex_unlock(p) + +typedef pthread_mutex_t recursiveMutex; + +#define NEED_RECURSIVE_MUTEX_INIT 1 +extern int recursiveMutexInit(recursiveMutex *m); +#define recursiveMutexDelete(p) pthread_mutex_destroy(p) +#define recursiveMutexLock(p) pthread_mutex_lock(p) +#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 */ + const char *name; /* name of the mutex */ + long count; /* # times locked */ + long unlocked; /* # times unlocked */ +#ifdef O_CONTENTION_STATISTICS + long collisions; /* # contentions */ +#endif + struct counting_mutex *next; /* next of allocated chain */ +} counting_mutex; + +extern counting_mutex *allocSimpleMutex(const char *name); +extern void freeSimpleMutex(counting_mutex *m); + +extern counting_mutex _PL_mutexes[]; /* Prolog mutexes */ + +#define L_MISC 0 +#define L_ALLOC 1 +#define L_ATOM 2 +#define L_FLAG 3 +#define L_FUNCTOR 4 +#define L_RECORD 5 +#define L_THREAD 6 +#define L_PREDICATE 7 +#define L_MODULE 8 +#define L_TABLE 9 +#define L_BREAK 10 +#define L_FILE 11 +#define L_PLFLAG 12 +#define L_OP 13 +#define L_INIT 14 +#define L_TERM 15 +#define L_GC 16 +#define L_AGC 17 +#define L_FOREIGN 18 +#define L_OS 19 + +#ifdef O_CONTENTION_STATISTICS +#define countingMutexLock(cm) \ + do \ + { if ( pthread_mutex_trylock(&(cm)->mutex) == EBUSY ) \ + { (cm)->collisions++; \ + pthread_mutex_lock(&(cm)->mutex); \ + } \ + (cm)->count++; \ + } while(0) +#else +#define countingMutexLock(cm) \ + do \ + { simpleMutexLock(&(cm)->mutex); \ + (cm)->count++; \ + } while(0) +#endif +#define countingMutexUnlock(cm) \ + do \ + { (cm)->unlocked++; \ + assert((cm)->unlocked <= (cm)->count); \ + simpleMutexUnlock(&(cm)->mutex); \ + } while(0) + +#define PL_LOCK(id) IF_MT(id, countingMutexLock(&_PL_mutexes[id])) +#define PL_UNLOCK(id) IF_MT(id, countingMutexUnlock(&_PL_mutexes[id])) + +#define IOLOCK recursiveMutex + +#endif + +#else +#define PL_LOCK(X) +#define PL_UNLOCK(X) + +typedef void * IOLOCK; +#endif + +#endif