]> git.lizzy.rs Git - minetest.git/blobdiff - src/porting.cpp
Cleanup jthread and fix win32 build
[minetest.git] / src / porting.cpp
index 58d71e4aa6db6287ddc8d57da7f660f789063ef2..a080a44e5bb5cbae1944003fad47779f1f1ff61e 100644 (file)
@@ -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
 */
@@ -275,8 +373,13 @@ void initializePaths()
        //TODO: Get path of executable. This assumes working directory is bin/
        dstream<<"WARNING: Relative path not properly supported on this platform"
                        <<std::endl;
-       path_share = std::string("..");
-       path_user = std::string("..");
+
+       /* scriptapi no longer allows paths that start with "..", so assuming that
+          the current working directory is bin/, strip off the last component. */
+       char *cwd = getcwd(NULL, 0);
+       pathRemoveFile(cwd, '/');
+       path_share = std::string(cwd);
+       path_user = std::string(cwd);
 
        #endif
 
@@ -379,7 +482,7 @@ void initializePaths()
 
        path_user = std::string(getenv("HOME")) + "/Library/Application Support/" + PROJECT_NAME;
 
-       #elif defined(__FreeBSD__)
+       #else // FreeBSD, and probably many other POSIX-like systems.
 
        path_share = STATIC_SHAREDIR;
        path_user = std::string(getenv("HOME")) + "/." + PROJECT_NAME;