File tree Expand file tree Collapse file tree 16 files changed +146
-6
lines changed Expand file tree Collapse file tree 16 files changed +146
-6
lines changed Original file line number Diff line number Diff line change @@ -26,6 +26,9 @@ class IPC_EXPORT condition {
26
26
bool open (char const *name) noexcept ;
27
27
void close () noexcept ;
28
28
29
+ void clear () noexcept ;
30
+ static void clear_storage (char const * name) noexcept ;
31
+
29
32
bool wait (ipc::sync::mutex &mtx, std::uint64_t tm = ipc::invalid_value) noexcept ;
30
33
bool notify (ipc::sync::mutex &mtx) noexcept ;
31
34
bool broadcast (ipc::sync::mutex &mtx) noexcept ;
Original file line number Diff line number Diff line change @@ -26,6 +26,9 @@ class IPC_EXPORT mutex {
26
26
bool open (char const *name) noexcept ;
27
27
void close () noexcept ;
28
28
29
+ void clear () noexcept ;
30
+ static void clear_storage (char const * name) noexcept ;
31
+
29
32
bool lock (std::uint64_t tm = ipc::invalid_value) noexcept ;
30
33
bool try_lock () noexcept (false ); // std::system_error
31
34
bool unlock () noexcept ;
Original file line number Diff line number Diff line change @@ -25,6 +25,9 @@ class IPC_EXPORT semaphore {
25
25
bool open (char const *name, std::uint32_t count = 0 ) noexcept ;
26
26
void close () noexcept ;
27
27
28
+ void clear () noexcept ;
29
+ static void clear_storage (char const * name) noexcept ;
30
+
28
31
bool wait (std::uint64_t tm = ipc::invalid_value) noexcept ;
29
32
bool post (std::uint32_t count = 1 ) noexcept ;
30
33
Original file line number Diff line number Diff line change @@ -136,7 +136,7 @@ class mutex {
136
136
}
137
137
138
138
template <typename F>
139
- void release_mutex (ipc::string const &name, F &&clear) {
139
+ static void release_mutex (ipc::string const &name, F &&clear) {
140
140
if (name.empty ()) return ;
141
141
auto &info = curr_prog::get ();
142
142
IPC_UNUSED_ std::lock_guard<std::mutex> guard {info.lock };
@@ -192,6 +192,23 @@ class mutex {
192
192
ref_ = nullptr ;
193
193
}
194
194
195
+ void clear () noexcept {
196
+ if (mutex_ != nullptr ) {
197
+ if (mutex_->name () != nullptr ) {
198
+ release_mutex (mutex_->name (), [] { return true ; });
199
+ }
200
+ mutex_->clear ();
201
+ }
202
+ mutex_ = nullptr ;
203
+ ref_ = nullptr ;
204
+ }
205
+
206
+ static void clear_storage (char const *name) noexcept {
207
+ if (name == nullptr ) return ;
208
+ release_mutex (name, [] { return true ; });
209
+ robust_mutex::clear_storage (name);
210
+ }
211
+
195
212
bool lock (std::uint64_t tm) noexcept {
196
213
if (!valid ()) return false ;
197
214
return mutex_->lock (tm);
Original file line number Diff line number Diff line change @@ -62,6 +62,15 @@ class obj_impl {
62
62
shm_.release ();
63
63
h_ = nullptr ;
64
64
}
65
+
66
+ void clear () noexcept {
67
+ shm_.clear (); // Make sure the storage is cleaned up.
68
+ h_ = nullptr ;
69
+ }
70
+
71
+ static void clear_storage (char const *name) noexcept {
72
+ ipc::shm::handle::clear_storage (name);
73
+ }
65
74
};
66
75
67
76
} // namespace sync
Original file line number Diff line number Diff line change @@ -88,6 +88,21 @@ class condition {
88
88
cond_ = nullptr ;
89
89
}
90
90
91
+ void clear () noexcept {
92
+ if ((shm_.ref () <= 1 ) && cond_ != nullptr ) {
93
+ int eno;
94
+ if ((eno = ::pthread_cond_destroy (cond_)) != 0 ) {
95
+ ipc::error (" fail pthread_cond_destroy[%d]\n " , eno);
96
+ }
97
+ }
98
+ shm_.clear (); // Make sure the storage is cleaned up.
99
+ cond_ = nullptr ;
100
+ }
101
+
102
+ static void clear_storage (char const *name) noexcept {
103
+ ipc::shm::handle::clear_storage (name);
104
+ }
105
+
91
106
bool wait (ipc::sync::mutex &mtx, std::uint64_t tm) noexcept {
92
107
if (!valid ()) return false ;
93
108
switch (tm) {
Original file line number Diff line number Diff line change @@ -71,7 +71,7 @@ class mutex {
71
71
}
72
72
73
73
template <typename F>
74
- void release_mutex (ipc::string const &name, F &&clear) {
74
+ static void release_mutex (ipc::string const &name, F &&clear) {
75
75
if (name.empty ()) return ;
76
76
auto &info = curr_prog::get ();
77
77
IPC_UNUSED_ std::lock_guard<std::mutex> guard {info.lock };
@@ -169,6 +169,30 @@ class mutex {
169
169
mutex_ = nullptr ;
170
170
}
171
171
172
+ void clear () noexcept {
173
+ if ((shm_ != nullptr ) && (mutex_ != nullptr )) {
174
+ if (shm_->name () != nullptr ) {
175
+ release_mutex (shm_->name (), [this ] {
176
+ int eno;
177
+ if ((eno = ::pthread_mutex_destroy (mutex_)) != 0 ) {
178
+ ipc::error (" fail pthread_mutex_destroy[%d]\n " , eno);
179
+ }
180
+ return true ;
181
+ });
182
+ }
183
+ shm_->clear ();
184
+ }
185
+ shm_ = nullptr ;
186
+ ref_ = nullptr ;
187
+ mutex_ = nullptr ;
188
+ }
189
+
190
+ static void clear_storage (char const *name) noexcept {
191
+ if (name == nullptr ) return ;
192
+ release_mutex (name, [] { return true ; });
193
+ ipc::shm::handle::clear_storage (name);
194
+ }
195
+
172
196
bool lock (std::uint64_t tm) noexcept {
173
197
if (!valid ()) return false ;
174
198
for (;;) {
Original file line number Diff line number Diff line change @@ -62,6 +62,24 @@ class semaphore {
62
62
}
63
63
}
64
64
65
+ void clear () noexcept {
66
+ if (valid ()) {
67
+ if (::sem_close (h_) != 0 ) {
68
+ ipc::error (" fail sem_close[%d]: %s\n " , errno);
69
+ }
70
+ h_ = SEM_FAILED;
71
+ }
72
+ char const *name = shm_.name ();
73
+ if (name == nullptr ) return ;
74
+ ::sem_unlink (name);
75
+ shm_.clear (); // Make sure the storage is cleaned up.
76
+ }
77
+
78
+ static void clear_storage (char const *name) noexcept {
79
+ ::sem_unlink (name);
80
+ ipc::shm::handle::clear_storage (name);
81
+ }
82
+
65
83
bool wait (std::uint64_t tm) noexcept {
66
84
if (!valid ()) return false ;
67
85
if (tm == invalid_value) {
Original file line number Diff line number Diff line change @@ -188,7 +188,7 @@ void remove(id_t id) noexcept {
188
188
}
189
189
}
190
190
191
- void remove (char const * name) {
191
+ void remove (char const * name) noexcept {
192
192
if (!is_valid_string (name)) {
193
193
ipc::error (" fail remove: name is empty\n " );
194
194
return ;
Original file line number Diff line number Diff line change @@ -67,6 +67,16 @@ class condition {
67
67
shm_.release ();
68
68
}
69
69
70
+ void clear () noexcept {
71
+ close ();
72
+ }
73
+
74
+ static void clear_storage (char const *name) noexcept {
75
+ ipc::shm::handle::clear_storage (name);
76
+ ipc::sync::mutex::clear_storage (name);
77
+ ipc::sync::semaphore::clear_storage (name);
78
+ }
79
+
70
80
bool wait (ipc::sync::mutex &mtx, std::uint64_t tm) noexcept {
71
81
if (!valid ()) return false ;
72
82
auto &cnt = counter ();
You can’t perform that action at this time.
0 commit comments