8000 Develop by mutouyun · Pull Request #63 · mutouyun/cpp-ipc · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

Develop #63

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 48 commits into from
Sep 21, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
48 commits
Select commit Hold shift + click to select a range
99f281c
update IPC_CONCEPT_
mutouyun Nov 28, 2020
321dc45
Merge pull request #26 from mutouyun/master
mutouyun Jan 3, 2021
dbd31e1
Merge pull request #29 from mutouyun/master
mutouyun Jan 11, 2021
1e5547e
update IPC_CONCEPT_
mutouyun Nov 28, 2020
c04e726
Merge branch 'develop' of https://github.com/mutouyun/cpp-ipc into de…
mutouyun Jun 6, 2021
55e75d4
add profiler from adah1972
mutouyun Jan 1, 2021
54bc338
test/profiler
mutouyun Jun 6, 2021
ff488e0
Revert "update IPC_CONCEPT_"
mutouyun Jun 6, 2021
e20dd7d
add sync::mutex for windows/linux
mutouyun Jun 20, 2021
e5bf3b7
for vs2015 error C3256
mutouyun Jun 20, 2021
3cf7d5b
using IPC_OS_* in test
mutouyun Jun 20, 2021
8cd2a40
update IPC_CONCEPT_
mutouyun Nov 28, 2020
563aabf
add profiler from adah1972
mutouyun Jan 1, 2021
85342dc
test/profiler
mutouyun Jun 6, 2021
1294450
Revert "update IPC_CONCEPT_"
mutouyun Jun 6, 2021
455c0b4
add sync::mutex for windows/linux
mutouyun Jun 20, 2021
d974641
for vs2015 error C3256
mutouyun Jun 20, 2021
a970ace
using IPC_OS_* in test
mutouyun Jun 20, 2021
0e0bbd7
remove .travis.yml
mutouyun Jun 20, 2021
2d0948c
Merge branch 'develop' of github.com:mutouyun/cpp-ipc into develop
mutouyun Jun 20, 2021
40eafcf
fix errors
mutouyun Jun 20, 2021
dd29ed5
fix errors
mutouyun Jun 20, 2021
d74f4c5
fix: mutex() noexcept is implicitly deleted
mutouyun Jun 20, 2021
78be14b
Merge branch 'develop' into master
mutouyun Aug 29, 2021
754661c
Merge pull request #59 from mutouyun/master
mutouyun Aug 29, 2021
1dc0419
ignore invalid id print in get_ref
mutouyun Aug 29, 2021
415be36
ipc::sync::mutex for linux
mutouyun Sep 11, 2021
d0e2a4d
add semaphore for win
mutouyun Sep 12, 2021
d37a674
add ut for sync::semaphore
mutouyun Sep 12, 2021
cd4b283
Update semaphore_linux.h
mutouyun Sep 12, 2021
1994243
fix: semaphore() noexcept is implicitly deleted
mutouyun Sep 12, 2021
ca9c5d1
ut for sem-linux
mutouyun Sep 12, 2021
baf645e
修正 recv timeout 接口cpu占用过高的问题
mutouyun Sep 16, 2021
91385d7
修正recv中断后counter无法下降的问题;添加新的示例
mutouyun Sep 17, 2021
8437704
避免wait_if的counter因为ABA问题导致计数错误
mutouyun Sep 17, 2021
94ad05c
调整ut
mutouyun Sep 17, 2021
68590dd
commit new demo
mutouyun Sep 19, 2021
be6f16f
revert some changes
mutouyun Sep 19, 2021
4ca300b
Merge branch 'issue-61' into develop
mutouyun Sep 19, 2021
0cccdac
merge issue-61; add condition for linux
mutouyun Sep 19, 2021
c1ceaa6
实现condition_win
mutouyun Sep 19, 2021
04fda1c
use sync to refactor waiter
mutouyun Sep 20, 2021
a9cb81b
missing file
mutouyun Sep 20, 2021
ed8b1fd
fix some bugs for linux-mutex
mutouyun Sep 20, 2021
b8f5e2b
validate close in waiter
mutouyun Sep 20, 2021
7a536b6
impl quit_waiting
mutouyun Sep 20, 2021
f6bd578
reduce the number of recheck times for the sleep function
mutouyun Sep 20, 2021
a457a89
using 'signal' to quit waiting explicitly
mutouyun Sep 21, 2021
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 0 additions & 31 deletions .travis.yml

This file was deleted.

17 changes: 13 additions & 4 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ if(NOT MSVC)
set(CMAKE_CXX_FLAGS_RELEASE "${CMAKE_CXX_FLAGS_RELEASE} -O2")
endif()

if (MSVC AND LIBIPC_USE_STATIC_CRT)
if (MSVC)
set(CompilerFlags
CMAKE_CXX_FLAGS
CMAKE_CXX_FLAGS_DEBUG
Expand All @@ -22,9 +22,17 @@ if (MSVC AND LIBIPC_USE_STATIC_CRT)
CMAKE_C_FLAGS_DEBUG
CMAKE_C_FLAGS_RELEASE
)
foreach(CompilerFlag ${CompilerFlags})
string(REPLACE "/MD" "/MT" ${CompilerFlag} "${${CompilerFlag}}")
endforeach()
if (LIBIPC_USE_STATIC_CRT)
foreach(CompilerFlag ${CompilerFlags})
string(REPLACE "/MD" "/MT" ${CompilerFlag} "${${CompilerFlag}}")
string(REPLACE "/MDd" "/MTd" ${CompilerFlag} "${${CompilerFlag}}")
endforeach()
else()
foreach(CompilerFlag ${CompilerFlags})
string(REPLACE "/MT" "/MD" ${CompilerFlag} "${${CompilerFlag}}")
string(REPLACE "/MTd" "/MDd" ${CompilerFlag} "${${CompilerFlag}}")
endforeach()
endif()
endif()

set(LIBRARY_OUTPUT_PATH ${CMAKE_BINARY_DIR}/bin)
Expand All @@ -50,6 +58,7 @@ endif()
if (LIBIPC_BUILD_DEMOS)
add_subdirectory(demo/chat)
add_subdirectory(demo/msg_que)
add_subdirectory(demo/send_recv)
endif()

install(
Expand Down
10 changes: 5 additions & 5 deletions demo/msg_que/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@ constexpr char const mode_r__[] = "r";
constexpr std::size_t const min_sz = 128;
constexpr std::size_t const max_sz = 1024 * 16;

std::atomic<bool> is_quit__{ false };
std::atomic<std::size_t> size_counter__{ 0 };
std::atomic<bool> is_quit__ {false};
std::atomic<std::size_t> size_counter__ {0};

using msg_que_t = ipc::chan<ipc::relat::single, ipc::relat::multi, ipc::trans::broadcast>;

Expand Down Expand Up @@ -127,10 +127,10 @@ int main(int argc, char ** argv) {
::signal(SIGHUP , exit);
#endif

if (std::string{ argv[1] } == mode_s__) {
std::string mode {argv[1]};
if (mode == mode_s__) {
do_send();
}
else if (std::string{ argv[1] } == mode_r__) {
} else if (mode == mode_r__) {
do_recv();
}
return 0;
Expand Down
11 changes: 11 additions & 0 deletions demo/send_recv/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
project(send_recv)

include_directories(
${LIBIPC_PROJECT_DIR}/3rdparty)

file(GLOB SRC_FILES ./*.cpp)
file(GLOB HEAD_FILES ./*.h)

add_executable(${PROJECT_NAME} ${SRC_FILES} ${HEAD_FILES})

target_link_libraries(${PROJECT_NAME} ipc)
72 changes: 72 additions & 0 deletions demo/send_recv/main.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@

#include <signal.h>

#include <iostream>
#include <string>
#include <thread>
#include <chrono>
#include <atomic>

#include "libipc/ipc.h"

namespace {

std::atomic<bool> is_quit__ {false};
ipc::channel *ipc__ = nullptr;

void do_send(int size, int interval) {
ipc::channel ipc {"ipc", ipc::sender};
ipc__ = &ipc;
std::string buffer(size, 'A');
while (!is_quit__.load(std::memory_order_acquire)) {
std::cout << "send size: " << buffer.size() + 1 << "\n";
ipc.send(buffer, 0/*tm*/);
std::this_thread::sleep_for(std::chrono::milliseconds(interval));
}
}

void do_recv(int interval) {
ipc::channel ipc {"ipc", ipc::receiver};
ipc__ = &ipc;
while (!is_quit__.load(std::memory_order_acquire)) {
ipc::buff_t recv;
for (int k = 1; recv.empty(); ++k) {
std::cout << "recv waiting... " << k << "\n";
recv = ipc.recv(interval);
if (is_quit__.load(std::memory_order_acquire)) return;
}
std::cout << "recv size: " << recv.size() << "\n";
}
}

} // namespace

int main(int argc, char ** argv) {
if (argc < 3) return -1;

auto exit = [](int) {
is_quit__.store(true, std::memory_order_release);
if (ipc__ != nullptr) ipc__->disconnect();
};
::signal(SIGINT , exit);
::signal(SIGABRT , exit);
::signal(SIGSEGV , exit);
::signal(SIGTERM , exit);
#if defined(WIN64) || defined(_WIN64) || defined(__WIN64__) || \
defined(WIN32) || defined(_WIN32) || defined(__WIN32__) || defined(__NT__) || \
defined(WINCE) || defined(_WIN32_WCE)
::signal(SIGBREAK, exit);
#else
::signal(SIGHUP , exit);
#endif

std::string mode {argv[1]};
if (mode == "send") {
if (argc < 4) return -1;
do_send(std::stoi(argv[2]) /*size*/,
std::stoi(argv[3]) /*interval*/);
} else if (mode == "recv") {
do_recv(std::stoi(argv[2]) /*interval*/);
}
return 0;
}
39 changes: 39 additions & 0 deletions include/libipc/condition.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
#pragma once

#include <cstdint> // std::uint64_t

#include "libipc/export.h"
#include "libipc/def.h"
#include "libipc/mutex.h"

namespace ipc {
namespace sync {

class IPC_EXPORT condition {
condition(condition const &) = delete;
condition &operator=(condition const &) = delete;

public:
condition();
explicit condition(char const *name);
~condition();

void const *native() const noexcept;
void *native() noexcept;

bool valid() const noexcept;

bool open(char const *name) noexcept;
void close() noexcept;

bool wait(ipc::sync::mutex &mtx, std::uint64_t tm = ipc::invalid_value) noexcept;
bool notify() noexcept;
bool broadcast() noexcept;

private:
class condition_;
condition_* p_;
};

} // namespace sync
} // namespace ipc
7 changes: 5 additions & 2 deletions include/libipc/def.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,16 @@ using uint_t = typename uint<N>::type;

// constants

enum : std::uint32_t {
invalid_value = (std::numeric_limits<std::uint32_t>::max)(),
default_timeout = 100, // ms
};

enum : std::size_t {
invalid_value = (std::numeric_limits<std::size_t>::max)(),
data_length = 64,
large_msg_limit = data_length,
large_msg_align = 1024,
large_msg_cache = 32,
default_timeout = 100 // ms
};

enum class relat { // multiplicity of the relationship
Expand Down
26 changes: 13 additions & 13 deletions include/libipc/ipc.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,12 @@ struct IPC_EXPORT chan_impl {
static char const * name(ipc::handle_t h);

static std::size_t recv_count(ipc::handle_t h);
static bool wait_for_recv(ipc::handle_t h, std::size_t r_count, std::size_t tm);
static bool wait_for_recv(ipc::handle_t h, std::size_t r_count, std::uint64_t tm);

static bool send(ipc::handle_t h, void const * data, std::size_t size, std::size_t tm);
static buff_t recv(ipc::handle_t h, std::size_t tm);
static bool send(ipc::handle_t h, void const * data, std::size_t size, std::uint64_t tm);
static buff_t recv(ipc::handle_t h, std::uint64_t tm);

static bool try_send(ipc::handle_t h, void const * data, std::size_t size, std::size_t tm);
static bool try_send(ipc::handle_t h, void const * data, std::size_t size, std::uint64_t tm);
static buff_t try_recv(ipc::handle_t h);
};

Expand Down Expand Up @@ -120,41 +120,41 @@ class chan_wrapper {
return detail_t::recv_count(h_);
}

bool wait_for_recv(std::size_t r_count, std::size_t tm = invalid_value) const {
bool wait_for_recv(std::size_t r_count, std::uint64_t tm = invalid_value) const {
return detail_t::wait_for_recv(h_, r_count, tm);
}

static bool wait_for_recv(char const * name, std::size_t r_count, std::size_t tm = invalid_value) {
static bool wait_for_recv(char const * name, std::size_t r_count, std::uint64_t tm = invalid_value) {
return chan_wrapper(name).wait_for_recv(r_count, tm);
}

/**
* If timeout, this function would call 'force_push' to send the data forcibly.
*/
bool send(void const * data, std::size_t size, std::size_t tm = default_timeout) {
bool send(void const * data, std::size_t size, std::uint64_t tm = default_timeout) {
return detail_t::send(h_, data, size, tm);
}
bool send(buff_t const & buff, std::size_t tm = default_timeout) {
bool send(buff_t const & buff, std::uint64_t tm = default_timeout) {
return this->send(buff.data(), buff.size(), tm);
}
bool send(std::string const & str, std::size_t tm = default_timeout) {
bool send(std::string const & str, std::uint64_t tm = default_timeout) {
return this->send(str.c_str(), str.size() + 1, tm);
}

/**
* If timeout, this function would just return false.
*/
bool try_send(void const * data, std::size_t size, std::size_t tm = default_timeout) {
bool try_send(void const * data, std::size_t size, std::uint64_t tm = default_timeout) {
return detail_t::try_send(h_, data, size, tm);
}
bool try_send(buff_t const & buff, std::size_t tm = default_timeout) {
bool try_send(buff_t const & buff, std::uint64_t tm = default_timeout) {
return this->try_send(buff.data(), buff.size(), tm);
}
bool try_send(std::string const & str, std::size_t tm = default_timeout) {
bool try_send(std::string const & str, std::uint64_t tm = default_timeout) {
return this->try_send(str.c_str(), str.size() + 1, tm);
}

buff_t recv(std::size_t tm = invalid_value) {
buff_t recv(std::uint64_t tm = invalid_value) {
return detail_t::recv(h_, tm);
}

Expand Down
39 changes: 39 additions & 0 deletions include/libipc/mutex.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
#pragma once

#include <cstdint> // std::uint64_t
#include <system_error>

#include "libipc/export.h"
#include "libipc/def.h"

namespace ipc {
namespace sync {

class IPC_EXPORT mutex {
mutex(mutex const &) = delete;
mutex &operator=(mutex const &) = delete;

public:
mutex();
explicit mutex(char const *name);
~mutex();

void const *native() const noexcept;
void *native() noexcept;

bool valid() const noexcept;

bool open(char const *name) noexcept;
void close() noexcept;

bool lock(std::uint64_t tm = ipc::invalid_value) noexcept;
bool try_lock() noexcept(false); // std::system_error
bool unlock() noexcept;

private:
class mutex_;
mutex_* p_;
};

} // namespace sync
} // namespace ipc
4 changes: 2 additions & 2 deletions include/libipc/rw_lock.h
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ inline void yield(K& k) noexcept {
++k;
}

template <std::size_t N = 4096, typename K, typename F>
template <std::size_t N = 32, typename K, typename F>
inline void sleep(K& k, F&& f) {
if (k < static_cast<K>(N)) {
std::this_thread::yield();
Expand All @@ -84,7 +84,7 @@ inline void sleep(K& k, F&& f) {
++k;
}

template <std::size_t N = 4096, typename K>
template <std::size_t N = 32, typename K>
inline void sleep(K& k) {
sleep<N>(k, [] {
std::this_thread::sleep_for(std::chrono::milliseconds(1));
Expand Down
Loading
0