X-Git-Url: http://git.megacz.com/?a=blobdiff_plain;f=rts%2Fposix%2FOSThreads.c;h=84b111fcb7404f4795a9b51be19f5b1583352ad3;hb=5cdb1ff5e889d3361e80e7b01bcb90b682dd9136;hp=cff37824cbc40cf1485d87c29295684be8ff0ab7;hpb=a91cc8496bf4ea7fe1e3ad9d97836152f3939ffa;p=ghc-hetmet.git diff --git a/rts/posix/OSThreads.c b/rts/posix/OSThreads.c index cff3782..84b111f 100644 --- a/rts/posix/OSThreads.c +++ b/rts/posix/OSThreads.c @@ -7,8 +7,9 @@ * * --------------------------------------------------------------------------*/ -#if defined(DEBUG) && defined(__linux__) +#if defined(__linux__) /* We want GNU extensions in DEBUG mode for mutex error checking */ +/* We also want the affinity API, which requires _GNU_SOURCE */ #define _GNU_SOURCE #endif @@ -26,6 +27,18 @@ #error pthreads.h is required for the threaded RTS on Posix platforms #endif +#if defined(HAVE_SCHED_H) +#include +#endif + +#ifdef HAVE_UNISTD_H +#include +#endif + +#if defined(darwin_HOST_OS) +#include +#endif + /* * This (allegedly) OS threads independent layer was initially * abstracted away from code that used Pthreads, so the functions @@ -93,19 +106,32 @@ osThreadId() return pthread_self(); } +rtsBool +osThreadIsAlive(OSThreadId id STG_UNUSED) +{ + // no good way to implement this on POSIX, AFAICT. Returning true + // is safe. + return rtsTrue; +} + void initMutex(Mutex* pMut) { -#if defined(DEBUG) && defined(linux_HOST_OS) +#if defined(DEBUG) pthread_mutexattr_t attr; pthread_mutexattr_init(&attr); - pthread_mutexattr_settype(&attr,PTHREAD_MUTEX_ERRORCHECK_NP); + pthread_mutexattr_settype(&attr,PTHREAD_MUTEX_ERRORCHECK); pthread_mutex_init(pMut,&attr); #else pthread_mutex_init(pMut,NULL); #endif return; } +void +closeMutex(Mutex* pMut) +{ + pthread_mutex_destroy(pMut); +} void newThreadLocalKey (ThreadLocalKey *key) @@ -135,6 +161,15 @@ setThreadLocalVar (ThreadLocalKey *key, void *value) } } +void +freeThreadLocalKey (ThreadLocalKey *key) +{ + int r; + if ((r = pthread_key_delete(*key)) != 0) { + barf("freeThreadLocalKey: %s", strerror(r)); + } +} + static void * forkOS_createThreadWrapper ( void * entry ) { @@ -157,6 +192,65 @@ forkOS_createThread ( HsStablePtr entry ) return result; } +nat +getNumberOfProcessors (void) +{ + static nat nproc = 0; + + if (nproc == 0) { +#if defined(HAVE_SYSCONF) && defined(_SC_NPROCESSORS_ONLN) + nproc = sysconf(_SC_NPROCESSORS_ONLN); +#elif defined(HAVE_SYSCONF) && defined(_SC_NPROCESSORS_CONF) + nproc = sysconf(_SC_NPROCESSORS_CONF); +#else + nproc = 1; +#endif + } + + return nproc; +} + +#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 +// to run on CPU n, n+m, n+2m etc. +void +setThreadAffinity (nat n, nat m) +{ + nat nproc; + cpu_set_t cs; + nat i; + + nproc = getNumberOfProcessors(); + CPU_ZERO(&cs); + for (i = n; i < nproc; i+=m) { + CPU_SET(i, &cs); + } + sched_setaffinity(0, sizeof(cpu_set_t), &cs); +} + +#elif defined(darwin_HOST_OS) && defined(THREAD_AFFINITY_POLICY) +// Schedules the current thread in the affinity set identified by tag n. +void +setThreadAffinity (nat n, nat m GNUC3_ATTRIBUTE(__unused__)) +{ + thread_affinity_policy_data_t policy; + + policy.affinity_tag = n; + thread_policy_set(mach_thread_self(), + THREAD_AFFINITY_POLICY, + (thread_policy_t) &policy, + THREAD_AFFINITY_POLICY_COUNT); +} + +#else +void +setThreadAffinity (nat n GNUC3_ATTRIBUTE(__unused__), + nat m GNUC3_ATTRIBUTE(__unused__)) +{ +} +#endif + #else /* !defined(THREADED_RTS) */ int