msleep(1)

This commit is contained in:
mutouyun 2019-01-05 14:35:25 +08:00
parent 2fbfa8dd0b
commit ea52920b7d
3 changed files with 22 additions and 11 deletions

View File

@ -7,6 +7,7 @@
#include <atomic> #include <atomic>
#include <tuple> #include <tuple>
#include <thread> #include <thread>
#include <chrono>
#include "def.h" #include "def.h"
#include "circ_elem_array.h" #include "circ_elem_array.h"
@ -95,7 +96,7 @@ public:
template <typename F> template <typename F>
static queue* multi_wait_for(F&& upd) noexcept { static queue* multi_wait_for(F&& upd) noexcept {
while (1) { for (uint_t<32> k = 0;;) {
auto [ques, size] = upd(); auto [ques, size] = upd();
for (std::size_t i = 0; i < static_cast<std::size_t>(size); ++i) { for (std::size_t i = 0; i < static_cast<std::size_t>(size); ++i) {
queue* que = ques[i]; queue* que = ques[i];
@ -105,7 +106,11 @@ public:
return que; return que;
} }
} }
std::this_thread::yield(); if (k < 4096) {
std::this_thread::yield();
++k;
}
else std::this_thread::sleep_for(std::chrono::milliseconds(1));
} }
} }

View File

@ -2,6 +2,7 @@
#include <atomic> #include <atomic>
#include <thread> #include <thread>
#include <chrono>
#include <limits> #include <limits>
#include <type_traits> #include <type_traits>
@ -56,12 +57,17 @@
namespace ipc { namespace ipc {
inline void yield(unsigned k) noexcept { inline void yield(unsigned& k) noexcept {
if (k < 4) { /* Do nothing */ } if (k < 4) { /* Do nothing */ }
else else
if (k < 16) { IPC_LOCK_PAUSE_(); } if (k < 16) { IPC_LOCK_PAUSE_(); }
else else
{ std::this_thread::yield(); } if (k < 32) { std::this_thread::yield(); }
else {
std::this_thread::sleep_for(std::chrono::milliseconds(1));
return;
}
++k;
} }
} // namespace ipc } // namespace ipc
@ -88,14 +94,14 @@ public:
rw_lock& operator=(rw_lock&&) = delete; rw_lock& operator=(rw_lock&&) = delete;
void lock() noexcept { void lock() noexcept {
for (unsigned k = 0;; ++k) { for (unsigned k = 0;;) {
auto old = lc_.fetch_or(w_flag, std::memory_order_acquire); auto old = lc_.fetch_or(w_flag, std::memory_order_acquire);
if (!old) return; // got w-lock if (!old) return; // got w-lock
if (!(old & w_flag)) break; // other thread having r-lock if (!(old & w_flag)) break; // other thread having r-lock
yield(k); // other thread having w-lock yield(k); // other thread having w-lock
} }
// wait for reading finished // wait for reading finished
for (unsigned k = 0; lc_.load(std::memory_order_acquire) & w_mask; ++k) { for (unsigned k = 0; lc_.load(std::memory_order_acquire) & w_mask;) {
yield(k); yield(k);
} }
} }
@ -106,7 +112,7 @@ public:
void lock_shared() noexcept { void lock_shared() noexcept {
auto old = lc_.load(std::memory_order_relaxed); auto old = lc_.load(std::memory_order_relaxed);
for (unsigned k = 0;; ++k) { for (unsigned k = 0;;) {
// if w_flag set, just continue // if w_flag set, just continue
if (old & w_flag) { if (old & w_flag) {
yield(k); yield(k);

View File

@ -314,10 +314,10 @@ void test_lock_performance() {
} }
void Unit::test_rw_lock() { void Unit::test_rw_lock() {
// test_lock_performance<1, 1>(); test_lock_performance<1, 1>();
// test_lock_performance<4, 4>(); test_lock_performance<4, 4>();
// test_lock_performance<1, 8>(); test_lock_performance<1, 8>();
// test_lock_performance<8, 1>(); test_lock_performance<8, 1>();
} }
void Unit::test_send_recv() { void Unit::test_send_recv() {