Add threading support on posix

This commit is contained in:
Roman Chistokhodov 2016-09-21 00:25:34 +03:00
parent d32b4b6da1
commit 1c6be18b9d
2 changed files with 76 additions and 2 deletions

View file

@ -512,13 +512,87 @@ void ThreadSetPriority(q_threadpriority type)
setpriority(PRIO_PROCESS, 0, val);
}
#include <stddef.h>
#ifdef __unix__
#include <unistd.h>
static int UnixThreadDefault()
{
long sysconfResult = sysconf(_SC_NPROCESSORS_ONLN);
if (sysconfResult) {
return (int)sysconfResult;
}
return -1;
}
#endif
#ifdef __linux__
#include <sched.h>
static int PlatformThreadDefault()
{
#ifdef _GNU_SOURCE
cpu_set_t cs;
CPU_ZERO(&cs);
if (sched_getaffinity(0, sizeof(cs), &cs) != 0) {
return UnixThreadDefault();
} else {
int i;
int count = 0;
for (i = 0; i < CPU_COUNT(&cs); i++)
{
if (CPU_ISSET(i, &cs))
count++;
}
return count;
}
#else
return UnixThreadDefault();
#endif
}
#elif defined(__FreeBSD__) || defined(__NetBSD__) || defined(__APPLE__)
#include <sys/types.h>
#include <sys/sysctl.h>
static int PlatformThreadDefault()
{
#ifdef __APPLE__
const char* name = "machdep.cpu.core_count";
#else
const char* name = "hw.ncpu";
#endif
unsigned int number;
size_t len = sizeof(unsigned int);
sysctlbyname(name, &number, &len, NULL, 0);
if (number < 1) {
return UnixThreadDefault();
} else {
return (int)number;
}
}
#elif defined(__unix__)
static int PlatformThreadDefault()
{
return UnixThreadDefault();
}
#else
static int PlatformThreadDefault()
{
return -1;
}
#endif
void ThreadSetDefault()
{
if (g_numthreads == -1)
{
g_numthreads = PlatformThreadDefault();
if (g_numthreads == -1) {
g_numthreads = 1;
}
}
}
typedef void* pthread_addr_t;
pthread_mutex_t* my_mutex;

View file

@ -22,7 +22,7 @@ typedef void (*q_threadfunction) (int);
#define DEFAULT_NUMTHREADS -1
#endif
#ifdef SYSTEM_POSIX
#define DEFAULT_NUMTHREADS 1
#define DEFAULT_NUMTHREADS -1
#endif
#define DEFAULT_THREAD_PRIORITY eThreadPriorityNormal