diff --git a/src/backend/common/Thread.h b/src/backend/common/Thread.h index ab88441c..fb6c618b 100644 --- a/src/backend/common/Thread.h +++ b/src/backend/common/Thread.h @@ -1,12 +1,6 @@ /* XMRig - * Copyright 2010 Jeff Garzik - * Copyright 2012-2014 pooler - * Copyright 2014 Lucas Jones - * Copyright 2014-2016 Wolf9466 - * Copyright 2016 Jay D Dee - * Copyright 2017-2018 XMR-Stak , - * Copyright 2018-2019 SChernykh - * Copyright 2016-2019 XMRig , + * Copyright (c) 2018-2020 SChernykh + * Copyright (c) 2016-2020 XMRig , * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by @@ -27,12 +21,17 @@ #include "backend/common/interfaces/IWorker.h" -#include "base/tools/Object.h" #include +#ifdef XMRIG_OS_APPLE +# include +# include +#endif + + namespace xmrig { @@ -46,21 +45,48 @@ public: XMRIG_DISABLE_COPY_MOVE_DEFAULT(Thread) inline Thread(IBackend *backend, size_t id, const T &config) : m_id(id), m_config(config), m_backend(backend) {} + +# ifdef XMRIG_OS_APPLE + inline ~Thread() { pthread_join(m_thread, nullptr); delete m_worker; } + + inline void start(void *(*callback)(void *)) + { + if (m_config.affinity >= 0) { + pthread_create_suspended_np(&m_thread, nullptr, callback, this); + + mach_port_t mach_thread = pthread_mach_thread_np(m_thread); + thread_affinity_policy_data_t policy = { static_cast(m_config.affinity + 1) }; + + thread_policy_set(mach_thread, THREAD_AFFINITY_POLICY, reinterpret_cast(&policy), THREAD_AFFINITY_POLICY_COUNT); + thread_resume(mach_thread); + } + else { + pthread_create(&m_thread, nullptr, callback, this); + } + } +# else inline ~Thread() { m_thread.join(); delete m_worker; } + inline void start(void *(*callback)(void *)) { m_thread = std::thread(callback, this); } +# endif + inline const T &config() const { return m_config; } inline IBackend *backend() const { return m_backend; } inline IWorker *worker() const { return m_worker; } inline size_t id() const { return m_id; } inline void setWorker(IWorker *worker) { m_worker = worker; } - inline void start(void (*callback) (void *)) { m_thread = std::thread(callback, this); } private: const size_t m_id = 0; const T m_config; IBackend *m_backend; IWorker *m_worker = nullptr; + + #ifdef XMRIG_OS_APPLE + pthread_t m_thread{}; +# else std::thread m_thread; +# endif }; diff --git a/src/backend/common/Workers.cpp b/src/backend/common/Workers.cpp index f015b5f7..9282a53c 100644 --- a/src/backend/common/Workers.cpp +++ b/src/backend/common/Workers.cpp @@ -174,7 +174,7 @@ xmrig::IWorker *xmrig::Workers::create(Thread *) template -void xmrig::Workers::onReady(void *arg) +void *xmrig::Workers::onReady(void *arg) { auto handle = static_cast* >(arg); @@ -187,13 +187,15 @@ void xmrig::Workers::onReady(void *arg) handle->backend()->start(worker, false); delete worker; - return; + return nullptr; } assert(handle->backend() != nullptr); handle->setWorker(worker); handle->backend()->start(worker, true); + + return nullptr; } diff --git a/src/backend/common/Workers.h b/src/backend/common/Workers.h index 1313384c..0ef3b889 100644 --- a/src/backend/common/Workers.h +++ b/src/backend/common/Workers.h @@ -65,7 +65,7 @@ public: private: static IWorker *create(Thread *handle); - static void onReady(void *arg); + static void *onReady(void *arg); void start(const std::vector &data, bool sleep); diff --git a/src/backend/cuda/CudaLaunchData.cpp b/src/backend/cuda/CudaLaunchData.cpp index 8a066c32..2f791d8b 100644 --- a/src/backend/cuda/CudaLaunchData.cpp +++ b/src/backend/cuda/CudaLaunchData.cpp @@ -1,13 +1,6 @@ /* XMRig - * Copyright 2010 Jeff Garzik - * Copyright 2012-2014 pooler - * Copyright 2014 Lucas Jones - * Copyright 2014-2016 Wolf9466 - * Copyright 2016 Jay D Dee - * Copyright 2017-2018 XMR-Stak , - * Copyright 2018 Lee Clagett - * Copyright 2018-2019 SChernykh - * Copyright 2016-2019 XMRig , + * Copyright (c) 2018-2020 SChernykh + * Copyright (c) 2016-2020 XMRig , * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by @@ -32,6 +25,7 @@ xmrig::CudaLaunchData::CudaLaunchData(const Miner *miner, const Algorithm &algor algorithm(algorithm), device(device), thread(thread), + affinity(thread.affinity()), miner(miner) { } diff --git a/src/backend/cuda/CudaLaunchData.h b/src/backend/cuda/CudaLaunchData.h index bb8b0fb2..26a8310a 100644 --- a/src/backend/cuda/CudaLaunchData.h +++ b/src/backend/cuda/CudaLaunchData.h @@ -1,13 +1,6 @@ /* XMRig - * Copyright 2010 Jeff Garzik - * Copyright 2012-2014 pooler - * Copyright 2014 Lucas Jones - * Copyright 2014-2016 Wolf9466 - * Copyright 2016 Jay D Dee - * Copyright 2017-2018 XMR-Stak , - * Copyright 2018 Lee Clagett - * Copyright 2018-2019 SChernykh - * Copyright 2016-2019 XMRig , + * Copyright (c) 2018-2020 SChernykh + * Copyright (c) 2016-2020 XMRig , * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by @@ -56,6 +49,7 @@ public: const Algorithm algorithm; const CudaDevice &device; const CudaThread thread; + const int64_t affinity; const Miner *miner; const uint32_t benchSize = 0; }; diff --git a/src/base/kernel/Platform_hwloc.cpp b/src/base/kernel/Platform_hwloc.cpp index 8ec06975..8d9d9f59 100644 --- a/src/base/kernel/Platform_hwloc.cpp +++ b/src/base/kernel/Platform_hwloc.cpp @@ -26,6 +26,7 @@ #include +#ifndef XMRIG_OS_APPLE bool xmrig::Platform::setThreadAffinity(uint64_t cpu_id) { auto cpu = static_cast(Cpu::info()); @@ -44,3 +45,4 @@ bool xmrig::Platform::setThreadAffinity(uint64_t cpu_id) std::this_thread::sleep_for(std::chrono::milliseconds(1)); return result; } +#endif diff --git a/src/base/kernel/Platform_mac.cpp b/src/base/kernel/Platform_mac.cpp index 27797ff7..4bd7a40a 100644 --- a/src/base/kernel/Platform_mac.cpp +++ b/src/base/kernel/Platform_mac.cpp @@ -19,8 +19,6 @@ #include #include -#include -#include #include #include #include @@ -50,18 +48,10 @@ char *xmrig::Platform::createUserAgent() } -#ifndef XMRIG_FEATURE_HWLOC bool xmrig::Platform::setThreadAffinity(uint64_t cpu_id) { - thread_port_t mach_thread; - thread_affinity_policy_data_t policy = { static_cast(cpu_id) }; - mach_thread = pthread_mach_thread_np(pthread_self()); - - const bool result = (thread_policy_set(mach_thread, THREAD_AFFINITY_POLICY, (thread_policy_t)&policy, 1) == KERN_SUCCESS); - std::this_thread::sleep_for(std::chrono::milliseconds(1)); - return result; + return true; } -#endif void xmrig::Platform::setProcessPriority(int)