Merge pull request #166 from mutouyun/feature/imp-log

refactor(log): Replace utility/log with imp/log interface
This commit is contained in:
木头云 2025-12-15 20:18:06 +08:00 committed by GitHub
commit 3269bde1a5
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
21 changed files with 390 additions and 334 deletions

View File

@ -19,7 +19,7 @@
#include "libipc/rw_lock.h" #include "libipc/rw_lock.h"
#include "libipc/waiter.h" #include "libipc/waiter.h"
#include "libipc/utility/log.h" #include "libipc/imp/log.h"
#include "libipc/utility/id_pool.h" #include "libipc/utility/id_pool.h"
#include "libipc/utility/scope_guard.h" #include "libipc/utility/scope_guard.h"
#include "libipc/utility/utility.h" #include "libipc/utility/utility.h"
@ -76,6 +76,7 @@ ipc::buff_t make_cache(T &data, std::size_t size) {
} }
acc_t *cc_acc(std::string const &pref) { acc_t *cc_acc(std::string const &pref) {
LIBIPC_LOG();
static auto *phs = new ipc::unordered_map<std::string, ipc::shm::handle>; // no delete static auto *phs = new ipc::unordered_map<std::string, ipc::shm::handle>; // no delete
static std::mutex lock; static std::mutex lock;
std::lock_guard<std::mutex> guard {lock}; std::lock_guard<std::mutex> guard {lock};
@ -84,7 +85,7 @@ acc_t *cc_acc(std::string const &pref) {
std::string shm_name {ipc::make_prefix(pref, "CA_CONN__")}; std::string shm_name {ipc::make_prefix(pref, "CA_CONN__")};
ipc::shm::handle h; ipc::shm::handle h;
if (!h.acquire(shm_name.c_str(), sizeof(acc_t))) { if (!h.acquire(shm_name.c_str(), sizeof(acc_t))) {
ipc::error("[cc_acc] acquire failed: %s\n", shm_name.c_str()); log.error("[cc_acc] acquire failed: ", shm_name);
return nullptr; return nullptr;
} }
it = phs->emplace(pref, std::move(h)).first; it = phs->emplace(pref, std::move(h)).first;
@ -217,10 +218,11 @@ auto& chunk_storages() {
std::mutex lock_; std::mutex lock_;
static bool make_handle(ipc::shm::handle &h, std::string const &shm_name, std::size_t chunk_size) { static bool make_handle(ipc::shm::handle &h, std::string const &shm_name, std::size_t chunk_size) {
LIBIPC_LOG();
if (!h.valid() && if (!h.valid() &&
!h.acquire( shm_name.c_str(), !h.acquire( shm_name.c_str(),
sizeof(chunk_info_t) + chunk_info_t::chunks_mem_size(chunk_size) )) { sizeof(chunk_info_t) + chunk_info_t::chunks_mem_size(chunk_size) )) {
ipc::error("[chunk_storages] chunk_shm.id_info_.acquire failed: chunk_size = %zd\n", chunk_size); log.error("[chunk_storages] chunk_shm.id_info_.acquire failed: chunk_size = ", chunk_size);
return false; return false;
} }
return true; return true;
@ -228,6 +230,7 @@ auto& chunk_storages() {
public: public:
chunk_info_t *get_info(conn_info_head *inf, std::size_t chunk_size) { chunk_info_t *get_info(conn_info_head *inf, std::size_t chunk_size) {
LIBIPC_LOG();
std::string pref {(inf == nullptr) ? std::string{} : inf->prefix_}; std::string pref {(inf == nullptr) ? std::string{} : inf->prefix_};
std::string shm_name {ipc::make_prefix(pref, "CHUNK_INFO__", chunk_size)}; std::string shm_name {ipc::make_prefix(pref, "CHUNK_INFO__", chunk_size)};
ipc::shm::handle *h; ipc::shm::handle *h;
@ -240,7 +243,7 @@ auto& chunk_storages() {
} }
auto *info = static_cast<chunk_info_t*>(h->get()); auto *info = static_cast<chunk_info_t*>(h->get());
if (info == nullptr) { if (info == nullptr) {
ipc::error("[chunk_storages] chunk_shm.id_info_.get failed: chunk_size = %zd\n", chunk_size); log.error("[chunk_storages] chunk_shm.id_info_.get failed: chunk_size = ", chunk_size);
return nullptr; return nullptr;
} }
return info; return info;
@ -290,8 +293,9 @@ std::pair<ipc::storage_id_t, void*> acquire_storage(conn_info_head *inf, std::si
} }
void *find_storage(ipc::storage_id_t id, conn_info_head *inf, std::size_t size) { void *find_storage(ipc::storage_id_t id, conn_info_head *inf, std::size_t size) {
LIBIPC_LOG();
if (id < 0) { if (id < 0) {
ipc::error("[find_storage] id is invalid: id = %ld, size = %zd\n", (long)id, size); log.error("[find_storage] id is invalid: id = ", (long)id, ", size = ", size);
return nullptr; return nullptr;
} }
std::size_t chunk_size = calc_chunk_size(size); std::size_t chunk_size = calc_chunk_size(size);
@ -301,8 +305,9 @@ void *find_storage(ipc::storage_id_t id, conn_info_head *inf, std::size_t size)
} }
void release_storage(ipc::storage_id_t id, conn_info_head *inf, std::size_t size) { void release_storage(ipc::storage_id_t id, conn_info_head *inf, std::size_t size) {
LIBIPC_LOG();
if (id < 0) { if (id < 0) {
ipc::error("[release_storage] id is invalid: id = %ld, size = %zd\n", (long)id, size); log.error("[release_storage] id is invalid: id = ", (long)id, ", size = ", size);
return; return;
} }
std::size_t chunk_size = calc_chunk_size(size); std::size_t chunk_size = calc_chunk_size(size);
@ -334,8 +339,9 @@ bool sub_rc(ipc::wr<Rp, Rc, ipc::trans::broadcast>,
template <typename Flag> template <typename Flag>
void recycle_storage(ipc::storage_id_t id, conn_info_head *inf, std::size_t size, ipc::circ::cc_t curr_conns, ipc::circ::cc_t conn_id) { void recycle_storage(ipc::storage_id_t id, conn_info_head *inf, std::size_t size, ipc::circ::cc_t curr_conns, ipc::circ::cc_t conn_id) {
LIBIPC_LOG();
if (id < 0) { if (id < 0) {
ipc::error("[recycle_storage] id is invalid: id = %ld, size = %zd\n", (long)id, size); log.error("[recycle_storage] id is invalid: id = ", (long)id, ", size = ", size);
return; return;
} }
std::size_t chunk_size = calc_chunk_size(size); std::size_t chunk_size = calc_chunk_size(size);
@ -355,11 +361,12 @@ void recycle_storage(ipc::storage_id_t id, conn_info_head *inf, std::size_t size
template <typename MsgT> template <typename MsgT>
bool clear_message(conn_info_head *inf, void* p) { bool clear_message(conn_info_head *inf, void* p) {
LIBIPC_LOG();
auto msg = static_cast<MsgT*>(p); auto msg = static_cast<MsgT*>(p);
if (msg->storage_) { if (msg->storage_) {
std::int32_t r_size = static_cast<std::int32_t>(ipc::data_length) + msg->remain_; std::int32_t r_size = static_cast<std::int32_t>(ipc::data_length) + msg->remain_;
if (r_size <= 0) { if (r_size <= 0) {
ipc::error("[clear_message] invalid msg size: %d\n", (int)r_size); log.error("[clear_message] invalid msg size: ", (int)r_size);
return true; return true;
} }
release_storage(*reinterpret_cast<ipc::storage_id_t*>(&msg->data_), release_storage(*reinterpret_cast<ipc::storage_id_t*>(&msg->data_),
@ -518,33 +525,34 @@ static bool wait_for_recv(ipc::handle_t h, std::size_t r_count, std::uint64_t tm
template <typename F> template <typename F>
static bool send(F&& gen_push, ipc::handle_t h, void const * data, std::size_t size) { static bool send(F&& gen_push, ipc::handle_t h, void const * data, std::size_t size) {
LIBIPC_LOG();
if (data == nullptr || size == 0) { if (data == nullptr || size == 0) {
ipc::error("fail: send(%p, %zd)\n", data, size); log.error("fail: send(", data, ", ", size, ")");
return false; return false;
} }
auto que = queue_of(h); auto que = queue_of(h);
if (que == nullptr) { if (que == nullptr) {
ipc::error("fail: send, queue_of(h) == nullptr\n"); log.error("fail: send, queue_of(h) == nullptr");
return false; return false;
} }
if (que->elems() == nullptr) { if (que->elems() == nullptr) {
ipc::error("fail: send, queue_of(h)->elems() == nullptr\n"); log.error("fail: send, queue_of(h)->elems() == nullptr");
return false; return false;
} }
if (!que->ready_sending()) { if (!que->ready_sending()) {
ipc::error("fail: send, que->ready_sending() == false\n"); log.error("fail: send, que->ready_sending() == false");
return false; return false;
} }
ipc::circ::cc_t conns = que->elems()->connections(std::memory_order_relaxed); ipc::circ::cc_t conns = que->elems()->connections(std::memory_order_relaxed);
if (conns == 0) { if (conns == 0) {
ipc::error("fail: send, there is no receiver on this connection.\n"); log.error("fail: send, there is no receiver on this connection.");
return false; return false;
} }
// calc a new message id // calc a new message id
conn_info_t *inf = info_of(h); conn_info_t *inf = info_of(h);
auto acc = inf->acc(); auto acc = inf->acc();
if (acc == nullptr) { if (acc == nullptr) {
ipc::error("fail: send, info_of(h)->acc() == nullptr\n"); log.error("fail: send, info_of(h)->acc() == nullptr");
return false; return false;
} }
auto msg_id = acc->fetch_add(1, std::memory_order_relaxed); auto msg_id = acc->fetch_add(1, std::memory_order_relaxed);
@ -558,7 +566,7 @@ static bool send(F&& gen_push, ipc::handle_t h, void const * data, std::size_t s
static_cast<std::int32_t>(ipc::data_length), &(dat.first), 0); static_cast<std::int32_t>(ipc::data_length), &(dat.first), 0);
} }
// try using message fragment // try using message fragment
//ipc::log("fail: shm::handle for big message. msg_id: %zd, size: %zd\n", msg_id, size); //log.debug("fail: shm::handle for big message. msg_id: ", msg_id, ", size: ", size);
} }
// push message fragment // push message fragment
std::int32_t offset = 0; std::int32_t offset = 0;
@ -581,14 +589,15 @@ static bool send(F&& gen_push, ipc::handle_t h, void const * data, std::size_t s
} }
static bool send(ipc::handle_t h, void const * data, std::size_t size, std::uint64_t tm) { static bool send(ipc::handle_t h, void const * data, std::size_t size, std::uint64_t tm) {
return send([tm](auto *info, auto *que, auto msg_id) { LIBIPC_LOG();
return [tm, info, que, msg_id](std::int32_t remain, void const * data, std::size_t size) { return send([tm, &log](auto *info, auto *que, auto msg_id) {
return [tm, &log, info, que, msg_id](std::int32_t remain, void const * data, std::size_t size) {
if (!wait_for(info->wt_waiter_, [&] { if (!wait_for(info->wt_waiter_, [&] {
return !que->push( return !que->push(
[](void*) { return true; }, [](void*) { return true; },
info->cc_id_, msg_id, remain, data, size); info->cc_id_, msg_id, remain, data, size);
}, tm)) { }, tm)) {
ipc::log("force_push: msg_id = %zd, remain = %d, size = %zd\n", msg_id, remain, size); log.debug("force_push: msg_id = ", msg_id, ", remain = ", remain, ", size = ", size);
if (!que->force_push( if (!que->force_push(
[info](void* p) { return clear_message<typename queue_t::value_t>(info, p); }, [info](void* p) { return clear_message<typename queue_t::value_t>(info, p); },
info->cc_id_, msg_id, remain, data, size)) { info->cc_id_, msg_id, remain, data, size)) {
@ -618,9 +627,10 @@ static bool try_send(ipc::handle_t h, void const * data, std::size_t size, std::
} }
static ipc::buff_t recv(ipc::handle_t h, std::uint64_t tm) { static ipc::buff_t recv(ipc::handle_t h, std::uint64_t tm) {
LIBIPC_LOG();
auto que = queue_of(h); auto que = queue_of(h);
if (que == nullptr) { if (que == nullptr) {
ipc::error("fail: recv, queue_of(h) == nullptr\n"); log.error("fail: recv, queue_of(h) == nullptr");
return {}; return {};
} }
if (!que->connected()) { if (!que->connected()) {
@ -648,7 +658,7 @@ static ipc::buff_t recv(ipc::handle_t h, std::uint64_t tm) {
// msg.remain_ may minus & abs(msg.remain_) < data_length // msg.remain_ may minus & abs(msg.remain_) < data_length
std::int32_t r_size = static_cast<std::int32_t>(ipc::data_length) + msg.remain_; std::int32_t r_size = static_cast<std::int32_t>(ipc::data_length) + msg.remain_;
if (r_size <= 0) { if (r_size <= 0) {
ipc::error("fail: recv, r_size = %d\n", (int)r_size); log.error("fail: recv, r_size = ", (int)r_size);
return {}; return {};
} }
std::size_t msg_size = static_cast<std::size_t>(r_size); std::size_t msg_size = static_cast<std::size_t>(r_size);
@ -669,7 +679,7 @@ static ipc::buff_t recv(ipc::handle_t h, std::uint64_t tm) {
que->connected_id() que->connected_id()
}); });
if (r_info == nullptr) { if (r_info == nullptr) {
ipc::log("fail: ipc::mem::$new<recycle_t>.\n"); log.error("fail: ipc::mem::$new<recycle_t>.");
return ipc::buff_t{buf, msg_size}; // no recycle return ipc::buff_t{buf, msg_size}; // no recycle
} else { } else {
return ipc::buff_t{buf, msg_size, [](void* p_info, std::size_t size) { return ipc::buff_t{buf, msg_size, [](void* p_info, std::size_t size) {
@ -685,7 +695,7 @@ static ipc::buff_t recv(ipc::handle_t h, std::uint64_t tm) {
}, r_info}; }, r_info};
} }
} else { } else {
ipc::log("fail: shm::handle for large message. msg_id: %zd, buf_id: %zd, size: %zd\n", msg.id_, buf_id, msg_size); log.error("fail: shm::handle for large message. msg_id: ", msg.id_, ", buf_id: ", buf_id, ", size: ", msg_size);
continue; continue;
} }
} }

View File

@ -1,6 +1,6 @@
#pragma once #pragma once
#include "libipc/utility/log.h" #include "libipc/imp/log.h"
#include "libipc/mutex.h" #include "libipc/mutex.h"
#include "get_wait_time.h" #include "get_wait_time.h"
@ -19,11 +19,12 @@ public:
~condition() = default; ~condition() = default;
bool wait(ipc::sync::mutex &mtx, std::uint64_t tm) noexcept { bool wait(ipc::sync::mutex &mtx, std::uint64_t tm) noexcept {
LIBIPC_LOG();
if (!valid()) return false; if (!valid()) return false;
if (tm == invalid_value) { if (tm == invalid_value) {
int eno = A0_SYSERR(a0_cnd_wait(native(), static_cast<a0_mtx_t *>(mtx.native()))); int eno = A0_SYSERR(a0_cnd_wait(native(), static_cast<a0_mtx_t *>(mtx.native())));
if (eno != 0) { if (eno != 0) {
ipc::error("fail condition wait[%d]\n", eno); log.error("fail condition wait[", eno, "]");
return false; return false;
} }
} else { } else {
@ -31,8 +32,7 @@ public:
int eno = A0_SYSERR(a0_cnd_timedwait(native(), static_cast<a0_mtx_t *>(mtx.native()), {ts})); int eno = A0_SYSERR(a0_cnd_timedwait(native(), static_cast<a0_mtx_t *>(mtx.native()), {ts}));
if (eno != 0) { if (eno != 0) {
if (eno != ETIMEDOUT) { if (eno != ETIMEDOUT) {
ipc::error("fail condition timedwait[%d]: tm = %zd, tv_sec = %ld, tv_nsec = %ld\n", log.error("fail condition timedwait[", eno, "]: tm = ", tm, ", tv_sec = ", ts.tv_sec, ", tv_nsec = ", ts.tv_nsec);
eno, tm, ts.tv_sec, ts.tv_nsec);
} }
return false; return false;
} }
@ -41,20 +41,22 @@ public:
} }
bool notify(ipc::sync::mutex &mtx) noexcept { bool notify(ipc::sync::mutex &mtx) noexcept {
LIBIPC_LOG();
if (!valid()) return false; if (!valid()) return false;
int eno = A0_SYSERR(a0_cnd_signal(native(), static_cast<a0_mtx_t *>(mtx.native()))); int eno = A0_SYSERR(a0_cnd_signal(native(), static_cast<a0_mtx_t *>(mtx.native())));
if (eno != 0) { if (eno != 0) {
ipc::error("fail condition notify[%d]\n", eno); log.error("fail condition notify[", eno, "]");
return false; return false;
} }
return true; return true;
} }
bool broadcast(ipc::sync::mutex &mtx) noexcept { bool broadcast(ipc::sync::mutex &mtx) noexcept {
LIBIPC_LOG();
if (!valid()) return false; if (!valid()) return false;
int eno = A0_SYSERR(a0_cnd_broadcast(native(), static_cast<a0_mtx_t *>(mtx.native()))); int eno = A0_SYSERR(a0_cnd_broadcast(native(), static_cast<a0_mtx_t *>(mtx.native())));
if (eno != 0) { if (eno != 0) {
ipc::error("fail condition broadcast[%d]\n", eno); log.error("fail condition broadcast[", eno, "]");
return false; return false;
} }
return true; return true;

View File

@ -4,7 +4,7 @@
#include <cinttypes> #include <cinttypes>
#include <system_error> #include <system_error>
#include "libipc/utility/log.h" #include "libipc/imp/log.h"
#include "a0/time.h" #include "a0/time.h"
#include "a0/err_macro.h" #include "a0/err_macro.h"
@ -14,30 +14,31 @@ namespace linux_ {
namespace detail { namespace detail {
inline bool calc_wait_time(timespec &ts, std::uint64_t tm /*ms*/) noexcept { inline bool calc_wait_time(timespec &ts, std::uint64_t tm /*ms*/) noexcept {
LIBIPC_LOG();
std::int64_t add_ns = static_cast<std::int64_t>(tm * 1000000ull); std::int64_t add_ns = static_cast<std::int64_t>(tm * 1000000ull);
if (add_ns < 0) { if (add_ns < 0) {
ipc::error("invalid time = " PRIu64 "\n", tm); log.error("invalid time = ", tm);
return false; return false;
} }
a0_time_mono_t now; a0_time_mono_t now;
int eno = A0_SYSERR(a0_time_mono_now(&now)); int eno = A0_SYSERR(a0_time_mono_now(&now));
if (eno != 0) { if (eno != 0) {
ipc::error("fail get time[%d]\n", eno); log.error("fail get time[", eno, "]");
return false; return false;
} }
a0_time_mono_t *target = reinterpret_cast<a0_time_mono_t *>(&ts); a0_time_mono_t *target = reinterpret_cast<a0_time_mono_t *>(&ts);
if ((eno = A0_SYSERR(a0_time_mono_add(now, add_ns, target))) != 0) { if ((eno = A0_SYSERR(a0_time_mono_add(now, add_ns, target))) != 0) {
ipc::error("fail get time[%d]\n", eno); log.error("fail get time[", eno, "]");
return false; return false;
} }
return true; return true;
} }
inline timespec make_timespec(std::uint64_t tm /*ms*/) noexcept(false) { inline timespec make_timespec(std::uint64_t tm /*ms*/) noexcept(false) {
LIBIPC_LOG();
timespec ts {}; timespec ts {};
if (!calc_wait_time(ts, tm)) { if (!calc_wait_time(ts, tm)) {
ipc::error("fail calc_wait_time: tm = %zd, tv_sec = %ld, tv_nsec = %ld\n", log.error("fail calc_wait_time: tm = ", tm, ", tv_sec = ", ts.tv_sec, ", tv_nsec = ", ts.tv_nsec);
tm, ts.tv_sec, ts.tv_nsec);
throw std::system_error{static_cast<int>(errno), std::system_category()}; throw std::system_error{static_cast<int>(errno), std::system_category()};
} }
return ts; return ts;

View File

@ -6,7 +6,7 @@
#include <atomic> #include <atomic>
#include "libipc/platform/detail.h" #include "libipc/platform/detail.h"
#include "libipc/utility/log.h" #include "libipc/imp/log.h"
#include "libipc/mem/resource.h" #include "libipc/mem/resource.h"
#include "libipc/shm.h" #include "libipc/shm.h"
@ -23,6 +23,7 @@ namespace sync {
class robust_mutex : public sync::obj_impl<a0_mtx_t> { class robust_mutex : public sync::obj_impl<a0_mtx_t> {
public: public:
bool lock(std::uint64_t tm) noexcept { bool lock(std::uint64_t tm) noexcept {
LIBIPC_LOG();
if (!valid()) return false; if (!valid()) return false;
for (;;) { for (;;) {
auto ts = linux_::detail::make_timespec(tm); auto ts = linux_::detail::make_timespec(tm);
@ -37,24 +38,25 @@ public:
case EOWNERDEAD: { case EOWNERDEAD: {
int eno2 = A0_SYSERR(a0_mtx_consistent(native())); int eno2 = A0_SYSERR(a0_mtx_consistent(native()));
if (eno2 != 0) { if (eno2 != 0) {
ipc::error("fail mutex lock[%d] -> consistent[%d]\n", eno, eno2); log.error("fail mutex lock[", eno, "] -> consistent[", eno2, "]");
return false; return false;
} }
int eno3 = A0_SYSERR(a0_mtx_unlock(native())); int eno3 = A0_SYSERR(a0_mtx_unlock(native()));
if (eno3 != 0) { if (eno3 != 0) {
ipc::error("fail mutex lock[%d] -> unlock[%d]\n", eno, eno3); log.error("fail mutex lock[", eno, "] -> unlock[", eno3, "]");
return false; return false;
} }
} }
break; // loop again break; // loop again
default: default:
ipc::error("fail mutex lock[%d]\n", eno); log.error("fail mutex lock[", eno, "]");
return false; return false;
} }
} }
} }
bool try_lock() noexcept(false) { bool try_lock() noexcept(false) {
LIBIPC_LOG();
if (!valid()) return false; if (!valid()) return false;
int eno = A0_SYSERR(a0_mtx_timedlock(native(), {linux_::detail::make_timespec(0)})); int eno = A0_SYSERR(a0_mtx_timedlock(native(), {linux_::detail::make_timespec(0)}));
switch (eno) { switch (eno) {
@ -65,28 +67,29 @@ public:
case EOWNERDEAD: { case EOWNERDEAD: {
int eno2 = A0_SYSERR(a0_mtx_consistent(native())); int eno2 = A0_SYSERR(a0_mtx_consistent(native()));
if (eno2 != 0) { if (eno2 != 0) {
ipc::error("fail mutex try_lock[%d] -> consistent[%d]\n", eno, eno2); log.error("fail mutex try_lock[", eno, "] -> consistent[", eno2, "]");
break; break;
} }
int eno3 = A0_SYSERR(a0_mtx_unlock(native())); int eno3 = A0_SYSERR(a0_mtx_unlock(native()));
if (eno3 != 0) { if (eno3 != 0) {
ipc::error("fail mutex try_lock[%d] -> unlock[%d]\n", eno, eno3); log.error("fail mutex try_lock[", eno, "] -> unlock[", eno3, "]");
break; break;
} }
} }
break; break;
default: default:
ipc::error("fail mutex try_lock[%d]\n", eno); log.error("fail mutex try_lock[", eno, "]");
break; break;
} }
throw std::system_error{eno, std::system_category()}; throw std::system_error{eno, std::system_category()};
} }
bool unlock() noexcept { bool unlock() noexcept {
LIBIPC_LOG();
if (!valid()) return false; if (!valid()) return false;
int eno = A0_SYSERR(a0_mtx_unlock(native())); int eno = A0_SYSERR(a0_mtx_unlock(native()));
if (eno != 0) { if (eno != 0) {
ipc::error("fail mutex unlock[%d]\n", eno); log.error("fail mutex unlock[", eno, "]");
return false; return false;
} }
return true; return true;

View File

@ -1,6 +1,6 @@
#pragma once #pragma once
#include "libipc/utility/log.h" #include "libipc/imp/log.h"
#include "libipc/shm.h" #include "libipc/shm.h"
#include "a0/empty.h" #include "a0/empty.h"
@ -19,8 +19,9 @@ protected:
sync_t *h_ = nullptr; sync_t *h_ = nullptr;
sync_t *acquire_handle(char const *name) { sync_t *acquire_handle(char const *name) {
LIBIPC_LOG();
if (!shm_.acquire(name, sizeof(sync_t))) { if (!shm_.acquire(name, sizeof(sync_t))) {
ipc::error("[acquire_handle] fail shm.acquire: %s\n", name); log.error("[acquire_handle] fail shm.acquire: ", name);
return nullptr; return nullptr;
} }
return static_cast<sync_t *>(shm_.get()); return static_cast<sync_t *>(shm_.get());

View File

@ -5,7 +5,7 @@
#include <pthread.h> #include <pthread.h>
#include "libipc/utility/log.h" #include "libipc/imp/log.h"
#include "libipc/utility/scope_guard.h" #include "libipc/utility/scope_guard.h"
#include "libipc/mutex.h" #include "libipc/mutex.h"
#include "libipc/shm.h" #include "libipc/shm.h"
@ -21,8 +21,9 @@ class condition {
pthread_cond_t *cond_ = nullptr; pthread_cond_t *cond_ = nullptr;
pthread_cond_t *acquire_cond(char const *name) { pthread_cond_t *acquire_cond(char const *name) {
LIBIPC_LOG();
if (!shm_.acquire(name, sizeof(pthread_cond_t))) { if (!shm_.acquire(name, sizeof(pthread_cond_t))) {
ipc::error("[acquire_cond] fail shm.acquire: %s\n", name); log.error("[acquire_cond] fail shm.acquire: ", name);
return nullptr; return nullptr;
} }
return static_cast<pthread_cond_t *>(shm_.get()); return static_cast<pthread_cond_t *>(shm_.get());
@ -47,6 +48,7 @@ public:
} }
bool open(char const *name) noexcept { bool open(char const *name) noexcept {
LIBIPC_LOG();
close(); close();
if ((cond_ = acquire_cond(name)) == nullptr) { if ((cond_ = acquire_cond(name)) == nullptr) {
return false; return false;
@ -60,17 +62,17 @@ public:
int eno; int eno;
pthread_condattr_t cond_attr; pthread_condattr_t cond_attr;
if ((eno = ::pthread_condattr_init(&cond_attr)) != 0) { if ((eno = ::pthread_condattr_init(&cond_attr)) != 0) {
ipc::error("fail pthread_condattr_init[%d]\n", eno); log.error("fail pthread_condattr_init[", eno, "]");
return false; return false;
} }
LIBIPC_UNUSED auto guard_cond_attr = guard([&cond_attr] { ::pthread_condattr_destroy(&cond_attr); }); LIBIPC_UNUSED auto guard_cond_attr = guard([&cond_attr] { ::pthread_condattr_destroy(&cond_attr); });
if ((eno = ::pthread_condattr_setpshared(&cond_attr, PTHREAD_PROCESS_SHARED)) != 0) { if ((eno = ::pthread_condattr_setpshared(&cond_attr, PTHREAD_PROCESS_SHARED)) != 0) {
ipc::error("fail pthread_condattr_setpshared[%d]\n", eno); log.error("fail pthread_condattr_setpshared[", eno, "]");
return false; return false;
} }
*cond_ = PTHREAD_COND_INITIALIZER; *cond_ = PTHREAD_COND_INITIALIZER;
if ((eno = ::pthread_cond_init(cond_, &cond_attr)) != 0) { if ((eno = ::pthread_cond_init(cond_, &cond_attr)) != 0) {
ipc::error("fail pthread_cond_init[%d]\n", eno); log.error("fail pthread_cond_init[", eno, "]");
return false; return false;
} }
finally.dismiss(); finally.dismiss();
@ -78,10 +80,11 @@ public:
} }
void close() noexcept { void close() noexcept {
LIBIPC_LOG();
if ((shm_.ref() <= 1) && cond_ != nullptr) { if ((shm_.ref() <= 1) && cond_ != nullptr) {
int eno; int eno;
if ((eno = ::pthread_cond_destroy(cond_)) != 0) { if ((eno = ::pthread_cond_destroy(cond_)) != 0) {
ipc::error("fail pthread_cond_destroy[%d]\n", eno); log.error("fail pthread_cond_destroy[", eno, "]");
} }
} }
shm_.release(); shm_.release();
@ -92,7 +95,7 @@ public:
if ((shm_.ref() <= 1) && cond_ != nullptr) { if ((shm_.ref() <= 1) && cond_ != nullptr) {
int eno; int eno;
if ((eno = ::pthread_cond_destroy(cond_)) != 0) { if ((eno = ::pthread_cond_destroy(cond_)) != 0) {
ipc::error("fail pthread_cond_destroy[%d]\n", eno); log.error("fail pthread_cond_destroy[", eno, "]");
} }
} }
shm_.clear(); // Make sure the storage is cleaned up. shm_.clear(); // Make sure the storage is cleaned up.
@ -104,12 +107,13 @@ public:
} }
bool wait(ipc::sync::mutex &mtx, std::uint64_t tm) noexcept { bool wait(ipc::sync::mutex &mtx, std::uint64_t tm) noexcept {
LIBIPC_LOG();
if (!valid()) return false; if (!valid()) return false;
switch (tm) { switch (tm) {
case invalid_value: { case invalid_value: {
int eno; int eno;
if ((eno = ::pthread_cond_wait(cond_, static_cast<pthread_mutex_t *>(mtx.native()))) != 0) { if ((eno = ::pthread_cond_wait(cond_, static_cast<pthread_mutex_t *>(mtx.native()))) != 0) {
ipc::error("fail pthread_cond_wait[%d]\n", eno); log.error("fail pthread_cond_wait[", eno, "]");
return false; return false;
} }
} }
@ -119,8 +123,7 @@ public:
int eno; int eno;
if ((eno = ::pthread_cond_timedwait(cond_, static_cast<pthread_mutex_t *>(mtx.native()), &ts)) != 0) { if ((eno = ::pthread_cond_timedwait(cond_, static_cast<pthread_mutex_t *>(mtx.native()), &ts)) != 0) {
if (eno != ETIMEDOUT) { if (eno != ETIMEDOUT) {
ipc::error("fail pthread_cond_timedwait[%d]: tm = %zd, tv_sec = %ld, tv_nsec = %ld\n", log.error("fail pthread_cond_timedwait[", eno, "]: tm = ", tm, ", tv_sec = ", ts.tv_sec, ", tv_nsec = ", ts.tv_nsec);
eno, tm, ts.tv_sec, ts.tv_nsec);
} }
return false; return false;
} }
@ -134,7 +137,7 @@ public:
if (!valid()) return false; if (!valid()) return false;
int eno; int eno;
if ((eno = ::pthread_cond_signal(cond_)) != 0) { if ((eno = ::pthread_cond_signal(cond_)) != 0) {
ipc::error("fail pthread_cond_signal[%d]\n", eno); log.error("fail pthread_cond_signal[", eno, "]");
return false; return false;
} }
return true; return true;
@ -144,7 +147,7 @@ public:
if (!valid()) return false; if (!valid()) return false;
int eno; int eno;
if ((eno = ::pthread_cond_broadcast(cond_)) != 0) { if ((eno = ::pthread_cond_broadcast(cond_)) != 0) {
ipc::error("fail pthread_cond_broadcast[%d]\n", eno); log.error("fail pthread_cond_broadcast[", eno, "]");
return false; return false;
} }
return true; return true;

View File

@ -7,17 +7,18 @@
#include <time.h> #include <time.h>
#include <errno.h> #include <errno.h>
#include "libipc/utility/log.h" #include "libipc/imp/log.h"
namespace ipc { namespace ipc {
namespace posix_ { namespace posix_ {
namespace detail { namespace detail {
inline bool calc_wait_time(timespec &ts, std::uint64_t tm /*ms*/) noexcept { inline bool calc_wait_time(timespec &ts, std::uint64_t tm /*ms*/) noexcept {
LIBIPC_LOG();
timeval now; timeval now;
int eno = ::gettimeofday(&now, NULL); int eno = ::gettimeofday(&now, NULL);
if (eno != 0) { if (eno != 0) {
ipc::error("fail gettimeofday [%d]\n", eno); log.error("fail gettimeofday [", eno, "]");
return false; return false;
} }
ts.tv_nsec = (now.tv_usec + (tm % 1000) * 1000) * 1000; ts.tv_nsec = (now.tv_usec + (tm % 1000) * 1000) * 1000;
@ -27,10 +28,10 @@ inline bool calc_wait_time(timespec &ts, std::uint64_t tm /*ms*/) noexcept {
} }
inline timespec make_timespec(std::uint64_t tm /*ms*/) noexcept(false) { inline timespec make_timespec(std::uint64_t tm /*ms*/) noexcept(false) {
LIBIPC_LOG();
timespec ts {}; timespec ts {};
if (!calc_wait_time(ts, tm)) { if (!calc_wait_time(ts, tm)) {
ipc::error("fail calc_wait_time: tm = %zd, tv_sec = %ld, tv_nsec = %ld\n", log.error("fail calc_wait_time: tm = ", tm, ", tv_sec = ", ts.tv_sec, ", tv_nsec = ", ts.tv_nsec);
tm, ts.tv_sec, ts.tv_nsec);
throw std::system_error{static_cast<int>(errno), std::system_category()}; throw std::system_error{static_cast<int>(errno), std::system_category()};
} }
return ts; return ts;

View File

@ -10,7 +10,7 @@
#include <pthread.h> #include <pthread.h>
#include "libipc/platform/detail.h" #include "libipc/platform/detail.h"
#include "libipc/utility/log.h" #include "libipc/imp/log.h"
#include "libipc/utility/scope_guard.h" #include "libipc/utility/scope_guard.h"
#include "libipc/mem/resource.h" #include "libipc/mem/resource.h"
#include "libipc/shm.h" #include "libipc/shm.h"
@ -127,21 +127,21 @@ public:
int eno; int eno;
pthread_mutexattr_t mutex_attr; pthread_mutexattr_t mutex_attr;
if ((eno = ::pthread_mutexattr_init(&mutex_attr)) != 0) { if ((eno = ::pthread_mutexattr_init(&mutex_attr)) != 0) {
ipc::error("fail pthread_mutexattr_init[%d]\n", eno); log.error("fail pthread_mutexattr_init[", eno, "]");
return false; return false;
} }
LIBIPC_UNUSED auto guard_mutex_attr = guard([&mutex_attr] { ::pthread_mutexattr_destroy(&mutex_attr); }); LIBIPC_UNUSED auto guard_mutex_attr = guard([&mutex_attr] { ::pthread_mutexattr_destroy(&mutex_attr); });
if ((eno = ::pthread_mutexattr_setpshared(&mutex_attr, PTHREAD_PROCESS_SHARED)) != 0) { if ((eno = ::pthread_mutexattr_setpshared(&mutex_attr, PTHREAD_PROCESS_SHARED)) != 0) {
ipc::error("fail pthread_mutexattr_setpshared[%d]\n", eno); log.error("fail pthread_mutexattr_setpshared[", eno, "]");
return false; return false;
} }
if ((eno = ::pthread_mutexattr_setrobust(&mutex_attr, PTHREAD_MUTEX_ROBUST)) != 0) { if ((eno = ::pthread_mutexattr_setrobust(&mutex_attr, PTHREAD_MUTEX_ROBUST)) != 0) {
ipc::error("fail pthread_mutexattr_setrobust[%d]\n", eno); log.error("fail pthread_mutexattr_setrobust[", eno, "]");
return false; return false;
} }
*mutex_ = PTHREAD_MUTEX_INITIALIZER; *mutex_ = PTHREAD_MUTEX_INITIALIZER;
if ((eno = ::pthread_mutex_init(mutex_, &mutex_attr)) != 0) { if ((eno = ::pthread_mutex_init(mutex_, &mutex_attr)) != 0) {
ipc::error("fail pthread_mutex_init[%d]\n", eno); log.error("fail pthread_mutex_init[", eno, "]");
return false; return false;
} }
finally.dismiss(); finally.dismiss();
@ -149,6 +149,7 @@ public:
} }
void close() noexcept { void close() noexcept {
LIBIPC_LOG();
if ((ref_ != nullptr) && (shm_ != nullptr) && (mutex_ != nullptr)) { if ((ref_ != nullptr) && (shm_ != nullptr) && (mutex_ != nullptr)) {
if (shm_->name() != nullptr) { if (shm_->name() != nullptr) {
release_mutex(shm_->name(), [this] { release_mutex(shm_->name(), [this] {
@ -165,7 +166,7 @@ public:
int eno; int eno;
if ((eno = ::pthread_mutex_destroy(mutex_)) != 0) { if ((eno = ::pthread_mutex_destroy(mutex_)) != 0) {
ipc::error("fail pthread_mutex_destroy[%d]\n", eno); log.error("fail pthread_mutex_destroy[", eno, "]");
} }
return true; return true;
} }
@ -179,6 +180,7 @@ public:
} }
void clear() noexcept { void clear() noexcept {
LIBIPC_LOG();
if ((shm_ != nullptr) && (mutex_ != nullptr)) { if ((shm_ != nullptr) && (mutex_ != nullptr)) {
if (shm_->name() != nullptr) { if (shm_->name() != nullptr) {
release_mutex(shm_->name(), [this] { release_mutex(shm_->name(), [this] {
@ -187,7 +189,7 @@ public:
int eno; int eno;
if ((eno = ::pthread_mutex_destroy(mutex_)) != 0) { if ((eno = ::pthread_mutex_destroy(mutex_)) != 0) {
ipc::error("fail pthread_mutex_destroy[%d]\n", eno); log.error("fail pthread_mutex_destroy[", eno, "]");
} }
shm_->clear(); shm_->clear();
return true; return true;
@ -206,6 +208,7 @@ public:
} }
bool lock(std::uint64_t tm) noexcept { bool lock(std::uint64_t tm) noexcept {
LIBIPC_LOG();
if (!valid()) return false; if (!valid()) return false;
for (;;) { for (;;) {
auto ts = posix_::detail::make_timespec(tm); auto ts = posix_::detail::make_timespec(tm);
@ -222,7 +225,7 @@ public:
// but the previous owner died. We need to make it consistent. // but the previous owner died. We need to make it consistent.
int eno2 = ::pthread_mutex_consistent(mutex_); int eno2 = ::pthread_mutex_consistent(mutex_);
if (eno2 != 0) { if (eno2 != 0) {
ipc::error("fail pthread_mutex_lock[%d], pthread_mutex_consistent[%d]\n", eno, eno2); log.error("fail pthread_mutex_lock[", eno, "], pthread_mutex_consistent[", eno2, "]");
return false; return false;
} }
// After calling pthread_mutex_consistent(), the mutex is now in a // After calling pthread_mutex_consistent(), the mutex is now in a
@ -230,7 +233,7 @@ public:
return true; return true;
} }
default: default:
ipc::error("fail pthread_mutex_lock[%d]\n", eno); log.error("fail pthread_mutex_lock[", eno, "]");
return false; return false;
} }
} }
@ -250,7 +253,7 @@ public:
// but the previous owner died. We need to make it consistent. // but the previous owner died. We need to make it consistent.
int eno2 = ::pthread_mutex_consistent(mutex_); int eno2 = ::pthread_mutex_consistent(mutex_);
if (eno2 != 0) { if (eno2 != 0) {
ipc::error("fail pthread_mutex_timedlock[%d], pthread_mutex_consistent[%d]\n", eno, eno2); log.error("fail pthread_mutex_timedlock[", eno, "], pthread_mutex_consistent[", eno2, "]");
throw std::system_error{eno2, std::system_category()}; throw std::system_error{eno2, std::system_category()};
} }
// After calling pthread_mutex_consistent(), the mutex is now in a // After calling pthread_mutex_consistent(), the mutex is now in a
@ -258,17 +261,18 @@ public:
return true; return true;
} }
default: default:
ipc::error("fail pthread_mutex_timedlock[%d]\n", eno); log.error("fail pthread_mutex_timedlock[", eno, "]");
break; break;
} }
throw std::system_error{eno, std::system_category()}; throw std::system_error{eno, std::system_category()};
} }
bool unlock() noexcept { bool unlock() noexcept {
LIBIPC_LOG();
if (!valid()) return false; if (!valid()) return false;
int eno; int eno;
if ((eno = ::pthread_mutex_unlock(mutex_)) != 0) { if ((eno = ::pthread_mutex_unlock(mutex_)) != 0) {
ipc::error("fail pthread_mutex_unlock[%d]\n", eno); log.error("fail pthread_mutex_unlock[", eno, "]");
return false; return false;
} }
return true; return true;

View File

@ -7,7 +7,7 @@
#include <semaphore.h> #include <semaphore.h>
#include <errno.h> #include <errno.h>
#include "libipc/utility/log.h" #include "libipc/imp/log.h"
#include "libipc/shm.h" #include "libipc/shm.h"
#include "get_wait_time.h" #include "get_wait_time.h"
@ -34,9 +34,10 @@ public:
} }
bool open(char const *name, std::uint32_t count) noexcept { bool open(char const *name, std::uint32_t count) noexcept {
LIBIPC_LOG();
close(); close();
if (!shm_.acquire(name, 1)) { if (!shm_.acquire(name, 1)) {
ipc::error("[open_semaphore] fail shm.acquire: %s\n", name); log.error("[open_semaphore] fail shm.acquire: ", name);
return false; return false;
} }
// POSIX semaphore names must start with "/" on some platforms (e.g., FreeBSD) // POSIX semaphore names must start with "/" on some platforms (e.g., FreeBSD)
@ -48,22 +49,23 @@ public:
} }
h_ = ::sem_open(sem_name_.c_str(), O_CREAT, 0666, static_cast<unsigned>(count)); h_ = ::sem_open(sem_name_.c_str(), O_CREAT, 0666, static_cast<unsigned>(count));
if (h_ == SEM_FAILED) { if (h_ == SEM_FAILED) {
ipc::error("fail sem_open[%d]: %s\n", errno, sem_name_.c_str()); log.error("fail sem_open[", errno, "]: ", sem_name_);
return false; return false;
} }
return true; return true;
} }
void close() noexcept { void close() noexcept {
LIBIPC_LOG();
if (!valid()) return; if (!valid()) return;
if (::sem_close(h_) != 0) { if (::sem_close(h_) != 0) {
ipc::error("fail sem_close[%d]: %s\n", errno); log.error("fail sem_close[", errno, "]");
} }
h_ = SEM_FAILED; h_ = SEM_FAILED;
if (!sem_name_.empty() && shm_.name() != nullptr) { if (!sem_name_.empty() && shm_.name() != nullptr) {
if (shm_.release() <= 1) { if (shm_.release() <= 1) {
if (::sem_unlink(sem_name_.c_str()) != 0) { if (::sem_unlink(sem_name_.c_str()) != 0) {
ipc::error("fail sem_unlink[%d]: %s, name: %s\n", errno, sem_name_.c_str()); log.error("fail sem_unlink[", errno, "]: ", sem_name_);
} }
} }
} }
@ -71,9 +73,10 @@ public:
} }
void clear() noexcept { void clear() noexcept {
LIBIPC_LOG();
if (valid()) { if (valid()) {
if (::sem_close(h_) != 0) { if (::sem_close(h_) != 0) {
ipc::error("fail sem_close[%d]: %s\n", errno); log.error("fail sem_close[", errno, "]");
} }
h_ = SEM_FAILED; h_ = SEM_FAILED;
} }
@ -97,18 +100,18 @@ public:
} }
bool wait(std::uint64_t tm) noexcept { bool wait(std::uint64_t tm) noexcept {
LIBIPC_LOG();
if (!valid()) return false; if (!valid()) return false;
if (tm == invalid_value) { if (tm == invalid_value) {
if (::sem_wait(h_) != 0) { if (::sem_wait(h_) != 0) {
ipc::error("fail sem_wait[%d]: %s\n", errno); log.error("fail sem_wait[", errno, "]");
return false; return false;
} }
} else { } else {
auto ts = posix_::detail::make_timespec(tm); auto ts = posix_::detail::make_timespec(tm);
if (::sem_timedwait(h_, &ts) != 0) { if (::sem_timedwait(h_, &ts) != 0) {
if (errno != ETIMEDOUT) { if (errno != ETIMEDOUT) {
ipc::error("fail sem_timedwait[%d]: tm = %zd, tv_sec = %ld, tv_nsec = %ld\n", log.error("fail sem_timedwait[", errno, "]: tm = ", tm, ", tv_sec = ", ts.tv_sec, ", tv_nsec = ", ts.tv_nsec);
errno, tm, ts.tv_sec, ts.tv_nsec);
} }
return false; return false;
} }
@ -117,10 +120,11 @@ public:
} }
bool post(std::uint32_t count) noexcept { bool post(std::uint32_t count) noexcept {
LIBIPC_LOG();
if (!valid()) return false; if (!valid()) return false;
for (std::uint32_t i = 0; i < count; ++i) { for (std::uint32_t i = 0; i < count; ++i) {
if (::sem_post(h_) != 0) { if (::sem_post(h_) != 0) {
ipc::error("fail sem_post[%d]: %s\n", errno); log.error("fail sem_post[", errno, "]");
return false; return false;
} }
} }

View File

@ -14,7 +14,7 @@
#include "libipc/shm.h" #include "libipc/shm.h"
#include "libipc/def.h" #include "libipc/def.h"
#include "libipc/utility/log.h" #include "libipc/imp/log.h"
#include "libipc/mem/resource.h" #include "libipc/mem/resource.h"
#include "libipc/mem/new.h" #include "libipc/mem/new.h"
@ -45,8 +45,9 @@ namespace ipc {
namespace shm { namespace shm {
id_t acquire(char const * name, std::size_t size, unsigned mode) { id_t acquire(char const * name, std::size_t size, unsigned mode) {
LIBIPC_LOG();
if (!is_valid_string(name)) { if (!is_valid_string(name)) {
ipc::error("fail acquire: name is empty\n"); log.error("fail acquire: name is empty");
return nullptr; return nullptr;
} }
// For portable use, a shared memory object should be identified by name of the form /somename. // For portable use, a shared memory object should be identified by name of the form /somename.
@ -79,7 +80,7 @@ id_t acquire(char const * name, std::size_t size, unsigned mode) {
if (fd == -1) { if (fd == -1) {
// only open shm not log error when file not exist // only open shm not log error when file not exist
if (open != mode || ENOENT != errno) { if (open != mode || ENOENT != errno) {
ipc::error("fail shm_open[%d]: %s\n", errno, op_name.c_str()); log.error("fail shm_open[", errno, "]: ", op_name);
} }
return nullptr; return nullptr;
} }
@ -105,21 +106,23 @@ std::int32_t get_ref(id_t id) {
} }
void sub_ref(id_t id) { void sub_ref(id_t id) {
LIBIPC_LOG();
if (id == nullptr) { if (id == nullptr) {
ipc::error("fail sub_ref: invalid id (null)\n"); log.error("fail sub_ref: invalid id (null)");
return; return;
} }
auto ii = static_cast<id_info_t*>(id); auto ii = static_cast<id_info_t*>(id);
if (ii->mem_ == nullptr || ii->size_ == 0) { if (ii->mem_ == nullptr || ii->size_ == 0) {
ipc::error("fail sub_ref: invalid id (mem = %p, size = %zd)\n", ii->mem_, ii->size_); log.error("fail sub_ref: invalid id (mem = ", ii->mem_, ", size = ", ii->size_, ")");
return; return;
} }
acc_of(ii->mem_, ii->size_).fetch_sub(1, std::memory_order_acq_rel); acc_of(ii->mem_, ii->size_).fetch_sub(1, std::memory_order_acq_rel);
} }
void * get_mem(id_t id, std::size_t * size) { void * get_mem(id_t id, std::size_t * size) {
LIBIPC_LOG();
if (id == nullptr) { if (id == nullptr) {
ipc::error("fail get_mem: invalid id (null)\n"); log.error("fail get_mem: invalid id (null)");
return nullptr; return nullptr;
} }
auto ii = static_cast<id_info_t*>(id); auto ii = static_cast<id_info_t*>(id);
@ -129,31 +132,31 @@ void * get_mem(id_t id, std::size_t * size) {
} }
int fd = ii->fd_; int fd = ii->fd_;
if (fd == -1) { if (fd == -1) {
ipc::error("fail get_mem: invalid id (fd = -1)\n"); log.error("fail get_mem: invalid id (fd = -1)");
return nullptr; return nullptr;
} }
if (ii->size_ == 0) { if (ii->size_ == 0) {
struct stat st; struct stat st;
if (::fstat(fd, &st) != 0) { if (::fstat(fd, &st) != 0) {
ipc::error("fail fstat[%d]: %s, size = %zd\n", errno, ii->name_.c_str(), ii->size_); log.error("fail fstat[", errno, "]: ", ii->name_, ", size = ", ii->size_);
return nullptr; return nullptr;
} }
ii->size_ = static_cast<std::size_t>(st.st_size); ii->size_ = static_cast<std::size_t>(st.st_size);
if ((ii->size_ <= sizeof(info_t)) || (ii->size_ % sizeof(info_t))) { if ((ii->size_ <= sizeof(info_t)) || (ii->size_ % sizeof(info_t))) {
ipc::error("fail get_mem: %s, invalid size = %zd\n", ii->name_.c_str(), ii->size_); log.error("fail get_mem: ", ii->name_, ", invalid size = ", ii->size_);
return nullptr; return nullptr;
} }
} }
else { else {
ii->size_ = calc_size(ii->size_); ii->size_ = calc_size(ii->size_);
if (::ftruncate(fd, static_cast<off_t>(ii->size_)) != 0) { if (::ftruncate(fd, static_cast<off_t>(ii->size_)) != 0) {
ipc::error("fail ftruncate[%d]: %s, size = %zd\n", errno, ii->name_.c_str(), ii->size_); log.error("fail ftruncate[", errno, "]: ", ii->name_, ", size = ", ii->size_);
return nullptr; return nullptr;
} }
} }
void* mem = ::mmap(nullptr, ii->size_, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0); void* mem = ::mmap(nullptr, ii->size_, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
if (mem == MAP_FAILED) { if (mem == MAP_FAILED) {
ipc::error("fail mmap[%d]: %s, size = %zd\n", errno, ii->name_.c_str(), ii->size_); log.error("fail mmap[", errno, "]: ", ii->name_, ", size = ", ii->size_);
return nullptr; return nullptr;
} }
::close(fd); ::close(fd);
@ -165,22 +168,22 @@ void * get_mem(id_t id, std::size_t * size) {
} }
std::int32_t release(id_t id) noexcept { std::int32_t release(id_t id) noexcept {
LIBIPC_LOG();
if (id == nullptr) { if (id == nullptr) {
ipc::error("fail release: invalid id (null)\n"); log.error("fail release: invalid id (null)");
return -1; return -1;
} }
std::int32_t ret = -1; std::int32_t ret = -1;
auto ii = static_cast<id_info_t*>(id); auto ii = static_cast<id_info_t*>(id);
if (ii->mem_ == nullptr || ii->size_ == 0) { if (ii->mem_ == nullptr || ii->size_ == 0) {
ipc::error("fail release: invalid id (mem = %p, size = %zd), name = %s\n", log.error("fail release: invalid id (mem = ", ii->mem_, ", size = ", ii->size_, "), name = ", ii->name_);
ii->mem_, ii->size_, ii->name_.c_str());
} }
else if ((ret = acc_of(ii->mem_, ii->size_).fetch_sub(1, std::memory_order_acq_rel)) <= 1) { else if ((ret = acc_of(ii->mem_, ii->size_).fetch_sub(1, std::memory_order_acq_rel)) <= 1) {
::munmap(ii->mem_, ii->size_); ::munmap(ii->mem_, ii->size_);
if (!ii->name_.empty()) { if (!ii->name_.empty()) {
int unlink_ret = ::shm_unlink(ii->name_.c_str()); int unlink_ret = ::shm_unlink(ii->name_.c_str());
if (unlink_ret == -1) { if (unlink_ret == -1) {
ipc::error("fail shm_unlink[%d]: %s\n", errno, ii->name_.c_str()); log.error("fail shm_unlink[", errno, "]: ", ii->name_);
} }
} }
} }
@ -190,8 +193,9 @@ std::int32_t release(id_t id) noexcept {
} }
void remove(id_t id) noexcept { void remove(id_t id) noexcept {
LIBIPC_LOG();
if (id == nullptr) { if (id == nullptr) {
ipc::error("fail remove: invalid id (null)\n"); log.error("fail remove: invalid id (null)");
return; return;
} }
auto ii = static_cast<id_info_t*>(id); auto ii = static_cast<id_info_t*>(id);
@ -200,14 +204,15 @@ void remove(id_t id) noexcept {
if (!name.empty()) { if (!name.empty()) {
int unlink_ret = ::shm_unlink(name.c_str()); int unlink_ret = ::shm_unlink(name.c_str());
if (unlink_ret == -1) { if (unlink_ret == -1) {
ipc::error("fail shm_unlink[%d]: %s\n", errno, name.c_str()); log.error("fail shm_unlink[", errno, "]: ", name);
} }
} }
} }
void remove(char const * name) noexcept { void remove(char const * name) noexcept {
LIBIPC_LOG();
if (!is_valid_string(name)) { if (!is_valid_string(name)) {
ipc::error("fail remove: name is empty\n"); log.error("fail remove: name is empty");
return; return;
} }
// For portable use, a shared memory object should be identified by name of the form /somename. // For portable use, a shared memory object should be identified by name of the form /somename.
@ -219,7 +224,7 @@ void remove(char const * name) noexcept {
} }
int unlink_ret = ::shm_unlink(op_name.c_str()); int unlink_ret = ::shm_unlink(op_name.c_str());
if (unlink_ret == -1) { if (unlink_ret == -1) {
ipc::error("fail shm_unlink[%d]: %s\n", errno, op_name.c_str()); log.error("fail shm_unlink[", errno, "]: ", op_name);
} }
} }

View File

@ -10,7 +10,7 @@
#include <Windows.h> #include <Windows.h>
#endif #endif
#include "libipc/utility/log.h" #include "libipc/imp/log.h"
#include "libipc/utility/scope_guard.h" #include "libipc/utility/scope_guard.h"
#include "libipc/platform/detail.h" #include "libipc/platform/detail.h"
#include "libipc/mutex.h" #include "libipc/mutex.h"

View File

@ -5,29 +5,31 @@
namespace ipc { namespace ipc {
namespace detail { namespace detail {
inline LPSECURITY_ATTRIBUTES get_sa() { struct sa_initiator {
static struct initiator { SECURITY_DESCRIPTOR sd_;
SECURITY_ATTRIBUTES sa_;
bool succ_ = false;
SECURITY_DESCRIPTOR sd_; template <typename Logger>
SECURITY_ATTRIBUTES sa_; sa_initiator(Logger const &log) {
if (!::InitializeSecurityDescriptor(&sd_, SECURITY_DESCRIPTOR_REVISION)) {
bool succ_ = false; log.error("fail InitializeSecurityDescriptor[", static_cast<int>(::GetLastError()), "]");
return;
initiator() {
if (!::InitializeSecurityDescriptor(&sd_, SECURITY_DESCRIPTOR_REVISION)) {
ipc::error("fail InitializeSecurityDescriptor[%d]\n", static_cast<int>(::GetLastError()));
return;
}
if (!::SetSecurityDescriptorDacl(&sd_, TRUE, NULL, FALSE)) {
ipc::error("fail SetSecurityDescriptorDacl[%d]\n", static_cast<int>(::GetLastError()));
return;
}
sa_.nLength = sizeof(SECURITY_ATTRIBUTES);
sa_.bInheritHandle = FALSE;
sa_.lpSecurityDescriptor = &sd_;
succ_ = true;
} }
} handle; if (!::SetSecurityDescriptorDacl(&sd_, TRUE, NULL, FALSE)) {
log.error("fail SetSecurityDescriptorDacl[", static_cast<int>(::GetLastError()), "]");
return;
}
sa_.nLength = sizeof(SECURITY_ATTRIBUTES);
sa_.bInheritHandle = FALSE;
sa_.lpSecurityDescriptor = &sd_;
succ_ = true;
}
};
inline LPSECURITY_ATTRIBUTES get_sa() {
LIBIPC_LOG();
static sa_initiator handle(log);
return handle.succ_ ? &handle.sa_ : nullptr; return handle.succ_ ? &handle.sa_ : nullptr;
} }

View File

@ -9,7 +9,7 @@
#include <Windows.h> #include <Windows.h>
#endif #endif
#include "libipc/utility/log.h" #include "libipc/imp/log.h"
#include "to_tchar.h" #include "to_tchar.h"
#include "get_sa.h" #include "get_sa.h"
@ -36,10 +36,11 @@ public:
} }
bool open(char const *name) noexcept { bool open(char const *name) noexcept {
LIBIPC_LOG();
close(); close();
h_ = ::CreateMutex(detail::get_sa(), FALSE, detail::to_tchar(name).c_str()); h_ = ::CreateMutex(detail::get_sa(), FALSE, detail::to_tchar(name).c_str());
if (h_ == NULL) { if (h_ == NULL) {
ipc::error("fail CreateMutex[%lu]: %s\n", ::GetLastError(), name); log.error("fail CreateMutex[", static_cast<unsigned long>(::GetLastError()), "]: ", name);
return false; return false;
} }
return true; return true;
@ -59,6 +60,7 @@ public:
} }
bool lock(std::uint64_t tm) noexcept { bool lock(std::uint64_t tm) noexcept {
LIBIPC_LOG();
DWORD ret, ms = (tm == invalid_value) ? INFINITE : static_cast<DWORD>(tm); DWORD ret, ms = (tm == invalid_value) ? INFINITE : static_cast<DWORD>(tm);
for(;;) { for(;;) {
switch ((ret = ::WaitForSingleObject(h_, ms))) { switch ((ret = ::WaitForSingleObject(h_, ms))) {
@ -67,19 +69,20 @@ public:
case WAIT_TIMEOUT: case WAIT_TIMEOUT:
return false; return false;
case WAIT_ABANDONED: case WAIT_ABANDONED:
ipc::log("fail WaitForSingleObject[%lu]: WAIT_ABANDONED, try again.\n", ::GetLastError()); log.warning("fail WaitForSingleObject[", ::GetLastError(), "]: WAIT_ABANDONED, try again.");
if (!unlock()) { if (!unlock()) {
return false; return false;
} }
break; // loop again break; // loop again
default: default:
ipc::error("fail WaitForSingleObject[%lu]: 0x%08X\n", ::GetLastError(), ret); log.error("fail WaitForSingleObject[", ::GetLastError(), "]: 0x", std::hex, ret, std::dec);
return false; return false;
} }
} }
} }
bool try_lock() noexcept(false) { bool try_lock() noexcept(false) {
LIBIPC_LOG();
DWORD ret = ::WaitForSingleObject(h_, 0); DWORD ret = ::WaitForSingleObject(h_, 0);
switch (ret) { switch (ret) {
case WAIT_OBJECT_0: case WAIT_OBJECT_0:
@ -90,14 +93,15 @@ public:
unlock(); unlock();
LIBIPC_FALLTHROUGH; LIBIPC_FALLTHROUGH;
default: default:
ipc::error("fail WaitForSingleObject[%lu]: 0x%08X\n", ::GetLastError(), ret); log.error("fail WaitForSingleObject[", ::GetLastError(), "]: 0x", std::hex, ret, std::dec);
throw std::system_error{static_cast<int>(ret), std::system_category()}; throw std::system_error{static_cast<int>(ret), std::system_category()};
} }
} }
bool unlock() noexcept { bool unlock() noexcept {
LIBIPC_LOG();
if (!::ReleaseMutex(h_)) { if (!::ReleaseMutex(h_)) {
ipc::error("fail ReleaseMutex[%lu]\n", ::GetLastError()); log.error("fail ReleaseMutex[", ::GetLastError(), "]");
return false; return false;
} }
return true; return true;

View File

@ -8,7 +8,7 @@
#include <Windows.h> #include <Windows.h>
#endif #endif
#include "libipc/utility/log.h" #include "libipc/imp/log.h"
#include "to_tchar.h" #include "to_tchar.h"
#include "get_sa.h" #include "get_sa.h"
@ -33,12 +33,13 @@ public:
} }
bool open(char const *name, std::uint32_t count) noexcept { bool open(char const *name, std::uint32_t count) noexcept {
LIBIPC_LOG();
close(); close();
h_ = ::CreateSemaphore(detail::get_sa(), h_ = ::CreateSemaphore(detail::get_sa(),
static_cast<LONG>(count), LONG_MAX, static_cast<LONG>(count), LONG_MAX,
detail::to_tchar(name).c_str()); detail::to_tchar(name).c_str());
if (h_ == NULL) { if (h_ == NULL) {
ipc::error("fail CreateSemaphore[%lu]: %s\n", ::GetLastError(), name); log.error("fail CreateSemaphore[", static_cast<unsigned long>(::GetLastError()), "]: ", name);
return false; return false;
} }
return true; return true;
@ -58,6 +59,7 @@ public:
} }
bool wait(std::uint64_t tm) noexcept { bool wait(std::uint64_t tm) noexcept {
LIBIPC_LOG();
DWORD ret, ms = (tm == invalid_value) ? INFINITE : static_cast<DWORD>(tm); DWORD ret, ms = (tm == invalid_value) ? INFINITE : static_cast<DWORD>(tm);
switch ((ret = ::WaitForSingleObject(h_, ms))) { switch ((ret = ::WaitForSingleObject(h_, ms))) {
case WAIT_OBJECT_0: case WAIT_OBJECT_0:
@ -66,14 +68,15 @@ public:
return false; return false;
case WAIT_ABANDONED: case WAIT_ABANDONED:
default: default:
ipc::error("fail WaitForSingleObject[%lu]: 0x%08X\n", ::GetLastError(), ret); log.error("fail WaitForSingleObject[", ::GetLastError(), "]: 0x", std::hex, ret, std::dec);
return false; return false;
} }
} }
bool post(std::uint32_t count) noexcept { bool post(std::uint32_t count) noexcept {
LIBIPC_LOG();
if (!::ReleaseSemaphore(h_, static_cast<LONG>(count), NULL)) { if (!::ReleaseSemaphore(h_, static_cast<LONG>(count), NULL)) {
ipc::error("fail ReleaseSemaphore[%lu]\n", ::GetLastError()); log.error("fail ReleaseSemaphore[", ::GetLastError(), "]");
return false; return false;
} }
return true; return true;

View File

@ -12,7 +12,7 @@
#include "libipc/shm.h" #include "libipc/shm.h"
#include "libipc/def.h" #include "libipc/def.h"
#include "libipc/utility/log.h" #include "libipc/imp/log.h"
#include "libipc/mem/resource.h" #include "libipc/mem/resource.h"
#include "libipc/mem/new.h" #include "libipc/mem/new.h"
@ -45,8 +45,9 @@ namespace ipc {
namespace shm { namespace shm {
id_t acquire(char const * name, std::size_t size, unsigned mode) { id_t acquire(char const * name, std::size_t size, unsigned mode) {
LIBIPC_LOG();
if (!is_valid_string(name)) { if (!is_valid_string(name)) {
ipc::error("fail acquire: name is empty\n"); log.error("fail acquire: name is empty");
return nullptr; return nullptr;
} }
HANDLE h; HANDLE h;
@ -55,7 +56,7 @@ id_t acquire(char const * name, std::size_t size, unsigned mode) {
if (mode == open) { if (mode == open) {
h = ::OpenFileMapping(FILE_MAP_ALL_ACCESS, FALSE, fmt_name.c_str()); h = ::OpenFileMapping(FILE_MAP_ALL_ACCESS, FALSE, fmt_name.c_str());
if (h == NULL) { if (h == NULL) {
ipc::error("fail OpenFileMapping[%d]: %s\n", static_cast<int>(::GetLastError()), name); log.error("fail OpenFileMapping[", static_cast<int>(::GetLastError()), "]: ", name);
return nullptr; return nullptr;
} }
} }
@ -72,7 +73,7 @@ id_t acquire(char const * name, std::size_t size, unsigned mode) {
h = NULL; h = NULL;
} }
if (h == NULL) { if (h == NULL) {
ipc::error("fail CreateFileMapping[%d]: %s\n", static_cast<int>(err), name); log.error("fail CreateFileMapping[", static_cast<int>(err), "]: ", name);
return nullptr; return nullptr;
} }
} }
@ -94,21 +95,23 @@ std::int32_t get_ref(id_t id) {
} }
void sub_ref(id_t id) { void sub_ref(id_t id) {
LIBIPC_LOG();
if (id == nullptr) { if (id == nullptr) {
ipc::error("fail sub_ref: invalid id (null)\n"); log.error("fail sub_ref: invalid id (null)");
return; return;
} }
auto ii = static_cast<id_info_t*>(id); auto ii = static_cast<id_info_t*>(id);
if (ii->mem_ == nullptr || ii->size_ == 0) { if (ii->mem_ == nullptr || ii->size_ == 0) {
ipc::error("fail sub_ref: invalid id (mem = %p, size = %zd)\n", ii->mem_, ii->size_); log.error("fail sub_ref: invalid id (mem = ", ii->mem_, ", size = ", ii->size_, ")");
return; return;
} }
acc_of(ii->mem_, calc_size(ii->size_)).fetch_sub(1, std::memory_order_acq_rel); acc_of(ii->mem_, calc_size(ii->size_)).fetch_sub(1, std::memory_order_acq_rel);
} }
void * get_mem(id_t id, std::size_t * size) { void * get_mem(id_t id, std::size_t * size) {
LIBIPC_LOG();
if (id == nullptr) { if (id == nullptr) {
ipc::error("fail get_mem: invalid id (null)\n"); log.error("fail get_mem: invalid id (null)");
return nullptr; return nullptr;
} }
auto ii = static_cast<id_info_t*>(id); auto ii = static_cast<id_info_t*>(id);
@ -117,17 +120,17 @@ void * get_mem(id_t id, std::size_t * size) {
return ii->mem_; return ii->mem_;
} }
if (ii->h_ == NULL) { if (ii->h_ == NULL) {
ipc::error("fail to_mem: invalid id (h = null)\n"); log.error("fail to_mem: invalid id (h = null)");
return nullptr; return nullptr;
} }
LPVOID mem = ::MapViewOfFile(ii->h_, FILE_MAP_ALL_ACCESS, 0, 0, 0); LPVOID mem = ::MapViewOfFile(ii->h_, FILE_MAP_ALL_ACCESS, 0, 0, 0);
if (mem == NULL) { if (mem == NULL) {
ipc::error("fail MapViewOfFile[%d]\n", static_cast<int>(::GetLastError())); log.error("fail MapViewOfFile[", static_cast<int>(::GetLastError()), "]");
return nullptr; return nullptr;
} }
MEMORY_BASIC_INFORMATION mem_info; MEMORY_BASIC_INFORMATION mem_info;
if (::VirtualQuery(mem, &mem_info, sizeof(mem_info)) == 0) { if (::VirtualQuery(mem, &mem_info, sizeof(mem_info)) == 0) {
ipc::error("fail VirtualQuery[%d]\n", static_cast<int>(::GetLastError())); log.error("fail VirtualQuery[", static_cast<int>(::GetLastError()), "]");
return nullptr; return nullptr;
} }
std::size_t actual_size = static_cast<std::size_t>(mem_info.RegionSize); std::size_t actual_size = static_cast<std::size_t>(mem_info.RegionSize);
@ -144,21 +147,22 @@ void * get_mem(id_t id, std::size_t * size) {
} }
std::int32_t release(id_t id) noexcept { std::int32_t release(id_t id) noexcept {
LIBIPC_LOG();
if (id == nullptr) { if (id == nullptr) {
ipc::error("fail release: invalid id (null)\n"); log.error("fail release: invalid id (null)");
return -1; return -1;
} }
std::int32_t ret = -1; std::int32_t ret = -1;
auto ii = static_cast<id_info_t*>(id); auto ii = static_cast<id_info_t*>(id);
if (ii->mem_ == nullptr || ii->size_ == 0) { if (ii->mem_ == nullptr || ii->size_ == 0) {
ipc::error("fail release: invalid id (mem = %p, size = %zd)\n", ii->mem_, ii->size_); log.error("fail release: invalid id (mem = ", ii->mem_, ", size = ", ii->size_, ")");
} }
else { else {
ret = acc_of(ii->mem_, calc_size(ii->size_)).fetch_sub(1, std::memory_order_acq_rel); ret = acc_of(ii->mem_, calc_size(ii->size_)).fetch_sub(1, std::memory_order_acq_rel);
::UnmapViewOfFile(static_cast<LPCVOID>(ii->mem_)); ::UnmapViewOfFile(static_cast<LPCVOID>(ii->mem_));
} }
if (ii->h_ == NULL) { if (ii->h_ == NULL) {
ipc::error("fail release: invalid id (h = null)\n"); log.error("fail release: invalid id (h = null)");
} }
else ::CloseHandle(ii->h_); else ::CloseHandle(ii->h_);
mem::$delete(ii); mem::$delete(ii);
@ -166,16 +170,18 @@ std::int32_t release(id_t id) noexcept {
} }
void remove(id_t id) noexcept { void remove(id_t id) noexcept {
LIBIPC_LOG();
if (id == nullptr) { if (id == nullptr) {
ipc::error("fail release: invalid id (null)\n"); log.error("fail release: invalid id (null)");
return; return;
} }
release(id); release(id);
} }
void remove(char const * name) noexcept { void remove(char const * name) noexcept {
LIBIPC_LOG();
if (!is_valid_string(name)) { if (!is_valid_string(name)) {
ipc::error("fail remove: name is empty\n"); log.error("fail remove: name is empty");
return; return;
} }
// Do Nothing. // Do Nothing.

View File

@ -10,7 +10,7 @@
#include "libipc/platform/detail.h" #include "libipc/platform/detail.h"
#include "libipc/circ/elem_def.h" #include "libipc/circ/elem_def.h"
#include "libipc/utility/log.h" #include "libipc/imp/log.h"
#include "libipc/utility/utility.h" #include "libipc/utility/utility.h"
namespace ipc { namespace ipc {
@ -242,6 +242,7 @@ struct prod_cons_impl<wr<relat::single, relat::multi, trans::broadcast>> {
template <typename W, typename F, typename E> template <typename W, typename F, typename E>
bool force_push(W* wrapper, F&& f, E* elems) { bool force_push(W* wrapper, F&& f, E* elems) {
LIBIPC_LOG();
E* el; E* el;
epoch_ += ep_incr; epoch_ += ep_incr;
for (unsigned k = 0;;) { for (unsigned k = 0;;) {
@ -252,7 +253,7 @@ struct prod_cons_impl<wr<relat::single, relat::multi, trans::broadcast>> {
auto cur_rc = el->rc_.load(std::memory_order_acquire); auto cur_rc = el->rc_.load(std::memory_order_acquire);
circ::cc_t rem_cc = cur_rc & ep_mask; circ::cc_t rem_cc = cur_rc & ep_mask;
if (cc & rem_cc) { if (cc & rem_cc) {
ipc::log("force_push: k = %u, cc = %u, rem_cc = %u\n", k, cc, rem_cc); log.debug("force_push: k = ", k, ", cc = ", cc, ", rem_cc = ", rem_cc);
cc = wrapper->elems()->disconnect_receiver(rem_cc); // disconnect all invalid readers cc = wrapper->elems()->disconnect_receiver(rem_cc); // disconnect all invalid readers
if (cc == 0) return false; // no reader if (cc == 0) return false; // no reader
} }
@ -364,6 +365,7 @@ struct prod_cons_impl<wr<relat::multi, relat::multi, trans::broadcast>> {
template <typename W, typename F, typename E> template <typename W, typename F, typename E>
bool force_push(W* wrapper, F&& f, E* elems) { bool force_push(W* wrapper, F&& f, E* elems) {
LIBIPC_LOG();
E* el; E* el;
circ::u2_t cur_ct; circ::u2_t cur_ct;
rc_t epoch = epoch_.fetch_add(ep_incr, std::memory_order_release) + ep_incr; rc_t epoch = epoch_.fetch_add(ep_incr, std::memory_order_release) + ep_incr;
@ -375,7 +377,7 @@ struct prod_cons_impl<wr<relat::multi, relat::multi, trans::broadcast>> {
auto cur_rc = el->rc_.load(std::memory_order_acquire); auto cur_rc = el->rc_.load(std::memory_order_acquire);
circ::cc_t rem_cc = cur_rc & rc_mask; circ::cc_t rem_cc = cur_rc & rc_mask;
if (cc & rem_cc) { if (cc & rem_cc) {
ipc::log("force_push: k = %u, cc = %u, rem_cc = %u\n", k, cc, rem_cc); log.debug("force_push: k = ", k, ", cc = ", cc, ", rem_cc = ", rem_cc);
cc = wrapper->elems()->disconnect_receiver(rem_cc); // disconnect all invalid readers cc = wrapper->elems()->disconnect_receiver(rem_cc); // disconnect all invalid readers
if (cc == 0) return false; // no reader if (cc == 0) return false; // no reader
} }

View File

@ -15,7 +15,7 @@
#include "libipc/shm.h" #include "libipc/shm.h"
#include "libipc/rw_lock.h" #include "libipc/rw_lock.h"
#include "libipc/utility/log.h" #include "libipc/imp/log.h"
#include "libipc/platform/detail.h" #include "libipc/platform/detail.h"
#include "libipc/circ/elem_def.h" #include "libipc/circ/elem_def.h"
#include "libipc/mem/resource.h" #include "libipc/mem/resource.h"
@ -30,8 +30,9 @@ protected:
template <typename Elems> template <typename Elems>
Elems* open(char const * name) { Elems* open(char const * name) {
LIBIPC_LOG();
if (!is_valid_string(name)) { if (!is_valid_string(name)) {
ipc::error("fail open waiter: name is empty!\n"); log.error("fail open waiter: name is empty!");
return nullptr; return nullptr;
} }
if (!elems_h_.acquire(name, sizeof(Elems))) { if (!elems_h_.acquire(name, sizeof(Elems))) {
@ -39,7 +40,7 @@ protected:
} }
auto elems = static_cast<Elems*>(elems_h_.get()); auto elems = static_cast<Elems*>(elems_h_.get());
if (elems == nullptr) { if (elems == nullptr) {
ipc::error("fail acquire elems: %s\n", name); log.error("fail acquire elems: ", name);
return nullptr; return nullptr;
} }
elems->init(); elems->init();

View File

@ -5,7 +5,7 @@
#include "libipc/shm.h" #include "libipc/shm.h"
#include "libipc/utility/pimpl.h" #include "libipc/utility/pimpl.h"
#include "libipc/utility/log.h" #include "libipc/imp/log.h"
#include "libipc/mem/resource.h" #include "libipc/mem/resource.h"
namespace ipc { namespace ipc {
@ -69,12 +69,13 @@ void handle::sub_ref() noexcept {
} }
bool handle::acquire(char const * name, std::size_t size, unsigned mode) { bool handle::acquire(char const * name, std::size_t size, unsigned mode) {
LIBIPC_LOG();
if (!is_valid_string(name)) { if (!is_valid_string(name)) {
ipc::error("fail acquire: name is empty\n"); log.error("fail acquire: name is empty");
return false; return false;
} }
if (size == 0) { if (size == 0) {
ipc::error("fail acquire: size is 0\n"); log.error("fail acquire: size is 0");
return false; return false;
} }
release(); release();

View File

@ -2,7 +2,7 @@
#include "libipc/condition.h" #include "libipc/condition.h"
#include "libipc/utility/pimpl.h" #include "libipc/utility/pimpl.h"
#include "libipc/utility/log.h" #include "libipc/imp/log.h"
#include "libipc/mem/resource.h" #include "libipc/mem/resource.h"
#include "libipc/platform/detail.h" #include "libipc/platform/detail.h"
#if defined(LIBIPC_OS_WIN) #if defined(LIBIPC_OS_WIN)
@ -50,8 +50,9 @@ bool condition::valid() const noexcept {
} }
bool condition::open(char const *name) noexcept { bool condition::open(char const *name) noexcept {
LIBIPC_LOG();
if (!is_valid_string(name)) { if (!is_valid_string(name)) {
ipc::error("fail condition open: name is empty\n"); log.error("fail condition open: name is empty");
return false; return false;
} }
return impl(p_)->cond_.open(name); return impl(p_)->cond_.open(name);

View File

@ -2,7 +2,7 @@
#include "libipc/mutex.h" #include "libipc/mutex.h"
#include "libipc/utility/pimpl.h" #include "libipc/utility/pimpl.h"
#include "libipc/utility/log.h" #include "libipc/imp/log.h"
#include "libipc/mem/resource.h" #include "libipc/mem/resource.h"
#include "libipc/platform/detail.h" #include "libipc/platform/detail.h"
#if defined(LIBIPC_OS_WIN) #if defined(LIBIPC_OS_WIN)
@ -50,8 +50,9 @@ bool mutex::valid() const noexcept {
} }
bool mutex::open(char const *name) noexcept { bool mutex::open(char const *name) noexcept {
LIBIPC_LOG();
if (!is_valid_string(name)) { if (!is_valid_string(name)) {
ipc::error("fail mutex open: name is empty\n"); log.error("fail mutex open: name is empty");
return false; return false;
} }
return impl(p_)->lock_.open(name); return impl(p_)->lock_.open(name);

View File

@ -2,7 +2,7 @@
#include "libipc/semaphore.h" #include "libipc/semaphore.h"
#include "libipc/utility/pimpl.h" #include "libipc/utility/pimpl.h"
#include "libipc/utility/log.h" #include "libipc/imp/log.h"
#include "libipc/mem/resource.h" #include "libipc/mem/resource.h"
#include "libipc/platform/detail.h" #include "libipc/platform/detail.h"
#if defined(LIBIPC_OS_WIN) #if defined(LIBIPC_OS_WIN)
@ -49,8 +49,9 @@ bool semaphore::valid() const noexcept {
} }
bool semaphore::open(char const *name, std::uint32_t count) noexcept { bool semaphore::open(char const *name, std::uint32_t count) noexcept {
LIBIPC_LOG();
if (!is_valid_string(name)) { if (!is_valid_string(name)) {
ipc::error("fail semaphore open: name is empty\n"); log.error("fail semaphore open: name is empty");
return false; return false;
} }
return impl(p_)->sem_.open(name, count); return impl(p_)->sem_.open(name, count);