mirror of
https://github.com/RPCSX/rpcsx.git
synced 2026-04-05 06:26:49 +00:00
SSemaphore basic implementation
Set for RSX
This commit is contained in:
parent
7fca980887
commit
1c4ae999d6
8 changed files with 127 additions and 18 deletions
75
Utilities/SSemaphore.cpp
Normal file
75
Utilities/SSemaphore.cpp
Normal file
|
|
@ -0,0 +1,75 @@
|
|||
#include "stdafx.h"
|
||||
#include "Utilities/SSemaphore.h"
|
||||
|
||||
bool SSemaphore::wait(u64 timeout)
|
||||
{
|
||||
std::unique_lock<std::mutex> lock(m_cv_mutex);
|
||||
|
||||
u64 counter = 0;
|
||||
while (true)
|
||||
{
|
||||
if (Emu.IsStopped())
|
||||
{
|
||||
return false;
|
||||
}
|
||||
if (timeout && counter >= timeout)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
m_cond.wait_for(lock, std::chrono::milliseconds(1));
|
||||
counter++;
|
||||
|
||||
std::lock_guard<std::mutex> lock(m_mutex);
|
||||
if (m_count)
|
||||
{
|
||||
m_count--;
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
bool SSemaphore::try_wait()
|
||||
{
|
||||
std::lock_guard<std::mutex> lock(m_mutex);
|
||||
|
||||
if (m_count)
|
||||
{
|
||||
m_count--;
|
||||
return true;
|
||||
}
|
||||
else
|
||||
{
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
void SSemaphore::post(u32 value)
|
||||
{
|
||||
std::lock_guard<std::mutex> lock(m_mutex);
|
||||
|
||||
if (m_count >= m_max)
|
||||
{
|
||||
value = 0;
|
||||
}
|
||||
else if (value > (m_max - m_count))
|
||||
{
|
||||
value = m_max - m_count;
|
||||
}
|
||||
|
||||
while (value)
|
||||
{
|
||||
m_count++;
|
||||
value--;
|
||||
m_cond.notify_one();
|
||||
}
|
||||
}
|
||||
|
||||
bool SSemaphore::post_and_wait()
|
||||
{
|
||||
if (try_wait()) return false;
|
||||
|
||||
post();
|
||||
wait();
|
||||
|
||||
return true;
|
||||
}
|
||||
34
Utilities/SSemaphore.h
Normal file
34
Utilities/SSemaphore.h
Normal file
|
|
@ -0,0 +1,34 @@
|
|||
#pragma once
|
||||
|
||||
class SSemaphore
|
||||
{
|
||||
const u32 m_max;
|
||||
u32 m_count;
|
||||
std::mutex m_mutex, m_cv_mutex;
|
||||
std::condition_variable m_cond;
|
||||
|
||||
public:
|
||||
SSemaphore(u32 value, u32 max = 1)
|
||||
: m_max(max > 0 ? max : 0xffffffff)
|
||||
, m_count(value > m_max ? m_max : value)
|
||||
{
|
||||
}
|
||||
|
||||
SSemaphore()
|
||||
: m_max(0xffffffff)
|
||||
, m_count(0)
|
||||
{
|
||||
}
|
||||
|
||||
~SSemaphore()
|
||||
{
|
||||
}
|
||||
|
||||
bool wait(u64 timeout = 0);
|
||||
|
||||
bool try_wait();
|
||||
|
||||
void post(u32 value = 1);
|
||||
|
||||
bool post_and_wait();
|
||||
};
|
||||
|
|
@ -6,6 +6,7 @@
|
|||
#include <mutex>
|
||||
#include <atomic>
|
||||
#include <condition_variable>
|
||||
#include <Utilities/SSemaphore.h>
|
||||
|
||||
class ThreadExec;
|
||||
|
||||
|
|
@ -138,16 +139,6 @@ public:
|
|||
bool IsBusy() const { return m_busy; }
|
||||
};
|
||||
|
||||
static __forceinline bool SemaphorePostAndWait(rSemaphore& sem)
|
||||
{
|
||||
if(sem.TryWait() != rSEMA_BUSY) return false;
|
||||
|
||||
sem.Post();
|
||||
sem.Wait();
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/*
|
||||
class StepThread : public ThreadBase
|
||||
{
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue