Skip to content

Commit 28fdf17

Browse files
committed
Added cleanup interfaces for ipc chan
1 parent 17dcde9 commit 28fdf17

File tree

3 files changed

+76
-2
lines changed

3 files changed

+76
-2
lines changed

include/libipc/ipc.h

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,13 @@ struct IPC_EXPORT chan_impl {
2929

3030
static char const * name(ipc::handle_t h);
3131

32-
static std::size_t recv_count(ipc::handle_t h);
33-
static bool wait_for_recv(ipc::handle_t h, std::size_t r_count, std::uint64_t tm);
32+
// Force cleanup of all shared memory storage that handles depend on.
33+
static void clear(ipc::handle_t h) noexcept;
34+
static void clear_storage(char const * name) noexcept;
35+
static void clear_storage(prefix, char const * name) noexcept;
36+
37+
static std::size_t recv_count (ipc::handle_t h);
38+
static bool wait_for_recv(ipc::handle_t h, std::size_t r_count, std::uint64_t tm);
3439

3540
static bool send(ipc::handle_t h, void const * data, std::size_t size, std::uint64_t tm);
3641
static buff_t recv(ipc::handle_t h, std::uint64_t tm);
@@ -83,6 +88,19 @@ class chan_wrapper {
8388
return detail_t::name(h_);
8489
}
8590

91+
// Clear shared memory files under opened handle.
92+
void clear() noexcept {
93+
detail_t::clear(h_);
94+
}
95+
96+
static void clear_storage(char const * name) noexcept {
97+
detail_t::clear_storage(name);
98+
}
99+
100+
static void clear_storage(prefix pref, char const * name) noexcept {
101+
detail_t::clear_storage(pref, name);
102+
}
103+
86104
ipc::handle_t handle() const noexcept {
87105
return h_;
88106
}

src/libipc/ipc.cpp

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,22 @@ struct conn_info_head {
136136
}
137137
}
138138

139+
void clear() noexcept {
140+
cc_waiter_.clear();
141+
wt_waiter_.clear();
142+
rd_waiter_.clear();
143+
acc_h_.clear();
144+
}
145+
146+
static void clear_storage(char const * prefix, char const * name) noexcept {
147+
auto p = ipc::make_string(prefix);
148+
auto n = ipc::make_string(name);
149+
ipc::detail::waiter::clear_storage(ipc::make_prefix(p, {"CC_CONN__", n}).c_str());
150+
ipc::detail::waiter::clear_storage(ipc::make_prefix(p, {"WT_CONN__", n}).c_str());
151+
ipc::detail::waiter::clear_storage(ipc::make_prefix(p, {"RD_CONN__", n}).c_str());
152+
ipc::shm::handle::clear_storage(ipc::make_prefix(p, {"AC_CONN__", n}).c_str());
153+
}
154+
139155
void quit_waiting() {
140156
cc_waiter_.quit_waiting();
141157
wt_waiter_.quit_waiting();
@@ -386,6 +402,20 @@ struct queue_generator {
386402
}
387403
}
388404

405+
void clear() noexcept {
406+
que_.clear();
407+
conn_info_head::clear();
408+
}
409+
410+
static void clear_storage(char const * prefix, char const * name) noexcept {
411+
queue_t::clear_storage(ipc::make_prefix(prefix, {
412+
"QU_CONN__",
413+
ipc::to_string(DataSize), "__",
414+
ipc::to_string(AlignSize), "__",
415+
name}).c_str());
416+
conn_info_head::clear_storage(prefix, name);
417+
}
418+
389419
void disconnect_receiver() {
390420
bool dis = que_.disconnect();
391421
this->quit_waiting();
@@ -739,6 +769,27 @@ char const * chan_impl<Flag>::name(ipc::handle_t h) {
739769
return (info == nullptr) ? nullptr : info->name_.c_str();
740770
}
741771

772+
template <typename Flag>
773+
void chan_impl<Flag>::clear(ipc::handle_t h) noexcept {
774+
disconnect(h);
775+
using conn_info_t = typename detail_impl<policy_t<Flag>>::conn_info_t;
776+
auto conn_info_p = static_cast<conn_info_t *>(h);
777+
if (conn_info_p == nullptr) return;
778+
conn_info_p->clear();
779+
destroy(h);
780+
}
781+
782+
template <typename Flag>
783+
void chan_impl<Flag>::clear_storage(char const * name) noexcept {
784+
chan_impl<Flag>::clear_storage({nullptr}, name);
785+
}
786+
787+
template <typename Flag>
788+
void chan_impl<Flag>::clear_storage(prefix pref, char const * name) noexcept {
789+
using conn_info_t = typename detail_impl<policy_t<Flag>>::conn_info_t;
790+
conn_info_t::clear_storage(pref.str, name);
791+
}
792+
742793
template <typename Flag>
743794
std::size_t chan_impl<Flag>::recv_count(ipc::handle_t h) {
744795
return detail_impl<policy_t<Flag>>::recv_count(h);

src/libipc/queue.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,11 @@ class queue_base : public queue_conn {
131131
return elems_ != nullptr;
132132
}
133133

134+
void clear() noexcept {
135+
base_t::clear();
136+
elems_ = nullptr;
137+
}
138+
134139
elems_t * elems() noexcept { return elems_; }
135140
elems_t const * elems() const noexcept { return elems_; }
136141

0 commit comments

Comments
 (0)