Skip to content

Commit dd532b3

Browse files
committed
Update lightweightsemaphore.h
... to support building on zOS. Signed-off-by: v1gnesh <v1gnesh@users.noreply.github.com>
1 parent afaca84 commit dd532b3

1 file changed

Lines changed: 76 additions & 0 deletions

File tree

lightweightsemaphore.h

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@ extern "C" {
2626
#include <mach/mach.h>
2727
#elif defined(__unix__)
2828
#include <semaphore.h>
29+
#elif defined(__MVS__)
30+
#include <zos-semaphore.h>
2931

3032
#if defined(__GLIBC_PREREQ) && defined(_GNU_SOURCE)
3133
#if __GLIBC_PREREQ(2,30)
@@ -253,6 +255,80 @@ class Semaphore
253255
}
254256
}
255257
};
258+
#elif defined(__MVS__)
259+
//---------------------------------------------------------
260+
// Semaphore (MVS aka z/OS)
261+
//---------------------------------------------------------
262+
class Semaphore
263+
{
264+
private:
265+
sem_t m_sema;
266+
267+
Semaphore(const Semaphore& other) MOODYCAMEL_DELETE_FUNCTION;
268+
Semaphore& operator=(const Semaphore& other) MOODYCAMEL_DELETE_FUNCTION;
269+
270+
public:
271+
Semaphore(int initialCount = 0)
272+
{
273+
assert(initialCount >= 0);
274+
int rc = sem_init(&m_sema, 0, initialCount);
275+
assert(rc == 0);
276+
(void)rc;
277+
}
278+
279+
~Semaphore()
280+
{
281+
sem_destroy(&m_sema);
282+
}
283+
284+
bool wait()
285+
{
286+
// http://stackoverflow.com/questions/2013181/gdb-causes-sem-wait-to-fail-with-eintr-error
287+
int rc;
288+
do {
289+
rc = sem_wait(&m_sema);
290+
} while (rc == -1 && errno == EINTR);
291+
return rc == 0;
292+
}
293+
294+
bool try_wait()
295+
{
296+
int rc;
297+
do {
298+
rc = sem_trywait(&m_sema);
299+
} while (rc == -1 && errno == EINTR);
300+
return rc == 0;
301+
}
302+
303+
bool timed_wait(std::uint64_t usecs)
304+
{
305+
struct timespec ts;
306+
const int usecs_in_1_sec = 1000000;
307+
const int nsecs_in_1_sec = 1000000000;
308+
309+
ts.tv_sec = usecs / usecs_in_1_sec;
310+
ts.tv_nsec = (usecs % usecs_in_1_sec) * 1000;
311+
312+
int rc;
313+
do {
314+
rc = sem_timedwait(&m_sema, &ts);
315+
} while (rc == -1 && errno == EINTR);
316+
return rc == 0;
317+
}
318+
319+
void signal()
320+
{
321+
while (sem_post(&m_sema) == -1);
322+
}
323+
324+
void signal(int count)
325+
{
326+
while (count-- > 0)
327+
{
328+
while (sem_post(&m_sema) == -1);
329+
}
330+
}
331+
};
256332
#else
257333
#error Unsupported platform! (No semaphore wrapper available)
258334
#endif

0 commit comments

Comments
 (0)