X-Git-Url: http://git.megacz.com/?p=ghc-hetmet.git;a=blobdiff_plain;f=rts%2Fposix%2FOSThreads.c;h=283155345ca0280114c69f10081da6f5ed39be64;hp=ee4958a2e84e079db17b34484fd1336039f5cfa9;hb=83d563cb9ede0ba792836e529b1e2929db926355;hpb=dd3d9333b2e2f6a0959bb4aebbad5bf23e968fb6 diff --git a/rts/posix/OSThreads.c b/rts/posix/OSThreads.c index ee4958a..2831553 100644 --- a/rts/posix/OSThreads.c +++ b/rts/posix/OSThreads.c @@ -7,14 +7,23 @@ * * --------------------------------------------------------------------------*/ -#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 +#include "PosixSource.h" + +#if defined(freebsd_HOST_OS) +/* Inclusion of system headers usually requires __BSD_VISIBLE on FreeBSD, + * because of some specific types, like u_char, u_int, etc. */ +#define __BSD_VISIBLE 1 +#endif + #include "Rts.h" + #if defined(THREADED_RTS) -#include "OSThreads.h" #include "RtsUtils.h" #include "Task.h" @@ -22,10 +31,36 @@ #include #endif +#if defined(darwin_HOST_OS) || defined(freebsd_HOST_OS) +#include +#include +#endif + #if !defined(HAVE_PTHREAD_H) #error pthreads.h is required for the threaded RTS on Posix platforms #endif +#if defined(HAVE_SCHED_H) +#include +#endif + +#if defined(HAVE_SYS_CPUSET_H) +#include +#include +#endif + +#ifdef HAVE_UNISTD_H +#include +#endif + +#if defined(darwin_HOST_OS) +#include +#endif + +#ifdef HAVE_SIGNAL_H +# include +#endif + /* * This (allegedly) OS threads independent layer was initially * abstracted away from code that used Pthreads, so the functions @@ -107,11 +142,7 @@ initMutex(Mutex* pMut) #if defined(DEBUG) pthread_mutexattr_t attr; pthread_mutexattr_init(&attr); -#if defined(linux_HOST_OS) - pthread_mutexattr_settype(&attr,PTHREAD_MUTEX_ERRORCHECK_NP); -#else pthread_mutexattr_settype(&attr,PTHREAD_MUTEX_ERRORCHECK); -#endif pthread_mutex_init(pMut,&attr); #else pthread_mutex_init(pMut,NULL); @@ -183,6 +214,92 @@ 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); +#elif defined(darwin_HOST_OS) || defined(freebsd_HOST_OS) + size_t size = sizeof(nat); + if(0 != sysctlbyname("hw.ncpu",&nproc,&size,NULL,0)) + nproc = 1; +#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); +} + +#elif defined(HAVE_SYS_CPUSET_H) /* FreeBSD 7.1+ */ +void +setThreadAffinity(nat n, nat m) +{ + nat nproc; + cpuset_t cs; + nat i; + + nproc = getNumberOfProcessors(); + CPU_ZERO(&cs); + + for (i = n; i < nproc; i += m) + CPU_SET(i, &cs); + + cpuset_setaffinity(CPU_LEVEL_WHICH, CPU_WHICH_TID, -1, sizeof(cpuset_t), &cs); +} + +#else +void +setThreadAffinity (nat n GNUC3_ATTRIBUTE(__unused__), + nat m GNUC3_ATTRIBUTE(__unused__)) +{ +} +#endif + +void +interruptOSThread (OSThreadId id) +{ + pthread_kill(id, SIGPIPE); +} + #else /* !defined(THREADED_RTS) */ int