From 7b78deccf2394a759ccefe1ce72b91712b6a4247 Mon Sep 17 00:00:00 2001 From: Bob Chen Date: Fri, 15 Dec 2023 19:27:06 +0800 Subject: [PATCH 1/3] minor fix for std-compat --- thread/std-compat.h | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/thread/std-compat.h b/thread/std-compat.h index 91c56b5d..a71a3b18 100644 --- a/thread/std-compat.h +++ b/thread/std-compat.h @@ -222,9 +222,10 @@ class unique_lock { } Mutex* release() noexcept { + auto* mu = m_mutex; m_mutex = nullptr; m_owns = false; - return m_mutex; + return mu; } Mutex* mutex() const noexcept { From 70570eed35f1c24a20634696edd5b4bfa0a74b47 Mon Sep 17 00:00:00 2001 From: Bob Chen Date: Fri, 15 Dec 2023 19:37:01 +0800 Subject: [PATCH 2/3] rpc::string add empty method --- rpc/serialize.h | 1 + 1 file changed, 1 insertion(+) diff --git a/rpc/serialize.h b/rpc/serialize.h index 0aa327bd..d0ee6c46 100644 --- a/rpc/serialize.h +++ b/rpc/serialize.h @@ -105,6 +105,7 @@ namespace rpc const char* c_str() const { return cbegin(); } std::string_view sv() const { return {c_str(), size() - 1}; } std::string to_std() { return std::string(sv()); }; + bool empty() const { return _len == 0; } bool operator==(const string& rhs) const { return sv() == rhs.sv(); } bool operator!=(const string& rhs) const { return !(*this == rhs); } bool operator<(const string& rhs) const { return sv() < rhs.sv(); } From 6e4355a078e724b22b28ea2b499ad8a40b773c39 Mon Sep 17 00:00:00 2001 From: Bob Chen Date: Sat, 16 Dec 2023 17:40:48 +0800 Subject: [PATCH 3/3] set io_uring eventfd non-blocking --- examples/sync-primitive/sync-primitive.cpp | 7 +++---- io/iouring-wrapper.cpp | 8 +++----- rpc/serialize.h | 2 +- 3 files changed, 7 insertions(+), 10 deletions(-) diff --git a/examples/sync-primitive/sync-primitive.cpp b/examples/sync-primitive/sync-primitive.cpp index 61977b9a..173e8ea4 100644 --- a/examples/sync-primitive/sync-primitive.cpp +++ b/examples/sync-primitive/sync-primitive.cpp @@ -43,7 +43,7 @@ struct Message { std::chrono::time_point start; }; -static const int num_producers = 16, num_consumers = 16; +static const int num_producers = 8, num_consumers = 8; static LockfreeMPMCRingQueue ring; static std::atomic qps{0}, latency{0}; @@ -90,7 +90,7 @@ int main() { Message message; ring.push(&message); { - std::unique_lock l(message.mu); + std::unique_lock l(message.mu); message.cv.wait(l, [&] { return message.done; }); } auto end = std::chrono::steady_clock::now(); @@ -117,7 +117,7 @@ int main() { m->sem.signal(1); #else { - std::unique_lock l(m->mu); + std::unique_lock l(m->mu); m->done = true; m->cv.notify_one(); } @@ -128,7 +128,6 @@ int main() { // Show QPS and latency photon::thread_create11([&] { - photon::init(photon::INIT_EVENT_DEFAULT, photon::INIT_IO_NONE); while (true) { photon::thread_sleep(1); auto prev_qps = qps.exchange(0, std::memory_order_seq_cst); diff --git a/io/iouring-wrapper.cpp b/io/iouring-wrapper.cpp index d0e29059..77579daa 100644 --- a/io/iouring-wrapper.cpp +++ b/io/iouring-wrapper.cpp @@ -133,7 +133,7 @@ class iouringEngine : public MasterEventEngine, public CascadingEventEngine, pub } } - m_eventfd = eventfd(0, EFD_CLOEXEC); + m_eventfd = eventfd(0, EFD_CLOEXEC | EFD_NONBLOCK); if (m_eventfd < 0) { LOG_ERRNO_RETURN(0, -1, "iouring: failed to create eventfd"); } @@ -285,10 +285,8 @@ class iouringEngine : public MasterEventEngine, public CascadingEventEngine, pub if (ret < 0) { return errno == ETIMEDOUT ? 0 : -1; } - uint64_t value = 0; - if (eventfd_read(m_eventfd, &value)) { - LOG_ERROR("iouring: error reading cascading event fd, `", ERRNO()); - } + uint64_t value; + eventfd_read(m_eventfd, &value); // Reap events size_t num = 0; diff --git a/rpc/serialize.h b/rpc/serialize.h index d0ee6c46..df5763db 100644 --- a/rpc/serialize.h +++ b/rpc/serialize.h @@ -71,6 +71,7 @@ namespace rpc const T& operator[](long i) const { return ((T*)_ptr)[i]; } const T& front() const { return (*this)[0]; } const T& back() const { return (*this)[(long)size() - 1]; } + bool empty() const { return _len == 0; } void assign(const T* x, size_t size) { buffer::assign(x, sizeof(*x) * size); } void assign(const std::vector& vec) { assign(&vec[0], vec.size()); } @@ -105,7 +106,6 @@ namespace rpc const char* c_str() const { return cbegin(); } std::string_view sv() const { return {c_str(), size() - 1}; } std::string to_std() { return std::string(sv()); }; - bool empty() const { return _len == 0; } bool operator==(const string& rhs) const { return sv() == rhs.sv(); } bool operator!=(const string& rhs) const { return !(*this == rhs); } bool operator<(const string& rhs) const { return sv() < rhs.sv(); }