Merge pull request #2410 from lioncash/swatomic

Software: Convert most volatile variables to atomics
This commit is contained in:
Markus Wick 2015-05-22 14:11:16 +02:00
commit ad9dae30a8
3 changed files with 29 additions and 27 deletions

View file

@ -2,6 +2,7 @@
// Licensed under GPLv2 // Licensed under GPLv2
// Refer to the license.txt file included. // Refer to the license.txt file included.
#include <atomic>
#include "Common/Atomic.h" #include "Common/Atomic.h"
#include "Common/ChunkFile.h" #include "Common/ChunkFile.h"
#include "Common/CommonTypes.h" #include "Common/CommonTypes.h"
@ -41,8 +42,8 @@ static u8 commandBuffer[commandBufferSize];
static u32 readPos; static u32 readPos;
static u32 writePos; static u32 writePos;
static int et_UpdateInterrupts; static int et_UpdateInterrupts;
static volatile bool interruptSet; static std::atomic<bool> interruptSet;
static volatile bool interruptWaiting; static std::atomic<bool> interruptWaiting;
static CPReg cpreg; // shared between gfx and emulator thread static CPReg cpreg; // shared between gfx and emulator thread
@ -92,8 +93,8 @@ void Init()
readPos = 0; readPos = 0;
writePos = 0; writePos = 0;
interruptSet = false; interruptSet.store(false);
interruptWaiting = false; interruptWaiting.store(false);
g_video_buffer_read_ptr = nullptr; g_video_buffer_read_ptr = nullptr;
g_bSkipCurrentFrame = false; g_bSkipCurrentFrame = false;
@ -195,17 +196,17 @@ void UpdateInterrupts(u64 userdata)
{ {
if (userdata) if (userdata)
{ {
interruptSet = true; interruptSet.store(true);
INFO_LOG(COMMANDPROCESSOR,"Interrupt set"); INFO_LOG(COMMANDPROCESSOR,"Interrupt set");
ProcessorInterface::SetInterrupt(INT_CAUSE_CP, true); ProcessorInterface::SetInterrupt(INT_CAUSE_CP, true);
} }
else else
{ {
interruptSet = false; interruptSet.store(false);
INFO_LOG(COMMANDPROCESSOR,"Interrupt cleared"); INFO_LOG(COMMANDPROCESSOR,"Interrupt cleared");
ProcessorInterface::SetInterrupt(INT_CAUSE_CP, false); ProcessorInterface::SetInterrupt(INT_CAUSE_CP, false);
} }
interruptWaiting = false; interruptWaiting.store(false);
} }
void UpdateInterruptsFromVideoBackend(u64 userdata) void UpdateInterruptsFromVideoBackend(u64 userdata)
@ -285,12 +286,12 @@ static void SetStatus()
bool interrupt = bpInt || ovfInt || undfInt; bool interrupt = bpInt || ovfInt || undfInt;
if (interrupt != interruptSet && !interruptWaiting) if (interrupt != interruptSet.load() && !interruptWaiting.load())
{ {
u64 userdata = interrupt?1:0; u64 userdata = interrupt?1:0;
if (SConfig::GetInstance().m_LocalCoreStartupParameter.bCPUThread) if (SConfig::GetInstance().m_LocalCoreStartupParameter.bCPUThread)
{ {
interruptWaiting = true; interruptWaiting.store(true);
SWCommandProcessor::UpdateInterruptsFromVideoBackend(userdata); SWCommandProcessor::UpdateInterruptsFromVideoBackend(userdata);
} }
else else

View file

@ -3,6 +3,7 @@
// Refer to the license.txt file included. // Refer to the license.txt file included.
#include <algorithm> #include <algorithm>
#include <atomic>
#include <string> #include <string>
#include "Common/CommonTypes.h" #include "Common/CommonTypes.h"
@ -26,7 +27,7 @@ static GLuint program;
static u8 *s_xfbColorTexture[2]; static u8 *s_xfbColorTexture[2];
static int s_currentColorTexture = 0; static int s_currentColorTexture = 0;
static volatile bool s_bScreenshot; static std::atomic<bool> s_bScreenshot;
static std::mutex s_criticalScreenshot; static std::mutex s_criticalScreenshot;
static std::string s_sScreenshotName; static std::string s_sScreenshotName;
@ -37,7 +38,7 @@ static RasterFont* s_pfont = nullptr;
void SWRenderer::Init() void SWRenderer::Init()
{ {
s_bScreenshot = false; s_bScreenshot.store(false);
} }
void SWRenderer::Shutdown() void SWRenderer::Shutdown()
@ -109,7 +110,7 @@ void SWRenderer::SetScreenshot(const char *_szFilename)
{ {
std::lock_guard<std::mutex> lk(s_criticalScreenshot); std::lock_guard<std::mutex> lk(s_criticalScreenshot);
s_sScreenshotName = _szFilename; s_sScreenshotName = _szFilename;
s_bScreenshot = true; s_bScreenshot.store(true);
} }
void SWRenderer::RenderText(const char* pstr, int left, int top, u32 color) void SWRenderer::RenderText(const char* pstr, int left, int top, u32 color)
@ -222,13 +223,13 @@ void SWRenderer::DrawTexture(u8 *texture, int width, int height)
// FIXME: This should add black bars when the game has set the VI to render less than the full xfb. // FIXME: This should add black bars when the game has set the VI to render less than the full xfb.
// Save screenshot // Save screenshot
if (s_bScreenshot) if (s_bScreenshot.load())
{ {
std::lock_guard<std::mutex> lk(s_criticalScreenshot); std::lock_guard<std::mutex> lk(s_criticalScreenshot);
TextureToPng(texture, width * 4, s_sScreenshotName, width, height, false); TextureToPng(texture, width * 4, s_sScreenshotName, width, height, false);
// Reset settings // Reset settings
s_sScreenshotName.clear(); s_sScreenshotName.clear();
s_bScreenshot = false; s_bScreenshot.store(false);
} }
GLsizei glWidth = (GLsizei)GLInterface->GetBackBufferWidth(); GLsizei glWidth = (GLsizei)GLInterface->GetBackBufferWidth();

View file

@ -2,9 +2,9 @@
// Licensed under GPLv2 // Licensed under GPLv2
// Refer to the license.txt file included. // Refer to the license.txt file included.
#include <atomic>
#include <string> #include <string>
#include "Common/Atomic.h"
#include "Common/CommonTypes.h" #include "Common/CommonTypes.h"
#include "Common/FileUtil.h" #include "Common/FileUtil.h"
#include "Common/StringUtil.h" #include "Common/StringUtil.h"
@ -41,7 +41,7 @@
#define VSYNC_ENABLED 0 #define VSYNC_ENABLED 0
static volatile u32 s_swapRequested = false; static std::atomic<bool> s_swapRequested;
static volatile struct static volatile struct
{ {
@ -53,8 +53,8 @@ static volatile struct
namespace SW namespace SW
{ {
static volatile bool fifoStateRun = false; static std::atomic<bool> fifoStateRun;
static volatile bool emuRunningState = false; static std::atomic<bool> emuRunningState;
static std::mutex m_csSWVidOccupied; static std::mutex m_csSWVidOccupied;
std::string VideoSoftware::GetName() const std::string VideoSoftware::GetName() const
@ -144,12 +144,12 @@ void VideoSoftware::PauseAndLock(bool doLock, bool unpauseOnUnlock)
void VideoSoftware::RunLoop(bool enable) void VideoSoftware::RunLoop(bool enable)
{ {
emuRunningState = enable; emuRunningState.store(enable);
} }
void VideoSoftware::EmuStateChange(EMUSTATE_CHANGE newState) void VideoSoftware::EmuStateChange(EMUSTATE_CHANGE newState)
{ {
emuRunningState = (newState == EMUSTATE_CHANGE_PLAY) ? true : false; emuRunningState.store(newState == EMUSTATE_CHANGE_PLAY);
} }
void VideoSoftware::Shutdown() void VideoSoftware::Shutdown()
@ -243,7 +243,7 @@ void VideoSoftware::Video_EndField()
// If we are in dual core mode, notify the GPU thread about the new color texture. // If we are in dual core mode, notify the GPU thread about the new color texture.
if (SConfig::GetInstance().m_LocalCoreStartupParameter.bCPUThread) if (SConfig::GetInstance().m_LocalCoreStartupParameter.bCPUThread)
Common::AtomicStoreRelease(s_swapRequested, true); s_swapRequested.store(true);
else else
SWRenderer::Swap(s_beginFieldArgs.fbWidth, s_beginFieldArgs.fbHeight); SWRenderer::Swap(s_beginFieldArgs.fbWidth, s_beginFieldArgs.fbHeight);
} }
@ -300,10 +300,10 @@ bool VideoSoftware::Video_Screenshot(const std::string& filename)
// Run from the graphics thread // Run from the graphics thread
static void VideoFifo_CheckSwapRequest() static void VideoFifo_CheckSwapRequest()
{ {
if (Common::AtomicLoadAcquire(s_swapRequested)) if (s_swapRequested.load())
{ {
SWRenderer::Swap(s_beginFieldArgs.fbWidth, s_beginFieldArgs.fbHeight); SWRenderer::Swap(s_beginFieldArgs.fbWidth, s_beginFieldArgs.fbHeight);
Common::AtomicStoreRelease(s_swapRequested, false); s_swapRequested.store(false);
} }
} }
@ -313,9 +313,9 @@ static void VideoFifo_CheckSwapRequest()
void VideoSoftware::Video_EnterLoop() void VideoSoftware::Video_EnterLoop()
{ {
std::lock_guard<std::mutex> lk(m_csSWVidOccupied); std::lock_guard<std::mutex> lk(m_csSWVidOccupied);
fifoStateRun = true; fifoStateRun.store(true);
while (fifoStateRun) while (fifoStateRun.load())
{ {
VideoFifo_CheckSwapRequest(); VideoFifo_CheckSwapRequest();
g_video_backend->PeekMessages(); g_video_backend->PeekMessages();
@ -325,7 +325,7 @@ void VideoSoftware::Video_EnterLoop()
Common::YieldCPU(); Common::YieldCPU();
} }
while (!emuRunningState && fifoStateRun) while (!emuRunningState.load() && fifoStateRun.load())
{ {
g_video_backend->PeekMessages(); g_video_backend->PeekMessages();
VideoFifo_CheckSwapRequest(); VideoFifo_CheckSwapRequest();
@ -338,7 +338,7 @@ void VideoSoftware::Video_EnterLoop()
void VideoSoftware::Video_ExitLoop() void VideoSoftware::Video_ExitLoop()
{ {
fifoStateRun = false; fifoStateRun.store(false);
} }
// TODO : could use the OSD class in video common, we would need to implement the Renderer class // TODO : could use the OSD class in video common, we would need to implement the Renderer class