diff --git a/src/libipc/ipc.cpp b/src/libipc/ipc.cpp index f938a5d..4af11cc 100755 --- a/src/libipc/ipc.cpp +++ b/src/libipc/ipc.cpp @@ -19,7 +19,7 @@ #include "libipc/rw_lock.h" #include "libipc/waiter.h" -#include "libipc/utility/log.h" +#include "libipc/imp/log.h" #include "libipc/utility/id_pool.h" #include "libipc/utility/scope_guard.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) { + LIBIPC_LOG(); static auto *phs = new ipc::unordered_map; // no delete static std::mutex lock; std::lock_guard guard {lock}; @@ -84,7 +85,7 @@ acc_t *cc_acc(std::string const &pref) { std::string shm_name {ipc::make_prefix(pref, "CA_CONN__")}; ipc::shm::handle h; 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; } it = phs->emplace(pref, std::move(h)).first; @@ -217,10 +218,11 @@ auto& chunk_storages() { std::mutex lock_; static bool make_handle(ipc::shm::handle &h, std::string const &shm_name, std::size_t chunk_size) { + LIBIPC_LOG(); if (!h.valid() && !h.acquire( shm_name.c_str(), 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 true; @@ -228,6 +230,7 @@ auto& chunk_storages() { public: 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 shm_name {ipc::make_prefix(pref, "CHUNK_INFO__", chunk_size)}; ipc::shm::handle *h; @@ -240,7 +243,7 @@ auto& chunk_storages() { } auto *info = static_cast(h->get()); 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 info; @@ -290,8 +293,9 @@ std::pair acquire_storage(conn_info_head *inf, std::si } void *find_storage(ipc::storage_id_t id, conn_info_head *inf, std::size_t size) { + LIBIPC_LOG(); 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; } 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) { + LIBIPC_LOG(); 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; } std::size_t chunk_size = calc_chunk_size(size); @@ -334,8 +339,9 @@ bool sub_rc(ipc::wr, template 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) { - 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; } 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 bool clear_message(conn_info_head *inf, void* p) { + LIBIPC_LOG(); auto msg = static_cast(p); if (msg->storage_) { std::int32_t r_size = static_cast(ipc::data_length) + msg->remain_; 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; } release_storage(*reinterpret_cast(&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 static bool send(F&& gen_push, ipc::handle_t h, void const * data, std::size_t size) { + LIBIPC_LOG(); if (data == nullptr || size == 0) { - ipc::error("fail: send(%p, %zd)\n", data, size); + log.error("fail: send(", data, ", ", size, ")"); return false; } auto que = queue_of(h); if (que == nullptr) { - ipc::error("fail: send, queue_of(h) == nullptr\n"); + log.error("fail: send, queue_of(h) == nullptr"); return false; } 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; } if (!que->ready_sending()) { - ipc::error("fail: send, que->ready_sending() == false\n"); + log.error("fail: send, que->ready_sending() == false"); return false; } ipc::circ::cc_t conns = que->elems()->connections(std::memory_order_relaxed); 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; } // calc a new message id conn_info_t *inf = info_of(h); auto acc = inf->acc(); if (acc == nullptr) { - ipc::error("fail: send, info_of(h)->acc() == nullptr\n"); + log.error("fail: send, info_of(h)->acc() == nullptr"); return false; } 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(ipc::data_length), &(dat.first), 0); } // 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 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) { - return send([tm](auto *info, auto *que, auto msg_id) { - return [tm, info, que, msg_id](std::int32_t remain, void const * data, std::size_t size) { + LIBIPC_LOG(); + 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_, [&] { return !que->push( [](void*) { return true; }, info->cc_id_, msg_id, remain, data, size); }, 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( [info](void* p) { return clear_message(info, p); }, 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) { + LIBIPC_LOG(); auto que = queue_of(h); if (que == nullptr) { - ipc::error("fail: recv, queue_of(h) == nullptr\n"); + log.error("fail: recv, queue_of(h) == nullptr"); return {}; } 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 std::int32_t r_size = static_cast(ipc::data_length) + msg.remain_; 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 {}; } std::size_t msg_size = static_cast(r_size); @@ -669,7 +679,7 @@ static ipc::buff_t recv(ipc::handle_t h, std::uint64_t tm) { que->connected_id() }); if (r_info == nullptr) { - ipc::log("fail: ipc::mem::$new.\n"); + log.error("fail: ipc::mem::$new."); return ipc::buff_t{buf, msg_size}; // no recycle } else { 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}; } } 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; } } diff --git a/src/libipc/platform/linux/condition.h b/src/libipc/platform/linux/condition.h index b13920f..88b280b 100644 --- a/src/libipc/platform/linux/condition.h +++ b/src/libipc/platform/linux/condition.h @@ -1,6 +1,6 @@ #pragma once -#include "libipc/utility/log.h" +#include "libipc/imp/log.h" #include "libipc/mutex.h" #include "get_wait_time.h" @@ -19,11 +19,12 @@ class condition : public sync::obj_impl { ~condition() = default; bool wait(ipc::sync::mutex &mtx, std::uint64_t tm) noexcept { + LIBIPC_LOG(); if (!valid()) return false; if (tm == invalid_value) { int eno = A0_SYSERR(a0_cnd_wait(native(), static_cast(mtx.native()))); if (eno != 0) { - ipc::error("fail condition wait[%d]\n", eno); + log.error("fail condition wait[", eno, "]"); return false; } } else { @@ -31,8 +32,7 @@ class condition : public sync::obj_impl { int eno = A0_SYSERR(a0_cnd_timedwait(native(), static_cast(mtx.native()), {ts})); if (eno != 0) { if (eno != ETIMEDOUT) { - ipc::error("fail condition timedwait[%d]: tm = %zd, tv_sec = %ld, tv_nsec = %ld\n", - eno, tm, ts.tv_sec, ts.tv_nsec); + log.error("fail condition timedwait[", eno, "]: tm = ", tm, ", tv_sec = ", ts.tv_sec, ", tv_nsec = ", ts.tv_nsec); } return false; } @@ -41,20 +41,22 @@ class condition : public sync::obj_impl { } bool notify(ipc::sync::mutex &mtx) noexcept { + LIBIPC_LOG(); if (!valid()) return false; int eno = A0_SYSERR(a0_cnd_signal(native(), static_cast(mtx.native()))); if (eno != 0) { - ipc::error("fail condition notify[%d]\n", eno); + log.error("fail condition notify[", eno, "]"); return false; } return true; } bool broadcast(ipc::sync::mutex &mtx) noexcept { + LIBIPC_LOG(); if (!valid()) return false; int eno = A0_SYSERR(a0_cnd_broadcast(native(), static_cast(mtx.native()))); if (eno != 0) { - ipc::error("fail condition broadcast[%d]\n", eno); + log.error("fail condition broadcast[", eno, "]"); return false; } return true; diff --git a/src/libipc/platform/linux/get_wait_time.h b/src/libipc/platform/linux/get_wait_time.h index 3600d8d..8af32f7 100644 --- a/src/libipc/platform/linux/get_wait_time.h +++ b/src/libipc/platform/linux/get_wait_time.h @@ -4,7 +4,7 @@ #include #include -#include "libipc/utility/log.h" +#include "libipc/imp/log.h" #include "a0/time.h" #include "a0/err_macro.h" @@ -14,30 +14,31 @@ namespace linux_ { namespace detail { inline bool calc_wait_time(timespec &ts, std::uint64_t tm /*ms*/) noexcept { + LIBIPC_LOG(); std::int64_t add_ns = static_cast(tm * 1000000ull); if (add_ns < 0) { - ipc::error("invalid time = " PRIu64 "\n", tm); + log.error("invalid time = ", tm); return false; } a0_time_mono_t now; int eno = A0_SYSERR(a0_time_mono_now(&now)); if (eno != 0) { - ipc::error("fail get time[%d]\n", eno); + log.error("fail get time[", eno, "]"); return false; } a0_time_mono_t *target = reinterpret_cast(&ts); 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 true; } inline timespec make_timespec(std::uint64_t tm /*ms*/) noexcept(false) { + LIBIPC_LOG(); timespec ts {}; if (!calc_wait_time(ts, tm)) { - ipc::error("fail calc_wait_time: tm = %zd, tv_sec = %ld, tv_nsec = %ld\n", - tm, ts.tv_sec, ts.tv_nsec); + log.error("fail calc_wait_time: tm = ", tm, ", tv_sec = ", ts.tv_sec, ", tv_nsec = ", ts.tv_nsec); throw std::system_error{static_cast(errno), std::system_category()}; } return ts; diff --git a/src/libipc/platform/linux/mutex.h b/src/libipc/platform/linux/mutex.h index 021e006..296530b 100644 --- a/src/libipc/platform/linux/mutex.h +++ b/src/libipc/platform/linux/mutex.h @@ -6,7 +6,7 @@ #include #include "libipc/platform/detail.h" -#include "libipc/utility/log.h" +#include "libipc/imp/log.h" #include "libipc/mem/resource.h" #include "libipc/shm.h" @@ -23,6 +23,7 @@ namespace sync { class robust_mutex : public sync::obj_impl { public: bool lock(std::uint64_t tm) noexcept { + LIBIPC_LOG(); if (!valid()) return false; for (;;) { auto ts = linux_::detail::make_timespec(tm); @@ -37,24 +38,25 @@ class robust_mutex : public sync::obj_impl { case EOWNERDEAD: { int eno2 = A0_SYSERR(a0_mtx_consistent(native())); if (eno2 != 0) { - ipc::error("fail mutex lock[%d] -> consistent[%d]\n", eno, eno2); + log.error("fail mutex lock[", eno, "] -> consistent[", eno2, "]"); return false; } int eno3 = A0_SYSERR(a0_mtx_unlock(native())); if (eno3 != 0) { - ipc::error("fail mutex lock[%d] -> unlock[%d]\n", eno, eno3); + log.error("fail mutex lock[", eno, "] -> unlock[", eno3, "]"); return false; } } break; // loop again default: - ipc::error("fail mutex lock[%d]\n", eno); + log.error("fail mutex lock[", eno, "]"); return false; } } } bool try_lock() noexcept(false) { + LIBIPC_LOG(); if (!valid()) return false; int eno = A0_SYSERR(a0_mtx_timedlock(native(), {linux_::detail::make_timespec(0)})); switch (eno) { @@ -65,28 +67,29 @@ class robust_mutex : public sync::obj_impl { case EOWNERDEAD: { int eno2 = A0_SYSERR(a0_mtx_consistent(native())); 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; } int eno3 = A0_SYSERR(a0_mtx_unlock(native())); 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; default: - ipc::error("fail mutex try_lock[%d]\n", eno); + log.error("fail mutex try_lock[", eno, "]"); break; } throw std::system_error{eno, std::system_category()}; } bool unlock() noexcept { + LIBIPC_LOG(); if (!valid()) return false; int eno = A0_SYSERR(a0_mtx_unlock(native())); if (eno != 0) { - ipc::error("fail mutex unlock[%d]\n", eno); + log.error("fail mutex unlock[", eno, "]"); return false; } return true; diff --git a/src/libipc/platform/linux/sync_obj_impl.h b/src/libipc/platform/linux/sync_obj_impl.h index f7cb018..4a94c14 100644 --- a/src/libipc/platform/linux/sync_obj_impl.h +++ b/src/libipc/platform/linux/sync_obj_impl.h @@ -1,6 +1,6 @@ #pragma once -#include "libipc/utility/log.h" +#include "libipc/imp/log.h" #include "libipc/shm.h" #include "a0/empty.h" @@ -19,8 +19,9 @@ class obj_impl { sync_t *h_ = nullptr; sync_t *acquire_handle(char const *name) { + LIBIPC_LOG(); 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 static_cast(shm_.get()); diff --git a/src/libipc/platform/posix/condition.h b/src/libipc/platform/posix/condition.h index 2b543a2..ea66a96 100644 --- a/src/libipc/platform/posix/condition.h +++ b/src/libipc/platform/posix/condition.h @@ -5,7 +5,7 @@ #include -#include "libipc/utility/log.h" +#include "libipc/imp/log.h" #include "libipc/utility/scope_guard.h" #include "libipc/mutex.h" #include "libipc/shm.h" @@ -21,8 +21,9 @@ class condition { pthread_cond_t *cond_ = nullptr; pthread_cond_t *acquire_cond(char const *name) { + LIBIPC_LOG(); 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 static_cast(shm_.get()); @@ -47,6 +48,7 @@ class condition { } bool open(char const *name) noexcept { + LIBIPC_LOG(); close(); if ((cond_ = acquire_cond(name)) == nullptr) { return false; @@ -60,17 +62,17 @@ class condition { int eno; pthread_condattr_t cond_attr; 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; } 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) { - ipc::error("fail pthread_condattr_setpshared[%d]\n", eno); + log.error("fail pthread_condattr_setpshared[", eno, "]"); return false; } *cond_ = PTHREAD_COND_INITIALIZER; 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; } finally.dismiss(); @@ -78,10 +80,11 @@ class condition { } void close() noexcept { + LIBIPC_LOG(); if ((shm_.ref() <= 1) && cond_ != nullptr) { int eno; 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(); @@ -92,7 +95,7 @@ class condition { if ((shm_.ref() <= 1) && cond_ != nullptr) { int eno; 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. @@ -104,12 +107,13 @@ class condition { } bool wait(ipc::sync::mutex &mtx, std::uint64_t tm) noexcept { + LIBIPC_LOG(); if (!valid()) return false; switch (tm) { case invalid_value: { int eno; if ((eno = ::pthread_cond_wait(cond_, static_cast(mtx.native()))) != 0) { - ipc::error("fail pthread_cond_wait[%d]\n", eno); + log.error("fail pthread_cond_wait[", eno, "]"); return false; } } @@ -119,8 +123,7 @@ class condition { int eno; if ((eno = ::pthread_cond_timedwait(cond_, static_cast(mtx.native()), &ts)) != 0) { if (eno != ETIMEDOUT) { - ipc::error("fail pthread_cond_timedwait[%d]: tm = %zd, tv_sec = %ld, tv_nsec = %ld\n", - eno, tm, ts.tv_sec, ts.tv_nsec); + log.error("fail pthread_cond_timedwait[", eno, "]: tm = ", tm, ", tv_sec = ", ts.tv_sec, ", tv_nsec = ", ts.tv_nsec); } return false; } @@ -134,7 +137,7 @@ class condition { if (!valid()) return false; int eno; 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 true; @@ -144,7 +147,7 @@ class condition { if (!valid()) return false; int eno; 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 true; diff --git a/src/libipc/platform/posix/get_wait_time.h b/src/libipc/platform/posix/get_wait_time.h index 44faf7d..889804a 100644 --- a/src/libipc/platform/posix/get_wait_time.h +++ b/src/libipc/platform/posix/get_wait_time.h @@ -7,17 +7,18 @@ #include #include -#include "libipc/utility/log.h" +#include "libipc/imp/log.h" namespace ipc { namespace posix_ { namespace detail { inline bool calc_wait_time(timespec &ts, std::uint64_t tm /*ms*/) noexcept { + LIBIPC_LOG(); timeval now; int eno = ::gettimeofday(&now, NULL); if (eno != 0) { - ipc::error("fail gettimeofday [%d]\n", eno); + log.error("fail gettimeofday [", eno, "]"); return false; } 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) { + LIBIPC_LOG(); timespec ts {}; if (!calc_wait_time(ts, tm)) { - ipc::error("fail calc_wait_time: tm = %zd, tv_sec = %ld, tv_nsec = %ld\n", - tm, ts.tv_sec, ts.tv_nsec); + log.error("fail calc_wait_time: tm = ", tm, ", tv_sec = ", ts.tv_sec, ", tv_nsec = ", ts.tv_nsec); throw std::system_error{static_cast(errno), std::system_category()}; } return ts; diff --git a/src/libipc/platform/posix/mutex.h b/src/libipc/platform/posix/mutex.h index 050dfc6..16f2647 100644 --- a/src/libipc/platform/posix/mutex.h +++ b/src/libipc/platform/posix/mutex.h @@ -10,7 +10,7 @@ #include #include "libipc/platform/detail.h" -#include "libipc/utility/log.h" +#include "libipc/imp/log.h" #include "libipc/utility/scope_guard.h" #include "libipc/mem/resource.h" #include "libipc/shm.h" @@ -127,21 +127,21 @@ class mutex { int eno; pthread_mutexattr_t mutex_attr; 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; } 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) { - ipc::error("fail pthread_mutexattr_setpshared[%d]\n", eno); + log.error("fail pthread_mutexattr_setpshared[", eno, "]"); return false; } 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; } *mutex_ = PTHREAD_MUTEX_INITIALIZER; 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; } finally.dismiss(); @@ -149,6 +149,7 @@ class mutex { } void close() noexcept { + LIBIPC_LOG(); if ((ref_ != nullptr) && (shm_ != nullptr) && (mutex_ != nullptr)) { if (shm_->name() != nullptr) { release_mutex(shm_->name(), [this] { @@ -165,7 +166,7 @@ class mutex { int eno; 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; } @@ -179,6 +180,7 @@ class mutex { } void clear() noexcept { + LIBIPC_LOG(); if ((shm_ != nullptr) && (mutex_ != nullptr)) { if (shm_->name() != nullptr) { release_mutex(shm_->name(), [this] { @@ -187,7 +189,7 @@ class mutex { int eno; 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(); return true; @@ -206,6 +208,7 @@ class mutex { } bool lock(std::uint64_t tm) noexcept { + LIBIPC_LOG(); if (!valid()) return false; for (;;) { auto ts = posix_::detail::make_timespec(tm); @@ -222,7 +225,7 @@ class mutex { // but the previous owner died. We need to make it consistent. int eno2 = ::pthread_mutex_consistent(mutex_); 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; } // After calling pthread_mutex_consistent(), the mutex is now in a @@ -230,7 +233,7 @@ class mutex { return true; } default: - ipc::error("fail pthread_mutex_lock[%d]\n", eno); + log.error("fail pthread_mutex_lock[", eno, "]"); return false; } } @@ -250,7 +253,7 @@ class mutex { // but the previous owner died. We need to make it consistent. int eno2 = ::pthread_mutex_consistent(mutex_); 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()}; } // After calling pthread_mutex_consistent(), the mutex is now in a @@ -258,17 +261,18 @@ class mutex { return true; } default: - ipc::error("fail pthread_mutex_timedlock[%d]\n", eno); + log.error("fail pthread_mutex_timedlock[", eno, "]"); break; } throw std::system_error{eno, std::system_category()}; } bool unlock() noexcept { + LIBIPC_LOG(); if (!valid()) return false; int eno; 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 true; diff --git a/src/libipc/platform/posix/semaphore_impl.h b/src/libipc/platform/posix/semaphore_impl.h index 8d5a1e8..d9d4213 100644 --- a/src/libipc/platform/posix/semaphore_impl.h +++ b/src/libipc/platform/posix/semaphore_impl.h @@ -7,7 +7,7 @@ #include #include -#include "libipc/utility/log.h" +#include "libipc/imp/log.h" #include "libipc/shm.h" #include "get_wait_time.h" @@ -34,9 +34,10 @@ class semaphore { } bool open(char const *name, std::uint32_t count) noexcept { + LIBIPC_LOG(); close(); 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; } // POSIX semaphore names must start with "/" on some platforms (e.g., FreeBSD) @@ -48,22 +49,23 @@ class semaphore { } h_ = ::sem_open(sem_name_.c_str(), O_CREAT, 0666, static_cast(count)); 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 true; } void close() noexcept { + LIBIPC_LOG(); if (!valid()) return; if (::sem_close(h_) != 0) { - ipc::error("fail sem_close[%d]: %s\n", errno); + log.error("fail sem_close[", errno, "]"); } h_ = SEM_FAILED; if (!sem_name_.empty() && shm_.name() != nullptr) { if (shm_.release() <= 1) { 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 @@ class semaphore { } void clear() noexcept { + LIBIPC_LOG(); if (valid()) { if (::sem_close(h_) != 0) { - ipc::error("fail sem_close[%d]: %s\n", errno); + log.error("fail sem_close[", errno, "]"); } h_ = SEM_FAILED; } @@ -97,18 +100,18 @@ class semaphore { } bool wait(std::uint64_t tm) noexcept { + LIBIPC_LOG(); if (!valid()) return false; if (tm == invalid_value) { if (::sem_wait(h_) != 0) { - ipc::error("fail sem_wait[%d]: %s\n", errno); + log.error("fail sem_wait[", errno, "]"); return false; } } else { auto ts = posix_::detail::make_timespec(tm); if (::sem_timedwait(h_, &ts) != 0) { if (errno != ETIMEDOUT) { - ipc::error("fail sem_timedwait[%d]: tm = %zd, tv_sec = %ld, tv_nsec = %ld\n", - errno, tm, ts.tv_sec, ts.tv_nsec); + log.error("fail sem_timedwait[", errno, "]: tm = ", tm, ", tv_sec = ", ts.tv_sec, ", tv_nsec = ", ts.tv_nsec); } return false; } @@ -117,10 +120,11 @@ class semaphore { } bool post(std::uint32_t count) noexcept { + LIBIPC_LOG(); if (!valid()) return false; for (std::uint32_t i = 0; i < count; ++i) { if (::sem_post(h_) != 0) { - ipc::error("fail sem_post[%d]: %s\n", errno); + log.error("fail sem_post[", errno, "]"); return false; } } diff --git a/src/libipc/platform/posix/shm_posix.cpp b/src/libipc/platform/posix/shm_posix.cpp index bd03bf2..6a7713c 100644 --- a/src/libipc/platform/posix/shm_posix.cpp +++ b/src/libipc/platform/posix/shm_posix.cpp @@ -14,7 +14,7 @@ #include "libipc/shm.h" #include "libipc/def.h" -#include "libipc/utility/log.h" +#include "libipc/imp/log.h" #include "libipc/mem/resource.h" #include "libipc/mem/new.h" @@ -45,8 +45,9 @@ namespace ipc { namespace shm { id_t acquire(char const * name, std::size_t size, unsigned mode) { + LIBIPC_LOG(); if (!is_valid_string(name)) { - ipc::error("fail acquire: name is empty\n"); + log.error("fail acquire: name is empty"); return nullptr; } // 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) { // only open shm not log error when file not exist 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; } @@ -105,21 +106,23 @@ std::int32_t get_ref(id_t id) { } void sub_ref(id_t id) { + LIBIPC_LOG(); if (id == nullptr) { - ipc::error("fail sub_ref: invalid id (null)\n"); + log.error("fail sub_ref: invalid id (null)"); return; } auto ii = static_cast(id); 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; } acc_of(ii->mem_, ii->size_).fetch_sub(1, std::memory_order_acq_rel); } void * get_mem(id_t id, std::size_t * size) { + LIBIPC_LOG(); if (id == nullptr) { - ipc::error("fail get_mem: invalid id (null)\n"); + log.error("fail get_mem: invalid id (null)"); return nullptr; } auto ii = static_cast(id); @@ -129,31 +132,31 @@ void * get_mem(id_t id, std::size_t * size) { } int fd = ii->fd_; if (fd == -1) { - ipc::error("fail get_mem: invalid id (fd = -1)\n"); + log.error("fail get_mem: invalid id (fd = -1)"); return nullptr; } if (ii->size_ == 0) { struct stat st; 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; } ii->size_ = static_cast(st.st_size); 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; } } else { ii->size_ = calc_size(ii->size_); if (::ftruncate(fd, static_cast(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; } } void* mem = ::mmap(nullptr, ii->size_, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0); 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; } ::close(fd); @@ -165,22 +168,22 @@ void * get_mem(id_t id, std::size_t * size) { } std::int32_t release(id_t id) noexcept { + LIBIPC_LOG(); if (id == nullptr) { - ipc::error("fail release: invalid id (null)\n"); + log.error("fail release: invalid id (null)"); return -1; } std::int32_t ret = -1; auto ii = static_cast(id); if (ii->mem_ == nullptr || ii->size_ == 0) { - ipc::error("fail release: invalid id (mem = %p, size = %zd), name = %s\n", - ii->mem_, ii->size_, ii->name_.c_str()); + log.error("fail release: invalid id (mem = ", ii->mem_, ", size = ", ii->size_, "), name = ", ii->name_); } else if ((ret = acc_of(ii->mem_, ii->size_).fetch_sub(1, std::memory_order_acq_rel)) <= 1) { ::munmap(ii->mem_, ii->size_); if (!ii->name_.empty()) { int unlink_ret = ::shm_unlink(ii->name_.c_str()); 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 { + LIBIPC_LOG(); if (id == nullptr) { - ipc::error("fail remove: invalid id (null)\n"); + log.error("fail remove: invalid id (null)"); return; } auto ii = static_cast(id); @@ -200,14 +204,15 @@ void remove(id_t id) noexcept { if (!name.empty()) { int unlink_ret = ::shm_unlink(name.c_str()); 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 { + LIBIPC_LOG(); if (!is_valid_string(name)) { - ipc::error("fail remove: name is empty\n"); + log.error("fail remove: name is empty"); return; } // 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()); 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); } } diff --git a/src/libipc/platform/win/condition.h b/src/libipc/platform/win/condition.h index 1bc3851..5b2da27 100644 --- a/src/libipc/platform/win/condition.h +++ b/src/libipc/platform/win/condition.h @@ -10,7 +10,7 @@ #include #endif -#include "libipc/utility/log.h" +#include "libipc/imp/log.h" #include "libipc/utility/scope_guard.h" #include "libipc/platform/detail.h" #include "libipc/mutex.h" diff --git a/src/libipc/platform/win/get_sa.h b/src/libipc/platform/win/get_sa.h index 74b68d4..a81eed2 100644 --- a/src/libipc/platform/win/get_sa.h +++ b/src/libipc/platform/win/get_sa.h @@ -5,29 +5,31 @@ namespace ipc { namespace detail { -inline LPSECURITY_ATTRIBUTES get_sa() { - static struct initiator { - - SECURITY_DESCRIPTOR sd_; - SECURITY_ATTRIBUTES sa_; - - bool succ_ = false; +struct sa_initiator { + SECURITY_DESCRIPTOR sd_; + SECURITY_ATTRIBUTES sa_; + bool succ_ = false; - initiator() { - if (!::InitializeSecurityDescriptor(&sd_, SECURITY_DESCRIPTOR_REVISION)) { - ipc::error("fail InitializeSecurityDescriptor[%d]\n", static_cast(::GetLastError())); - return; - } - if (!::SetSecurityDescriptorDacl(&sd_, TRUE, NULL, FALSE)) { - ipc::error("fail SetSecurityDescriptorDacl[%d]\n", static_cast(::GetLastError())); - return; - } - sa_.nLength = sizeof(SECURITY_ATTRIBUTES); - sa_.bInheritHandle = FALSE; - sa_.lpSecurityDescriptor = &sd_; - succ_ = true; + template + sa_initiator(Logger const &log) { + if (!::InitializeSecurityDescriptor(&sd_, SECURITY_DESCRIPTOR_REVISION)) { + log.error("fail InitializeSecurityDescriptor[", static_cast(::GetLastError()), "]"); + return; } - } handle; + if (!::SetSecurityDescriptorDacl(&sd_, TRUE, NULL, FALSE)) { + log.error("fail SetSecurityDescriptorDacl[", static_cast(::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; } diff --git a/src/libipc/platform/win/mutex.h b/src/libipc/platform/win/mutex.h index e80c96b..ae0e243 100644 --- a/src/libipc/platform/win/mutex.h +++ b/src/libipc/platform/win/mutex.h @@ -9,7 +9,7 @@ #include #endif -#include "libipc/utility/log.h" +#include "libipc/imp/log.h" #include "to_tchar.h" #include "get_sa.h" @@ -36,10 +36,11 @@ class mutex { } bool open(char const *name) noexcept { + LIBIPC_LOG(); close(); h_ = ::CreateMutex(detail::get_sa(), FALSE, detail::to_tchar(name).c_str()); if (h_ == NULL) { - ipc::error("fail CreateMutex[%lu]: %s\n", ::GetLastError(), name); + log.error("fail CreateMutex[", static_cast(::GetLastError()), "]: ", name); return false; } return true; @@ -59,6 +60,7 @@ class mutex { } bool lock(std::uint64_t tm) noexcept { + LIBIPC_LOG(); DWORD ret, ms = (tm == invalid_value) ? INFINITE : static_cast(tm); for(;;) { switch ((ret = ::WaitForSingleObject(h_, ms))) { @@ -67,19 +69,20 @@ class mutex { case WAIT_TIMEOUT: return false; 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()) { return false; } break; // loop again default: - ipc::error("fail WaitForSingleObject[%lu]: 0x%08X\n", ::GetLastError(), ret); + log.error("fail WaitForSingleObject[", ::GetLastError(), "]: 0x", std::hex, ret, std::dec); return false; } } } bool try_lock() noexcept(false) { + LIBIPC_LOG(); DWORD ret = ::WaitForSingleObject(h_, 0); switch (ret) { case WAIT_OBJECT_0: @@ -90,14 +93,15 @@ class mutex { unlock(); LIBIPC_FALLTHROUGH; 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(ret), std::system_category()}; } } bool unlock() noexcept { + LIBIPC_LOG(); if (!::ReleaseMutex(h_)) { - ipc::error("fail ReleaseMutex[%lu]\n", ::GetLastError()); + log.error("fail ReleaseMutex[", ::GetLastError(), "]"); return false; } return true; diff --git a/src/libipc/platform/win/semaphore.h b/src/libipc/platform/win/semaphore.h index 8185369..b5915e9 100644 --- a/src/libipc/platform/win/semaphore.h +++ b/src/libipc/platform/win/semaphore.h @@ -8,7 +8,7 @@ #include #endif -#include "libipc/utility/log.h" +#include "libipc/imp/log.h" #include "to_tchar.h" #include "get_sa.h" @@ -33,12 +33,13 @@ class semaphore { } bool open(char const *name, std::uint32_t count) noexcept { + LIBIPC_LOG(); close(); h_ = ::CreateSemaphore(detail::get_sa(), static_cast(count), LONG_MAX, detail::to_tchar(name).c_str()); if (h_ == NULL) { - ipc::error("fail CreateSemaphore[%lu]: %s\n", ::GetLastError(), name); + log.error("fail CreateSemaphore[", static_cast(::GetLastError()), "]: ", name); return false; } return true; @@ -58,6 +59,7 @@ class semaphore { } bool wait(std::uint64_t tm) noexcept { + LIBIPC_LOG(); DWORD ret, ms = (tm == invalid_value) ? INFINITE : static_cast(tm); switch ((ret = ::WaitForSingleObject(h_, ms))) { case WAIT_OBJECT_0: @@ -66,14 +68,15 @@ class semaphore { return false; case WAIT_ABANDONED: default: - ipc::error("fail WaitForSingleObject[%lu]: 0x%08X\n", ::GetLastError(), ret); + log.error("fail WaitForSingleObject[", ::GetLastError(), "]: 0x", std::hex, ret, std::dec); return false; } } bool post(std::uint32_t count) noexcept { + LIBIPC_LOG(); if (!::ReleaseSemaphore(h_, static_cast(count), NULL)) { - ipc::error("fail ReleaseSemaphore[%lu]\n", ::GetLastError()); + log.error("fail ReleaseSemaphore[", ::GetLastError(), "]"); return false; } return true; diff --git a/src/libipc/platform/win/shm_win.cpp b/src/libipc/platform/win/shm_win.cpp index a8a0ce5..5a7e612 100755 --- a/src/libipc/platform/win/shm_win.cpp +++ b/src/libipc/platform/win/shm_win.cpp @@ -1,186 +1,192 @@ - -#if defined(__MINGW32__) -#include -#else -#include -#endif - -#include -#include -#include - -#include "libipc/shm.h" -#include "libipc/def.h" - -#include "libipc/utility/log.h" -#include "libipc/mem/resource.h" -#include "libipc/mem/new.h" - -#include "to_tchar.h" -#include "get_sa.h" - -namespace { - -struct info_t { - std::atomic acc_; -}; - -struct id_info_t { - HANDLE h_ = NULL; - void* mem_ = nullptr; - std::size_t size_ = 0; -}; - -constexpr std::size_t calc_size(std::size_t size) { - return ((((size - 1) / alignof(info_t)) + 1) * alignof(info_t)) + sizeof(info_t); -} - -inline auto& acc_of(void* mem, std::size_t size) { - return reinterpret_cast(static_cast(mem) + size - sizeof(info_t))->acc_; -} - -} // internal-linkage - -namespace ipc { -namespace shm { - -id_t acquire(char const * name, std::size_t size, unsigned mode) { - if (!is_valid_string(name)) { - ipc::error("fail acquire: name is empty\n"); - return nullptr; - } - HANDLE h; - auto fmt_name = ipc::detail::to_tchar(name); - // Opens a named file mapping object. - if (mode == open) { - h = ::OpenFileMapping(FILE_MAP_ALL_ACCESS, FALSE, fmt_name.c_str()); - if (h == NULL) { - ipc::error("fail OpenFileMapping[%d]: %s\n", static_cast(::GetLastError()), name); - return nullptr; - } - } - // Creates or opens a named file mapping object for a specified file. - else { - std::size_t alloc_size = calc_size(size); - h = ::CreateFileMapping(INVALID_HANDLE_VALUE, detail::get_sa(), PAGE_READWRITE | SEC_COMMIT, - 0, static_cast(alloc_size), fmt_name.c_str()); - DWORD err = ::GetLastError(); - // If the object exists before the function call, the function returns a handle to the existing object - // (with its current size, not the specified size), and GetLastError returns ERROR_ALREADY_EXISTS. - if ((mode == create) && (err == ERROR_ALREADY_EXISTS)) { - if (h != NULL) ::CloseHandle(h); - h = NULL; - } - if (h == NULL) { - ipc::error("fail CreateFileMapping[%d]: %s\n", static_cast(err), name); - return nullptr; - } - } - auto ii = mem::$new(); - ii->h_ = h; - ii->size_ = size; - return ii; -} - -std::int32_t get_ref(id_t id) { - if (id == nullptr) { - return 0; - } - auto ii = static_cast(id); - if (ii->mem_ == nullptr || ii->size_ == 0) { - return 0; - } - return acc_of(ii->mem_, calc_size(ii->size_)).load(std::memory_order_acquire); -} - -void sub_ref(id_t id) { - if (id == nullptr) { - ipc::error("fail sub_ref: invalid id (null)\n"); - return; - } - auto ii = static_cast(id); - if (ii->mem_ == nullptr || ii->size_ == 0) { - ipc::error("fail sub_ref: invalid id (mem = %p, size = %zd)\n", ii->mem_, ii->size_); - return; - } - 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) { - if (id == nullptr) { - ipc::error("fail get_mem: invalid id (null)\n"); - return nullptr; - } - auto ii = static_cast(id); - if (ii->mem_ != nullptr) { - if (size != nullptr) *size = ii->size_; - return ii->mem_; - } - if (ii->h_ == NULL) { - ipc::error("fail to_mem: invalid id (h = null)\n"); - return nullptr; - } - LPVOID mem = ::MapViewOfFile(ii->h_, FILE_MAP_ALL_ACCESS, 0, 0, 0); - if (mem == NULL) { - ipc::error("fail MapViewOfFile[%d]\n", static_cast(::GetLastError())); - return nullptr; - } - MEMORY_BASIC_INFORMATION mem_info; - if (::VirtualQuery(mem, &mem_info, sizeof(mem_info)) == 0) { - ipc::error("fail VirtualQuery[%d]\n", static_cast(::GetLastError())); - return nullptr; - } - std::size_t actual_size = static_cast(mem_info.RegionSize); - if (ii->size_ == 0) { - // Opening existing shared memory - ii->size_ = actual_size - sizeof(info_t); - } - // else: Keep user-requested size (already set in acquire) - ii->mem_ = mem; - if (size != nullptr) *size = ii->size_; - // Initialize or increment reference counter - acc_of(mem, calc_size(ii->size_)).fetch_add(1, std::memory_order_release); - return static_cast(mem); -} - -std::int32_t release(id_t id) noexcept { - if (id == nullptr) { - ipc::error("fail release: invalid id (null)\n"); - return -1; - } - std::int32_t ret = -1; - auto ii = static_cast(id); - if (ii->mem_ == nullptr || ii->size_ == 0) { - ipc::error("fail release: invalid id (mem = %p, size = %zd)\n", ii->mem_, ii->size_); - } - else { - ret = acc_of(ii->mem_, calc_size(ii->size_)).fetch_sub(1, std::memory_order_acq_rel); - ::UnmapViewOfFile(static_cast(ii->mem_)); - } - if (ii->h_ == NULL) { - ipc::error("fail release: invalid id (h = null)\n"); - } - else ::CloseHandle(ii->h_); - mem::$delete(ii); - return ret; -} - -void remove(id_t id) noexcept { - if (id == nullptr) { - ipc::error("fail release: invalid id (null)\n"); - return; - } - release(id); -} - -void remove(char const * name) noexcept { - if (!is_valid_string(name)) { - ipc::error("fail remove: name is empty\n"); - return; - } - // Do Nothing. -} - -} // namespace shm -} // namespace ipc - + +#if defined(__MINGW32__) +#include +#else +#include +#endif + +#include +#include +#include + +#include "libipc/shm.h" +#include "libipc/def.h" + +#include "libipc/imp/log.h" +#include "libipc/mem/resource.h" +#include "libipc/mem/new.h" + +#include "to_tchar.h" +#include "get_sa.h" + +namespace { + +struct info_t { + std::atomic acc_; +}; + +struct id_info_t { + HANDLE h_ = NULL; + void* mem_ = nullptr; + std::size_t size_ = 0; +}; + +constexpr std::size_t calc_size(std::size_t size) { + return ((((size - 1) / alignof(info_t)) + 1) * alignof(info_t)) + sizeof(info_t); +} + +inline auto& acc_of(void* mem, std::size_t size) { + return reinterpret_cast(static_cast(mem) + size - sizeof(info_t))->acc_; +} + +} // internal-linkage + +namespace ipc { +namespace shm { + +id_t acquire(char const * name, std::size_t size, unsigned mode) { + LIBIPC_LOG(); + if (!is_valid_string(name)) { + log.error("fail acquire: name is empty"); + return nullptr; + } + HANDLE h; + auto fmt_name = ipc::detail::to_tchar(name); + // Opens a named file mapping object. + if (mode == open) { + h = ::OpenFileMapping(FILE_MAP_ALL_ACCESS, FALSE, fmt_name.c_str()); + if (h == NULL) { + log.error("fail OpenFileMapping[", static_cast(::GetLastError()), "]: ", name); + return nullptr; + } + } + // Creates or opens a named file mapping object for a specified file. + else { + std::size_t alloc_size = calc_size(size); + h = ::CreateFileMapping(INVALID_HANDLE_VALUE, detail::get_sa(), PAGE_READWRITE | SEC_COMMIT, + 0, static_cast(alloc_size), fmt_name.c_str()); + DWORD err = ::GetLastError(); + // If the object exists before the function call, the function returns a handle to the existing object + // (with its current size, not the specified size), and GetLastError returns ERROR_ALREADY_EXISTS. + if ((mode == create) && (err == ERROR_ALREADY_EXISTS)) { + if (h != NULL) ::CloseHandle(h); + h = NULL; + } + if (h == NULL) { + log.error("fail CreateFileMapping[", static_cast(err), "]: ", name); + return nullptr; + } + } + auto ii = mem::$new(); + ii->h_ = h; + ii->size_ = size; + return ii; +} + +std::int32_t get_ref(id_t id) { + if (id == nullptr) { + return 0; + } + auto ii = static_cast(id); + if (ii->mem_ == nullptr || ii->size_ == 0) { + return 0; + } + return acc_of(ii->mem_, calc_size(ii->size_)).load(std::memory_order_acquire); +} + +void sub_ref(id_t id) { + LIBIPC_LOG(); + if (id == nullptr) { + log.error("fail sub_ref: invalid id (null)"); + return; + } + auto ii = static_cast(id); + if (ii->mem_ == nullptr || ii->size_ == 0) { + log.error("fail sub_ref: invalid id (mem = ", ii->mem_, ", size = ", ii->size_, ")"); + return; + } + 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) { + LIBIPC_LOG(); + if (id == nullptr) { + log.error("fail get_mem: invalid id (null)"); + return nullptr; + } + auto ii = static_cast(id); + if (ii->mem_ != nullptr) { + if (size != nullptr) *size = ii->size_; + return ii->mem_; + } + if (ii->h_ == NULL) { + log.error("fail to_mem: invalid id (h = null)"); + return nullptr; + } + LPVOID mem = ::MapViewOfFile(ii->h_, FILE_MAP_ALL_ACCESS, 0, 0, 0); + if (mem == NULL) { + log.error("fail MapViewOfFile[", static_cast(::GetLastError()), "]"); + return nullptr; + } + MEMORY_BASIC_INFORMATION mem_info; + if (::VirtualQuery(mem, &mem_info, sizeof(mem_info)) == 0) { + log.error("fail VirtualQuery[", static_cast(::GetLastError()), "]"); + return nullptr; + } + std::size_t actual_size = static_cast(mem_info.RegionSize); + if (ii->size_ == 0) { + // Opening existing shared memory + ii->size_ = actual_size - sizeof(info_t); + } + // else: Keep user-requested size (already set in acquire) + ii->mem_ = mem; + if (size != nullptr) *size = ii->size_; + // Initialize or increment reference counter + acc_of(mem, calc_size(ii->size_)).fetch_add(1, std::memory_order_release); + return static_cast(mem); +} + +std::int32_t release(id_t id) noexcept { + LIBIPC_LOG(); + if (id == nullptr) { + log.error("fail release: invalid id (null)"); + return -1; + } + std::int32_t ret = -1; + auto ii = static_cast(id); + if (ii->mem_ == nullptr || ii->size_ == 0) { + log.error("fail release: invalid id (mem = ", ii->mem_, ", size = ", ii->size_, ")"); + } + else { + ret = acc_of(ii->mem_, calc_size(ii->size_)).fetch_sub(1, std::memory_order_acq_rel); + ::UnmapViewOfFile(static_cast(ii->mem_)); + } + if (ii->h_ == NULL) { + log.error("fail release: invalid id (h = null)"); + } + else ::CloseHandle(ii->h_); + mem::$delete(ii); + return ret; +} + +void remove(id_t id) noexcept { + LIBIPC_LOG(); + if (id == nullptr) { + log.error("fail release: invalid id (null)"); + return; + } + release(id); +} + +void remove(char const * name) noexcept { + LIBIPC_LOG(); + if (!is_valid_string(name)) { + log.error("fail remove: name is empty"); + return; + } + // Do Nothing. +} + +} // namespace shm +} // namespace ipc + diff --git a/src/libipc/prod_cons.h b/src/libipc/prod_cons.h index 28d99bd..ca00b7c 100755 --- a/src/libipc/prod_cons.h +++ b/src/libipc/prod_cons.h @@ -10,7 +10,7 @@ #include "libipc/platform/detail.h" #include "libipc/circ/elem_def.h" -#include "libipc/utility/log.h" +#include "libipc/imp/log.h" #include "libipc/utility/utility.h" namespace ipc { @@ -242,6 +242,7 @@ struct prod_cons_impl> { template bool force_push(W* wrapper, F&& f, E* elems) { + LIBIPC_LOG(); E* el; epoch_ += ep_incr; for (unsigned k = 0;;) { @@ -252,7 +253,7 @@ struct prod_cons_impl> { auto cur_rc = el->rc_.load(std::memory_order_acquire); circ::cc_t rem_cc = cur_rc & ep_mask; 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 if (cc == 0) return false; // no reader } @@ -364,6 +365,7 @@ struct prod_cons_impl> { template bool force_push(W* wrapper, F&& f, E* elems) { + LIBIPC_LOG(); E* el; circ::u2_t cur_ct; rc_t epoch = epoch_.fetch_add(ep_incr, std::memory_order_release) + ep_incr; @@ -375,7 +377,7 @@ struct prod_cons_impl> { auto cur_rc = el->rc_.load(std::memory_order_acquire); circ::cc_t rem_cc = cur_rc & rc_mask; 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 if (cc == 0) return false; // no reader } diff --git a/src/libipc/queue.h b/src/libipc/queue.h index 5000bad..579e37a 100755 --- a/src/libipc/queue.h +++ b/src/libipc/queue.h @@ -15,7 +15,7 @@ #include "libipc/shm.h" #include "libipc/rw_lock.h" -#include "libipc/utility/log.h" +#include "libipc/imp/log.h" #include "libipc/platform/detail.h" #include "libipc/circ/elem_def.h" #include "libipc/mem/resource.h" @@ -30,8 +30,9 @@ class queue_conn { template Elems* open(char const * name) { + LIBIPC_LOG(); if (!is_valid_string(name)) { - ipc::error("fail open waiter: name is empty!\n"); + log.error("fail open waiter: name is empty!"); return nullptr; } if (!elems_h_.acquire(name, sizeof(Elems))) { @@ -39,7 +40,7 @@ class queue_conn { } auto elems = static_cast(elems_h_.get()); if (elems == nullptr) { - ipc::error("fail acquire elems: %s\n", name); + log.error("fail acquire elems: ", name); return nullptr; } elems->init(); diff --git a/src/libipc/shm.cpp b/src/libipc/shm.cpp index 40a374e..e7b9818 100755 --- a/src/libipc/shm.cpp +++ b/src/libipc/shm.cpp @@ -5,7 +5,7 @@ #include "libipc/shm.h" #include "libipc/utility/pimpl.h" -#include "libipc/utility/log.h" +#include "libipc/imp/log.h" #include "libipc/mem/resource.h" namespace ipc { @@ -69,12 +69,13 @@ void handle::sub_ref() noexcept { } bool handle::acquire(char const * name, std::size_t size, unsigned mode) { + LIBIPC_LOG(); if (!is_valid_string(name)) { - ipc::error("fail acquire: name is empty\n"); + log.error("fail acquire: name is empty"); return false; } if (size == 0) { - ipc::error("fail acquire: size is 0\n"); + log.error("fail acquire: size is 0"); return false; } release(); diff --git a/src/libipc/sync/condition.cpp b/src/libipc/sync/condition.cpp index ed75c2c..0e34a6f 100644 --- a/src/libipc/sync/condition.cpp +++ b/src/libipc/sync/condition.cpp @@ -2,7 +2,7 @@ #include "libipc/condition.h" #include "libipc/utility/pimpl.h" -#include "libipc/utility/log.h" +#include "libipc/imp/log.h" #include "libipc/mem/resource.h" #include "libipc/platform/detail.h" #if defined(LIBIPC_OS_WIN) @@ -50,8 +50,9 @@ bool condition::valid() const noexcept { } bool condition::open(char const *name) noexcept { + LIBIPC_LOG(); 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 impl(p_)->cond_.open(name); diff --git a/src/libipc/sync/mutex.cpp b/src/libipc/sync/mutex.cpp index ce75edd..fb3b7c9 100644 --- a/src/libipc/sync/mutex.cpp +++ b/src/libipc/sync/mutex.cpp @@ -2,7 +2,7 @@ #include "libipc/mutex.h" #include "libipc/utility/pimpl.h" -#include "libipc/utility/log.h" +#include "libipc/imp/log.h" #include "libipc/mem/resource.h" #include "libipc/platform/detail.h" #if defined(LIBIPC_OS_WIN) @@ -50,8 +50,9 @@ bool mutex::valid() const noexcept { } bool mutex::open(char const *name) noexcept { + LIBIPC_LOG(); 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 impl(p_)->lock_.open(name); diff --git a/src/libipc/sync/semaphore.cpp b/src/libipc/sync/semaphore.cpp index e252ed0..8f1dbb7 100644 --- a/src/libipc/sync/semaphore.cpp +++ b/src/libipc/sync/semaphore.cpp @@ -2,7 +2,7 @@ #include "libipc/semaphore.h" #include "libipc/utility/pimpl.h" -#include "libipc/utility/log.h" +#include "libipc/imp/log.h" #include "libipc/mem/resource.h" #include "libipc/platform/detail.h" #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 { + LIBIPC_LOG(); 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 impl(p_)->sem_.open(name, count);