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 <tuple>
#include <thread>
#include <chrono>
#include "def.h"
#include "circ_elem_array.h"
@ -95,7 +96,7 @@ public:
template <typename F>
static queue* multi_wait_for(F&& upd) noexcept {
while (1) {
for (uint_t<32> k = 0;;) {
auto [ques, size] = upd();
for (std::size_t i = 0; i < static_cast<std::size_t>(size); ++i) {
queue* que = ques[i];
@ -105,7 +106,11 @@ public:
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 <thread>
#include <chrono>
#include <limits>
#include <type_traits>
@ -56,12 +57,17 @@
namespace ipc {
inline void yield(unsigned k) noexcept {
inline void yield(unsigned& k) noexcept {
if (k < 4) { /* Do nothing */ }
else
if (k < 16) { IPC_LOCK_PAUSE_(); }
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
@ -88,14 +94,14 @@ public:
rw_lock& operator=(rw_lock&&) = delete;
void lock() noexcept {
for (unsigned k = 0;; ++k) {
for (unsigned k = 0;;) {
auto old = lc_.fetch_or(w_flag, std::memory_order_acquire);
if (!old) return; // got w-lock
if (!(old & w_flag)) break; // other thread having r-lock
yield(k); // other thread having w-lock
}
// 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);
}
}
@ -106,7 +112,7 @@ public:
void lock_shared() noexcept {
auto old = lc_.load(std::memory_order_relaxed);
for (unsigned k = 0;; ++k) {
for (unsigned k = 0;;) {
// if w_flag set, just continue
if (old & w_flag) {
yield(k);

View File

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