From cf5547227d9fffd9fb0043ce0b5633b831536eb6 Mon Sep 17 00:00:00 2001 From: David CARLIER Date: Sun, 23 Aug 2020 14:39:32 +0100 Subject: Complete Haiku platform support. (#10311) Fixing linkage/libraries missing issue. Implements missing platform specifics. --- src/threading/thread.cpp | 4 ++++ 1 file changed, 4 insertions(+) (limited to 'src/threading/thread.cpp') diff --git a/src/threading/thread.cpp b/src/threading/thread.cpp index e0f808c4d..f678a09be 100644 --- a/src/threading/thread.cpp +++ b/src/threading/thread.cpp @@ -225,6 +225,10 @@ void Thread::setName(const std::string &name) pthread_setname_np(name.c_str()); +#elif defined(__HAIKU__) + + rename_thread(find_thread(NULL), name.c_str()); + #elif defined(_MSC_VER) // Windows itself doesn't support thread names, -- cgit v1.2.3 From b262184acf34896f36b4270aba29546fc5b0e65b Mon Sep 17 00:00:00 2001 From: David CARLIER Date: Tue, 25 Aug 2020 19:50:41 +0100 Subject: NetBSD build fix proposal. (#10308) Fixing thread naming call and let the class setting RANDOM_MIN/RANDOM_MAX. --- src/noise.h | 7 +++++++ src/threading/thread.cpp | 2 +- 2 files changed, 8 insertions(+), 1 deletion(-) (limited to 'src/threading/thread.cpp') diff --git a/src/noise.h b/src/noise.h index 7b5e83251..613879890 100644 --- a/src/noise.h +++ b/src/noise.h @@ -29,6 +29,13 @@ #include "exceptions.h" #include "util/string.h" +#if defined(RANDOM_MIN) +#undef RANDOM_MIN +#endif +#if defined(RANDOM_MAX) +#undef RANDOM_MAX +#endif + extern FlagDesc flagdesc_noiseparams[]; // Note: this class is not polymorphic so that its high level of diff --git a/src/threading/thread.cpp b/src/threading/thread.cpp index f678a09be..eb51516c6 100644 --- a/src/threading/thread.cpp +++ b/src/threading/thread.cpp @@ -219,7 +219,7 @@ void Thread::setName(const std::string &name) #elif defined(__NetBSD__) - pthread_setname_np(pthread_self(), name.c_str()); + pthread_setname_np(pthread_self(), "%s", const_cast(name.c_str())); #elif defined(__APPLE__) -- cgit v1.2.3 From 0683bea283d456253de343f37720789382ece6b2 Mon Sep 17 00:00:00 2001 From: David CARLIER Date: Wed, 9 Sep 2020 18:12:58 +0100 Subject: Add NetBSD cpu affinity support code (#10378) --- src/threading/thread.cpp | 9 +++++++++ 1 file changed, 9 insertions(+) (limited to 'src/threading/thread.cpp') diff --git a/src/threading/thread.cpp b/src/threading/thread.cpp index eb51516c6..e19e6ae60 100644 --- a/src/threading/thread.cpp +++ b/src/threading/thread.cpp @@ -33,6 +33,8 @@ DEALINGS IN THE SOFTWARE. #include #elif defined(__FreeBSD__) || defined(__OpenBSD__) || defined(__DragonFly__) #include +#elif defined(__NetBSD__) + #include #elif defined(_MSC_VER) struct THREADNAME_INFO { DWORD dwType; // Must be 0x1000 @@ -285,7 +287,14 @@ bool Thread::bindToProcessor(unsigned int proc_number) CPU_SET(proc_number, &cpuset); return pthread_setaffinity_np(getThreadHandle(), sizeof(cpuset), &cpuset) == 0; +#elif defined(__NetBSD__) + cpuset_t *cpuset = cpuset_create(); + if (cpuset == NULL) + return false; + int r = pthread_setaffinity_np(getThreadHandle(), cpuset_size(cpuset), cpuset); + cpuset_destroy(cpuset); + return r == 0; #elif defined(__sun) || defined(sun) return processor_bind(P_LWPID, P_MYID, proc_number, NULL) == 0 -- cgit v1.2.3 From 3fb1f45301880a3aa901b752af1ecc912efe5915 Mon Sep 17 00:00:00 2001 From: Sebastien Marie Date: Thu, 10 Sep 2020 12:19:18 +0200 Subject: Remove Thread::kill() and related unittest (#10317) Closes: #6065 --- src/threading/thread.cpp | 54 +++++++++++++++++------------------------ src/threading/thread.h | 8 ------ src/unittest/test_threading.cpp | 25 ------------------- 3 files changed, 22 insertions(+), 65 deletions(-) (limited to 'src/threading/thread.cpp') diff --git a/src/threading/thread.cpp b/src/threading/thread.cpp index e19e6ae60..5cfc60995 100644 --- a/src/threading/thread.cpp +++ b/src/threading/thread.cpp @@ -73,7 +73,28 @@ Thread::Thread(const std::string &name) : Thread::~Thread() { - kill(); + // kill the thread if running + if (!m_running) { + wait(); + } else { + + m_running = false; + +#if defined(_WIN32) + // See https://msdn.microsoft.com/en-us/library/hh920601.aspx#thread__native_handle_method + TerminateThread((HANDLE) m_thread_obj->native_handle(), 0); + CloseHandle((HANDLE) m_thread_obj->native_handle()); +#else + // We need to pthread_kill instead on Android since NDKv5's pthread + // implementation is incomplete. +# ifdef __ANDROID__ + pthread_kill(getThreadHandle(), SIGKILL); +# else + pthread_cancel(getThreadHandle()); +# endif + wait(); +#endif + } // Make sure start finished mutex is unlocked before it's destroyed if (m_start_finished_mutex.try_lock()) @@ -138,37 +159,6 @@ bool Thread::wait() } -bool Thread::kill() -{ - if (!m_running) { - wait(); - return false; - } - - m_running = false; - -#if defined(_WIN32) - // See https://msdn.microsoft.com/en-us/library/hh920601.aspx#thread__native_handle_method - TerminateThread((HANDLE) m_thread_obj->native_handle(), 0); - CloseHandle((HANDLE) m_thread_obj->native_handle()); -#else - // We need to pthread_kill instead on Android since NDKv5's pthread - // implementation is incomplete. -# ifdef __ANDROID__ - pthread_kill(getThreadHandle(), SIGKILL); -# else - pthread_cancel(getThreadHandle()); -# endif - wait(); -#endif - - m_retval = nullptr; - m_joinable = false; - m_request_stop = false; - - return true; -} - bool Thread::getReturnValue(void **ret) { diff --git a/src/threading/thread.h b/src/threading/thread.h index 3946335f5..45fb171da 100644 --- a/src/threading/thread.h +++ b/src/threading/thread.h @@ -74,14 +74,6 @@ public: */ bool stop(); - /* - * Immediately terminates the thread. - * This should be used with extreme caution, as the thread will not have - * any opportunity to release resources it may be holding (such as memory - * or locks). - */ - bool kill(); - /* * Waits for thread to finish. * Note: This does not stop a thread, you have to do this on your own. diff --git a/src/unittest/test_threading.cpp b/src/unittest/test_threading.cpp index 8d4d814fd..65ef7c02d 100644 --- a/src/unittest/test_threading.cpp +++ b/src/unittest/test_threading.cpp @@ -31,7 +31,6 @@ public: void runTests(IGameDef *gamedef); void testStartStopWait(); - void testThreadKill(); void testAtomicSemaphoreThread(); }; @@ -40,7 +39,6 @@ static TestThreading g_test_instance; void TestThreading::runTests(IGameDef *gamedef) { TEST(testStartStopWait); - TEST(testThreadKill); TEST(testAtomicSemaphoreThread); } @@ -111,29 +109,6 @@ void TestThreading::testStartStopWait() } -void TestThreading::testThreadKill() -{ - SimpleTestThread *thread = new SimpleTestThread(300); - - UASSERT(thread->start() == true); - - // kill()ing is quite violent, so let's make sure our victim is sleeping - // before we do this... so we don't corrupt the rest of the program's state - sleep_ms(100); - UASSERT(thread->kill() == true); - - // The state of the thread object should be reset if all went well - UASSERT(thread->isRunning() == false); - UASSERT(thread->start() == true); - UASSERT(thread->stop() == true); - UASSERT(thread->wait() == true); - - // kill() after already waiting should fail. - UASSERT(thread->kill() == false); - - delete thread; -} - class AtomicTestThread : public Thread { public: -- cgit v1.2.3