Small refactoring

This commit is contained in:
Nekotekina 2014-09-15 02:17:24 +04:00
parent 8f1a8450be
commit 597d07bf24
46 changed files with 348 additions and 372 deletions

View file

@ -4,12 +4,6 @@
union u128
{
struct
{
u64 hi;
u64 lo;
};
u64 _u64[2];
s64 _s64[2];
u32 _u32[4];
@ -92,31 +86,27 @@ union u128
//operator bool() const { return _u64[0] != 0 || _u64[1] != 0; }
static u128 from128(u64 hi, u64 lo)
{
u128 ret = { hi, lo };
return ret;
}
static u128 from64(u64 src)
{
u128 ret = { 0, src };
return ret;
}
static u128 from32(u32 src)
static u128 from64(u64 _0, u64 _1 = 0)
{
u128 ret;
ret._u32[0] = src;
ret._u32[1] = 0;
ret._u32[2] = 0;
ret._u32[3] = 0;
ret._u64[0] = _0;
ret._u64[1] = _1;
return ret;
}
static u128 from32(u32 _0, u32 _1 = 0, u32 _2 = 0, u32 _3 = 0)
{
u128 ret;
ret._u32[0] = _0;
ret._u32[1] = _1;
ret._u32[2] = _2;
ret._u32[3] = _3;
return ret;
}
static u128 fromBit(u32 bit)
{
u128 ret;
u128 ret = {};
ret._bit[bit] = true;
return ret;
}
@ -128,42 +118,42 @@ union u128
bool operator == (const u128& right) const
{
return (lo == right.lo) && (hi == right.hi);
return (_u64[0] == right._u64[0]) && (_u64[1] == right._u64[1]);
}
bool operator != (const u128& right) const
{
return (lo != right.lo) || (hi != right.hi);
return (_u64[0] != right._u64[0]) || (_u64[1] != right._u64[1]);
}
u128 operator | (const u128& right) const
{
return from128(hi | right.hi, lo | right.lo);
return from64(_u64[0] | right._u64[0], _u64[1] | right._u64[1]);
}
u128 operator & (const u128& right) const
{
return from128(hi & right.hi, lo & right.lo);
return from64(_u64[0] & right._u64[0], _u64[1] & right._u64[1]);
}
u128 operator ^ (const u128& right) const
{
return from128(hi ^ right.hi, lo ^ right.lo);
return from64(_u64[0] ^ right._u64[0], _u64[1] ^ right._u64[1]);
}
u128 operator ~ () const
{
return from128(~hi, ~lo);
return from64(~_u64[0], ~_u64[1]);
}
void clear()
{
hi = lo = 0;
_u64[1] = _u64[0] = 0;
}
std::string to_hex() const
{
return fmt::Format("%08x%08x%08x%08x", _u32[3], _u32[2], _u32[1], _u32[0]);
return fmt::Format("%16llx%16llx", _u64[1], _u64[0]);
}
std::string to_xyzw() const
@ -174,8 +164,8 @@ union u128
static __forceinline u128 byteswap(const u128 val)
{
u128 ret;
ret.lo = _byteswap_uint64(val.hi);
ret.hi = _byteswap_uint64(val.lo);
ret._u64[0] = _byteswap_uint64(val._u64[1]);
ret._u64[1] = _byteswap_uint64(val._u64[0]);
return ret;
}
};

View file

@ -20,24 +20,3 @@ void SM_Sleep()
std::this_thread::sleep_for(std::chrono::milliseconds(1));
}
}
thread_local size_t g_this_thread_id = 0;
size_t SM_GetCurrentThreadId()
{
return g_this_thread_id ? g_this_thread_id : g_this_thread_id = std::hash<std::thread::id>()(std::this_thread::get_id());
}
u32 SM_GetCurrentCPUThreadId()
{
if (CPUThread* t = GetCurrentCPUThread())
{
return t->GetId();
}
return 0;
}
be_t<u32> SM_GetCurrentCPUThreadIdBE()
{
return be_t<u32>::MakeFromLE(SM_GetCurrentCPUThreadId());
}

View file

@ -2,9 +2,6 @@
bool SM_IsAborted();
void SM_Sleep();
size_t SM_GetCurrentThreadId();
u32 SM_GetCurrentCPUThreadId();
be_t<u32> SM_GetCurrentCPUThreadIdBE();
enum SMutexResult
{
@ -134,44 +131,5 @@ public:
}
};
template<typename T, typename Tid, Tid (get_tid)()>
class SMutexLockerBase
{
T& sm;
public:
const Tid tid;
__forceinline SMutexLockerBase(T& _sm)
: sm(_sm)
, tid(get_tid())
{
if (!tid)
{
if (!SM_IsAborted())
{
assert(!"SMutexLockerBase: thread id == 0");
}
return;
}
sm.lock(tid);
}
__forceinline ~SMutexLockerBase()
{
if (tid) sm.unlock(tid);
}
};
typedef SMutexBase<size_t>
SMutexGeneral;
typedef SMutexBase<u32>
SMutex;
typedef SMutexBase<be_t<u32>>
SMutexBE;
typedef SMutexLockerBase<SMutexGeneral, size_t, SM_GetCurrentThreadId>
SMutexGeneralLocker;
typedef SMutexLockerBase<SMutex, u32, SM_GetCurrentCPUThreadId>
SMutexLocker;
typedef SMutexLockerBase<SMutexBE, be_t<u32>, SM_GetCurrentCPUThreadIdBE>
SMutexBELocker;

View file

@ -12,7 +12,27 @@ NamedThreadBase* GetCurrentNamedThread()
void SetCurrentNamedThread(NamedThreadBase* value)
{
g_tls_this_thread = value;
auto old_value = g_tls_this_thread;
if (old_value == value)
{
return;
}
if (value && value->m_tls_assigned.exchange(true))
{
LOG_ERROR(GENERAL, "Thread '%s' was already assigned to g_tls_this_thread of another thread", value->GetThreadName().c_str());
g_tls_this_thread = nullptr;
}
else
{
g_tls_this_thread = value;
}
if (old_value)
{
old_value->m_tls_assigned = false;
}
}
std::string NamedThreadBase::GetThreadName() const
@ -73,14 +93,15 @@ void ThreadBase::Start()
}
catch (const char* e)
{
LOG_ERROR(HLE, "%s: %s", GetThreadName().c_str(), e);
LOG_ERROR(GENERAL, "%s: %s", GetThreadName().c_str(), e);
}
catch (const std::string& e)
{
LOG_ERROR(HLE, "%s: %s", GetThreadName().c_str(), e.c_str());
LOG_ERROR(GENERAL, "%s: %s", GetThreadName().c_str(), e.c_str());
}
m_alive = false;
SetCurrentNamedThread(nullptr);
g_thread_count--;
});
}
@ -160,13 +181,14 @@ void thread::start(std::function<void()> func)
}
catch (const char* e)
{
LOG_ERROR(HLE, "%s: %s", name.c_str(), e);
LOG_ERROR(GENERAL, "%s: %s", name.c_str(), e);
}
catch (const std::string& e)
{
LOG_ERROR(HLE, "%s: %s", name.c_str(), e.c_str());
LOG_ERROR(GENERAL, "%s: %s", name.c_str(), e.c_str());
}
SetCurrentNamedThread(nullptr);
g_thread_count--;
});
}

View file

@ -5,16 +5,17 @@ static std::thread::id main_thread;
class NamedThreadBase
{
std::string m_name;
std::condition_variable m_signal_cv;
std::mutex m_signal_mtx;
public:
NamedThreadBase(const std::string& name) : m_name(name)
std::atomic<bool> m_tls_assigned;
NamedThreadBase(const std::string& name) : m_name(name), m_tls_assigned(false)
{
}
NamedThreadBase()
NamedThreadBase() : m_tls_assigned(false)
{
}

View file

@ -88,9 +88,9 @@ bool rMkpath(const std::string &path)
if(dir.size() == 0)
continue;
#ifdef _WIN32
if((ret = _mkdir(dir.c_str())) && errno != EEXIST){
if((ret = _mkdir(dir.c_str()) != 0) && errno != EEXIST){
#else
if((ret = mkdir(dir.c_str(), 0777)) && errno != EEXIST){
if((ret = mkdir(dir.c_str(), 0777) != 0) && errno != EEXIST){
#endif
return !ret;
}
@ -269,7 +269,7 @@ size_t rFile::Write(const void *buffer, size_t count)
bool rFile::Write(const std::string &text)
{
return reinterpret_cast<wxFile*>(handle)->Write(reinterpret_cast<const void*>(text.c_str()),text.size());
return reinterpret_cast<wxFile*>(handle)->Write(reinterpret_cast<const void*>(text.c_str()),text.size()) != 0;
}
bool rFile::Close()