aboutsummaryrefslogtreecommitdiff
path: root/src/porting.cpp
diff options
context:
space:
mode:
authorkwolekr <mirrorisim@gmail.com>2013-02-21 17:44:14 -0500
committerkwolekr <mirrorisim@gmail.com>2013-02-25 23:08:41 -0500
commit60e6284f30d31e11c1a464d9a8b3c5c392ceb3f0 (patch)
tree20fe2138ce254232e369c20482a39c5e0da17450 /src/porting.cpp
parent67c59645ecd76cbf7a8186d0850ab5950e9ea22a (diff)
downloadminetest-60e6284f30d31e11c1a464d9a8b3c5c392ceb3f0.tar.gz
minetest-60e6284f30d31e11c1a464d9a8b3c5c392ceb3f0.tar.bz2
minetest-60e6284f30d31e11c1a464d9a8b3c5c392ceb3f0.zip
Tune queue limits, some other adjustments
Diffstat (limited to 'src/porting.cpp')
-rw-r--r--src/porting.cpp132
1 files changed, 115 insertions, 17 deletions
diff --git a/src/porting.cpp b/src/porting.cpp
index 58d71e4aa..84df15b30 100644
--- a/src/porting.cpp
+++ b/src/porting.cpp
@@ -131,29 +131,127 @@ void signal_handler_init(void)
#endif
+
/*
Multithreading support
*/
int getNumberOfProcessors() {
- #if defined(_SC_NPROCESSORS_ONLN)
- return sysconf(_SC_NPROCESSORS_ONLN);
- #elif defined(__FreeBSD__) || defined(__APPLE__)
- unsigned int len, count;
- len = sizeof(count);
- return sysctlbyname("hw.ncpu", &count, &len, NULL, 0);
- #elif defined(_GNU_SOURCE)
- return get_nprocs();
- #elif defined(_WIN32)
- SYSTEM_INFO sysinfo;
- GetSystemInfo(&sysinfo);
- return sysinfo.dwNumberOfProcessors;
- #elif defined(PTW32_VERSION) || defined(__hpux)
- return pthread_num_processors_np();
- #else
- return 1;
- #endif
+#if defined(_SC_NPROCESSORS_ONLN)
+
+ return sysconf(_SC_NPROCESSORS_ONLN);
+
+#elif defined(__FreeBSD__) || defined(__APPLE__)
+
+ unsigned int len, count;
+ len = sizeof(count);
+ return sysctlbyname("hw.ncpu", &count, &len, NULL, 0);
+
+#elif defined(_GNU_SOURCE)
+
+ return get_nprocs();
+
+#elif defined(_WIN32)
+
+ SYSTEM_INFO sysinfo;
+ GetSystemInfo(&sysinfo);
+ return sysinfo.dwNumberOfProcessors;
+
+#elif defined(PTW32_VERSION) || defined(__hpux)
+
+ return pthread_num_processors_np();
+
+#else
+
+ return 1;
+
+#endif
}
+
+bool threadBindToProcessor(threadid_t tid, int pnumber) {
+#if defined(_WIN32)
+
+ HANDLE hThread = OpenThread(THREAD_ALL_ACCESS, 0, tid);
+ if (!hThread)
+ return false;
+
+ bool success = SetThreadAffinityMask(hThread, 1 << pnumber) != 0;
+
+ CloseHandle(hThread);
+ return success;
+
+#elif (defined(__FreeBSD__) && (__FreeBSD_version >= 702106)) \
+ || defined(__linux) || defined(linux)
+
+ cpu_set_t cpuset;
+
+ CPU_ZERO(&cpuset);
+ CPU_SET(pnumber, &cpuset);
+ return pthread_setaffinity_np(tid, sizeof(cpuset), &cpuset) == 0;
+
+#elif defined(__sun) || defined(sun)
+
+ return processor_bind(P_LWPID, MAKE_LWPID_PTHREAD(tid),
+ pnumber, NULL) == 0;
+
+#elif defined(_AIX)
+
+ return bindprocessor(BINDTHREAD, (tid_t)tid, pnumber) == 0;
+
+#elif defined(__hpux) || defined(hpux)
+
+ pthread_spu_t answer;
+
+ return pthread_processor_bind_np(PTHREAD_BIND_ADVISORY_NP,
+ &answer, pnumber, tid) == 0;
+
+#elif defined(__APPLE__)
+
+ struct thread_affinity_policy tapol;
+
+ thread_port_t threadport = pthread_mach_thread_np(tid);
+ tapol.affinity_tag = pnumber + 1;
+ return thread_policy_set(threadport, THREAD_AFFINITY_POLICY,
+ (thread_policy_t)&tapol, THREAD_AFFINITY_POLICY_COUNT) == KERN_SUCCESS;
+
+#else
+
+ return false;
+
+#endif
+}
+
+
+bool threadSetPriority(threadid_t tid, int prio) {
+#if defined(_WIN32)
+
+ HANDLE hThread = OpenThread(THREAD_ALL_ACCESS, 0, tid);
+ if (!hThread)
+ return false;
+
+ bool success = SetThreadPriority(hThread, prio) != 0;
+
+ CloseHandle(hThread);
+ return success;
+
+#else
+
+ struct sched_param sparam;
+ int policy;
+
+ if (pthread_getschedparam(tid, &policy, &sparam) != 0)
+ return false;
+
+ int min = sched_get_priority_min(policy);
+ int max = sched_get_priority_max(policy);
+
+ sparam.sched_priority = min + prio * (max - min) / THREAD_PRIORITY_HIGHEST;
+ return pthread_setschedparam(tid, policy, &sparam) == 0;
+
+#endif
+}
+
+
/*
Path mangler
*/