Skip to content

Commit

Permalink
Merge pull request #2431 from lioncash/boolatomic
Browse files Browse the repository at this point in the history
Core: Convert some volatile bools to atomics
  • Loading branch information
comex committed Jun 5, 2015
2 parents 01ec940 + de2e843 commit d571e1a
Show file tree
Hide file tree
Showing 6 changed files with 63 additions and 64 deletions.
26 changes: 13 additions & 13 deletions Source/Core/Core/HW/EXI_DeviceGecko.cpp
Expand Up @@ -14,7 +14,7 @@
u16 GeckoSockServer::server_port;
int GeckoSockServer::client_count;
std::thread GeckoSockServer::connectionThread;
volatile bool GeckoSockServer::server_running;
std::atomic<bool> GeckoSockServer::server_running;
std::mutex GeckoSockServer::connection_lock;
std::queue<std::unique_ptr<sf::TcpSocket>> GeckoSockServer::waiting_socks;

Expand All @@ -31,13 +31,13 @@ GeckoSockServer::~GeckoSockServer()
{
--client_count;

client_running = false;
client_running.store(false);
clientThread.join();
}

if (client_count <= 0)
{
server_running = false;
server_running.store(false);
connectionThread.join();
}
}
Expand All @@ -48,14 +48,14 @@ void GeckoSockServer::GeckoConnectionWaiter()

sf::TcpListener server;
server_port = 0xd6ec; // "dolphin gecko"
for (int bind_tries = 0; bind_tries <= 10 && !server_running; bind_tries++)
for (int bind_tries = 0; bind_tries <= 10 && !server_running.load(); bind_tries++)
{
server_running = server.listen(server_port) == sf::Socket::Done;
if (!server_running)
server_running.store(server.listen(server_port) == sf::Socket::Done);
if (!server_running.load())
server_port++;
}

if (!server_running)
if (!server_running.load())
return;

Core::DisplayMessage(
Expand All @@ -65,7 +65,7 @@ void GeckoSockServer::GeckoConnectionWaiter()
server.setBlocking(false);

auto new_client = std::make_unique<sf::TcpSocket>();
while (server_running)
while (server_running.load())
{
if (server.accept(*new_client) == sf::Socket::Done)
{
Expand All @@ -89,7 +89,7 @@ bool GeckoSockServer::GetAvailableSock()
client = std::move(waiting_socks.front());
if (clientThread.joinable())
{
client_running = false;
client_running.store(false);
clientThread.join();

recv_fifo = std::deque<u8>();
Expand All @@ -106,13 +106,13 @@ bool GeckoSockServer::GetAvailableSock()

void GeckoSockServer::ClientThread()
{
client_running = true;
client_running.store(true);

Common::SetCurrentThreadName("Gecko Client");

client->setBlocking(false);

while (client_running)
while (client_running.load())
{
bool did_nothing = true;

Expand All @@ -124,7 +124,7 @@ void GeckoSockServer::ClientThread()
std::size_t got = 0;

if (client->receive(&data[0], ArraySize(data), got) == sf::Socket::Disconnected)
client_running = false;
client_running.store(false);

if (got != 0)
{
Expand All @@ -141,7 +141,7 @@ void GeckoSockServer::ClientThread()
send_fifo.clear();

if (client->send(&packet[0], packet.size()) == sf::Socket::Disconnected)
client_running = false;
client_running.store(false);
}
} // unlock transfer

Expand Down
7 changes: 4 additions & 3 deletions Source/Core/Core/HW/EXI_DeviceGecko.h
Expand Up @@ -4,6 +4,7 @@

#pragma once

#include <atomic>
#include <deque>
#include <memory>
#include <mutex>
Expand Down Expand Up @@ -31,14 +32,14 @@ class GeckoSockServer
std::deque<u8> recv_fifo;

private:
static int client_count;
volatile bool client_running;
static int client_count;
std::atomic<bool> client_running;

// Only ever one server thread
static void GeckoConnectionWaiter();

static u16 server_port;
static volatile bool server_running;
static std::atomic<bool> server_running;
static std::thread connectionThread;
static std::mutex connection_lock;
static std::queue<std::unique_ptr<sf::TcpSocket>> waiting_socks;
Expand Down
42 changes: 20 additions & 22 deletions Source/Core/Core/HW/WiimoteReal/WiimoteReal.cpp
Expand Up @@ -302,7 +302,7 @@ void Wiimote::Update()
void Wiimote::Prepare(int _index)
{
m_index = _index;
m_need_prepare = true;
m_need_prepare.store(true);
}

bool Wiimote::PrepareOnThread()
Expand Down Expand Up @@ -406,27 +406,27 @@ static unsigned int CalculateWantedBB()

void WiimoteScanner::WantWiimotes(bool do_want)
{
m_want_wiimotes = do_want;
m_want_wiimotes.store(do_want);
}


void WiimoteScanner::WantBB(bool do_want)
{
m_want_bb = do_want;
m_want_bb.store(do_want);
}

void WiimoteScanner::StartScanning()
{
if (!m_run_thread)
if (!m_run_thread.load())
{
m_run_thread = true;
m_run_thread.store(true);
m_scan_thread = std::thread(&WiimoteScanner::ThreadFunc, this);
}
}

void WiimoteScanner::StopScanning()
{
m_run_thread = false;
m_run_thread.store(false);
if (m_scan_thread.joinable())
{
m_scan_thread.join();
Expand All @@ -448,14 +448,14 @@ void WiimoteScanner::ThreadFunc()

NOTICE_LOG(WIIMOTE, "Wiimote scanning has started.");

while (m_run_thread)
while (m_run_thread.load())
{
std::vector<Wiimote*> found_wiimotes;
Wiimote* found_board = nullptr;

//NOTICE_LOG(WIIMOTE, "In loop");

if (m_want_wiimotes || m_want_bb)
if (m_want_wiimotes.load() || m_want_bb.load())
{
FindWiimotes(found_wiimotes, found_board);
}
Expand All @@ -470,9 +470,10 @@ void WiimoteScanner::ThreadFunc()
// TODO: this is a fairly lame place for this
CheckForDisconnectedWiimotes();

if (m_want_wiimotes)
if (m_want_wiimotes.load())
HandleFoundWiimotes(found_wiimotes);
if (m_want_bb && found_board)

if (m_want_bb.load() && found_board)
TryToConnectBalanceBoard(found_board);

//std::this_thread::yield();
Expand All @@ -484,42 +485,39 @@ void WiimoteScanner::ThreadFunc()

bool Wiimote::Connect()
{
m_thread_ready = false;
m_thread_ready.store(false);
StartThread();
WaitReady();
return IsConnected();
}

void Wiimote::StartThread()
{
m_run_thread = true;
m_run_thread.store(true);
m_wiimote_thread = std::thread(&Wiimote::ThreadFunc, this);
}

void Wiimote::StopThread()
{
m_run_thread = false;
m_run_thread.store(false);
IOWakeup();
if (m_wiimote_thread.joinable())
m_wiimote_thread.join();
}

void Wiimote::SetReady()
{
if (!m_thread_ready)
if (!m_thread_ready.load())
{
{
std::lock_guard<std::mutex> Guard(m_thread_ready_mutex);
m_thread_ready = true;
}
m_thread_ready.store(true);
m_thread_ready_cond.notify_all();
}
}

void Wiimote::WaitReady()
{
std::unique_lock<std::mutex> lock(m_thread_ready_mutex);
while (!m_thread_ready)
while (!m_thread_ready.load())
{
m_thread_ready_cond.wait(lock);
}
Expand All @@ -546,11 +544,11 @@ void Wiimote::ThreadFunc()
}

// main loop
while (IsConnected() && m_run_thread)
while (IsConnected() && m_run_thread.load())
{
if (m_need_prepare)
if (m_need_prepare.load())
{
m_need_prepare = false;
m_need_prepare.store(false);
if (!PrepareOnThread())
{
ERROR_LOG(WIIMOTE, "Wiimote::PrepareOnThread failed. Disconnecting Wiimote %d.", m_index + 1);
Expand Down
13 changes: 7 additions & 6 deletions Source/Core/Core/HW/WiimoteReal/WiimoteReal.h
Expand Up @@ -4,6 +4,7 @@

#pragma once

#include <atomic>
#include <condition_variable>
#include <mutex>
#include <string>
Expand Down Expand Up @@ -96,11 +97,11 @@ friend class WiimoteEmu::Wiimote;

std::thread m_wiimote_thread;
// Whether to keep running the thread.
volatile bool m_run_thread;
std::atomic<bool> m_run_thread;
// Whether to call PrepareOnThread.
volatile bool m_need_prepare;
std::atomic<bool> m_need_prepare;
// Whether the thread has finished ConnectInternal.
volatile bool m_thread_ready;
std::atomic<bool> m_thread_ready;
std::mutex m_thread_ready_mutex;
std::condition_variable m_thread_ready_cond;

Expand Down Expand Up @@ -134,9 +135,9 @@ class WiimoteScanner

std::thread m_scan_thread;

volatile bool m_run_thread;
volatile bool m_want_wiimotes;
volatile bool m_want_bb;
std::atomic<bool> m_run_thread;
std::atomic<bool> m_want_wiimotes;
std::atomic<bool> m_want_bb;

#if defined(_WIN32)
void CheckDeviceType(std::basic_string<TCHAR> &devicepath, bool &real_wiimote, bool &is_bb);
Expand Down

0 comments on commit d571e1a

Please sign in to comment.