Skip to content

Commit e1f377d

Browse files
committed
Added a cleanup interface for the synchronization facilities
1 parent 29678f1 commit e1f377d

File tree

16 files changed

+146
-6
lines changed

16 files changed

+146
-6
lines changed

include/libipc/condition.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,9 @@ class IPC_EXPORT condition {
2626
bool open(char const *name) noexcept;
2727
void close() noexcept;
2828

29+
void clear() noexcept;
30+
static void clear_storage(char const * name) noexcept;
31+
2932
bool wait(ipc::sync::mutex &mtx, std::uint64_t tm = ipc::invalid_value) noexcept;
3033
bool notify(ipc::sync::mutex &mtx) noexcept;
3134
bool broadcast(ipc::sync::mutex &mtx) noexcept;

include/libipc/mutex.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,9 @@ class IPC_EXPORT mutex {
2626
bool open(char const *name) noexcept;
2727
void close() noexcept;
2828

29+
void clear() noexcept;
30+
static void clear_storage(char const * name) noexcept;
31+
2932
bool lock(std::uint64_t tm = ipc::invalid_value) noexcept;
3033
bool try_lock() noexcept(false); // std::system_error
3134
bool unlock() noexcept;

include/libipc/semaphore.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,9 @@ class IPC_EXPORT semaphore {
2525
bool open(char const *name, std::uint32_t count = 0) noexcept;
2626
void close() noexcept;
2727

28+
void clear() noexcept;
29+
static void clear_storage(char const * name) noexcept;
30+
2831
bool wait(std::uint64_t tm = ipc::invalid_value) noexcept;
2932
bool post(std::uint32_t count = 1) noexcept;
3033

src/libipc/platform/linux/mutex.h

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,7 @@ class mutex {
136136
}
137137

138138
template <typename F>
139-
void release_mutex(ipc::string const &name, F &&clear) {
139+
static void release_mutex(ipc::string const &name, F &&clear) {
140140
if (name.empty()) return;
141141
auto &info = curr_prog::get();
142142
IPC_UNUSED_ std::lock_guard<std::mutex> guard {info.lock};
@@ -192,6 +192,23 @@ class mutex {
192192
ref_ = nullptr;
193193
}
194194

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+
195212
bool lock(std::uint64_t tm) noexcept {
196213
if (!valid()) return false;
197214
return mutex_->lock(tm);

src/libipc/platform/linux/sync_obj_impl.h

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,15 @@ class obj_impl {
6262
shm_.release();
6363
h_ = nullptr;
6464
}
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+
}
6574
};
6675

6776
} // namespace sync

src/libipc/platform/posix/condition.h

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,21 @@ class condition {
8888
cond_ = nullptr;
8989
}
9090

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+
91106
bool wait(ipc::sync::mutex &mtx, std::uint64_t tm) noexcept {
92107
if (!valid()) return false;
93108
switch (tm) {

src/libipc/platform/posix/mutex.h

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ class mutex {
7171
}
7272

7373
template <typename F>
74-
void release_mutex(ipc::string const &name, F &&clear) {
74+
static void release_mutex(ipc::string const &name, F &&clear) {
7575
if (name.empty()) return;
7676
auto &info = curr_prog::get();
7777
IPC_UNUSED_ std::lock_guard<std::mutex> guard {info.lock};
@@ -169,6 +169,30 @@ class mutex {
169169
mutex_ = nullptr;
170170
}
171171

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+
172196
bool lock(std::uint64_t tm) noexcept {
173197
if (!valid()) return false;
174198
for (;;) {

src/libipc/platform/posix/semaphore_impl.h

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,24 @@ class semaphore {
6262
}
6363
}
6464

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+
6583
bool wait(std::uint64_t tm) noexcept {
6684
if (!valid()) return false;
6785
if (tm == invalid_value) {

src/libipc/platform/posix/shm_posix.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -188,7 +188,7 @@ void remove(id_t id) noexcept {
188188
}
189189
}
190190

191-
void remove(char const * name) {
191+
void remove(char const * name) noexcept {
192192
if (!is_valid_string(name)) {
193193
ipc::error("fail remove: name is empty\n");
194194
return;

src/libipc/platform/win/condition.h

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,16 @@ class condition {
6767
shm_.release();
6868
}
6969

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+
7080
bool wait(ipc::sync::mutex &mtx, std::uint64_t tm) noexcept {
7181
if (!valid()) return false;
7282
auto &cnt = counter();

0 commit comments

Comments
 (0)