Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Replace SharedBuffer::Semaphore with Core::SharedSemaphore #1737

Merged
merged 31 commits into from
Oct 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
31 commits
Select commit Hold shift + click to select a range
09f0905
core::binarysemaphore in sharedbuffer
nxtum Sep 6, 2024
e941b60
spelling, binairy to binary in sharedbuffer
nxtum Sep 6, 2024
50b233a
musl fix, added sem_timedwait
nxtum Sep 20, 2024
7d17be2
fix by initializing sem
nxtum Sep 20, 2024
485f5c8
Merge branch 'master' into moveSemClockwait
pwielders Sep 20, 2024
43e3730
fixed logic of jump
nxtum Sep 20, 2024
66a7c30
Merge branch 'master' into moveSemClockwait
nxtum Sep 23, 2024
0b988ff
hide sem_t
nxtum Sep 23, 2024
a44a7ea
detect musl
nxtum Sep 26, 2024
e69b147
Merge branch 'master' into moveSemClockwait
nxtum Sep 26, 2024
61d6d76
fix windows
nxtum Sep 26, 2024
b3633d7
sem open support, max method
nxtum Sep 27, 2024
abfe114
add unused
nxtum Sep 27, 2024
e161e76
errors semopen
nxtum Sep 27, 2024
6ecaa59
create/open sem
nxtum Sep 27, 2024
df94123
sharedbuffer offset calculations
nxtum Oct 2, 2024
eb32c9d
add sizeof to handle
nxtum Oct 2, 2024
7f2e3d5
Merge branch 'master' into moveSemClockwait
nxtum Oct 2, 2024
3da9c0f
fix alignment for buffer
nxtum Oct 3, 2024
064b4ce
align buffer 32/64
nxtum Oct 3, 2024
b31bf16
Merge branch 'master' into moveSemClockwait
pwielders Oct 4, 2024
74198e9
align adminstration
nxtum Oct 8, 2024
6ef107f
Merge branch 'moveSemClockwait' of github.com:nxtum/Thunder into move…
nxtum Oct 8, 2024
e0cb089
add variable unused
nxtum Oct 8, 2024
deeb57d
change isLocked to Count
nxtum Oct 9, 2024
17e456a
Merge branch 'master' into moveSemClockwait
pwielders Oct 9, 2024
75bc869
Merge branch 'master' into moveSemClockwait
pwielders Oct 9, 2024
ecc27dd
create windowsapi for ntquerysemaphore
nxtum Oct 9, 2024
ac01feb
spelling mistake
nxtum Oct 9, 2024
546f15a
name fix
nxtum Oct 9, 2024
b464746
remarks
nxtum Oct 9, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 30 additions & 0 deletions Source/core/Portability.h
Original file line number Diff line number Diff line change
Expand Up @@ -359,6 +359,20 @@ typedef std::string string;
//const std::basic_string<char>::size_type std::basic_string<char>::npos = (std::basic_string<char>::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__
Expand All @@ -385,6 +399,7 @@ typedef std::string string;
#include <typeinfo>
#include <cmath>
#include <thread>
#include <limits.h>

#include <string.h>
#include <termios.h>
Expand Down Expand Up @@ -412,9 +427,24 @@ typedef std::string string;
#include <sys/types.h>
#include <sys/wait.h>
#include <sys/mman.h> // memfd_create in Messaging/ConsoleRedirect.h
#include <sys/inotify.h>

#include <arpa/inet.h>

#ifndef _GNU_SOURCE
#define _GNU_SOURCE
#include <features.h>
#ifndef __USE_GNU
#define __MUSL__
#endif
#undef _GNU_SOURCE /* don't contaminate other includes unnecessarily */
#else
#include <features.h>
#ifndef __USE_GNU
#define __MUSL__
#endif
#endif

#ifdef __APPLE__
#include <pthread_impl.h>
#define SCHED_BATCH 99
Expand Down
4 changes: 2 additions & 2 deletions Source/core/Proxy.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down
150 changes: 20 additions & 130 deletions Source/core/SharedBuffer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,148 +26,38 @@ 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<Administration*>(PointerAlign(_administrationBuffer.Buffer())))
#ifdef __WINDOWS__
#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<uint8_t*>(_administration)[sizeof(Administration)])))
#else
, _producer(reinterpret_cast<uint8_t*>(_administration) + sizeof(Administration))
, _consumer(reinterpret_cast<uint8_t*>(_administration) + sizeof(Administration) + SharedSemaphore::Size())
#endif
, _customerAdministration(PointerAlign(&(reinterpret_cast<uint8_t*>(_administration)[sizeof(Administration) + (SharedSemaphore::Size() * 2)])))
{
Align<uint64_t>();
}
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
+ (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<Administration*>(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
, _customerAdministration(PointerAlign(&(reinterpret_cast<uint8_t*>(_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
#ifdef __WINDOWS__
, _producer((string(name) + ".producer").c_str(), 1, 1)
, _consumer((string(name) + ".consumer").c_str(), 0, 1)
#else
, _producer(reinterpret_cast<uint8_t*>(_administration) + sizeof(Administration), 1, 1)
, _consumer(reinterpret_cast<uint8_t*>(_administration) + sizeof(Administration) + SharedSemaphore::Size(), 0, 1)
# endif
, _customerAdministration(PointerAlign(&(reinterpret_cast<uint8_t*>(_administration)[sizeof(Administration) + (SharedSemaphore::Size() * 2)])))
{
_administration->_bytesWritten = 0;
Align<uint64_t>();
}

SharedBuffer::~SharedBuffer()
{
}
}
}
}
40 changes: 3 additions & 37 deletions Source/core/SharedBuffer.h
Original file line number Diff line number Diff line change
Expand Up @@ -65,46 +65,12 @@ 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;
nxtum marked this conversation as resolved.
Show resolved Hide resolved

#ifndef __WINDOWS__
sem_t _producer;
sem_t _consumer;
#endif
};

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,
Expand Down Expand Up @@ -215,8 +181,8 @@ namespace Core {
private:
DataElementFile _administrationBuffer;
Administration* _administration;
Semaphore _producer;
Semaphore _consumer;
Core::SharedSemaphore _producer;
Core::SharedSemaphore _consumer;
uint8_t* _customerAdministration;
};
}
Expand Down
Loading
Loading