mirror of
https://github.com/mutouyun/cpp-ipc.git
synced 2025-12-06 16:56:45 +08:00
remove useless codes
This commit is contained in:
parent
d48c943f22
commit
280cc81fa2
@ -46,9 +46,6 @@ enum class trans { // transmission
|
|||||||
template <relat Rp, relat Rc, trans Ts>
|
template <relat Rp, relat Rc, trans Ts>
|
||||||
struct wr {};
|
struct wr {};
|
||||||
|
|
||||||
// implement with multi routes
|
|
||||||
struct wr_routes {};
|
|
||||||
|
|
||||||
// concept helpers
|
// concept helpers
|
||||||
|
|
||||||
template <bool Cond, typename R>
|
template <bool Cond, typename R>
|
||||||
|
|||||||
@ -25,18 +25,6 @@ struct IPC_EXPORT chan_impl {
|
|||||||
static buff_t recv(handle_t h);
|
static buff_t recv(handle_t h);
|
||||||
};
|
};
|
||||||
|
|
||||||
template <>
|
|
||||||
struct IPC_EXPORT chan_impl<wr_routes> {
|
|
||||||
static handle_t connect (char const * name);
|
|
||||||
static void disconnect(handle_t h);
|
|
||||||
|
|
||||||
static std::size_t recv_count(handle_t h);
|
|
||||||
static bool wait_for_recv(handle_t h, std::size_t r_count);
|
|
||||||
|
|
||||||
static bool send(handle_t h, void const * data, std::size_t size);
|
|
||||||
static buff_t recv(handle_t h);
|
|
||||||
};
|
|
||||||
|
|
||||||
template <typename Flag>
|
template <typename Flag>
|
||||||
class chan_wrapper {
|
class chan_wrapper {
|
||||||
private:
|
private:
|
||||||
|
|||||||
115
src/channel.cpp
115
src/channel.cpp
@ -1,115 +0,0 @@
|
|||||||
#include <array>
|
|
||||||
#include <string>
|
|
||||||
#include <type_traits>
|
|
||||||
|
|
||||||
#include "ipc.h"
|
|
||||||
#include "shm.h"
|
|
||||||
#include "rw_lock.h"
|
|
||||||
#include "id_pool.h"
|
|
||||||
|
|
||||||
#include "platform/detail.h"
|
|
||||||
|
|
||||||
namespace {
|
|
||||||
|
|
||||||
using namespace ipc;
|
|
||||||
|
|
||||||
struct ch_info_t {
|
|
||||||
rw_lock lc_;
|
|
||||||
id_pool<> ch_acc_; // only support 255 channels with one name
|
|
||||||
};
|
|
||||||
|
|
||||||
struct ch_multi_routes {
|
|
||||||
shm::handle h_;
|
|
||||||
route r_;
|
|
||||||
|
|
||||||
std::size_t id_;
|
|
||||||
bool marked_ = false;
|
|
||||||
|
|
||||||
std::array<route, decltype(ch_info_t::ch_acc_)::max_count> rts_;
|
|
||||||
|
|
||||||
ch_info_t& info() {
|
|
||||||
return *static_cast<ch_info_t*>(h_.get());
|
|
||||||
}
|
|
||||||
|
|
||||||
auto& acc() {
|
|
||||||
return info().ch_acc_;
|
|
||||||
}
|
|
||||||
|
|
||||||
void mark_id() {
|
|
||||||
if (marked_) return;
|
|
||||||
marked_ = true;
|
|
||||||
IPC_UNUSED_ auto guard = ipc::detail::unique_lock(info().lc_);
|
|
||||||
acc().mark_acquired(id_);
|
|
||||||
}
|
|
||||||
|
|
||||||
auto& sender() {
|
|
||||||
mark_id();
|
|
||||||
return r_;
|
|
||||||
}
|
|
||||||
|
|
||||||
bool valid() const {
|
|
||||||
return h_.valid() && r_.valid();
|
|
||||||
}
|
|
||||||
|
|
||||||
bool connect(char const * name) {
|
|
||||||
if (name == nullptr || name[0] == '\0') {
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
disconnect();
|
|
||||||
if (!h_.acquire((std::string { name } + "_").c_str(), sizeof(ch_info_t))) {
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
{
|
|
||||||
IPC_UNUSED_ auto guard = ipc::detail::unique_lock(info().lc_);
|
|
||||||
if (acc().invalid()) acc().init();
|
|
||||||
id_ = acc().acquire();
|
|
||||||
}
|
|
||||||
if (id_ == invalid_value) {
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
r_.connect((name + std::to_string(id_)).c_str());
|
|
||||||
return valid();
|
|
||||||
}
|
|
||||||
|
|
||||||
void disconnect() {
|
|
||||||
if (!valid()) return;
|
|
||||||
{
|
|
||||||
IPC_UNUSED_ auto guard = ipc::detail::unique_lock(info().lc_);
|
|
||||||
acc().release(id_);
|
|
||||||
}
|
|
||||||
for (auto& rt : rts_) {
|
|
||||||
rt.disconnect();
|
|
||||||
}
|
|
||||||
r_.disconnect();
|
|
||||||
h_.release();
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
} // internal-linkage
|
|
||||||
|
|
||||||
namespace ipc {
|
|
||||||
|
|
||||||
ipc::handle_t chan_impl<wr_routes>::connect(char const * /*name*/) {
|
|
||||||
return nullptr;
|
|
||||||
}
|
|
||||||
|
|
||||||
void chan_impl<wr_routes>::disconnect(ipc::handle_t /*h*/) {
|
|
||||||
}
|
|
||||||
|
|
||||||
std::size_t chan_impl<wr_routes>::recv_count(ipc::handle_t /*h*/) {
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
bool chan_impl<wr_routes>::wait_for_recv(ipc::handle_t /*h*/, std::size_t /*r_count*/) {
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
bool chan_impl<wr_routes>::send(ipc::handle_t /*h*/, void const * /*data*/, std::size_t /*size*/) {
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
buff_t chan_impl<wr_routes>::recv(ipc::handle_t /*h*/) {
|
|
||||||
return {};
|
|
||||||
}
|
|
||||||
|
|
||||||
} // namespace ipc
|
|
||||||
127
src/id_pool.h
127
src/id_pool.h
@ -1,127 +0,0 @@
|
|||||||
#pragma once
|
|
||||||
|
|
||||||
#include <cstring>
|
|
||||||
#include <algorithm>
|
|
||||||
|
|
||||||
#include "def.h"
|
|
||||||
|
|
||||||
namespace ipc {
|
|
||||||
|
|
||||||
template <std::size_t DataSize, std::size_t AlignSize>
|
|
||||||
struct id_type {
|
|
||||||
uint_t<8> id_;
|
|
||||||
alignas(AlignSize) byte_t data_[DataSize] {};
|
|
||||||
|
|
||||||
id_type& operator=(uint_t<8> val) {
|
|
||||||
id_ = val;
|
|
||||||
return (*this);
|
|
||||||
}
|
|
||||||
|
|
||||||
operator uint_t<8>() const {
|
|
||||||
return id_;
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
template <std::size_t AlignSize>
|
|
||||||
struct id_type<0, AlignSize> {
|
|
||||||
uint_t<8> id_;
|
|
||||||
|
|
||||||
id_type& operator=(uint_t<8> val) {
|
|
||||||
id_ = val;
|
|
||||||
return (*this);
|
|
||||||
}
|
|
||||||
|
|
||||||
operator uint_t<8>() const {
|
|
||||||
return id_;
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
template <std::size_t DataSize = 0,
|
|
||||||
#if __cplusplus >= 201703L
|
|
||||||
std::size_t AlignSize = (std::min)(DataSize, alignof(std::size_t))>
|
|
||||||
#else /*__cplusplus < 201703L*/
|
|
||||||
std::size_t AlignSize = (alignof(std::size_t) < DataSize) ? alignof(std::size_t) : DataSize>
|
|
||||||
#endif/*__cplusplus < 201703L*/
|
|
||||||
class id_pool {
|
|
||||||
public:
|
|
||||||
enum : std::size_t {
|
|
||||||
max_count = (std::numeric_limits<uint_t<8>>::max)() // 255
|
|
||||||
};
|
|
||||||
|
|
||||||
private:
|
|
||||||
id_type<DataSize, AlignSize> next_[max_count];
|
|
||||||
|
|
||||||
uint_t<8> acquir_ = 0;
|
|
||||||
uint_t<8> cursor_ = 0;
|
|
||||||
|
|
||||||
public:
|
|
||||||
void init() {
|
|
||||||
acquir_ = max_count;
|
|
||||||
for (std::size_t i = 0; i < max_count;) {
|
|
||||||
i = next_[i] = static_cast<uint_t<8>>(i + 1);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
bool invalid() const {
|
|
||||||
static id_pool inv;
|
|
||||||
return std::memcmp(this, &inv, sizeof(id_pool)) == 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
bool empty() const {
|
|
||||||
return cursor_ == max_count;
|
|
||||||
}
|
|
||||||
|
|
||||||
std::size_t acquire() {
|
|
||||||
if (empty()) {
|
|
||||||
return invalid_value;
|
|
||||||
}
|
|
||||||
std::size_t id = cursor_;
|
|
||||||
cursor_ = next_[id]; // point to next
|
|
||||||
return id;
|
|
||||||
}
|
|
||||||
|
|
||||||
void mark_acquired(std::size_t id) {
|
|
||||||
next_[id] = acquir_;
|
|
||||||
acquir_ = static_cast<uint_t<8>>(id); // put it in acquired list
|
|
||||||
}
|
|
||||||
|
|
||||||
bool release(std::size_t id) {
|
|
||||||
if (id == invalid_value) return false;
|
|
||||||
if (acquir_ == max_count) return false;
|
|
||||||
if (acquir_ == id) {
|
|
||||||
acquir_ = next_[id]; // point to next
|
|
||||||
}
|
|
||||||
else {
|
|
||||||
uint_t<8> a = next_[acquir_], l = acquir_;
|
|
||||||
while (1) {
|
|
||||||
if (a == max_count) {
|
|
||||||
return false; // found nothing
|
|
||||||
}
|
|
||||||
if (a == id) {
|
|
||||||
next_[l] = next_[a];
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
l = a;
|
|
||||||
a = next_[a];
|
|
||||||
}
|
|
||||||
}
|
|
||||||
next_[id] = cursor_;
|
|
||||||
cursor_ = static_cast<uint_t<8>>(id); // put it back
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
template <typename F>
|
|
||||||
void for_acquired(F&& fr) {
|
|
||||||
auto a = acquir_;
|
|
||||||
while (a != max_count) {
|
|
||||||
if (!fr(a)) return;
|
|
||||||
a = next_[a];
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
void* at(std::size_t id) {
|
|
||||||
return next_[id].data_;
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
} // namespace ipc
|
|
||||||
@ -241,7 +241,7 @@ private slots:
|
|||||||
|
|
||||||
#include "test_circ.moc"
|
#include "test_circ.moc"
|
||||||
|
|
||||||
constexpr int LoopCount = 10000000;
|
constexpr int LoopCount = 1000000;
|
||||||
//constexpr int LoopCount = 1000/*0000*/;
|
//constexpr int LoopCount = 1000/*0000*/;
|
||||||
|
|
||||||
void Unit::initTestCase() {
|
void Unit::initTestCase() {
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user