From 09f0905b2ab1397b9539367e1eaa9ebacc4c332a Mon Sep 17 00:00:00 2001 From: nxtumUbun Date: Fri, 6 Sep 2024 03:31:24 -0700 Subject: [PATCH 01/23] core::binarysemaphore in sharedbuffer --- Source/core/SharedBuffer.cpp | 120 ++--------------------------------- Source/core/SharedBuffer.h | 32 +--------- Source/core/Sync.cpp | 23 +++---- Source/core/Sync.h | 20 +++--- 4 files changed, 29 insertions(+), 166 deletions(-) diff --git a/Source/core/SharedBuffer.cpp b/Source/core/SharedBuffer.cpp index c26704943..d6fce3340 100644 --- a/Source/core/SharedBuffer.cpp +++ b/Source/core/SharedBuffer.cpp @@ -26,119 +26,12 @@ namespace Thunder { namespace Core { -#ifdef __WINDOWS__ - SharedBuffer::Semaphore::Semaphore(const TCHAR sourceName[]) - : _semaphore(::CreateSemaphore(nullptr, 1, 1, sourceName)) - { - } -#else - SharedBuffer::Semaphore::Semaphore(sem_t* storage) - : _semaphore(storage) - { - ASSERT(storage != nullptr); - } -#endif - SharedBuffer::Semaphore::~Semaphore() - { -#ifdef __WINDOWS__ - if (_semaphore != nullptr) { - ::CloseHandle(_semaphore); - } -#else - sem_destroy(_semaphore); -#endif - } - - uint32_t SharedBuffer::Semaphore::Unlock() - { -#ifdef __WINDOWS__ - if (_semaphore != nullptr) { - BOOL result = ::ReleaseSemaphore(_semaphore, 1, nullptr); - - ASSERT(result != FALSE); - } -#else - VARIABLE_IS_NOT_USED int result = sem_post(_semaphore); - - ASSERT((result == 0) || (errno == EOVERFLOW)); -#endif - return ERROR_NONE; - } - - bool SharedBuffer::Semaphore::IsLocked() - { -#ifdef __WINDOWS__ - bool locked = (::WaitForSingleObjectEx(_semaphore, 0, FALSE) != WAIT_OBJECT_0); - - if (locked == false) { - ::ReleaseSemaphore(_semaphore, 1, nullptr); - } - - return (locked); -#else - int semValue = 0; - sem_getvalue(_semaphore, &semValue); - return (semValue == 0); -#endif - } - - uint32_t SharedBuffer::Semaphore::Lock(const uint32_t waitTime) - { - uint32_t result = Core::ERROR_GENERAL; -#ifdef __WINDOWS__ - if (_semaphore != nullptr) { - return (::WaitForSingleObjectEx(_semaphore, waitTime, FALSE) == WAIT_OBJECT_0 ? Core::ERROR_NONE : Core::ERROR_TIMEDOUT); - } -#elif defined(__APPLE__) - - uint32_t timeLeft = waitTime; - int semResult; - while (((semResult = sem_trywait(_semaphore)) != 0) && timeLeft > 0) { - ::SleepMs(100); - if (timeLeft != Core::infinite) { - timeLeft -= (timeLeft > 100 ? 100 : timeLeft); - } - } - result = semResult == 0 ? Core::ERROR_NONE : Core::ERROR_TIMEDOUT; -#else - - struct timespec structTime = {0,0}; - - clock_gettime(CLOCK_MONOTONIC, &structTime); - structTime.tv_nsec += ((waitTime % 1000) * 1000 * 1000); /* remainder, milliseconds to nanoseconds */ - structTime.tv_sec += (waitTime / 1000) + (structTime.tv_nsec / 1000000000); /* milliseconds to seconds */ - structTime.tv_nsec = structTime.tv_nsec % 1000000000; - - do { - if (sem_clockwait(_semaphore, CLOCK_MONOTONIC, &structTime) == 0) { - result = Core::ERROR_NONE; - } - else if ( errno == EINTR ) { - continue; - } - else if ( errno == ETIMEDOUT ) { - result = Core::ERROR_TIMEDOUT; - } - else { - ASSERT(false); - } - break; - } while (true); -#endif - return (result); - } - SharedBuffer::SharedBuffer(const TCHAR name[]) : DataElementFile(name, File::USER_READ | File::USER_WRITE | File::SHAREABLE, 0) , _administrationBuffer((string(name) + ".admin"), File::USER_READ | File::USER_WRITE | File::SHAREABLE, 0) , _administration(reinterpret_cast(PointerAlign(_administrationBuffer.Buffer()))) -#ifdef __WINDOWS__ - , _producer((string(name) + ".producer").c_str()) - , _consumer((string(name) + ".consumer").c_str()) -#else - , _producer(&(_administration->_producer)) - , _consumer(&(_administration->_consumer)) -#endif + , _producer(false) + , _consumer(false) , _customerAdministration(PointerAlign(&(reinterpret_cast(_administration)[sizeof(Administration)]))) { Align(); @@ -147,13 +40,8 @@ namespace Core { : DataElementFile(name, mode | File::SHAREABLE | File::CREATE, bufferSize) , _administrationBuffer((string(name) + ".admin"), mode | File::SHAREABLE | File::CREATE, administratorSize + sizeof(Administration) + (2 * sizeof(void*)) + 8 /* Align buffer on 64 bits boundary */) , _administration(reinterpret_cast(PointerAlign(_administrationBuffer.Buffer()))) -#ifdef __WINDOWS__ - , _producer((string(name) + ".producer").c_str()) - , _consumer((string(name) + ".consumer").c_str()) -#else - , _producer(&(_administration->_producer)) - , _consumer(&(_administration->_consumer)) -#endif + , _producer(false) + , _consumer(false) , _customerAdministration(PointerAlign(&(reinterpret_cast(_administration)[sizeof(Administration)]))) { diff --git a/Source/core/SharedBuffer.h b/Source/core/SharedBuffer.h index 664e34d98..cdd10ac7d 100644 --- a/Source/core/SharedBuffer.h +++ b/Source/core/SharedBuffer.h @@ -65,34 +65,6 @@ namespace Core { SharedBuffer& operator=(const SharedBuffer&) = delete; private: - class Semaphore { - private: - Semaphore() = delete; - Semaphore(const Semaphore&) = delete; - Semaphore& operator=(const Semaphore&) = delete; - - public: -#ifdef __WINDOWS__ - Semaphore(const TCHAR name[]); -#else - Semaphore(sem_t* storage); - //Semaphore(sem_t* storage, bool initialize) { - //} -#endif - ~Semaphore(); - - public: - uint32_t Lock(const uint32_t waitTime); - uint32_t Unlock(); - bool IsLocked(); - - private: -#ifdef __WINDOWS__ - HANDLE _semaphore; -#else - sem_t* _semaphore; -#endif - }; struct Administration { uint32_t _bytesWritten; @@ -215,8 +187,8 @@ namespace Core { private: DataElementFile _administrationBuffer; Administration* _administration; - Semaphore _producer; - Semaphore _consumer; + Core::BinairySemaphore _producer; + Core::BinairySemaphore _consumer; uint8_t* _customerAdministration; }; } diff --git a/Source/core/Sync.cpp b/Source/core/Sync.cpp index e5f04ef3a..86cc635c0 100644 --- a/Source/core/Sync.cpp +++ b/Source/core/Sync.cpp @@ -271,7 +271,7 @@ namespace Core { //---------------------------------------------------------------------------- //---------------------------------------------------------------------------- - // BinairySemaphore class + // BinarySemaphore class //---------------------------------------------------------------------------- //---------------------------------------------------------------------------- @@ -283,12 +283,12 @@ namespace Core { // sets the inital count an the maximum count. This way, on platform changes, // only the declaration/definition of the synchronisation object has to be defined // as being Binairy, not the coding. - BinairySemaphore::BinairySemaphore(unsigned int nInitialCount, unsigned int nMaxCount) + BinarySemaphore::BinarySemaphore(unsigned int nInitialCount, unsigned int nMaxCount) { DEBUG_VARIABLE(nMaxCount); ASSERT((nInitialCount == 0) || (nInitialCount == 1)); - TRACE_L5("Constructor BinairySemaphore (int, int) <%p>", (this)); + TRACE_L5("Constructor BinarySemaphore (int, int) <%p>", (this)); #ifdef __POSIX__ m_blLocked = (nInitialCount == 0); @@ -323,12 +323,12 @@ namespace Core { #endif } - BinairySemaphore::BinairySemaphore(bool blLocked) + BinarySemaphore::BinarySemaphore(bool blLocked) #ifdef __POSIX__ : m_blLocked(blLocked) #endif { - TRACE_L5("Constructor BinairySemaphore <%p>", (this)); + TRACE_L5("Constructor BinarySemaphore <%p>", (this)); #ifdef __POSIX__ pthread_condattr_t attr; @@ -360,9 +360,9 @@ namespace Core { #endif } - BinairySemaphore::~BinairySemaphore() + BinarySemaphore::~BinarySemaphore() { - TRACE_L5("Destructor BinairySemaphore <%p>", (this)); + TRACE_L5("Destructor BinarySemaphore <%p>", (this)); #ifdef __POSIX__ // If we really create it, we really have to destroy it. @@ -380,7 +380,7 @@ namespace Core { //---------------------------------------------------------------------------- uint32_t - BinairySemaphore::Lock() + BinarySemaphore::Lock() { #ifdef __WINDOWS__ @@ -423,7 +423,7 @@ namespace Core { } uint32_t - BinairySemaphore::Lock(unsigned int nTime) + BinarySemaphore::Lock(unsigned int nTime) { #ifdef __WINDOWS__ return (::WaitForSingleObjectEx(m_syncMutex, nTime, FALSE) == WAIT_OBJECT_0 ? Core::ERROR_NONE : Core::ERROR_TIMEDOUT); @@ -482,8 +482,8 @@ namespace Core { #endif } - void - BinairySemaphore::Unlock() + uint32_t + BinarySemaphore::Unlock() { #ifdef __POSIX__ @@ -505,6 +505,7 @@ namespace Core { #ifdef __WINDOWS__ ::ReleaseMutex(m_syncMutex); #endif + return ERROR_NONE; } //---------------------------------------------------------------------------- diff --git a/Source/core/Sync.h b/Source/core/Sync.h index 63540c773..f7f39ad7a 100644 --- a/Source/core/Sync.h +++ b/Source/core/Sync.h @@ -112,23 +112,22 @@ namespace Core { // =========================================================================== // class BinairySemaphore // =========================================================================== - - class EXTERNAL BinairySemaphore { + class EXTERNAL BinarySemaphore { private: - BinairySemaphore() = delete; - BinairySemaphore(const BinairySemaphore&) = delete; - BinairySemaphore& operator=(const BinairySemaphore&) = delete; + BinarySemaphore() = delete; + BinarySemaphore(const BinarySemaphore&) = delete; + BinarySemaphore& operator=(const BinarySemaphore&) = delete; public: // Methods - BinairySemaphore(unsigned int nInitialCount, unsigned int nMaxCount); - BinairySemaphore(bool blLocked); - ~BinairySemaphore(); + BinarySemaphore(unsigned int nInitialCount, unsigned int nMaxCount); + BinarySemaphore(bool blLocked); + ~BinarySemaphore(); uint32_t Lock(); // Time in milliseconds! uint32_t Lock(unsigned int nSeconds); - void Unlock(); + uint32_t Unlock(); bool Locked() const; protected: // Members @@ -143,6 +142,9 @@ namespace Core { #endif }; + // For compatability reaons with old class name + using BinairySemaphore = BinarySemaphore; + // =========================================================================== // class CountingSemaphore // =========================================================================== From e941b60f54880c432e0f500429ecf461af757f3b Mon Sep 17 00:00:00 2001 From: nxtumUbun Date: Fri, 6 Sep 2024 03:59:45 -0700 Subject: [PATCH 02/23] spelling, binairy to binary in sharedbuffer --- Source/core/SharedBuffer.h | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Source/core/SharedBuffer.h b/Source/core/SharedBuffer.h index cdd10ac7d..e69fe339b 100644 --- a/Source/core/SharedBuffer.h +++ b/Source/core/SharedBuffer.h @@ -187,8 +187,8 @@ namespace Core { private: DataElementFile _administrationBuffer; Administration* _administration; - Core::BinairySemaphore _producer; - Core::BinairySemaphore _consumer; + Core::BinarySemaphore _producer; + Core::BinarySemaphore _consumer; uint8_t* _customerAdministration; }; } From 50b233a024ce182153aebdde77b2fce7c3a70308 Mon Sep 17 00:00:00 2001 From: nxtumUbun Date: Fri, 20 Sep 2024 01:29:18 -0700 Subject: [PATCH 03/23] musl fix, added sem_timedwait --- Source/core/Portability.h | 4 + Source/core/Proxy.h | 4 +- Source/core/SharedBuffer.cpp | 30 +++--- Source/core/SharedBuffer.h | 6 +- Source/core/Sync.cpp | 174 +++++++++++++++++++++++++++++++++++ Source/core/Sync.h | 39 ++++++++ Source/core/Thread.cpp | 2 +- 7 files changed, 238 insertions(+), 21 deletions(-) diff --git a/Source/core/Portability.h b/Source/core/Portability.h index a02b46dde..ffe9b06d1 100644 --- a/Source/core/Portability.h +++ b/Source/core/Portability.h @@ -385,6 +385,7 @@ typedef std::string string; #include #include #include +#include #include #include @@ -412,9 +413,12 @@ typedef std::string string; #include #include #include // memfd_create in Messaging/ConsoleRedirect.h +#include #include +#define __MUSL__ 1 + #ifdef __APPLE__ #include #define SCHED_BATCH 99 diff --git a/Source/core/Proxy.h b/Source/core/Proxy.h index d51cce599..0b2830fe5 100644 --- a/Source/core/Proxy.h +++ b/Source/core/Proxy.h @@ -539,11 +539,11 @@ POP_WARNING() { return (!operator==(a_RHS)); } - inline bool operator==(const nullptr_t&) const + inline bool operator==(const std::nullptr_t&) const { return (_refCount == nullptr); } - inline bool operator!=(const nullptr_t&) const + inline bool operator!=(const std::nullptr_t&) const { return (_refCount != nullptr); } diff --git a/Source/core/SharedBuffer.cpp b/Source/core/SharedBuffer.cpp index d6fce3340..88eeb1b96 100644 --- a/Source/core/SharedBuffer.cpp +++ b/Source/core/SharedBuffer.cpp @@ -30,8 +30,13 @@ namespace Core { : DataElementFile(name, File::USER_READ | File::USER_WRITE | File::SHAREABLE, 0) , _administrationBuffer((string(name) + ".admin"), File::USER_READ | File::USER_WRITE | File::SHAREABLE, 0) , _administration(reinterpret_cast(PointerAlign(_administrationBuffer.Buffer()))) - , _producer(false) - , _consumer(false) + #ifdef __WINDOWS__ + , _producer((string(name) + ".producer").c_str()) + , _consumer((string(name) + ".consumer").c_str()) + #else + , _producer(&(_administration->_producer)) + , _consumer(&(_administration->_consumer)) + #endif , _customerAdministration(PointerAlign(&(reinterpret_cast(_administration)[sizeof(Administration)]))) { Align(); @@ -40,22 +45,17 @@ namespace Core { : DataElementFile(name, mode | File::SHAREABLE | File::CREATE, bufferSize) , _administrationBuffer((string(name) + ".admin"), mode | File::SHAREABLE | File::CREATE, administratorSize + sizeof(Administration) + (2 * sizeof(void*)) + 8 /* Align buffer on 64 bits boundary */) , _administration(reinterpret_cast(PointerAlign(_administrationBuffer.Buffer()))) - , _producer(false) - , _consumer(false) + #ifdef __WINDOWS__ + , _producer((string(name) + ".producer").c_str()) + , _consumer((string(name) + ".consumer").c_str()) + #else + , _producer(&(_administration->_producer), 1) + , _consumer(&(_administration->_consumer), 0) +# endif , _customerAdministration(PointerAlign(&(reinterpret_cast(_administration)[sizeof(Administration)]))) { - -#ifndef __WINDOWS__ - memset(_administration, 0, sizeof(Administration)); - - sem_init(&(_administration->_producer), 1, 1); /* Initial value is 1. */ - sem_init(&(_administration->_consumer), 1, 0); /* Initial value is 0. */ -#endif + _administration->_bytesWritten = 0; Align(); } - - SharedBuffer::~SharedBuffer() - { - } } } diff --git a/Source/core/SharedBuffer.h b/Source/core/SharedBuffer.h index e69fe339b..dff5e1189 100644 --- a/Source/core/SharedBuffer.h +++ b/Source/core/SharedBuffer.h @@ -76,7 +76,7 @@ namespace Core { }; public: - virtual ~SharedBuffer(); + ~SharedBuffer() override = default; // This is the consumer constructor. It should always take place, after, the producer // construct. The producer will create the Administration area, and the shared buffer, @@ -187,8 +187,8 @@ namespace Core { private: DataElementFile _administrationBuffer; Administration* _administration; - Core::BinarySemaphore _producer; - Core::BinarySemaphore _consumer; + Core::SharedSemaphore _producer; + Core::SharedSemaphore _consumer; uint8_t* _customerAdministration; }; } diff --git a/Source/core/Sync.cpp b/Source/core/Sync.cpp index 86cc635c0..756ee2968 100644 --- a/Source/core/Sync.cpp +++ b/Source/core/Sync.cpp @@ -783,6 +783,180 @@ namespace Core { return (nResult); } + //---------------------------------------------------------------------------- + //---------------------------------------------------------------------------- + // SharedSemaphore class + //---------------------------------------------------------------------------- + //---------------------------------------------------------------------------- + + //---------------------------------------------------------------------------- + // CONSTRUCTOR & DESTRUCTOR + //---------------------------------------------------------------------------- + + #ifdef __WINDOWS__ + SharedSemaphore::SharedSemaphore(const TCHAR sourceName[]) + : _semaphore(::CreateSemaphore(nullptr, 1, 1, sourceName)) + { + } +#else + SharedSemaphore::SharedSemaphore(sem_t* storage) + : _semaphore(storage) + { + ASSERT(storage != nullptr); + } +#endif + + SharedSemaphore::SharedSemaphore(void *storage, const uint32_t value) + { + ASSERT(storage != nullptr); + + sem_t* sem = reinterpret_cast(storage); + VARIABLE_IS_NOT_USED int result = sem_init(sem, 1, value); + ASSERT(result != -1); + } + + SharedSemaphore::~SharedSemaphore() + { +#ifdef __WINDOWS__ + if (_semaphore != nullptr) { + ::CloseHandle(_semaphore); + } +#else + sem_destroy(_semaphore); +#endif + } + + //---------------------------------------------------------------------------- + // PUBLIC METHODS + //---------------------------------------------------------------------------- + + uint32_t + SharedSemaphore::Unlock() + { +#ifdef __WINDOWS__ + if (_semaphore != nullptr) { + BOOL result = ::ReleaseSemaphore(_semaphore, 1, nullptr); + + ASSERT(result != FALSE); + } +#else + VARIABLE_IS_NOT_USED int result = sem_post(_semaphore); + + ASSERT((result == 0) || (errno == EOVERFLOW)); +#endif + return ERROR_NONE; + } + + bool + SharedSemaphore::IsLocked() + { +#ifdef __WINDOWS__ + bool locked = (::WaitForSingleObjectEx(_semaphore, 0, FALSE) != WAIT_OBJECT_0); + + if (locked == false) { + ::ReleaseSemaphore(_semaphore, 1, nullptr); + } + + return (locked); +#else + int semValue = 0; + sem_getvalue(_semaphore, &semValue); + return (semValue == 0); +#endif + } + + uint32_t + SharedSemaphore::Lock(const uint32_t waitTime) + { + uint32_t result = Core::ERROR_GENERAL; +#ifdef __WINDOWS__ + if (_semaphore != nullptr) { + return (::WaitForSingleObjectEx(_semaphore, waitTime, FALSE) == WAIT_OBJECT_0 ? Core::ERROR_NONE : Core::ERROR_TIMEDOUT); + } +#elif defined(__APPLE__) + + uint32_t timeLeft = waitTime; + int semResult; + while (((semResult = sem_trywait(_semaphore)) != 0) && timeLeft > 0) { + ::SleepMs(100); + if (timeLeft != Core::infinite) { + timeLeft -= (timeLeft > 100 ? 100 : timeLeft); + } + } + result = semResult == 0 ? Core::ERROR_NONE : Core::ERROR_TIMEDOUT; + +#elif defined(__MUSL__) + struct timespec referenceTime = {0,0}; + clock_gettime(CLOCK_MONOTONIC, &referenceTime); + referenceTime.tv_nsec += ((waitTime % 1000) * 1000 * 1000); /* remainder, milliseconds to nanoseconds */ + referenceTime.tv_sec += (waitTime / 1000) + (referenceTime.tv_nsec / 1000000000); /* milliseconds to seconds */ + referenceTime.tv_nsec = referenceTime.tv_nsec % 1000000000; + do { + + struct timespec structTime = {0,0}; + clock_gettime(CLOCK_REALTIME, &structTime); + structTime.tv_nsec += ((waitTime % 1000) * 1000 * 1000); /* remainder, milliseconds to nanoseconds */ + structTime.tv_sec += (waitTime / 1000) + (structTime.tv_nsec / 1000000000); /* milliseconds to seconds */ + structTime.tv_nsec = structTime.tv_nsec % 1000000000; + + if (sem_timedwait(_semaphore, &structTime) == 0) { + result = Core::ERROR_NONE; + } + else if ( errno == EINTR ) { + continue; + } + else if ( errno == ETIMEDOUT ) { + struct timespec currentMonoTime; + clock_gettime(CLOCK_MONOTONIC, ¤tMonoTime); + + struct timespec jumpTime; + jumpTime.tv_sec = (structTime.tv_sec - currentMonoTime.tv_sec) - (referenceTime.tv_sec - currentMonoTime.tv_sec); + + if(jumpTime.tv_sec != 0) { + result = Core::ERROR_TIMEDOUT; + break; + } + + if(referenceTime.tv_sec < currentMonoTime.tv_sec || (referenceTime.tv_sec == currentMonoTime.tv_sec && + referenceTime.tv_nsec < currentMonoTime.tv_nsec)) + { + result = Core::ERROR_TIMEDOUT; + break; + } + } + else { + ASSERT(false); + } + break; + } while (true); +#else + + struct timespec structTime = {0,0}; + + clock_gettime(CLOCK_MONOTONIC, &structTime); + structTime.tv_nsec += ((waitTime % 1000) * 1000 * 1000); /* remainder, milliseconds to nanoseconds */ + structTime.tv_sec += (waitTime / 1000) + (structTime.tv_nsec / 1000000000); /* milliseconds to seconds */ + structTime.tv_nsec = structTime.tv_nsec % 1000000000; + + do { + if (sem_clockwait(_semaphore, CLOCK_MONOTONIC, &structTime) == 0) { + result = Core::ERROR_NONE; + } + else if ( errno == EINTR ) { + continue; + } + else if ( errno == ETIMEDOUT ) { + result = Core::ERROR_TIMEDOUT; + } + else { + ASSERT(false); + } + break; + } while (true); +#endif + return (result); + } + //---------------------------------------------------------------------------- //---------------------------------------------------------------------------- // Event class (AVAILABLE WITHIN PROCESS SPACE) diff --git a/Source/core/Sync.h b/Source/core/Sync.h index f7f39ad7a..2bf0e6096 100644 --- a/Source/core/Sync.h +++ b/Source/core/Sync.h @@ -183,6 +183,45 @@ namespace Core { // For Windows platform compatibility typedef CountingSemaphore Semaphore; + // =========================================================================== + // class SharedSemaphore + // =========================================================================== + + class EXTERNAL SharedSemaphore { + public: + SharedSemaphore() = delete; + SharedSemaphore(const Semaphore&) = delete; + SharedSemaphore& operator=(const SharedSemaphore&) = delete; + +#ifdef __WINDOWS__ + SharedSemaphore(const TCHAR name[]); +#else + SharedSemaphore(sem_t* storage); + + /* + If pshared is nonzero, then the semaphore is shared between + processes, and should be located in a region of shared memory + (see shm_open(3), mmap(2), and shmget(2)) + Storage should be at least sizeof(sem_t)! + */ + SharedSemaphore(void *storage, const uint32_t value); + public: +#endif + ~SharedSemaphore(); + + public: + uint32_t Lock(const uint32_t waitTime); + uint32_t Unlock(); + bool IsLocked(); + + private: +#ifdef __WINDOWS__ + HANDLE _semaphore; +#else + sem_t* _semaphore; +#endif + }; + // =========================================================================== // class Event // =========================================================================== diff --git a/Source/core/Thread.cpp b/Source/core/Thread.cpp index 690a43af7..f40e2a354 100644 --- a/Source/core/Thread.cpp +++ b/Source/core/Thread.cpp @@ -469,7 +469,7 @@ POP_WARNING() } #ifdef __DEBUG__ - int Thread::GetCallstack(void** buffer, int size) + int Thread::GetCallstack(VARIABLE_IS_NOT_USED void** buffer, VARIABLE_IS_NOT_USED int size) { #if defined(THUNDER_BACKTRACE) return GetCallStack(m_hThreadInstance, buffer, size); From 7d17be2e73e26460b4a6878b8e92def63e51aa7e Mon Sep 17 00:00:00 2001 From: nxtumUbun Date: Fri, 20 Sep 2024 02:50:07 -0700 Subject: [PATCH 04/23] fix by initializing sem --- Source/core/Sync.cpp | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/Source/core/Sync.cpp b/Source/core/Sync.cpp index 756ee2968..c14cffdfe 100644 --- a/Source/core/Sync.cpp +++ b/Source/core/Sync.cpp @@ -804,17 +804,17 @@ namespace Core { { ASSERT(storage != nullptr); } -#endif SharedSemaphore::SharedSemaphore(void *storage, const uint32_t value) { ASSERT(storage != nullptr); sem_t* sem = reinterpret_cast(storage); + _semaphore = sem; VARIABLE_IS_NOT_USED int result = sem_init(sem, 1, value); ASSERT(result != -1); } - +#endif SharedSemaphore::~SharedSemaphore() { #ifdef __WINDOWS__ @@ -885,7 +885,8 @@ namespace Core { } result = semResult == 0 ? Core::ERROR_NONE : Core::ERROR_TIMEDOUT; -#elif defined(__MUSL__) +#elif defined(__MUSL2__) + struct timespec referenceTime = {0,0}; clock_gettime(CLOCK_MONOTONIC, &referenceTime); referenceTime.tv_nsec += ((waitTime % 1000) * 1000 * 1000); /* remainder, milliseconds to nanoseconds */ From 43e3730257ec7b3ed79cb46607a3ffbcdf60b24c Mon Sep 17 00:00:00 2001 From: nxtum <94901881+nxtum@users.noreply.github.com> Date: Fri, 20 Sep 2024 13:18:29 +0200 Subject: [PATCH 05/23] fixed logic of jump --- Source/core/Sync.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Source/core/Sync.cpp b/Source/core/Sync.cpp index c14cffdfe..f55c4acd4 100644 --- a/Source/core/Sync.cpp +++ b/Source/core/Sync.cpp @@ -911,7 +911,7 @@ namespace Core { clock_gettime(CLOCK_MONOTONIC, ¤tMonoTime); struct timespec jumpTime; - jumpTime.tv_sec = (structTime.tv_sec - currentMonoTime.tv_sec) - (referenceTime.tv_sec - currentMonoTime.tv_sec); + jumpTime.tv_sec = currentMonoTime.tv_sec - referenceTime.tv_sec; if(jumpTime.tv_sec != 0) { result = Core::ERROR_TIMEDOUT; From 0b988ff2372fe7f1437bdfa47f16507342cc8223 Mon Sep 17 00:00:00 2001 From: nxtumUbun Date: Mon, 23 Sep 2024 04:51:37 -0700 Subject: [PATCH 06/23] hide sem_t --- Source/core/SharedBuffer.cpp | 16 +++++------ Source/core/Sync.cpp | 53 ++++++++++++++++++++++-------------- Source/core/Sync.h | 12 ++++---- 3 files changed, 45 insertions(+), 36 deletions(-) diff --git a/Source/core/SharedBuffer.cpp b/Source/core/SharedBuffer.cpp index 88eeb1b96..47e8d1940 100644 --- a/Source/core/SharedBuffer.cpp +++ b/Source/core/SharedBuffer.cpp @@ -31,11 +31,11 @@ namespace Core { , _administrationBuffer((string(name) + ".admin"), File::USER_READ | File::USER_WRITE | File::SHAREABLE, 0) , _administration(reinterpret_cast(PointerAlign(_administrationBuffer.Buffer()))) #ifdef __WINDOWS__ - , _producer((string(name) + ".producer").c_str()) - , _consumer((string(name) + ".consumer").c_str()) + , _producer((string(name) + ".producer").c_str(), 1, 1) + , _consumer((string(name) + ".consumer").c_str(), 1, 1) #else - , _producer(&(_administration->_producer)) - , _consumer(&(_administration->_consumer)) + , _producer(&(_administration->_producer), 0, 0) + , _consumer(&(_administration->_consumer), 0, 0) #endif , _customerAdministration(PointerAlign(&(reinterpret_cast(_administration)[sizeof(Administration)]))) { @@ -46,11 +46,11 @@ namespace Core { , _administrationBuffer((string(name) + ".admin"), mode | File::SHAREABLE | File::CREATE, administratorSize + sizeof(Administration) + (2 * sizeof(void*)) + 8 /* Align buffer on 64 bits boundary */) , _administration(reinterpret_cast(PointerAlign(_administrationBuffer.Buffer()))) #ifdef __WINDOWS__ - , _producer((string(name) + ".producer").c_str()) - , _consumer((string(name) + ".consumer").c_str()) + , _producer((string(name) + ".producer").c_str(), 1, 1) + , _consumer((string(name) + ".consumer").c_str(), 0, 1) #else - , _producer(&(_administration->_producer), 1) - , _consumer(&(_administration->_consumer), 0) + , _producer(&(_administration->_producer), 1, 1) + , _consumer(&(_administration->_consumer), 0, 1) # endif , _customerAdministration(PointerAlign(&(reinterpret_cast(_administration)[sizeof(Administration)]))) { diff --git a/Source/core/Sync.cpp b/Source/core/Sync.cpp index f55c4acd4..410ec3668 100644 --- a/Source/core/Sync.cpp +++ b/Source/core/Sync.cpp @@ -46,6 +46,7 @@ #include #include #include +#include #endif #ifdef __APPLE__ #include @@ -794,25 +795,26 @@ namespace Core { //---------------------------------------------------------------------------- #ifdef __WINDOWS__ - SharedSemaphore::SharedSemaphore(const TCHAR sourceName[]) - : _semaphore(::CreateSemaphore(nullptr, 1, 1, sourceName)) + SharedSemaphore::SharedSemaphore(const TCHAR sourceName[], const uint32_t initCount, const uint32_t maxCount) + : _semaphore(::CreateSemaphore(nullptr, initCount, maxCount, sourceName)) { + ASSERT(initCount <= 1); + ASSERT(maxCount <= 1); + ASSERT(initCount <= maxCount); } #else - SharedSemaphore::SharedSemaphore(sem_t* storage) + SharedSemaphore::SharedSemaphore(void *storage, const uint32_t initCount, const uint32_t maxCount) : _semaphore(storage) { ASSERT(storage != nullptr); - } + ASSERT(initCount <= 1); + ASSERT(maxCount <= 1); + ASSERT(initCount <= maxCount); - SharedSemaphore::SharedSemaphore(void *storage, const uint32_t value) - { - ASSERT(storage != nullptr); - - sem_t* sem = reinterpret_cast(storage); - _semaphore = sem; - VARIABLE_IS_NOT_USED int result = sem_init(sem, 1, value); - ASSERT(result != -1); + if(initCount != 0 && maxCount != 0) { + VARIABLE_IS_NOT_USED int result = sem_init(static_cast(_semaphore), 1, initCount); + ASSERT(result != -1); + } } #endif SharedSemaphore::~SharedSemaphore() @@ -822,10 +824,19 @@ namespace Core { ::CloseHandle(_semaphore); } #else - sem_destroy(_semaphore); + sem_destroy(static_cast(_semaphore)); #endif } + size_t SharedSemaphore::Size() { + #ifdef __WINDOWS__ + ASSERT(false) + return 0; + #else + return sizeof(sem_t); + #endif + } + //---------------------------------------------------------------------------- // PUBLIC METHODS //---------------------------------------------------------------------------- @@ -840,7 +851,7 @@ namespace Core { ASSERT(result != FALSE); } #else - VARIABLE_IS_NOT_USED int result = sem_post(_semaphore); + VARIABLE_IS_NOT_USED int result = sem_post(static_cast(_semaphore)); ASSERT((result == 0) || (errno == EOVERFLOW)); #endif @@ -860,7 +871,7 @@ namespace Core { return (locked); #else int semValue = 0; - sem_getvalue(_semaphore, &semValue); + sem_getvalue(static_cast(_semaphore), &semValue); return (semValue == 0); #endif } @@ -877,7 +888,7 @@ namespace Core { uint32_t timeLeft = waitTime; int semResult; - while (((semResult = sem_trywait(_semaphore)) != 0) && timeLeft > 0) { + while (((semResult = sem_trywait(static_cast(_semaphore))) != 0) && timeLeft > 0) { ::SleepMs(100); if (timeLeft != Core::infinite) { timeLeft -= (timeLeft > 100 ? 100 : timeLeft); @@ -885,8 +896,8 @@ namespace Core { } result = semResult == 0 ? Core::ERROR_NONE : Core::ERROR_TIMEDOUT; -#elif defined(__MUSL2__) - +#elif defined(__MUSL__) + std::cout << "musl \n\n\n\n\n\n\n\n\n"; struct timespec referenceTime = {0,0}; clock_gettime(CLOCK_MONOTONIC, &referenceTime); referenceTime.tv_nsec += ((waitTime % 1000) * 1000 * 1000); /* remainder, milliseconds to nanoseconds */ @@ -900,7 +911,7 @@ namespace Core { structTime.tv_sec += (waitTime / 1000) + (structTime.tv_nsec / 1000000000); /* milliseconds to seconds */ structTime.tv_nsec = structTime.tv_nsec % 1000000000; - if (sem_timedwait(_semaphore, &structTime) == 0) { + if (sem_timedwait(static_cast(_semaphore), &structTime) == 0) { result = Core::ERROR_NONE; } else if ( errno == EINTR ) { @@ -933,14 +944,14 @@ namespace Core { #else struct timespec structTime = {0,0}; - + std::cout << "here" << std::endl; clock_gettime(CLOCK_MONOTONIC, &structTime); structTime.tv_nsec += ((waitTime % 1000) * 1000 * 1000); /* remainder, milliseconds to nanoseconds */ structTime.tv_sec += (waitTime / 1000) + (structTime.tv_nsec / 1000000000); /* milliseconds to seconds */ structTime.tv_nsec = structTime.tv_nsec % 1000000000; do { - if (sem_clockwait(_semaphore, CLOCK_MONOTONIC, &structTime) == 0) { + if (sem_clockwait(static_cast(_semaphore), CLOCK_MONOTONIC, &structTime) == 0) { result = Core::ERROR_NONE; } else if ( errno == EINTR ) { diff --git a/Source/core/Sync.h b/Source/core/Sync.h index 2bf0e6096..f618e68f5 100644 --- a/Source/core/Sync.h +++ b/Source/core/Sync.h @@ -29,7 +29,6 @@ #ifdef __LINUX__ #include -#include #endif namespace Thunder { @@ -194,31 +193,30 @@ namespace Core { SharedSemaphore& operator=(const SharedSemaphore&) = delete; #ifdef __WINDOWS__ - SharedSemaphore(const TCHAR name[]); + SharedSemaphore(const TCHAR name[], const uint32_t initValue, const uint32_t maxValue); #else - SharedSemaphore(sem_t* storage); - /* If pshared is nonzero, then the semaphore is shared between processes, and should be located in a region of shared memory (see shm_open(3), mmap(2), and shmget(2)) Storage should be at least sizeof(sem_t)! */ - SharedSemaphore(void *storage, const uint32_t value); + SharedSemaphore(void *storage, const uint32_t initValue, const uint32_t maxValue); public: #endif ~SharedSemaphore(); - public: uint32_t Lock(const uint32_t waitTime); uint32_t Unlock(); bool IsLocked(); + static size_t Size(); + private: #ifdef __WINDOWS__ HANDLE _semaphore; #else - sem_t* _semaphore; + void* _semaphore; #endif }; From a44a7ea1af082b0f022c2297153aa52fe9f378a8 Mon Sep 17 00:00:00 2001 From: nxtumUbun Date: Thu, 26 Sep 2024 04:43:24 -0700 Subject: [PATCH 07/23] detect musl --- Source/core/Portability.h | 14 +++++++++++++- Source/core/SharedBuffer.cpp | 4 ++-- Source/core/Sync.cpp | 25 +++++++++++-------------- 3 files changed, 26 insertions(+), 17 deletions(-) diff --git a/Source/core/Portability.h b/Source/core/Portability.h index fc29a5153..500cd3c8e 100644 --- a/Source/core/Portability.h +++ b/Source/core/Portability.h @@ -417,7 +417,19 @@ typedef std::string string; #include -#define __MUSL__ 1 +#ifndef _GNU_SOURCE + #define _GNU_SOURCE + #include + #ifndef __USE_GNU + #define __MUSL__ + #endif + #undef _GNU_SOURCE /* don't contaminate other includes unnecessarily */ +#else + #include + #ifndef __USE_GNU + #define __MUSL__ + #endif +#endif #ifdef __APPLE__ #include diff --git a/Source/core/SharedBuffer.cpp b/Source/core/SharedBuffer.cpp index 47e8d1940..665ed1752 100644 --- a/Source/core/SharedBuffer.cpp +++ b/Source/core/SharedBuffer.cpp @@ -31,8 +31,8 @@ namespace Core { , _administrationBuffer((string(name) + ".admin"), File::USER_READ | File::USER_WRITE | File::SHAREABLE, 0) , _administration(reinterpret_cast(PointerAlign(_administrationBuffer.Buffer()))) #ifdef __WINDOWS__ - , _producer((string(name) + ".producer").c_str(), 1, 1) - , _consumer((string(name) + ".consumer").c_str(), 1, 1) + , _producer((string(name) + ".producer").c_str(), 0, 0) + , _consumer((string(name) + ".consumer").c_str(), 0, 0) #else , _producer(&(_administration->_producer), 0, 0) , _consumer(&(_administration->_consumer), 0, 0) diff --git a/Source/core/Sync.cpp b/Source/core/Sync.cpp index 410ec3668..a93a1f1d9 100644 --- a/Source/core/Sync.cpp +++ b/Source/core/Sync.cpp @@ -794,7 +794,7 @@ namespace Core { // CONSTRUCTOR & DESTRUCTOR //---------------------------------------------------------------------------- - #ifdef __WINDOWS__ +#ifdef __WINDOWS__ SharedSemaphore::SharedSemaphore(const TCHAR sourceName[], const uint32_t initCount, const uint32_t maxCount) : _semaphore(::CreateSemaphore(nullptr, initCount, maxCount, sourceName)) { @@ -811,12 +811,13 @@ namespace Core { ASSERT(maxCount <= 1); ASSERT(initCount <= maxCount); - if(initCount != 0 && maxCount != 0) { + if (initCount != 0 && maxCount != 0) { VARIABLE_IS_NOT_USED int result = sem_init(static_cast(_semaphore), 1, initCount); ASSERT(result != -1); } } #endif + SharedSemaphore::~SharedSemaphore() { #ifdef __WINDOWS__ @@ -828,13 +829,14 @@ namespace Core { #endif } - size_t SharedSemaphore::Size() { - #ifdef __WINDOWS__ - ASSERT(false) - return 0; - #else - return sizeof(sem_t); - #endif + size_t SharedSemaphore::Size() + { +#ifdef __WINDOWS__ + ASSERT(false) + return 0; +#else + return sizeof(sem_t); +#endif } //---------------------------------------------------------------------------- @@ -847,12 +849,10 @@ namespace Core { #ifdef __WINDOWS__ if (_semaphore != nullptr) { BOOL result = ::ReleaseSemaphore(_semaphore, 1, nullptr); - ASSERT(result != FALSE); } #else VARIABLE_IS_NOT_USED int result = sem_post(static_cast(_semaphore)); - ASSERT((result == 0) || (errno == EOVERFLOW)); #endif return ERROR_NONE; @@ -897,7 +897,6 @@ namespace Core { result = semResult == 0 ? Core::ERROR_NONE : Core::ERROR_TIMEDOUT; #elif defined(__MUSL__) - std::cout << "musl \n\n\n\n\n\n\n\n\n"; struct timespec referenceTime = {0,0}; clock_gettime(CLOCK_MONOTONIC, &referenceTime); referenceTime.tv_nsec += ((waitTime % 1000) * 1000 * 1000); /* remainder, milliseconds to nanoseconds */ @@ -942,9 +941,7 @@ namespace Core { break; } while (true); #else - struct timespec structTime = {0,0}; - std::cout << "here" << std::endl; clock_gettime(CLOCK_MONOTONIC, &structTime); structTime.tv_nsec += ((waitTime % 1000) * 1000 * 1000); /* remainder, milliseconds to nanoseconds */ structTime.tv_sec += (waitTime / 1000) + (structTime.tv_nsec / 1000000000); /* milliseconds to seconds */ From 61d6d76dfa7b816c1503b31cc3aae4e32230f395 Mon Sep 17 00:00:00 2001 From: nxtum <94901881+nxtum@users.noreply.github.com> Date: Thu, 26 Sep 2024 14:27:45 +0200 Subject: [PATCH 08/23] fix windows --- Source/core/Sync.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Source/core/Sync.cpp b/Source/core/Sync.cpp index a93a1f1d9..2704f916a 100644 --- a/Source/core/Sync.cpp +++ b/Source/core/Sync.cpp @@ -832,7 +832,7 @@ namespace Core { size_t SharedSemaphore::Size() { #ifdef __WINDOWS__ - ASSERT(false) + ASSERT(false); return 0; #else return sizeof(sem_t); From b3633d723a1c91b46859f27ac9394f133bbfc0e4 Mon Sep 17 00:00:00 2001 From: nxtumUbun Date: Fri, 27 Sep 2024 00:49:10 -0700 Subject: [PATCH 09/23] sem open support, max method --- Source/core/Sync.cpp | 29 ++++++++++++++++++++++++----- Source/core/Sync.h | 5 +++-- 2 files changed, 27 insertions(+), 7 deletions(-) diff --git a/Source/core/Sync.cpp b/Source/core/Sync.cpp index 2704f916a..5619bbd61 100644 --- a/Source/core/Sync.cpp +++ b/Source/core/Sync.cpp @@ -794,15 +794,22 @@ namespace Core { // CONSTRUCTOR & DESTRUCTOR //---------------------------------------------------------------------------- -#ifdef __WINDOWS__ + SharedSemaphore::SharedSemaphore(const TCHAR sourceName[], const uint32_t initCount, const uint32_t maxCount) - : _semaphore(::CreateSemaphore(nullptr, initCount, maxCount, sourceName)) { ASSERT(initCount <= 1); ASSERT(maxCount <= 1); ASSERT(initCount <= maxCount); - } +#ifdef __WINDOWS__ + _semaphore(::CreateSemaphore(nullptr, initCount, maxCount, sourceName)); #else + _name = sourceName; + _semaphore = sem_open(sourceName, O_CREAT, O_RDWR, initCount); + ASSERT(_semaphore != SEM_FAILED); +#endif + } + +#ifndef __WINDOWS__ SharedSemaphore::SharedSemaphore(void *storage, const uint32_t initCount, const uint32_t maxCount) : _semaphore(storage) { @@ -824,8 +831,14 @@ namespace Core { if (_semaphore != nullptr) { ::CloseHandle(_semaphore); } -#else - sem_destroy(static_cast(_semaphore)); +#else + if(_name != nullptr) { + sem_close(static_cast(_semaphore)); + sem_unlink(_name); + } + else { + sem_destroy(static_cast(_semaphore)); + } #endif } @@ -839,6 +852,12 @@ namespace Core { #endif } + uint32_t SharedSemaphore::MaxCount() + { + // Currently max count is 1, as it is implemented as a binary shared semaphore + return 1; + } + //---------------------------------------------------------------------------- // PUBLIC METHODS //---------------------------------------------------------------------------- diff --git a/Source/core/Sync.h b/Source/core/Sync.h index f618e68f5..273b2a871 100644 --- a/Source/core/Sync.h +++ b/Source/core/Sync.h @@ -192,9 +192,8 @@ namespace Core { SharedSemaphore(const Semaphore&) = delete; SharedSemaphore& operator=(const SharedSemaphore&) = delete; -#ifdef __WINDOWS__ SharedSemaphore(const TCHAR name[], const uint32_t initValue, const uint32_t maxValue); -#else +#ifndef __WINDOWS__ /* If pshared is nonzero, then the semaphore is shared between processes, and should be located in a region of shared memory @@ -211,12 +210,14 @@ namespace Core { bool IsLocked(); static size_t Size(); + static uint32_t MaxCount(); private: #ifdef __WINDOWS__ HANDLE _semaphore; #else void* _semaphore; + const char* _name; #endif }; From abfe11490123210452dd13060d8121d1fc946997 Mon Sep 17 00:00:00 2001 From: nxtumUbun Date: Fri, 27 Sep 2024 00:58:22 -0700 Subject: [PATCH 10/23] add unused --- Source/core/Sync.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Source/core/Sync.cpp b/Source/core/Sync.cpp index 5619bbd61..753679767 100644 --- a/Source/core/Sync.cpp +++ b/Source/core/Sync.cpp @@ -795,7 +795,7 @@ namespace Core { //---------------------------------------------------------------------------- - SharedSemaphore::SharedSemaphore(const TCHAR sourceName[], const uint32_t initCount, const uint32_t maxCount) + SharedSemaphore::SharedSemaphore(const TCHAR sourceName[], const uint32_t initCount, VARIABLE_IS_NOT_USED const uint32_t maxCount) { ASSERT(initCount <= 1); ASSERT(maxCount <= 1); From e161e763d577aee149e2229976574fc208f03dea Mon Sep 17 00:00:00 2001 From: nxtumUbun Date: Fri, 27 Sep 2024 01:17:04 -0700 Subject: [PATCH 11/23] errors semopen --- Source/core/Sync.cpp | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/Source/core/Sync.cpp b/Source/core/Sync.cpp index 753679767..4d504e6c6 100644 --- a/Source/core/Sync.cpp +++ b/Source/core/Sync.cpp @@ -801,16 +801,16 @@ namespace Core { ASSERT(maxCount <= 1); ASSERT(initCount <= maxCount); #ifdef __WINDOWS__ - _semaphore(::CreateSemaphore(nullptr, initCount, maxCount, sourceName)); + _semaphore = (::CreateSemaphore(nullptr, initCount, maxCount, sourceName)); #else _name = sourceName; - _semaphore = sem_open(sourceName, O_CREAT, O_RDWR, initCount); + _semaphore = sem_open(sourceName, O_CREAT | O_RDWR, 0644, initCount); ASSERT(_semaphore != SEM_FAILED); #endif } #ifndef __WINDOWS__ - SharedSemaphore::SharedSemaphore(void *storage, const uint32_t initCount, const uint32_t maxCount) + SharedSemaphore::SharedSemaphore(void* storage, const uint32_t initCount, const uint32_t maxCount) : _semaphore(storage) { ASSERT(storage != nullptr); From 6ecaa5910bbb2949e7443390634642456f80f8d0 Mon Sep 17 00:00:00 2001 From: nxtumUbun Date: Fri, 27 Sep 2024 03:50:17 -0700 Subject: [PATCH 12/23] create/open sem --- Source/core/SharedBuffer.cpp | 8 ++++---- Source/core/Sync.cpp | 35 ++++++++++++++++++++++++++--------- Source/core/Sync.h | 6 +++++- 3 files changed, 35 insertions(+), 14 deletions(-) diff --git a/Source/core/SharedBuffer.cpp b/Source/core/SharedBuffer.cpp index 665ed1752..e8a55a25d 100644 --- a/Source/core/SharedBuffer.cpp +++ b/Source/core/SharedBuffer.cpp @@ -31,11 +31,11 @@ namespace Core { , _administrationBuffer((string(name) + ".admin"), File::USER_READ | File::USER_WRITE | File::SHAREABLE, 0) , _administration(reinterpret_cast(PointerAlign(_administrationBuffer.Buffer()))) #ifdef __WINDOWS__ - , _producer((string(name) + ".producer").c_str(), 0, 0) - , _consumer((string(name) + ".consumer").c_str(), 0, 0) + , _producer((string(name) + ".producer").c_str()) + , _consumer((string(name) + ".consumer").c_str()) #else - , _producer(&(_administration->_producer), 0, 0) - , _consumer(&(_administration->_consumer), 0, 0) + , _producer(&(_administration->_producer)) + , _consumer(&(_administration->_consumer)) #endif , _customerAdministration(PointerAlign(&(reinterpret_cast(_administration)[sizeof(Administration)]))) { diff --git a/Source/core/Sync.cpp b/Source/core/Sync.cpp index 4d504e6c6..78c6038ce 100644 --- a/Source/core/Sync.cpp +++ b/Source/core/Sync.cpp @@ -798,31 +798,48 @@ namespace Core { SharedSemaphore::SharedSemaphore(const TCHAR sourceName[], const uint32_t initCount, VARIABLE_IS_NOT_USED const uint32_t maxCount) { ASSERT(initCount <= 1); - ASSERT(maxCount <= 1); - ASSERT(initCount <= maxCount); + ASSERT(maxCount == 1); + #ifdef __WINDOWS__ _semaphore = (::CreateSemaphore(nullptr, initCount, maxCount, sourceName)); + ASSERT(_semaphore != nullptr); #else - _name = sourceName; - _semaphore = sem_open(sourceName, O_CREAT | O_RDWR, 0644, initCount); + _name = "/" + string(sourceName); + _semaphore = sem_open(_name.c_str(), O_CREAT | O_RDWR, 0644, initCount); ASSERT(_semaphore != SEM_FAILED); #endif } + SharedSemaphore::SharedSemaphore(const TCHAR sourceName[]) + { +#ifdef __WINDOWS__ + _semaphore = ::OpenSemaphore(SEMAPHORE_ALL_ACCESS, FALSE, sourceName); + ASSERT(_semaphore != nullptr); +#else + _semaphore = sem_open(sourceName, 0); + ASSERT(_semaphore != nullptr); +#endif + } + #ifndef __WINDOWS__ SharedSemaphore::SharedSemaphore(void* storage, const uint32_t initCount, const uint32_t maxCount) - : _semaphore(storage) + : _semaphore(storage), _name("") { ASSERT(storage != nullptr); ASSERT(initCount <= 1); - ASSERT(maxCount <= 1); - ASSERT(initCount <= maxCount); + ASSERT(maxCount == 1); if (initCount != 0 && maxCount != 0) { VARIABLE_IS_NOT_USED int result = sem_init(static_cast(_semaphore), 1, initCount); ASSERT(result != -1); } } + + SharedSemaphore::SharedSemaphore(void* storage) + : _semaphore(storage), _name("") + { + } + #endif SharedSemaphore::~SharedSemaphore() @@ -832,9 +849,9 @@ namespace Core { ::CloseHandle(_semaphore); } #else - if(_name != nullptr) { + if(_name.size() != 0) { sem_close(static_cast(_semaphore)); - sem_unlink(_name); + sem_unlink(_name.c_str()); } else { sem_destroy(static_cast(_semaphore)); diff --git a/Source/core/Sync.h b/Source/core/Sync.h index 273b2a871..95eaf0632 100644 --- a/Source/core/Sync.h +++ b/Source/core/Sync.h @@ -193,6 +193,8 @@ namespace Core { SharedSemaphore& operator=(const SharedSemaphore&) = delete; SharedSemaphore(const TCHAR name[], const uint32_t initValue, const uint32_t maxValue); + SharedSemaphore(const TCHAR name[]); + #ifndef __WINDOWS__ /* If pshared is nonzero, then the semaphore is shared between @@ -201,6 +203,8 @@ namespace Core { Storage should be at least sizeof(sem_t)! */ SharedSemaphore(void *storage, const uint32_t initValue, const uint32_t maxValue); + SharedSemaphore(void *storage); + public: #endif ~SharedSemaphore(); @@ -217,7 +221,7 @@ namespace Core { HANDLE _semaphore; #else void* _semaphore; - const char* _name; + string _name; #endif }; From df9412307b464ff1345fe3022ae0de7f158cc29c Mon Sep 17 00:00:00 2001 From: nxtumUbun Date: Wed, 2 Oct 2024 06:11:13 -0700 Subject: [PATCH 13/23] sharedbuffer offset calculations --- Source/core/SharedBuffer.cpp | 13 +++++++------ Source/core/SharedBuffer.h | 6 ------ Source/core/Sync.cpp | 7 +++---- Source/core/Sync.h | 2 +- 4 files changed, 11 insertions(+), 17 deletions(-) diff --git a/Source/core/SharedBuffer.cpp b/Source/core/SharedBuffer.cpp index e8a55a25d..f88e25056 100644 --- a/Source/core/SharedBuffer.cpp +++ b/Source/core/SharedBuffer.cpp @@ -34,8 +34,8 @@ namespace Core { , _producer((string(name) + ".producer").c_str()) , _consumer((string(name) + ".consumer").c_str()) #else - , _producer(&(_administration->_producer)) - , _consumer(&(_administration->_consumer)) + , _producer(reinterpret_cast(_administration) + sizeof(Administration)) + , _consumer(reinterpret_cast(_administration) + sizeof(Administration) + SharedSemaphore::Size()) #endif , _customerAdministration(PointerAlign(&(reinterpret_cast(_administration)[sizeof(Administration)]))) { @@ -43,14 +43,15 @@ namespace Core { } SharedBuffer::SharedBuffer(const TCHAR name[], const uint32_t mode, const uint32_t bufferSize, const uint16_t administratorSize) : DataElementFile(name, mode | File::SHAREABLE | File::CREATE, bufferSize) - , _administrationBuffer((string(name) + ".admin"), mode | File::SHAREABLE | File::CREATE, administratorSize + sizeof(Administration) + (2 * sizeof(void*)) + 8 /* Align buffer on 64 bits boundary */) + , _administrationBuffer((string(name) + ".admin"), mode | File::SHAREABLE | File::CREATE, sizeof(Administration) + (SharedSemaphore::Size() * 2) + administratorSize + + ((administratorSize % 8 == 0) ? 0 : (8 - (administratorSize % 8))) /* Align buffer on 64 bits boundary */) , _administration(reinterpret_cast(PointerAlign(_administrationBuffer.Buffer()))) #ifdef __WINDOWS__ , _producer((string(name) + ".producer").c_str(), 1, 1) , _consumer((string(name) + ".consumer").c_str(), 0, 1) #else - , _producer(&(_administration->_producer), 1, 1) - , _consumer(&(_administration->_consumer), 0, 1) + , _producer(reinterpret_cast(_administration) + sizeof(Administration), 1, 1) + , _consumer(reinterpret_cast(_administration) + sizeof(Administration) + SharedSemaphore::Size(), 0, 1) # endif , _customerAdministration(PointerAlign(&(reinterpret_cast(_administration)[sizeof(Administration)]))) { @@ -58,4 +59,4 @@ namespace Core { Align(); } } -} +} \ No newline at end of file diff --git a/Source/core/SharedBuffer.h b/Source/core/SharedBuffer.h index dff5e1189..77035525d 100644 --- a/Source/core/SharedBuffer.h +++ b/Source/core/SharedBuffer.h @@ -66,13 +66,7 @@ namespace Core { private: struct Administration { - uint32_t _bytesWritten; - -#ifndef __WINDOWS__ - sem_t _producer; - sem_t _consumer; -#endif }; public: diff --git a/Source/core/Sync.cpp b/Source/core/Sync.cpp index 78c6038ce..897df753f 100644 --- a/Source/core/Sync.cpp +++ b/Source/core/Sync.cpp @@ -810,7 +810,7 @@ namespace Core { #endif } - SharedSemaphore::SharedSemaphore(const TCHAR sourceName[]) + SharedSemaphore::SharedSemaphore(const TCHAR sourceName[]) { #ifdef __WINDOWS__ _semaphore = ::OpenSemaphore(SEMAPHORE_ALL_ACCESS, FALSE, sourceName); @@ -862,14 +862,13 @@ namespace Core { size_t SharedSemaphore::Size() { #ifdef __WINDOWS__ - ASSERT(false); - return 0; + return HANDLE; #else return sizeof(sem_t); #endif } - uint32_t SharedSemaphore::MaxCount() + uint32_t SharedSemaphore::MaxCount() const { // Currently max count is 1, as it is implemented as a binary shared semaphore return 1; diff --git a/Source/core/Sync.h b/Source/core/Sync.h index 95eaf0632..fd1b7936f 100644 --- a/Source/core/Sync.h +++ b/Source/core/Sync.h @@ -214,7 +214,7 @@ namespace Core { bool IsLocked(); static size_t Size(); - static uint32_t MaxCount(); + uint32_t MaxCount() const; private: #ifdef __WINDOWS__ From eb32c9d54b8851cfee014a68fe4a0d98aa85fa1c Mon Sep 17 00:00:00 2001 From: nxtumUbun Date: Wed, 2 Oct 2024 06:22:33 -0700 Subject: [PATCH 14/23] add sizeof to handle --- Source/core/Sync.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Source/core/Sync.cpp b/Source/core/Sync.cpp index 897df753f..d2f23f9eb 100644 --- a/Source/core/Sync.cpp +++ b/Source/core/Sync.cpp @@ -862,7 +862,7 @@ namespace Core { size_t SharedSemaphore::Size() { #ifdef __WINDOWS__ - return HANDLE; + return sizeof(HANDLE); #else return sizeof(sem_t); #endif From 3da9c0f0208fc3d4b1cf334395f744cbf042576f Mon Sep 17 00:00:00 2001 From: nxtumUbun Date: Wed, 2 Oct 2024 23:42:11 -0700 Subject: [PATCH 15/23] fix alignment for buffer --- Source/core/SharedBuffer.cpp | 2 +- Source/core/Sync.cpp | 1 + 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/Source/core/SharedBuffer.cpp b/Source/core/SharedBuffer.cpp index f88e25056..9bcf030e7 100644 --- a/Source/core/SharedBuffer.cpp +++ b/Source/core/SharedBuffer.cpp @@ -44,7 +44,7 @@ namespace Core { SharedBuffer::SharedBuffer(const TCHAR name[], const uint32_t mode, const uint32_t bufferSize, const uint16_t administratorSize) : DataElementFile(name, mode | File::SHAREABLE | File::CREATE, bufferSize) , _administrationBuffer((string(name) + ".admin"), mode | File::SHAREABLE | File::CREATE, sizeof(Administration) + (SharedSemaphore::Size() * 2) + administratorSize + - ((administratorSize % 8 == 0) ? 0 : (8 - (administratorSize % 8))) /* Align buffer on 64 bits boundary */) + ((sizeof(Administration) + (SharedSemaphore::Size() * 2) + administratorSize) % 8 == 0 ? 0 : (8 - ((sizeof(Administration) + (SharedSemaphore::Size() * 2) + administratorSize) % 8))) /* Align buffer on 64 bits boundary */) , _administration(reinterpret_cast(PointerAlign(_administrationBuffer.Buffer()))) #ifdef __WINDOWS__ , _producer((string(name) + ".producer").c_str(), 1, 1) diff --git a/Source/core/Sync.cpp b/Source/core/Sync.cpp index d2f23f9eb..1ef0d3581 100644 --- a/Source/core/Sync.cpp +++ b/Source/core/Sync.cpp @@ -838,6 +838,7 @@ namespace Core { SharedSemaphore::SharedSemaphore(void* storage) : _semaphore(storage), _name("") { + ASSERT(storage != nullptr); } #endif From 064b4ce62cf91e41e60b89c4618eceb5f7706428 Mon Sep 17 00:00:00 2001 From: nxtumUbun Date: Thu, 3 Oct 2024 02:36:20 -0700 Subject: [PATCH 16/23] align buffer 32/64 --- Source/core/SharedBuffer.cpp | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/Source/core/SharedBuffer.cpp b/Source/core/SharedBuffer.cpp index 9bcf030e7..f8ce36530 100644 --- a/Source/core/SharedBuffer.cpp +++ b/Source/core/SharedBuffer.cpp @@ -44,7 +44,8 @@ namespace Core { SharedBuffer::SharedBuffer(const TCHAR name[], const uint32_t mode, const uint32_t bufferSize, const uint16_t administratorSize) : DataElementFile(name, mode | File::SHAREABLE | File::CREATE, bufferSize) , _administrationBuffer((string(name) + ".admin"), mode | File::SHAREABLE | File::CREATE, sizeof(Administration) + (SharedSemaphore::Size() * 2) + administratorSize + - ((sizeof(Administration) + (SharedSemaphore::Size() * 2) + administratorSize) % 8 == 0 ? 0 : (8 - ((sizeof(Administration) + (SharedSemaphore::Size() * 2) + administratorSize) % 8))) /* Align buffer on 64 bits boundary */) + ((sizeof(Administration) + (SharedSemaphore::Size() * 2) + administratorSize) % sizeof(void*) == 0 ? + 0 : (sizeof(void*) - ((sizeof(Administration) + (SharedSemaphore::Size() * 2) + administratorSize) % sizeof(void*)))) /* Align buffer on 32/64 bits boundary */) , _administration(reinterpret_cast(PointerAlign(_administrationBuffer.Buffer()))) #ifdef __WINDOWS__ , _producer((string(name) + ".producer").c_str(), 1, 1) From 74198e9981048894a117ae780c69f9392a6cc88a Mon Sep 17 00:00:00 2001 From: nxtumUbun Date: Tue, 8 Oct 2024 04:51:41 -0700 Subject: [PATCH 17/23] align adminstration --- Source/core/SharedBuffer.cpp | 8 ++++---- Source/core/Sync.cpp | 11 ++++------- Source/core/Sync.h | 4 ---- 3 files changed, 8 insertions(+), 15 deletions(-) diff --git a/Source/core/SharedBuffer.cpp b/Source/core/SharedBuffer.cpp index f8ce36530..572b1f7d9 100644 --- a/Source/core/SharedBuffer.cpp +++ b/Source/core/SharedBuffer.cpp @@ -37,14 +37,14 @@ namespace Core { , _producer(reinterpret_cast(_administration) + sizeof(Administration)) , _consumer(reinterpret_cast(_administration) + sizeof(Administration) + SharedSemaphore::Size()) #endif - , _customerAdministration(PointerAlign(&(reinterpret_cast(_administration)[sizeof(Administration)]))) + , _customerAdministration(PointerAlign(&(reinterpret_cast(_administration)[sizeof(Administration) + (SharedSemaphore::Size() * 2)]))) { Align(); } SharedBuffer::SharedBuffer(const TCHAR name[], const uint32_t mode, const uint32_t bufferSize, const uint16_t administratorSize) : DataElementFile(name, mode | File::SHAREABLE | File::CREATE, bufferSize) - , _administrationBuffer((string(name) + ".admin"), mode | File::SHAREABLE | File::CREATE, sizeof(Administration) + (SharedSemaphore::Size() * 2) + administratorSize + - ((sizeof(Administration) + (SharedSemaphore::Size() * 2) + administratorSize) % sizeof(void*) == 0 ? + , _administrationBuffer((string(name) + ".admin"), mode | File::SHAREABLE | File::CREATE, sizeof(Administration) + (SharedSemaphore::Size() * 2) + administratorSize + + (sizeof(void*) * 2) + ((sizeof(Administration) + (SharedSemaphore::Size() * 2) + administratorSize) % sizeof(void*) == 0 ? 0 : (sizeof(void*) - ((sizeof(Administration) + (SharedSemaphore::Size() * 2) + administratorSize) % sizeof(void*)))) /* Align buffer on 32/64 bits boundary */) , _administration(reinterpret_cast(PointerAlign(_administrationBuffer.Buffer()))) #ifdef __WINDOWS__ @@ -54,7 +54,7 @@ namespace Core { , _producer(reinterpret_cast(_administration) + sizeof(Administration), 1, 1) , _consumer(reinterpret_cast(_administration) + sizeof(Administration) + SharedSemaphore::Size(), 0, 1) # endif - , _customerAdministration(PointerAlign(&(reinterpret_cast(_administration)[sizeof(Administration)]))) + , _customerAdministration(PointerAlign(&(reinterpret_cast(_administration)[sizeof(Administration) + (SharedSemaphore::Size() * 2)]))) { _administration->_bytesWritten = 0; Align(); diff --git a/Source/core/Sync.cpp b/Source/core/Sync.cpp index 1ef0d3581..60ac2ba5a 100644 --- a/Source/core/Sync.cpp +++ b/Source/core/Sync.cpp @@ -799,13 +799,12 @@ namespace Core { { ASSERT(initCount <= 1); ASSERT(maxCount == 1); - #ifdef __WINDOWS__ _semaphore = (::CreateSemaphore(nullptr, initCount, maxCount, sourceName)); ASSERT(_semaphore != nullptr); #else _name = "/" + string(sourceName); - _semaphore = sem_open(_name.c_str(), O_CREAT | O_RDWR, 0644, initCount); + _semaphore = sem_open(_name.c_str(), O_CREAT | O_RDWR | O_EXCL, 0644, initCount); ASSERT(_semaphore != SEM_FAILED); #endif } @@ -828,11 +827,9 @@ namespace Core { ASSERT(storage != nullptr); ASSERT(initCount <= 1); ASSERT(maxCount == 1); - - if (initCount != 0 && maxCount != 0) { - VARIABLE_IS_NOT_USED int result = sem_init(static_cast(_semaphore), 1, initCount); - ASSERT(result != -1); - } + memset(_semaphore, 0, sizeof(sem_t)); + VARIABLE_IS_NOT_USED int result = sem_init(static_cast(_semaphore), 1, initCount); + ASSERT(result != -1); } SharedSemaphore::SharedSemaphore(void* storage) diff --git a/Source/core/Sync.h b/Source/core/Sync.h index fd1b7936f..c984ba243 100644 --- a/Source/core/Sync.h +++ b/Source/core/Sync.h @@ -194,7 +194,6 @@ namespace Core { SharedSemaphore(const TCHAR name[], const uint32_t initValue, const uint32_t maxValue); SharedSemaphore(const TCHAR name[]); - #ifndef __WINDOWS__ /* If pshared is nonzero, then the semaphore is shared between @@ -204,7 +203,6 @@ namespace Core { */ SharedSemaphore(void *storage, const uint32_t initValue, const uint32_t maxValue); SharedSemaphore(void *storage); - public: #endif ~SharedSemaphore(); @@ -212,10 +210,8 @@ namespace Core { uint32_t Lock(const uint32_t waitTime); uint32_t Unlock(); bool IsLocked(); - static size_t Size(); uint32_t MaxCount() const; - private: #ifdef __WINDOWS__ HANDLE _semaphore; From e0cb0895482c15a5d6a451a770c759fffd8d744e Mon Sep 17 00:00:00 2001 From: nxtumUbun Date: Tue, 8 Oct 2024 04:57:19 -0700 Subject: [PATCH 18/23] add variable unused --- Source/core/Sync.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Source/core/Sync.cpp b/Source/core/Sync.cpp index 60ac2ba5a..2744df7d3 100644 --- a/Source/core/Sync.cpp +++ b/Source/core/Sync.cpp @@ -821,7 +821,7 @@ namespace Core { } #ifndef __WINDOWS__ - SharedSemaphore::SharedSemaphore(void* storage, const uint32_t initCount, const uint32_t maxCount) + SharedSemaphore::SharedSemaphore(void* storage, const uint32_t initCount, VARIABLE_IS_NOT_USED const uint32_t maxCount) : _semaphore(storage), _name("") { ASSERT(storage != nullptr); From deeb57d37cbdfeb2279fe8498c8cbc1af7672532 Mon Sep 17 00:00:00 2001 From: nxtumUbun Date: Wed, 9 Oct 2024 05:23:42 -0700 Subject: [PATCH 19/23] change isLocked to Count --- Source/core/Portability.h | 14 ++++++++++++++ Source/core/Sync.cpp | 23 ++++++++++++++--------- Source/core/Sync.h | 2 +- 3 files changed, 29 insertions(+), 10 deletions(-) diff --git a/Source/core/Portability.h b/Source/core/Portability.h index 500cd3c8e..d12529edb 100644 --- a/Source/core/Portability.h +++ b/Source/core/Portability.h @@ -359,6 +359,20 @@ typedef std::string string; //const std::basic_string::size_type std::basic_string::npos = (std::basic_string::size_type) - 1; //#endif +// NTQuerySemaphore (undocumented) is used to retrieve current count of a semaphore +using NTSTATUS = LONG; +using _NTQuerySemaphore = NTSTATUS(NTAPI*)( + HANDLE SemaphoreHandle, + DWORD SemaphoreInformationClass, + PVOID SemaphoreInformation, + ULONG SemaphoreInformationLength, + PULONG ReturnLength OPTIONAL +); +struct SEMAPHORE_BASIC_INFORMATION { + ULONG CurrentCount; + ULONG MaximumCount; +}; + #define LITTLE_ENDIAN_PLATFORM 1 #undef ERROR #define __WINDOWS__ diff --git a/Source/core/Sync.cpp b/Source/core/Sync.cpp index 2744df7d3..3f8c61ee7 100644 --- a/Source/core/Sync.cpp +++ b/Source/core/Sync.cpp @@ -891,21 +891,26 @@ namespace Core { return ERROR_NONE; } - bool - SharedSemaphore::IsLocked() + uint32_t + SharedSemaphore::Count() const { #ifdef __WINDOWS__ - bool locked = (::WaitForSingleObjectEx(_semaphore, 0, FALSE) != WAIT_OBJECT_0); - - if (locked == false) { - ::ReleaseSemaphore(_semaphore, 1, nullptr); + SEMAPHORE_BASIC_INFORMATION basicInfo; + NTSTATUS status; + auto NtQuerySemaphore = reinterpret_cast<_NTQuerySemaphore>(GetProcAddress(GetModuleHandle(L"ntdll.dll"), "NtQuerySemaphore")); + if (NtQuerySemaphore) { + status = NtQuerySemaphore(_semaphore, 0, &basicInfo, sizeof(SEMAPHORE_BASIC_INFORMATION), nullptr); + if (status == ERROR_SUCCESS) { + return basicInfo.CurrentCount; } - - return (locked); + } + ASSERT(FALSE); + return 0; +} #else int semValue = 0; sem_getvalue(static_cast(_semaphore), &semValue); - return (semValue == 0); + return semValue; #endif } diff --git a/Source/core/Sync.h b/Source/core/Sync.h index c984ba243..fbe30a663 100644 --- a/Source/core/Sync.h +++ b/Source/core/Sync.h @@ -209,7 +209,7 @@ namespace Core { public: uint32_t Lock(const uint32_t waitTime); uint32_t Unlock(); - bool IsLocked(); + uint32_t Count() const; static size_t Size(); uint32_t MaxCount() const; private: From ecc27dd8b341543ca11b38735571cf897d5ffe8b Mon Sep 17 00:00:00 2001 From: nxtumUbun Date: Wed, 9 Oct 2024 07:55:55 -0700 Subject: [PATCH 20/23] create windowsapi for ntquerysemaphore --- Source/core/Sync.cpp | 43 +++++++++++++++++++++++++++++++------------ 1 file changed, 31 insertions(+), 12 deletions(-) diff --git a/Source/core/Sync.cpp b/Source/core/Sync.cpp index 3f8c61ee7..a4c4601f3 100644 --- a/Source/core/Sync.cpp +++ b/Source/core/Sync.cpp @@ -794,6 +794,36 @@ namespace Core { // CONSTRUCTOR & DESTRUCTOR //---------------------------------------------------------------------------- +#ifdef __WINDOWS__ + class WindowsAPI { + public: + WindowsAPI(WindowsAPI&&) = delete; + WindowsAPI(const WindowsAPI&) = delete; + WindowsAPI& operator=(WindowsAPI&&) = delete; + WindowsAPI& operator=(const WindowsAPI&) = delete; + + WindowsAPI() { + HMODULE ntdll = GetModuleHandle(_T("ntdll.dll")); + _ntQuerySemaphore = reinterpret_cast<_NTQuerySemaphore>(GetProcAddress(GetModuleHandle(_T("ntdll.dll")), "NtQuerySemaphore")); + ASSERT (_NtQuerySemaphore != nullptr); + } + + uint32_t GetSemaphoreCount(HANDLE parameter) { + ASSERT (_ntQuerySemaphore != nullptr); + SEMAPHORE_BASIC_INFORMATION basicInfo; + NTSTATUS status; + status = NtQuerySemaphore(parameter, 0, &basicInfo, sizeof(SEMAPHORE_BASIC_INFORMATION), nullptr); + if (status == ERROR_SUCCESS) { + return (basicInfo.CurrentCount); + } + return(0); + } + + _NTQuerySemaphore _ntQuerySemaphore; + }; + + static WindowsAPI _windowsAPI; +#endif SharedSemaphore::SharedSemaphore(const TCHAR sourceName[], const uint32_t initCount, VARIABLE_IS_NOT_USED const uint32_t maxCount) { @@ -895,18 +925,7 @@ namespace Core { SharedSemaphore::Count() const { #ifdef __WINDOWS__ - SEMAPHORE_BASIC_INFORMATION basicInfo; - NTSTATUS status; - auto NtQuerySemaphore = reinterpret_cast<_NTQuerySemaphore>(GetProcAddress(GetModuleHandle(L"ntdll.dll"), "NtQuerySemaphore")); - if (NtQuerySemaphore) { - status = NtQuerySemaphore(_semaphore, 0, &basicInfo, sizeof(SEMAPHORE_BASIC_INFORMATION), nullptr); - if (status == ERROR_SUCCESS) { - return basicInfo.CurrentCount; - } - } - ASSERT(FALSE); - return 0; -} + return (_windowsAPI.GetSemaphoreCount(_semaphore)); #else int semValue = 0; sem_getvalue(static_cast(_semaphore), &semValue); From ac01febef6e9dee6b446315cdaf441d4ddcf7719 Mon Sep 17 00:00:00 2001 From: nxtumUbun Date: Wed, 9 Oct 2024 07:56:44 -0700 Subject: [PATCH 21/23] spelling mistake --- Source/core/Sync.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Source/core/Sync.cpp b/Source/core/Sync.cpp index a4c4601f3..f9f6cc18f 100644 --- a/Source/core/Sync.cpp +++ b/Source/core/Sync.cpp @@ -805,7 +805,7 @@ namespace Core { WindowsAPI() { HMODULE ntdll = GetModuleHandle(_T("ntdll.dll")); _ntQuerySemaphore = reinterpret_cast<_NTQuerySemaphore>(GetProcAddress(GetModuleHandle(_T("ntdll.dll")), "NtQuerySemaphore")); - ASSERT (_NtQuerySemaphore != nullptr); + ASSERT (_ntQuerySemaphore != nullptr); } uint32_t GetSemaphoreCount(HANDLE parameter) { From 546f15a349a378896e4e83936e861cba0bc75790 Mon Sep 17 00:00:00 2001 From: nxtumUbun Date: Wed, 9 Oct 2024 08:07:23 -0700 Subject: [PATCH 22/23] name fix --- Source/core/Sync.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Source/core/Sync.cpp b/Source/core/Sync.cpp index f9f6cc18f..366069cc6 100644 --- a/Source/core/Sync.cpp +++ b/Source/core/Sync.cpp @@ -812,7 +812,7 @@ namespace Core { ASSERT (_ntQuerySemaphore != nullptr); SEMAPHORE_BASIC_INFORMATION basicInfo; NTSTATUS status; - status = NtQuerySemaphore(parameter, 0, &basicInfo, sizeof(SEMAPHORE_BASIC_INFORMATION), nullptr); + status = _ntQuerySemaphore(parameter, 0, &basicInfo, sizeof(SEMAPHORE_BASIC_INFORMATION), nullptr); if (status == ERROR_SUCCESS) { return (basicInfo.CurrentCount); } From b46474605817807477f9d58ec05ba2b37bc39d90 Mon Sep 17 00:00:00 2001 From: nxtumUbun Date: Wed, 9 Oct 2024 08:48:50 -0700 Subject: [PATCH 23/23] remarks --- Source/core/Sync.cpp | 12 ++++-------- 1 file changed, 4 insertions(+), 8 deletions(-) diff --git a/Source/core/Sync.cpp b/Source/core/Sync.cpp index 366069cc6..545161e0d 100644 --- a/Source/core/Sync.cpp +++ b/Source/core/Sync.cpp @@ -802,23 +802,19 @@ namespace Core { WindowsAPI& operator=(WindowsAPI&&) = delete; WindowsAPI& operator=(const WindowsAPI&) = delete; + ~WindowsAPI() = default; WindowsAPI() { - HMODULE ntdll = GetModuleHandle(_T("ntdll.dll")); _ntQuerySemaphore = reinterpret_cast<_NTQuerySemaphore>(GetProcAddress(GetModuleHandle(_T("ntdll.dll")), "NtQuerySemaphore")); ASSERT (_ntQuerySemaphore != nullptr); } - uint32_t GetSemaphoreCount(HANDLE parameter) { - ASSERT (_ntQuerySemaphore != nullptr); + uint32_t GetSemaphoreCount(HANDLE parameter) const { SEMAPHORE_BASIC_INFORMATION basicInfo; NTSTATUS status; status = _ntQuerySemaphore(parameter, 0, &basicInfo, sizeof(SEMAPHORE_BASIC_INFORMATION), nullptr); - if (status == ERROR_SUCCESS) { - return (basicInfo.CurrentCount); - } - return(0); + return (status == ERROR_SUCCESS) ? basicInfo.CurrentCount : 0; } - + private: _NTQuerySemaphore _ntQuerySemaphore; };