15
15
#include < sys/types.h>
16
16
#include < unistd.h>
17
17
18
+ #include < string>
19
+
18
20
#include " eckit/config/Resource.h"
19
21
#include " eckit/exception/Exceptions.h"
20
22
#include " eckit/runtime/Application.h"
@@ -35,6 +37,44 @@ const char* FULL = "/eckit-shmem-ipc-full";
35
37
const char * EMPTY = " /eckit-shmem-ipc-empty" ;
36
38
37
39
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
+
38
78
void writer () {
39
79
40
80
// shm_unlink(PATH);
@@ -53,22 +93,19 @@ void writer() {
53
93
void * memptr = mmap (NULL , SIZE, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0 );
54
94
ASSERT (memptr != MAP_FAILED);
55
95
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
+
60
99
61
100
for (size_t i = 0 ; i < 10 ; i++) {
62
- SYSCALL ( sem_wait ( empty) );
101
+ empty. wait ( );
63
102
::memcpy (memptr, " hello\0 " , 6 );
64
- SYSCALL ( sem_post ( full) );
103
+ full. post ( );
65
104
}
66
105
67
106
68
107
SYSCALL (munmap (memptr, SIZE));
69
108
SYSCALL (close (fd));
70
- SYSCALL (sem_close (full));
71
- SYSCALL (sem_close (empty));
72
109
73
110
// SYSCALL(shm_unlink(PATH));
74
111
// SYSCALL(sem_unlink(FULL));
@@ -83,21 +120,17 @@ void reader() {
83
120
void * memptr = mmap (NULL , SIZE, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0 );
84
121
ASSERT (memptr != MAP_FAILED);
85
122
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 );
90
125
91
126
for (;;) {
92
- SYSCALL ( sem_wait ( full) );
127
+ full. wait ( );
93
128
std::cout << (char *)memptr << std::endl;
94
- SYSCALL ( sem_post ( empty) );
129
+ empty. post ( );
95
130
}
96
131
97
132
SYSCALL (munmap (memptr, SIZE));
98
133
SYSCALL (close (fd));
99
- SYSCALL (sem_close (empty));
100
- SYSCALL (sem_close (full));
101
134
}
102
135
103
136
0 commit comments