Commit 1732d7ac authored by shlevy's avatar shlevy Committed by Ben Gamari

Define thread primitives if they're supported.

On iOS, we use the pthread-based implementation of Itimer.c even for a
non-threaded RTS. Since 999c464d, this relies on synchronization
primitives like Mutex, so ensure those primitives are defined whenever
they are supported, even if !THREADED_RTS.

Fixes #12799.

Reviewers: erikd, austin, simonmar, bgamari

Reviewed By: simonmar, bgamari

Subscribers: thomie

Differential Revision: https://phabricator.haskell.org/D2712

GHC Trac Issues: #12799
parent 36e36227
......@@ -15,15 +15,13 @@
#ifndef RTS_OSTHREADS_H
#define RTS_OSTHREADS_H
#if defined(THREADED_RTS) /* to near the end */
#if defined(HAVE_PTHREAD_H) && !defined(mingw32_HOST_OS)
#if CMINUSMINUS
#define ACQUIRE_LOCK(mutex) foreign "C" pthread_mutex_lock(mutex)
#define RELEASE_LOCK(mutex) foreign "C" pthread_mutex_unlock(mutex)
#define ASSERT_LOCK_HELD(mutex) /* nothing */
#define OS_ACQUIRE_LOCK(mutex) foreign "C" pthread_mutex_lock(mutex)
#define OS_RELEASE_LOCK(mutex) foreign "C" pthread_mutex_unlock(mutex)
#define OS_ASSERT_LOCK_HELD(mutex) /* nothing */
#else
......@@ -47,7 +45,7 @@ typedef pthread_key_t ThreadLocalKey;
#endif
/* Always check the result of lock and unlock. */
#define ACQUIRE_LOCK(mutex) \
#define OS_ACQUIRE_LOCK(mutex) \
LOCK_DEBUG_BELCH("ACQUIRE_LOCK", mutex); \
if (pthread_mutex_lock(mutex) == EDEADLK) { \
barf("multiple ACQUIRE_LOCK: %s %d", __FILE__,__LINE__); \
......@@ -61,7 +59,7 @@ EXTERN_INLINE int TRY_ACQUIRE_LOCK(pthread_mutex_t *mutex)
return pthread_mutex_trylock(mutex);
}
#define RELEASE_LOCK(mutex) \
#define OS_RELEASE_LOCK(mutex) \
LOCK_DEBUG_BELCH("RELEASE_LOCK", mutex); \
if (pthread_mutex_unlock(mutex) != 0) { \
barf("RELEASE_LOCK: I do not own this lock: %s %d", __FILE__,__LINE__); \
......@@ -72,7 +70,7 @@ EXTERN_INLINE int TRY_ACQUIRE_LOCK(pthread_mutex_t *mutex)
// have been created with PTHREAD_MUTEX_ERRORCHECK, otherwise this
// assertion will hang. We always initialise mutexes with
// PTHREAD_MUTEX_ERRORCHECK when DEBUG is on (see rts/posix/OSThreads.h).
#define ASSERT_LOCK_HELD(mutex) ASSERT(pthread_mutex_lock(mutex) == EDEADLK)
#define OS_ASSERT_LOCK_HELD(mutex) ASSERT(pthread_mutex_lock(mutex) == EDEADLK)
#endif // CMINUSMINUS
......@@ -83,9 +81,9 @@ EXTERN_INLINE int TRY_ACQUIRE_LOCK(pthread_mutex_t *mutex)
/* We jump through a hoop here to get a CCall EnterCriticalSection
and LeaveCriticalSection, as that's what C-- wants. */
#define ACQUIRE_LOCK(mutex) foreign "stdcall" EnterCriticalSection(mutex)
#define RELEASE_LOCK(mutex) foreign "stdcall" LeaveCriticalSection(mutex)
#define ASSERT_LOCK_HELD(mutex) /* nothing */
#define OS_ACQUIRE_LOCK(mutex) foreign "stdcall" EnterCriticalSection(mutex)
#define OS_RELEASE_LOCK(mutex) foreign "stdcall" LeaveCriticalSection(mutex)
#define OS_ASSERT_LOCK_HELD(mutex) /* nothing */
#else
......@@ -115,23 +113,23 @@ typedef CRITICAL_SECTION Mutex;
#ifdef LOCK_DEBUG
#define ACQUIRE_LOCK(mutex) \
#define OS_ACQUIRE_LOCK(mutex) \
debugBelch("ACQUIRE_LOCK(0x%p) %s %d\n", mutex,__FILE__,__LINE__); \
EnterCriticalSection(mutex)
#define RELEASE_LOCK(mutex) \
#define OS_RELEASE_LOCK(mutex) \
debugBelch("RELEASE_LOCK(0x%p) %s %d\n", mutex,__FILE__,__LINE__); \
LeaveCriticalSection(mutex)
#define ASSERT_LOCK_HELD(mutex) /* nothing */
#define OS_ASSERT_LOCK_HELD(mutex) /* nothing */
#else
#define ACQUIRE_LOCK(mutex) EnterCriticalSection(mutex)
#define OS_ACQUIRE_LOCK(mutex) EnterCriticalSection(mutex)
#define TRY_ACQUIRE_LOCK(mutex) (TryEnterCriticalSection(mutex) == 0)
#define RELEASE_LOCK(mutex) LeaveCriticalSection(mutex)
#define OS_RELEASE_LOCK(mutex) LeaveCriticalSection(mutex)
// I don't know how to do this. TryEnterCriticalSection() doesn't do
// the right thing.
#define ASSERT_LOCK_HELD(mutex) /* nothing */
#define OS_ASSERT_LOCK_HELD(mutex) /* nothing */
#endif
......@@ -141,22 +139,22 @@ typedef HANDLE Mutex;
// casting to (Mutex *) here required due to use in .cmm files where
// the argument has (void *) type.
#define ACQUIRE_LOCK(mutex) \
#define OS_ACQUIRE_LOCK(mutex) \
if (WaitForSingleObject(*((Mutex *)mutex),INFINITE) == WAIT_FAILED) { \
barf("WaitForSingleObject: %d", GetLastError()); \
}
#define RELEASE_LOCK(mutex) \
#define OS_RELEASE_LOCK(mutex) \
if (ReleaseMutex(*((Mutex *)mutex)) == 0) { \
barf("ReleaseMutex: %d", GetLastError()); \
}
#define ASSERT_LOCK_HELD(mutex) /* nothing */
#define OS_ASSERT_LOCK_HELD(mutex) /* nothing */
#endif
#endif // CMINUSMINUS
# else
# elif defined(THREADED_RTS)
# error "Threads not supported"
# endif
......@@ -205,6 +203,12 @@ void setThreadNode (uint32_t node);
void releaseThreadNode (void);
#endif // !CMINUSMINUS
#ifdef THREADED_RTS
#define ACQUIRE_LOCK(l) OS_ACQUIRE_LOCK(l)
#define RELEASE_LOCK(l) OS_RELEASE_LOCK(l)
#define ASSERT_LOCK_HELD(l) OS_ASSERT_LOCK_HELD(l)
#else
#define ACQUIRE_LOCK(l)
......
......@@ -35,7 +35,6 @@
#endif
#endif
#if defined(THREADED_RTS)
#include "RtsUtils.h"
#include "Task.h"
......@@ -215,6 +214,8 @@ freeThreadLocalKey (ThreadLocalKey *key)
}
}
#if defined(THREADED_RTS)
static void *
forkOS_createThreadWrapper ( void * entry )
{
......@@ -266,6 +267,23 @@ getNumberOfProcessors (void)
return nproc;
}
#else /* !defined(THREADED_RTS) */
int
forkOS_createThread ( HsStablePtr entry STG_UNUSED )
{
return -1;
}
void freeThreadingResources (void) { /* nothing */ }
uint32_t getNumberOfProcessors (void)
{
return 1;
}
#endif /* defined(THREADED_RTS) */
#if defined(HAVE_SCHED_H) && defined(HAVE_SCHED_SETAFFINITY)
// Schedules the thread to run on CPU n of m. m may be less than the
// number of physical CPUs, in which case, the thread will be allowed
......@@ -353,23 +371,6 @@ interruptOSThread (OSThreadId id)
pthread_kill(id, SIGPIPE);
}
#else /* !defined(THREADED_RTS) */
int
forkOS_createThread ( HsStablePtr entry STG_UNUSED )
{
return -1;
}
void freeThreadingResources (void) { /* nothing */ }
uint32_t getNumberOfProcessors (void)
{
return 1;
}
#endif /* defined(THREADED_RTS) */
KernelThreadId kernelThreadId (void)
{
#if defined(linux_HOST_OS)
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment