Skip to content

Commit

Permalink
Merge pull request #2530 from psychocrypt/fix-threadLockingDuringStartup
Browse files Browse the repository at this point in the history
fix locking to secure affinity update
  • Loading branch information
fireice-uk authored Oct 14, 2019
2 parents ae15a68 + 0b1f88c commit ffdfa53
Show file tree
Hide file tree
Showing 4 changed files with 10 additions and 6 deletions.
2 changes: 1 addition & 1 deletion xmrstak/backend/amd/minethd.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,7 @@ void minethd::work_main()

order_fix.set_value();
std::unique_lock<std::mutex> lck(thd_aff_set);
lck.release();
lck.unlock();
std::this_thread::yield();

cryptonight_ctx* cpu_ctx;
Expand Down
2 changes: 1 addition & 1 deletion xmrstak/backend/cpu/minethd.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -843,7 +843,7 @@ void minethd::multiway_work_main()

order_fix.set_value();
std::unique_lock<std::mutex> lck(thd_aff_set);
lck.release();
lck.unlock();
std::this_thread::yield();

cryptonight_ctx* ctx[MAX_N];
Expand Down
11 changes: 7 additions & 4 deletions xmrstak/backend/nvidia/minethd.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ minethd::minethd(miner_work& pWork, size_t iNo, const jconf::thd_cfg& cfg)
ctx.memMode = cfg.memMode;
this->affinity = cfg.cpu_aff;

std::unique_lock<std::mutex> lck(thd_aff_set);
std::future<void> numa_guard = numa_promise.get_future();
thread_work_guard = thread_work_promise.get_future();

Expand All @@ -92,14 +93,15 @@ minethd::minethd(miner_work& pWork, size_t iNo, const jconf::thd_cfg& cfg)
* without concurrent threads (CUDA driver is less occupied).
*/
numa_guard.wait();

if(this->affinity >= 0) //-1 means no affinity
if(!cpu::minethd::thd_setaffinity(oWorkThd.native_handle(), affinity))
printer::inst()->print_msg(L1, "WARNING setting affinity failed.");
}

void minethd::start_mining()
{
thread_work_promise.set_value();
if(this->affinity >= 0) //-1 means no affinity
if(!cpu::minethd::thd_setaffinity(oWorkThd.native_handle(), affinity))
printer::inst()->print_msg(L1, "WARNING setting affinity failed.");
}

bool minethd::self_test()
Expand Down Expand Up @@ -194,7 +196,8 @@ void minethd::work_main()

// numa memory bind and gpu memory is initialized
numa_promise.set_value();

std::unique_lock<std::mutex> lck(thd_aff_set);
lck.unlock();
std::this_thread::yield();
// wait until all NVIDIA devices are initialized
thread_work_guard.wait();
Expand Down
1 change: 1 addition & 0 deletions xmrstak/backend/nvidia/minethd.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ class minethd : public iBackend

std::promise<void> numa_promise;
std::promise<void> thread_work_promise;
std::mutex thd_aff_set;

// block thread until all NVIDIA GPUs are initialized
std::future<void> thread_work_guard;
Expand Down

0 comments on commit ffdfa53

Please sign in to comment.