Index: configure.ac ================================================================== --- configure.ac +++ configure.ac @@ -157,10 +157,12 @@ ;; *) ACX_PTHREAD([ CPPLAGS="$CPPFLAGS $PTHREAD_CFLAGS" LIBS="$LIBS $PTHREAD_LIBS" + AC_DEFINE(OF_HAVE_PTHREADS, 1, + [Whether we have pthreads]) ], [ AC_MSG_ERROR(No supported threads found!)]) ;; esac Index: src/objfw-defs.h.in ================================================================== --- src/objfw-defs.h.in +++ src/objfw-defs.h.in @@ -2,8 +2,9 @@ #undef OF_BIG_ENDIAN #undef OF_GNU_RUNTIME #undef OF_HAVE_ASPRINTF #undef OF_HAVE_GCC_ATOMIC_OPS #undef OF_HAVE_LIBKERN_OSATOMIC_H +#undef OF_HAVE_PTHREADS #undef OF_PLUGINS #undef OF_THREADS #undef SIZE_MAX Index: src/threading.h ================================================================== --- src/threading.h +++ src/threading.h @@ -9,37 +9,41 @@ * the packaging of this file. */ #import "OFMacros.h" -#ifndef _WIN32 +#if !defined(OF_THREADS) || (!defined(OF_HAVE_PTHREADS) && defined(_WIN32)) +# error No threads available! +#endif + +#if defined(OF_HAVE_PTHREADS) #include typedef pthread_t of_thread_t; typedef pthread_mutex_t of_mutex_t; typedef pthread_key_t of_tlskey_t; -#else +#elif defined(_WIN32) #include typedef HANDLE of_thread_t; typedef CRITICAL_SECTION of_mutex_t; typedef DWORD of_tlskey_t; #endif -#ifndef _WIN32 +#if defined(OF_HAVE_PTHREADS) # define of_thread_is_current(t) pthread_equal(t, pthread_self()) # define of_thread_current() pthread_self() -#else +#elif defined(_WIN32) # define of_thread_is_current(t) (t == GetCurrentThread()) # define of_thread_current() GetCurrentThread() #endif static OF_INLINE BOOL of_thread_new(of_thread_t *thread, id (*main)(id), id data) { -#ifndef _WIN32 +#if defined(OF_HAVE_PTHREADS) return (pthread_create(thread, NULL, (void*(*)(void*))main, (void*)data) ? NO : YES); -#else +#elif defined(_WIN32) *thread = CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE)main, (void*)data, 0, NULL); return (thread == NULL ? NO : YES); #endif @@ -46,18 +50,18 @@ } static OF_INLINE BOOL of_thread_join(of_thread_t thread) { -#ifndef _WIN32 +#if defined(OF_HAVE_PTHREADS) void *ret; if (pthread_join(thread, &ret)) return NO; return (ret != PTHREAD_CANCELED ? YES : NO); -#else +#elif defined(_WIN32) if (WaitForSingleObject(thread, INFINITE)) return NO; CloseHandle(thread); @@ -66,13 +70,13 @@ } static OF_INLINE BOOL of_thread_cancel(of_thread_t thread) { -#ifndef _WIN32 +#if defined(OF_HAVE_PTHREADS) return (pthread_cancel(thread) ? NO : YES); -#else +#elif defined(_WIN32) if (thread != INVALID_HANDLE_VALUE) { TerminateThread(thread, 1); CloseHandle(thread); } @@ -81,67 +85,67 @@ } static OF_INLINE BOOL of_mutex_new(of_mutex_t *mutex) { -#ifndef _WIN32 +#if defined(OF_HAVE_PTHREADS) return (pthread_mutex_init(mutex, NULL) ? NO : YES); -#else +#elif defined(_WIN32) InitializeCriticalSection(mutex); return YES; #endif } static OF_INLINE BOOL of_mutex_free(of_mutex_t *mutex) { -#ifndef _WIN32 +#if defined(OF_HAVE_PTHREADS) return (pthread_mutex_destroy(mutex) ? NO : YES); -#else +#elif defined(_WIN32) DeleteCriticalSection(mutex); return YES; #endif } static OF_INLINE BOOL of_mutex_lock(of_mutex_t *mutex) { -#ifndef _WIN32 +#if defined(OF_HAVE_PTHREADS) return (pthread_mutex_lock(mutex) ? NO : YES); -#else +#elif defined(_WIN32) EnterCriticalSection(mutex); return YES; #endif } static OF_INLINE BOOL of_mutex_unlock(of_mutex_t *mutex) { -#ifndef _WIN32 +#if defined(OF_HAVE_PTHREADS) return (pthread_mutex_unlock(mutex) ? NO : YES); -#else +#elif defined(_WIN32) LeaveCriticalSection(mutex); return YES; #endif } static OF_INLINE BOOL of_tlskey_new(of_tlskey_t *key) { -#ifndef _WIN32 +#if defined(OF_HAVE_PTHREADS) return (pthread_key_create(key, NULL) ? NO : YES); -#else +#elif defined(_WIN32) return ((*key = TlsAlloc()) == TLS_OUT_OF_INDEXES ? NO : YES); #endif } static OF_INLINE id of_tlskey_get(of_tlskey_t key) { -#ifndef _WIN32 +#if defined(OF_HAVE_PTHREADS) void *ret = pthread_getspecific(key); -#else +#elif defined(_WIN32) void *ret = TlsGetValue(key); #endif /* NULL and nil might be different! */ if (ret == NULL) @@ -153,21 +157,21 @@ static OF_INLINE BOOL of_tlskey_set(of_tlskey_t key, id obj) { void *p = (obj != nil ? (void*)obj : NULL); -#ifndef _WIN32 +#if defined(OF_HAVE_PTHREADS) return (pthread_setspecific(key, p) ? NO : YES); -#else +#elif defined(_WIN32) return (TlsSetValue(key, p) ? YES : NO); #endif } static OF_INLINE BOOL of_tlskey_free(of_tlskey_t key) { -#ifndef _WIN32 +#if defined(OF_HAVE_PTHREADS) return (pthread_key_delete(key) ? NO : YES); -#else +#elif defined(_WIN32) return (TlsFree(key) ? YES : NO); #endif }