Skip to content

Commit 9d8bd2f

Browse files
committed
Update
1 parent c308ce1 commit 9d8bd2f

File tree

2 files changed

+52
-17
lines changed

2 files changed

+52
-17
lines changed

eckit.code-workspace

+3-1
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,9 @@
9292
"cfenv": "cpp",
9393
"cinttypes": "cpp",
9494
"semaphore": "cpp",
95-
"__functional_03": "cpp"
95+
"__functional_03": "cpp",
96+
"numbers": "cpp",
97+
"filesystem": "cpp"
9698
}
9799
}
98100
}

src/sandbox/shmem_ipc.cc

+49-16
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@
1515
#include <sys/types.h>
1616
#include <unistd.h>
1717

18+
#include <string>
19+
1820
#include "eckit/config/Resource.h"
1921
#include "eckit/exception/Exceptions.h"
2022
#include "eckit/runtime/Application.h"
@@ -35,6 +37,44 @@ const char* FULL = "/eckit-shmem-ipc-full";
3537
const char* EMPTY = "/eckit-shmem-ipc-empty";
3638

3739

40+
class NamedSemaphore {
41+
public:
42+
NamedSemaphore(const std::string& name, int value, bool create = true, bool unlink = false, int mode = 0664);
43+
~NamedSemaphore();
44+
45+
void wait();
46+
void post();
47+
48+
private:
49+
std::string name_;
50+
bool unlink_;
51+
sem_t* semaphore_ = SEM_FAILED;
52+
};
53+
54+
NamedSemaphore::NamedSemaphore(const std::string& name, int value, bool create, bool unlink, int mode) :
55+
name_(name), unlink_(unlink) {
56+
semaphore_ = sem_open(name_.c_str(), create ? O_CREAT : 0, mode, value);
57+
if (semaphore_ == SEM_FAILED) {
58+
throw FailedSystemCall("sem_open(" + name_ + ")");
59+
}
60+
}
61+
62+
NamedSemaphore::~NamedSemaphore() {
63+
if (semaphore_ != SEM_FAILED) {
64+
SYSCALL(::sem_close(semaphore_));
65+
}
66+
if (unlink_) {
67+
SYSCALL(::sem_unlink(name_.c_str()));
68+
}
69+
}
70+
71+
void NamedSemaphore::wait() {
72+
SYSCALL(::sem_wait(semaphore_));
73+
}
74+
void NamedSemaphore::post() {
75+
SYSCALL(::sem_post(semaphore_));
76+
}
77+
3878
void writer() {
3979

4080
// shm_unlink(PATH);
@@ -53,22 +93,19 @@ void writer() {
5393
void* memptr = mmap(NULL, SIZE, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
5494
ASSERT(memptr != MAP_FAILED);
5595

56-
sem_t* full = sem_open(FULL, O_CREAT, 0644, 0);
57-
ASSERT(full != SEM_FAILED);
58-
sem_t* empty = sem_open(EMPTY, O_CREAT, 0644, 1);
59-
ASSERT(empty != SEM_FAILED);
96+
NamedSemaphore full(FULL, 0);
97+
NamedSemaphore empty(EMPTY, 1);
98+
6099

61100
for (size_t i = 0; i < 10; i++) {
62-
SYSCALL(sem_wait(empty));
101+
empty.wait();
63102
::memcpy(memptr, "hello\0", 6);
64-
SYSCALL(sem_post(full));
103+
full.post();
65104
}
66105

67106

68107
SYSCALL(munmap(memptr, SIZE));
69108
SYSCALL(close(fd));
70-
SYSCALL(sem_close(full));
71-
SYSCALL(sem_close(empty));
72109

73110
// SYSCALL(shm_unlink(PATH));
74111
// SYSCALL(sem_unlink(FULL));
@@ -83,21 +120,17 @@ void reader() {
83120
void* memptr = mmap(NULL, SIZE, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
84121
ASSERT(memptr != MAP_FAILED);
85122

86-
sem_t* full = sem_open(FULL, O_CREAT, 0644, 0);
87-
ASSERT(full != SEM_FAILED);
88-
sem_t* empty = sem_open(EMPTY, O_CREAT, 0644, 1);
89-
ASSERT(empty != SEM_FAILED);
123+
NamedSemaphore full(FULL, 0);
124+
NamedSemaphore empty(EMPTY, 1);
90125

91126
for (;;) {
92-
SYSCALL(sem_wait(full));
127+
full.wait();
93128
std::cout << (char*)memptr << std::endl;
94-
SYSCALL(sem_post(empty));
129+
empty.post();
95130
}
96131

97132
SYSCALL(munmap(memptr, SIZE));
98133
SYSCALL(close(fd));
99-
SYSCALL(sem_close(empty));
100-
SYSCALL(sem_close(full));
101134
}
102135

103136

0 commit comments

Comments
 (0)