Update lightweightsemaphore.h

... to support building on zOS.

Signed-off-by: v1gnesh <v1gnesh@users.noreply.github.com>
This commit is contained in:
v1gnesh
2023-06-05 19:46:00 +05:30
parent f640d2846e
commit f26309af52

View File

@@ -28,8 +28,6 @@ extern "C" {
#include <zos-semaphore.h>
#elif defined(__unix__)
#include <semaphore.h>
#elif defined(__MVS__)
#include <zos-semaphore.h>
#if defined(__GLIBC_PREREQ) && defined(_GNU_SOURCE)
#if __GLIBC_PREREQ(2,30)
@@ -257,80 +255,6 @@ public:
}
}
};
#elif defined(__MVS__)
//---------------------------------------------------------
// Semaphore (MVS aka z/OS)
//---------------------------------------------------------
class Semaphore
{
private:
sem_t m_sema;
Semaphore(const Semaphore& other) MOODYCAMEL_DELETE_FUNCTION;
Semaphore& operator=(const Semaphore& other) MOODYCAMEL_DELETE_FUNCTION;
public:
Semaphore(int initialCount = 0)
{
assert(initialCount >= 0);
int rc = sem_init(&m_sema, 0, initialCount);
assert(rc == 0);
(void)rc;
}
~Semaphore()
{
sem_destroy(&m_sema);
}
bool wait()
{
// http://stackoverflow.com/questions/2013181/gdb-causes-sem-wait-to-fail-with-eintr-error
int rc;
do {
rc = sem_wait(&m_sema);
} while (rc == -1 && errno == EINTR);
return rc == 0;
}
bool try_wait()
{
int rc;
do {
rc = sem_trywait(&m_sema);
} while (rc == -1 && errno == EINTR);
return rc == 0;
}
bool timed_wait(std::uint64_t usecs)
{
struct timespec ts;
const int usecs_in_1_sec = 1000000;
const int nsecs_in_1_sec = 1000000000;
ts.tv_sec = usecs / usecs_in_1_sec;
ts.tv_nsec = (usecs % usecs_in_1_sec) * 1000;
int rc;
do {
rc = sem_timedwait(&m_sema, &ts);
} while (rc == -1 && errno == EINTR);
return rc == 0;
}
void signal()
{
while (sem_post(&m_sema) == -1);
}
void signal(int count)
{
while (count-- > 0)
{
while (sem_post(&m_sema) == -1);
}
}
};
#else
#error Unsupported platform! (No semaphore wrapper available)
#endif