Added QNX support

This commit is contained in:
mutouyun 2022-01-02 17:54:07 +08:00
parent 51828c2f7b
commit 2e35ab7685
14 changed files with 684 additions and 249 deletions

View File

@ -1,13 +1,8 @@
project(ipc) project(ipc)
if(UNIX)
file(GLOB SRC_FILES ${LIBIPC_PROJECT_DIR}/src/libipc/platform/linux/*.cpp
${LIBIPC_PROJECT_DIR}/src/libipc/platform/linux/a0/*.c)
else()
file(GLOB SRC_FILES ${LIBIPC_PROJECT_DIR}/src/libipc/platform/win/*.cpp)
endif()
aux_source_directory(${LIBIPC_PROJECT_DIR}/src/libipc SRC_FILES) aux_source_directory(${LIBIPC_PROJECT_DIR}/src/libipc SRC_FILES)
aux_source_directory(${LIBIPC_PROJECT_DIR}/src/libipc/sync SRC_FILES) aux_source_directory(${LIBIPC_PROJECT_DIR}/src/libipc/sync SRC_FILES)
aux_source_directory(${LIBIPC_PROJECT_DIR}/src/libipc/platform SRC_FILES)
file(GLOB HEAD_FILES file(GLOB HEAD_FILES
${LIBIPC_PROJECT_DIR}/include/libipc/*.h ${LIBIPC_PROJECT_DIR}/include/libipc/*.h
@ -39,7 +34,7 @@ set_target_properties(${PROJECT_NAME}
# set version # set version
set_target_properties(${PROJECT_NAME} set_target_properties(${PROJECT_NAME}
PROPERTIES PROPERTIES
VERSION 1.2.0 VERSION 1.1.2
SOVERSION 3) SOVERSION 3)
target_include_directories(${PROJECT_NAME} target_include_directories(${PROJECT_NAME}
@ -49,7 +44,7 @@ target_include_directories(${PROJECT_NAME}
if(NOT MSVC) if(NOT MSVC)
target_link_libraries(${PROJECT_NAME} PUBLIC target_link_libraries(${PROJECT_NAME} PUBLIC
pthread $<$<NOT:$<STREQUAL:${CMAKE_SYSTEM_NAME},QNX>>:pthread>
$<$<NOT:$<STREQUAL:${CMAKE_SYSTEM_NAME},Windows>>:rt>) $<$<NOT:$<STREQUAL:${CMAKE_SYSTEM_NAME},Windows>>:rt>)
endif() endif()

View File

@ -1,4 +1,22 @@
#pragma once #ifndef LIBIPC_SRC_PLATFORM_DETAIL_H_
#define LIBIPC_SRC_PLATFORM_DETAIL_H_
// detect platform
#if defined(WIN64) || defined(_WIN64) || defined(__WIN64__) || \
defined(WIN32) || defined(_WIN32) || defined(__WIN32__) || defined(__NT__) || \
defined(WINCE) || defined(_WIN32_WCE)
# define IPC_OS_WINDOWS_
#elif defined(__linux__) || defined(__linux)
# define IPC_OS_LINUX_
#elif defined(__QNX__)
# define IPC_OS_QNX_
#elif defined(__APPLE__)
#elif defined(__ANDROID__)
// TBD
#endif
#if defined(__cplusplus)
#include <memory> #include <memory>
#include <mutex> #include <mutex>
@ -22,18 +40,6 @@
# error "IPC_CONSTEXPR_ has been defined." # error "IPC_CONSTEXPR_ has been defined."
#endif #endif
// detect platform
#if defined(WIN64) || defined(_WIN64) || defined(__WIN64__) || \
defined(WIN32) || defined(_WIN32) || defined(__WIN32__) || defined(__NT__) || \
defined(WINCE) || defined(_WIN32_WCE)
# define IPC_OS_WINDOWS_
#endif/*WIN*/
#if defined(__linux__) || defined(__linux)
# define IPC_OS_LINUX_
#endif/*linux*/
#if __cplusplus >= 201703L #if __cplusplus >= 201703L
#define IPC_UNUSED_ [[maybe_unused]] #define IPC_UNUSED_ [[maybe_unused]]
@ -123,17 +129,8 @@ constexpr const T& (min)(const T& a, const T& b) {
#endif/*__cplusplus < 201703L*/ #endif/*__cplusplus < 201703L*/
template <typename T, typename U>
auto horrible_cast(U rhs) noexcept
-> typename std::enable_if<std::is_trivially_copyable<T>::value
&& std::is_trivially_copyable<U>::value, T>::type {
union {
T t;
U u;
} r = {};
r.u = rhs;
return r.t;
}
} // namespace detail } // namespace detail
} // namespace ipc } // namespace ipc
#endif // defined(__cplusplus)
#endif // LIBIPC_SRC_PLATFORM_DETAIL_H_

View File

@ -0,0 +1,13 @@
#include "libipc/platform/detail.h"
#if defined(IPC_OS_WINDOWS_)
#elif defined(IPC_OS_LINUX_)
#include "libipc/platform/linux/a0/err.c"
#include "libipc/platform/linux/a0/mtx.c"
#include "libipc/platform/linux/a0/strconv.c"
#include "libipc/platform/linux/a0/tid.c"
#include "libipc/platform/linux/a0/time.c"
#elif defined(IPC_OS_QNX_)
#else/*IPC_OS*/
# error "Unsupported platform."
#endif

View File

@ -0,0 +1,9 @@
#include "libipc/platform/detail.h"
#if defined(IPC_OS_WINDOWS_)
#include "libipc/platform/win/shm_win.cpp"
#elif defined(IPC_OS_LINUX_) || defined(IPC_OS_QNX_)
#include "libipc/platform/linux/shm_posix.cpp"
#else/*IPC_OS*/
# error "Unsupported platform."
#endif

View File

@ -0,0 +1,140 @@
#pragma once
#include <cstdint>
#include <cstring>
#include <pthread.h>
#include "libipc/platform/get_wait_time.h"
#include "libipc/utility/log.h"
#include "libipc/utility/scope_guard.h"
#include "libipc/mutex.h"
#include "libipc/shm.h"
namespace ipc {
namespace detail {
namespace sync {
class condition {
ipc::shm::handle shm_;
pthread_cond_t *cond_ = nullptr;
pthread_cond_t *acquire_cond(char const *name) {
if (!shm_.acquire(name, sizeof(pthread_cond_t))) {
ipc::error("[acquire_cond] fail shm.acquire: %s\n", name);
return nullptr;
}
return static_cast<pthread_cond_t *>(shm_.get());
}
public:
condition() = default;
~condition() = default;
pthread_cond_t const *native() const noexcept {
return cond_;
}
pthread_cond_t *native() noexcept {
return cond_;
}
bool valid() const noexcept {
static const char tmp[sizeof(pthread_cond_t)] {};
return (cond_ != nullptr)
&& (std::memcmp(tmp, cond_, sizeof(pthread_cond_t)) != 0);
}
bool open(char const *name) noexcept {
close();
if ((cond_ = acquire_cond(name)) == nullptr) {
return false;
}
if (shm_.ref() > 1) {
return valid();
}
::pthread_cond_destroy(cond_);
auto finally = ipc::guard([this] { close(); }); // close when failed
// init 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);
return false;
}
IPC_UNUSED_ auto guard_cond_attr = unique_ptr(&cond_attr, ::pthread_condattr_destroy);
if ((eno = ::pthread_condattr_setpshared(&cond_attr, PTHREAD_PROCESS_SHARED)) != 0) {
ipc::error("fail pthread_condattr_setpshared[%d]\n", 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);
return false;
}
finally.dismiss();
return valid();
}
void close() noexcept {
if ((shm_.ref() <= 1) && cond_ != nullptr) {
int eno;
if ((eno = ::pthread_cond_destroy(cond_)) != 0) {
ipc::error("fail pthread_cond_destroy[%d]\n", eno);
}
}
shm_.release();
cond_ = nullptr;
}
bool wait(ipc::sync::mutex &mtx, std::uint64_t tm) noexcept {
if (!valid()) return false;
switch (tm) {
case invalid_value: {
int eno;
if ((eno = ::pthread_cond_wait(cond_, static_cast<pthread_mutex_t *>(mtx.native()))) != 0) {
ipc::error("fail pthread_cond_wait[%d]\n", eno);
return false;
}
}
break;
default: {
auto ts = detail::make_timespec(tm);
int eno;
if ((eno = ::pthread_cond_timedwait(cond_, static_cast<pthread_mutex_t *>(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);
}
return false;
}
}
break;
}
return true;
}
bool notify() noexcept {
if (!valid()) return false;
int eno;
if ((eno = ::pthread_cond_signal(cond_)) != 0) {
ipc::error("fail pthread_cond_signal[%d]\n", eno);
return false;
}
return true;
}
bool broadcast() noexcept {
if (!valid()) return false;
int eno;
if ((eno = ::pthread_cond_broadcast(cond_)) != 0) {
ipc::error("fail pthread_cond_broadcast[%d]\n", eno);
return false;
}
return true;
}
};
} // namespace sync
} // namespace detail
} // namespace ipc

View File

@ -0,0 +1,39 @@
#pragma once
#include <cstdint>
#include <system_error>
#include <sys/time.h>
#include <time.h>
#include <errno.h>
#include "libipc/utility/log.h"
namespace ipc {
namespace detail {
inline bool calc_wait_time(timespec &ts, std::uint64_t tm /*ms*/) noexcept {
timeval now;
int eno = ::gettimeofday(&now, NULL);
if (eno != 0) {
ipc::error("fail gettimeofday [%d]\n", eno);
return false;
}
ts.tv_nsec = (now.tv_usec + (tm % 1000) * 1000) * 1000;
ts.tv_sec = now.tv_sec + (tm / 1000) + (ts.tv_nsec / 1000000000l);
ts.tv_nsec %= 1000000000l;
return true;
}
inline timespec make_timespec(std::uint64_t tm /*ms*/) noexcept(false) {
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);
throw std::system_error{static_cast<int>(errno), std::system_category()};
}
return ts;
}
} // namespace detail
} // namespace ipc

View File

@ -0,0 +1,236 @@
#pragma once
#include <cstring>
#include <cassert>
#include <cstdint>
#include <system_error>
#include <mutex>
#include <atomic>
#include <pthread.h>
#include "libipc/platform/get_wait_time.h"
#include "libipc/platform/detail.h"
#include "libipc/utility/log.h"
#include "libipc/utility/scope_guard.h"
#include "libipc/memory/resource.h"
#include "libipc/shm.h"
namespace ipc {
namespace detail {
namespace sync {
class mutex {
ipc::shm::handle *shm_ = nullptr;
std::atomic<std::int32_t> *ref_ = nullptr;
pthread_mutex_t *mutex_ = nullptr;
struct curr_prog {
struct shm_data {
ipc::shm::handle shm;
std::atomic<std::int32_t> ref;
struct init {
char const *name;
std::size_t size;
};
shm_data(init arg)
: shm{arg.name, arg.size}, ref{0} {}
};
ipc::map<ipc::string, shm_data> mutex_handles;
std::mutex lock;
static curr_prog &get() {
static curr_prog info;
return info;
}
};
pthread_mutex_t *acquire_mutex(char const *name) {
if (name == nullptr) {
return nullptr;
}
auto &info = curr_prog::get();
IPC_UNUSED_ std::lock_guard<std::mutex> guard {info.lock};
auto it = info.mutex_handles.find(name);
if (it == info.mutex_handles.end()) {
it = curr_prog::get().mutex_handles.emplace(name,
curr_prog::shm_data::init{name, sizeof(pthread_mutex_t)}).first;
}
shm_ = &it->second.shm;
ref_ = &it->second.ref;
if (shm_ == nullptr) {
return nullptr;
}
return static_cast<pthread_mutex_t *>(shm_->get());
}
template <typename F>
void release_mutex(ipc::string const &name, F &&clear) {
if (name.empty()) return;
IPC_UNUSED_ std::lock_guard<std::mutex> guard {curr_prog::get().lock};
auto it = curr_prog::get().mutex_handles.find(name);
if (it == curr_prog::get().mutex_handles.end()) {
return;
}
if (clear()) {
curr_prog::get().mutex_handles.erase(it);
}
}
public:
mutex() = default;
~mutex() = default;
pthread_mutex_t const *native() const noexcept {
return mutex_;
}
pthread_mutex_t *native() noexcept {
return mutex_;
}
bool valid() const noexcept {
static const char tmp[sizeof(pthread_mutex_t)] {};
return (shm_ != nullptr) && (ref_ != nullptr) && (mutex_ != nullptr)
&& (std::memcmp(tmp, mutex_, sizeof(pthread_mutex_t)) != 0);
}
bool open(char const *name) noexcept {
close();
if ((mutex_ = acquire_mutex(name)) == nullptr) {
return false;
}
auto self_ref = ref_->fetch_add(1, std::memory_order_relaxed);
if (shm_->ref() > 1 || self_ref > 0) {
return valid();
}
::pthread_mutex_destroy(mutex_);
auto finally = ipc::guard([this] { close(); }); // close when failed
// init 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);
return false;
}
IPC_UNUSED_ auto guard_mutex_attr = unique_ptr(&mutex_attr, ::pthread_mutexattr_destroy);
if ((eno = ::pthread_mutexattr_setpshared(&mutex_attr, PTHREAD_PROCESS_SHARED)) != 0) {
ipc::error("fail pthread_mutexattr_setpshared[%d]\n", eno);
return false;
}
if ((eno = ::pthread_mutexattr_setrobust(&mutex_attr, PTHREAD_MUTEX_ROBUST)) != 0) {
ipc::error("fail pthread_mutexattr_setrobust[%d]\n", 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);
return false;
}
finally.dismiss();
return valid();
}
void close() noexcept {
if ((ref_ != nullptr) && (shm_ != nullptr) && (mutex_ != nullptr)) {
if (shm_->name() != nullptr) {
release_mutex(shm_->name(), [this] {
auto self_ref = ref_->fetch_sub(1, std::memory_order_relaxed);
if ((shm_->ref() <= 1) && (self_ref <= 1)) {
int eno;
if ((eno = ::pthread_mutex_destroy(mutex_)) != 0) {
ipc::error("fail pthread_mutex_destroy[%d]\n", eno);
}
return true;
}
return false;
});
} else shm_->release();
}
shm_ = nullptr;
ref_ = nullptr;
mutex_ = nullptr;
}
bool lock(std::uint64_t tm) noexcept {
if (!valid()) return false;
for (;;) {
auto ts = detail::make_timespec(tm);
int eno = (tm == invalid_value)
? ::pthread_mutex_lock(mutex_)
: ::pthread_mutex_timedlock(mutex_, &ts);
switch (eno) {
case 0:
return true;
case ETIMEDOUT:
return false;
case EOWNERDEAD: {
if (shm_->ref() > 1) {
shm_->sub_ref();
}
int eno2 = ::pthread_mutex_consistent(mutex_);
if (eno2 != 0) {
ipc::error("fail pthread_mutex_lock[%d], pthread_mutex_consistent[%d]\n", eno, eno2);
return false;
}
int eno3 = ::pthread_mutex_unlock(mutex_);
if (eno3 != 0) {
ipc::error("fail pthread_mutex_lock[%d], pthread_mutex_unlock[%d]\n", eno, eno3);
return false;
}
}
break; // loop again
default:
ipc::error("fail pthread_mutex_lock[%d]\n", eno);
return false;
}
}
}
bool try_lock() noexcept(false) {
if (!valid()) return false;
auto ts = detail::make_timespec(0);
int eno = ::pthread_mutex_timedlock(mutex_, &ts);
switch (eno) {
case 0:
return true;
case ETIMEDOUT:
return false;
case EOWNERDEAD: {
if (shm_->ref() > 1) {
shm_->sub_ref();
}
int eno2 = ::pthread_mutex_consistent(mutex_);
if (eno2 != 0) {
ipc::error("fail pthread_mutex_timedlock[%d], pthread_mutex_consistent[%d]\n", eno, eno2);
break;
}
int eno3 = ::pthread_mutex_unlock(mutex_);
if (eno3 != 0) {
ipc::error("fail pthread_mutex_timedlock[%d], pthread_mutex_unlock[%d]\n", eno, eno3);
break;
}
}
break;
default:
ipc::error("fail pthread_mutex_timedlock[%d]\n", eno);
break;
}
throw std::system_error{eno, std::system_category()};
}
bool unlock() noexcept {
if (!valid()) return false;
int eno;
if ((eno = ::pthread_mutex_unlock(mutex_)) != 0) {
ipc::error("fail pthread_mutex_unlock[%d]\n", eno);
return false;
}
return true;
}
};
} // namespace sync
} // namespace detail
} // namespace ipc

View File

@ -1,198 +1,197 @@
#include <sys/shm.h> #include <sys/stat.h>
#include <sys/stat.h> #include <sys/mman.h>
#include <sys/mman.h> #include <sys/types.h>
#include <sys/types.h> #include <unistd.h>
#include <unistd.h> #include <fcntl.h>
#include <fcntl.h> #include <errno.h>
#include <errno.h>
#include <atomic>
#include <atomic> #include <string>
#include <string> #include <utility>
#include <utility> #include <cstring>
#include <cstring>
#include "libipc/shm.h"
#include "libipc/shm.h" #include "libipc/def.h"
#include "libipc/def.h" #include "libipc/pool_alloc.h"
#include "libipc/pool_alloc.h"
#include "libipc/utility/log.h"
#include "libipc/utility/log.h" #include "libipc/memory/resource.h"
#include "libipc/memory/resource.h"
namespace {
namespace {
struct info_t {
struct info_t { std::atomic<std::int32_t> acc_;
std::atomic<std::int32_t> acc_; };
};
struct id_info_t {
struct id_info_t { int fd_ = -1;
int fd_ = -1; void* mem_ = nullptr;
void* mem_ = nullptr; std::size_t size_ = 0;
std::size_t size_ = 0; ipc::string name_;
ipc::string name_; };
};
constexpr std::size_t calc_size(std::size_t size) {
constexpr std::size_t calc_size(std::size_t size) { return ((((size - 1) / alignof(info_t)) + 1) * alignof(info_t)) + sizeof(info_t);
return ((((size - 1) / alignof(info_t)) + 1) * alignof(info_t)) + sizeof(info_t); }
}
inline auto& acc_of(void* mem, std::size_t size) {
inline auto& acc_of(void* mem, std::size_t size) { return reinterpret_cast<info_t*>(static_cast<ipc::byte_t*>(mem) + size - sizeof(info_t))->acc_;
return reinterpret_cast<info_t*>(static_cast<ipc::byte_t*>(mem) + size - sizeof(info_t))->acc_; }
}
} // internal-linkage
} // internal-linkage
namespace ipc {
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) { if (name == nullptr || name[0] == '\0') {
if (name == nullptr || name[0] == '\0') { ipc::error("fail acquire: name is empty\n");
ipc::error("fail acquire: name is empty\n"); return nullptr;
return nullptr; }
} ipc::string op_name = ipc::string{"__IPC_SHM__"} + name;
ipc::string op_name = ipc::string{"__IPC_SHM__"} + name; // Open the object for read-write access.
// Open the object for read-write access. int flag = O_RDWR;
int flag = O_RDWR; switch (mode) {
switch (mode) { case open:
case open: size = 0;
size = 0; break;
break; // The check for the existence of the object,
// The check for the existence of the object, // and its creation if it does not exist, are performed atomically.
// and its creation if it does not exist, are performed atomically. case create:
case create: flag |= O_CREAT | O_EXCL;
flag |= O_CREAT | O_EXCL; break;
break; // Create the shared memory object if it does not exist.
// Create the shared memory object if it does not exist. default:
default: flag |= O_CREAT;
flag |= O_CREAT; break;
break; }
} int fd = ::shm_open(op_name.c_str(), flag, S_IRUSR | S_IWUSR |
int fd = ::shm_open(op_name.c_str(), flag, S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP |
S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH);
S_IROTH | S_IWOTH); if (fd == -1) {
if (fd == -1) { ipc::error("fail shm_open[%d]: %s\n", errno, name);
ipc::error("fail shm_open[%d]: %s\n", errno, name); return nullptr;
return nullptr; }
} auto ii = mem::alloc<id_info_t>();
auto ii = mem::alloc<id_info_t>(); ii->fd_ = fd;
ii->fd_ = fd; ii->size_ = size;
ii->size_ = size; ii->name_ = std::move(op_name);
ii->name_ = std::move(op_name); return ii;
return ii; }
}
std::int32_t get_ref(id_t id) {
std::int32_t get_ref(id_t id) { if (id == nullptr) {
if (id == nullptr) { return 0;
return 0; }
} 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) { return 0;
return 0; }
} return acc_of(ii->mem_, ii->size_).load(std::memory_order_acquire);
return acc_of(ii->mem_, ii->size_).load(std::memory_order_acquire); }
}
void sub_ref(id_t id) {
void sub_ref(id_t id) { if (id == nullptr) {
if (id == nullptr) { ipc::error("fail sub_ref: invalid id (null)\n");
ipc::error("fail sub_ref: invalid id (null)\n"); 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_);
ipc::error("fail sub_ref: invalid id (mem = %p, size = %zd)\n", ii->mem_, 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) { if (id == nullptr) {
if (id == nullptr) { ipc::error("fail get_mem: invalid id (null)\n");
ipc::error("fail get_mem: invalid id (null)\n"); return nullptr;
return nullptr; }
} auto ii = static_cast<id_info_t*>(id);
auto ii = static_cast<id_info_t*>(id); if (ii->mem_ != nullptr) {
if (ii->mem_ != nullptr) { if (size != nullptr) *size = ii->size_;
if (size != nullptr) *size = ii->size_; return ii->mem_;
return ii->mem_; }
} int fd = ii->fd_;
int fd = ii->fd_; if (fd == -1) {
if (fd == -1) { ipc::error("fail get_mem: invalid id (fd = -1)\n");
ipc::error("fail get_mem: invalid id (fd = -1)\n"); 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_);
ipc::error("fail fstat[%d]: %s, size = %zd\n", errno, ii->name_.c_str(), 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_);
ipc::error("fail get_mem: %s, invalid size = %zd\n", ii->name_.c_str(), 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_);
ipc::error("fail ftruncate[%d]: %s, size = %zd\n", errno, ii->name_.c_str(), 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_);
ipc::error("fail mmap[%d]: %s, size = %zd\n", errno, ii->name_.c_str(), ii->size_); return nullptr;
return nullptr; }
} ::close(fd);
::close(fd); ii->fd_ = -1;
ii->fd_ = -1; ii->mem_ = mem;
ii->mem_ = mem; if (size != nullptr) *size = ii->size_;
if (size != nullptr) *size = ii->size_; acc_of(mem, ii->size_).fetch_add(1, std::memory_order_release);
acc_of(mem, ii->size_).fetch_add(1, std::memory_order_release); return mem;
return mem; }
}
std::int32_t release(id_t id) {
std::int32_t release(id_t id) { if (id == nullptr) {
if (id == nullptr) { ipc::error("fail release: invalid id (null)\n");
ipc::error("fail release: invalid id (null)\n"); 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_);
ipc::error("fail release: invalid id (mem = %p, size = %zd)\n", ii->mem_, ii->size_); }
} 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()) { ::shm_unlink(ii->name_.c_str());
::shm_unlink(ii->name_.c_str()); }
} }
} else ::munmap(ii->mem_, ii->size_);
else ::munmap(ii->mem_, ii->size_); mem::free(ii);
mem::free(ii); return ret;
return ret; }
}
void remove(id_t id) {
void remove(id_t id) { if (id == nullptr) {
if (id == nullptr) { ipc::error("fail remove: invalid id (null)\n");
ipc::error("fail remove: invalid id (null)\n"); return;
return; }
} auto ii = static_cast<id_info_t*>(id);
auto ii = static_cast<id_info_t*>(id); auto name = std::move(ii->name_);
auto name = std::move(ii->name_); release(id);
release(id); if (!name.empty()) {
if (!name.empty()) { ::shm_unlink(name.c_str());
::shm_unlink(name.c_str()); }
} }
}
void remove(char const * name) {
void remove(char const * name) { if (name == nullptr || name[0] == '\0') {
if (name == nullptr || name[0] == '\0') { ipc::error("fail remove: name is empty\n");
ipc::error("fail remove: name is empty\n"); return;
return; }
} ::shm_unlink((ipc::string{"__IPC_SHM__"} + name).c_str());
::shm_unlink((ipc::string{"__IPC_SHM__"} + name).c_str()); }
}
} // namespace shm
} // namespace shm } // namespace ipc
} // namespace ipc

View File

@ -8,7 +8,9 @@
#include "libipc/platform/win/condition.h" #include "libipc/platform/win/condition.h"
#elif defined(IPC_OS_LINUX_) #elif defined(IPC_OS_LINUX_)
#include "libipc/platform/linux/condition.h" #include "libipc/platform/linux/condition.h"
#else/*linux*/ #elif defined(IPC_OS_QNX_)
#include "libipc/platform/posix/condition.h"
#else/*IPC_OS*/
# error "Unsupported platform." # error "Unsupported platform."
#endif #endif

View File

@ -8,7 +8,9 @@
#include "libipc/platform/win/mutex.h" #include "libipc/platform/win/mutex.h"
#elif defined(IPC_OS_LINUX_) #elif defined(IPC_OS_LINUX_)
#include "libipc/platform/linux/mutex.h" #include "libipc/platform/linux/mutex.h"
#else/*linux*/ #elif defined(IPC_OS_QNX_)
#include "libipc/platform/posix/mutex.h"
#else/*IPC_OS*/
# error "Unsupported platform." # error "Unsupported platform."
#endif #endif

View File

@ -6,9 +6,9 @@
#include "libipc/platform/detail.h" #include "libipc/platform/detail.h"
#if defined(IPC_OS_WINDOWS_) #if defined(IPC_OS_WINDOWS_)
#include "libipc/platform/win/semaphore.h" #include "libipc/platform/win/semaphore.h"
#elif defined(IPC_OS_LINUX_) #elif defined(IPC_OS_LINUX_) || defined(IPC_OS_QNX_)
#include "libipc/platform/linux/semaphore_impl.h" #include "libipc/platform/posix/semaphore_impl.h"
#else/*linux*/ #else/*IPC_OS*/
# error "Unsupported platform." # error "Unsupported platform."
#endif #endif

View File

@ -1,8 +1,9 @@
#pragma once #pragma once
#include <utility> // std::forward, std::integer_sequence #include <utility> // std::forward, std::integer_sequence
#include <cstddef> // std::size_t #include <cstddef> // std::size_t
#include <new> // std::hardware_destructive_interference_size #include <new> // std::hardware_destructive_interference_size
#include <type_traits> // std::is_trivially_copyable
#include "libipc/platform/detail.h" #include "libipc/platform/detail.h"
@ -44,13 +45,15 @@ enum {
}; };
template <typename T, typename U> template <typename T, typename U>
T horrible_cast(U val) { auto horrible_cast(U rhs) noexcept
-> typename std::enable_if<std::is_trivially_copyable<T>::value
&& std::is_trivially_copyable<U>::value, T>::type {
union { union {
T out; T t;
U in; U u;
} u; } r = {};
u.in = val; r.u = rhs;
return u.out; return r.t;
} }
IPC_CONSTEXPR_ std::size_t make_align(std::size_t align, std::size_t size) { IPC_CONSTEXPR_ std::size_t make_align(std::size_t align, std::size_t size) {

View File

@ -153,16 +153,16 @@ void test_sr(char const * name, int s_cnt, int r_cnt) {
TEST(IPC, basic) { TEST(IPC, basic) {
test_basic<relat::single, relat::single, trans::unicast >("ssu"); test_basic<relat::single, relat::single, trans::unicast >("ssu");
test_basic<relat::single, relat::multi , trans::unicast >("smu"); //test_basic<relat::single, relat::multi , trans::unicast >("smu");
test_basic<relat::multi , relat::multi , trans::unicast >("mmu"); //test_basic<relat::multi , relat::multi , trans::unicast >("mmu");
test_basic<relat::single, relat::multi , trans::broadcast>("smb"); test_basic<relat::single, relat::multi , trans::broadcast>("smb");
test_basic<relat::multi , relat::multi , trans::broadcast>("mmb"); test_basic<relat::multi , relat::multi , trans::broadcast>("mmb");
} }
TEST(IPC, 1v1) { TEST(IPC, 1v1) {
test_sr<relat::single, relat::single, trans::unicast >("ssu", 1, 1); test_sr<relat::single, relat::single, trans::unicast >("ssu", 1, 1);
test_sr<relat::single, relat::multi , trans::unicast >("smu", 1, 1); //test_sr<relat::single, relat::multi , trans::unicast >("smu", 1, 1);
test_sr<relat::multi , relat::multi , trans::unicast >("mmu", 1, 1); //test_sr<relat::multi , relat::multi , trans::unicast >("mmu", 1, 1);
test_sr<relat::single, relat::multi , trans::broadcast>("smb", 1, 1); test_sr<relat::single, relat::multi , trans::broadcast>("smb", 1, 1);
test_sr<relat::multi , relat::multi , trans::broadcast>("mmb", 1, 1); test_sr<relat::multi , relat::multi , trans::broadcast>("mmb", 1, 1);
} }