Skip to content

Issue 71 #93

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Feb 25, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion include/libipc/ipc.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ enum : unsigned {

template <typename Flag>
struct IPC_EXPORT chan_impl {
static ipc::handle_t inited();

static bool connect (ipc::handle_t * ph, char const * name, unsigned mode);
static bool reconnect (ipc::handle_t * ph, unsigned mode);
static void disconnect(ipc::handle_t h);
Expand All @@ -41,7 +43,7 @@ class chan_wrapper {
private:
using detail_t = chan_impl<Flag>;

ipc::handle_t h_ = nullptr;
ipc::handle_t h_ = detail_t::inited();
unsigned mode_ = ipc::sender;
bool connected_ = false;

Expand Down
6 changes: 6 additions & 0 deletions src/libipc/ipc.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -630,6 +630,12 @@ using policy_t = ipc::policy::choose<ipc::circ::elem_array, Flag>;

namespace ipc {

template <typename Flag>
ipc::handle_t chan_impl<Flag>::inited() {
ipc::detail::waiter::init();
return nullptr;
}

template <typename Flag>
bool chan_impl<Flag>::connect(ipc::handle_t * ph, char const * name, unsigned mode) {
return detail_impl<policy_t<Flag>>::connect(ph, name, mode & receiver);
Expand Down
18 changes: 12 additions & 6 deletions src/libipc/platform/linux/mutex.h
Original file line number Diff line number Diff line change
Expand Up @@ -125,8 +125,8 @@ class mutex {
IPC_UNUSED_ std::lock_guard<std::mutex> guard {info.lock};
auto it = info.mutex_handles.find(name);
if (it == info.mutex_handles.end()) {
it = curr_prog::get().mutex_handles.emplace(name,
curr_prog::shm_data::init{name}).first;
it = info.mutex_handles.emplace(name,
curr_prog::shm_data::init{name}).first;
}
mutex_ = &it->second.mtx;
ref_ = &it->second.ref;
Expand All @@ -135,20 +135,26 @@ class mutex {
template <typename F>
void release_mutex(ipc::string const &name, F &&clear) {
if (name.empty()) return;
IPC_UNUSED_ std::lock_guard<std::mutex> guard {curr_prog::get().lock};
auto it = curr_prog::get().mutex_handles.find(name);
if (it == curr_prog::get().mutex_handles.end()) {
auto &info = curr_prog::get();
IPC_UNUSED_ std::lock_guard<std::mutex> guard {info.lock};
auto it = info.mutex_handles.find(name);
if (it == info.mutex_handles.end()) {
return;
}
if (clear()) {
curr_prog::get().mutex_handles.erase(it);
info.mutex_handles.erase(it);
}
}

public:
mutex() = default;
~mutex() = default;

static void init() {
// Avoid exception problems caused by static member initialization order.
curr_prog::get();
}

a0_mtx_t const *native() const noexcept {
return valid() ? mutex_->native() : nullptr;
}
Expand Down
27 changes: 19 additions & 8 deletions src/libipc/platform/posix/mutex.h
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,8 @@ class mutex {
IPC_UNUSED_ std::lock_guard<std::mutex> guard {info.lock};
auto it = info.mutex_handles.find(name);
if (it == info.mutex_handles.end()) {
it = curr_prog::get().mutex_handles.emplace(name,
curr_prog::shm_data::init{name, sizeof(pthread_mutex_t)}).first;
it = info.mutex_handles.emplace(name,
curr_prog::shm_data::init{name, sizeof(pthread_mutex_t)}).first;
}
shm_ = &it->second.shm;
ref_ = &it->second.ref;
Expand All @@ -69,20 +69,32 @@ class mutex {
template <typename F>
void release_mutex(ipc::string const &name, F &&clear) {
if (name.empty()) return;
IPC_UNUSED_ std::lock_guard<std::mutex> guard {curr_prog::get().lock};
auto it = curr_prog::get().mutex_handles.find(name);
if (it == curr_prog::get().mutex_handles.end()) {
auto &info = curr_prog::get();
IPC_UNUSED_ std::lock_guard<std::mutex> guard {info.lock};
auto it = info.mutex_handles.find(name);
if (it == info.mutex_handles.end()) {
return;
}
if (clear()) {
curr_prog::get().mutex_handles.erase(it);
info.mutex_handles.erase(it);
}
}

static pthread_mutex_t const &zero_mem() {
static const pthread_mutex_t tmp{};
return tmp;
}

public:
mutex() = default;
~mutex() = default;

static void init() {
// Avoid exception problems caused by static member initialization order.
zero_mem();
curr_prog::get();
}

pthread_mutex_t const *native() const noexcept {
return mutex_;
}
Expand All @@ -92,9 +104,8 @@ class mutex {
}

bool valid() const noexcept {
static const char tmp[sizeof(pthread_mutex_t)] {};
return (shm_ != nullptr) && (ref_ != nullptr) && (mutex_ != nullptr)
&& (std::memcmp(tmp, mutex_, sizeof(pthread_mutex_t)) != 0);
&& (std::memcmp(&zero_mem(), mutex_, sizeof(pthread_mutex_t)) != 0);
}

bool open(char const *name) noexcept {
Expand Down
2 changes: 2 additions & 0 deletions src/libipc/platform/win/mutex.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ class mutex {
mutex() noexcept = default;
~mutex() noexcept = default;

static void init() {}

HANDLE native() const noexcept {
return h_;
}
Expand Down
22 changes: 22 additions & 0 deletions src/libipc/sync/waiter.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
#include "libipc/waiter.h"

#include "libipc/platform/detail.h"
#if defined(IPC_OS_WINDOWS_)
#include "libipc/platform/win/mutex.h"
#elif defined(IPC_OS_LINUX_)
#include "libipc/platform/linux/mutex.h"
#elif defined(IPC_OS_QNX_)
#include "libipc/platform/posix/mutex.h"
#else/*IPC_OS*/
# error "Unsupported platform."
#endif

namespace ipc {
namespace detail {

void waiter::init() {
ipc::detail::sync::mutex::init();
}

} // namespace detail
} // namespace ipc
2 changes: 2 additions & 0 deletions src/libipc/waiter.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ class waiter {
std::atomic<bool> quit_ {false};

public:
static void init();

waiter() = default;
waiter(char const *name) {
open(name);
Expand Down