mirror of
https://github.com/mutouyun/cpp-ipc.git
synced 2025-12-06 16:56:45 +08:00
printf failure-log
This commit is contained in:
parent
34ea7095d9
commit
b2e48fc138
@ -3,8 +3,12 @@
|
||||
#include <atomic>
|
||||
#include <cstddef>
|
||||
#include <cstdint>
|
||||
#include <new>
|
||||
|
||||
#include "rw_lock.h"
|
||||
|
||||
#include "platform/waiter.h"
|
||||
#include "platform/detail.h"
|
||||
|
||||
namespace ipc {
|
||||
namespace circ {
|
||||
@ -38,9 +42,24 @@ constexpr u1_t index_of(u2_t c) noexcept {
|
||||
|
||||
class conn_head {
|
||||
ipc::detail::waiter cc_waiter_, waiter_;
|
||||
std::atomic<std::size_t> cc_; // connection counter
|
||||
std::atomic<std::size_t> cc_ { 0 }; // connection counter
|
||||
|
||||
ipc::spin_lock lc_;
|
||||
std::atomic<bool> constructed_;
|
||||
|
||||
public:
|
||||
void init() {
|
||||
/* DCLP */
|
||||
if (!constructed_.load(std::memory_order_acquire)) {
|
||||
IPC_UNUSED_ auto guard = ipc::detail::unique_lock(lc_);
|
||||
if (!constructed_.load(std::memory_order_relaxed)) {
|
||||
::new (this) conn_head;
|
||||
constructed_.store(true, std::memory_order_release);
|
||||
::printf("init...\n");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
conn_head() = default;
|
||||
conn_head(const conn_head&) = delete;
|
||||
conn_head& operator=(const conn_head&) = delete;
|
||||
|
||||
@ -42,12 +42,14 @@ public:
|
||||
if (w_ == nullptr) return false;
|
||||
close();
|
||||
h_ = w_->open(name);
|
||||
::printf("%s: %p\n", name, h_);
|
||||
return valid();
|
||||
}
|
||||
|
||||
void close() {
|
||||
if (!valid()) return;
|
||||
w_->close(h_);
|
||||
::printf("close %p\n", h_);
|
||||
h_ = waiter_t::invalid();
|
||||
}
|
||||
|
||||
|
||||
@ -36,26 +36,32 @@ public:
|
||||
// init mutex
|
||||
pthread_mutexattr_t mutex_attr;
|
||||
if (::pthread_mutexattr_init(&mutex_attr) != 0) {
|
||||
::printf("fail pthread_mutexattr_init\n");
|
||||
return invalid();
|
||||
}
|
||||
IPC_UNUSED_ auto guard_mutex_attr = unique_ptr(&mutex_attr, ::pthread_mutexattr_destroy);
|
||||
if (::pthread_mutexattr_setpshared(&mutex_attr, PTHREAD_PROCESS_SHARED) != 0) {
|
||||
::printf("fail pthread_mutexattr_setpshared\n");
|
||||
return invalid();
|
||||
}
|
||||
if (::pthread_mutex_init(&mutex_, &mutex_attr) != 0) {
|
||||
::printf("fail pthread_mutex_init\n");
|
||||
return invalid();
|
||||
}
|
||||
auto guard_mutex = unique_ptr(&mutex_, ::pthread_mutex_destroy);
|
||||
// init condition
|
||||
pthread_condattr_t cond_attr;
|
||||
if (::pthread_condattr_init(&cond_attr) != 0) {
|
||||
::printf("fail pthread_condattr_init\n");
|
||||
return invalid();
|
||||
}
|
||||
IPC_UNUSED_ auto guard_cond_attr = unique_ptr(&cond_attr, ::pthread_condattr_destroy);
|
||||
if (::pthread_condattr_setpshared(&cond_attr, PTHREAD_PROCESS_SHARED) != 0) {
|
||||
::printf("fail pthread_condattr_setpshared\n");
|
||||
return invalid();
|
||||
}
|
||||
if (::pthread_cond_init(&cond_, &cond_attr) != 0) {
|
||||
::printf("fail pthread_cond_init\n");
|
||||
return invalid();
|
||||
}
|
||||
// no need to guard condition
|
||||
|
||||
@ -1,5 +1,9 @@
|
||||
#pragma once
|
||||
|
||||
#if __has_include(<pthread.h>)
|
||||
#include "waiter_linux.h"
|
||||
#else /*!__has_include(<pthread.h>)*/
|
||||
|
||||
#include <Windows.h>
|
||||
|
||||
#include <algorithm>
|
||||
@ -55,3 +59,5 @@ public:
|
||||
|
||||
} // namespace detail
|
||||
} // namespace ipc
|
||||
|
||||
#endif/*!__has_include(<pthread.h>)*/
|
||||
|
||||
11
src/queue.h
11
src/queue.h
@ -34,6 +34,7 @@ protected:
|
||||
if (elems == nullptr) {
|
||||
return nullptr;
|
||||
}
|
||||
elems->init();
|
||||
dismiss_ = false;
|
||||
return elems;
|
||||
}
|
||||
@ -46,7 +47,9 @@ protected:
|
||||
}
|
||||
assert(elems != nullptr);
|
||||
waiter_.attach(&(elems->waiter()));
|
||||
waiter_.open((std::string{ "__IPC_WAITER__" } + name).c_str());
|
||||
if (!waiter_.open((std::string{ "__IPC_WAITER__" } + name).c_str())) {
|
||||
return;
|
||||
}
|
||||
cc_waiter_.attach(&(elems->conn_waiter()));
|
||||
cc_waiter_.open((std::string{ "__IPC_CC_WAITER__" } + name).c_str());
|
||||
}
|
||||
@ -140,11 +143,7 @@ public:
|
||||
}
|
||||
|
||||
/* not virtual */ ~queue_base(void) {
|
||||
if (!this->dismiss_ && (elems_ != nullptr)) {
|
||||
shm::release(elems_, sizeof(Elems));
|
||||
}
|
||||
dismiss_ = true;
|
||||
// base_t::close(elems_);
|
||||
base_t::close(elems_);
|
||||
}
|
||||
|
||||
constexpr elems_t * elems() const noexcept {
|
||||
|
||||
@ -24,7 +24,7 @@ private slots:
|
||||
|
||||
void test_alloc_free();
|
||||
void test_linear();
|
||||
} unit__;
|
||||
} /*unit__*/;
|
||||
|
||||
#include "test_mem.moc"
|
||||
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user