ReleaseSemaphore mayn't wake WaitForSingleObject up soon

This commit is contained in:
mutouyun 2019-02-12 23:28:57 +08:00
parent 4262f7bd34
commit a6d88a1208
3 changed files with 29 additions and 21 deletions

View File

@ -8,6 +8,7 @@
#include <atomic> #include <atomic>
#include "def.h" #include "def.h"
#include "export.h"
// pre-defined // pre-defined
@ -82,7 +83,7 @@ constexpr auto shared_lock(T&& lc) {
#endif/*__cplusplus < 201703L*/ #endif/*__cplusplus < 201703L*/
std::size_t calc_unique_id(); IPC_EXPORT std::size_t calc_unique_id();
template <typename F, typename D> template <typename F, typename D>
constexpr decltype(auto) static_switch(std::size_t /*i*/, std::index_sequence<>, F&& /*f*/, D&& def) { constexpr decltype(auto) static_switch(std::size_t /*i*/, std::index_sequence<>, F&& /*f*/, D&& def) {

View File

@ -2,11 +2,7 @@
#include <Windows.h> #include <Windows.h>
#include <algorithm>
#include <iterator>
#include <atomic> #include <atomic>
#include <array>
#include <tuple>
#include "rw_lock.h" #include "rw_lock.h"
#include "pool_alloc.h" #include "pool_alloc.h"
@ -51,19 +47,25 @@ public:
void notify(handle_t h) { void notify(handle_t h) {
if (h == invalid()) return; if (h == invalid()) return;
IPC_UNUSED_ auto guard = ipc::detail::unique_lock(lock_); {
if (counter_ == 0) return; IPC_UNUSED_ auto guard = ipc::detail::unique_lock(lock_);
-- counter_; if (counter_ == 0) return;
::ReleaseSemaphore(h, 1, NULL); -- counter_;
::ReleaseSemaphore(h, 1, NULL);
}
::Sleep(1);
} }
void broadcast(handle_t h) { void broadcast(handle_t h) {
if (h == invalid()) return; if (h == invalid()) return;
IPC_UNUSED_ auto guard = ipc::detail::unique_lock(lock_); {
if (counter_ == 0) return; IPC_UNUSED_ auto guard = ipc::detail::unique_lock(lock_);
long all_count = counter_; if (counter_ == 0) return;
counter_ = 0; long all_count = counter_;
::ReleaseSemaphore(h, all_count, NULL); counter_ = 0;
::ReleaseSemaphore(h, all_count, NULL);
}
::Sleep(1);
} }
}; };

View File

@ -368,7 +368,7 @@ void Unit::test_route_rtt() {
for (std::size_t i = 0;; ++i) { for (std::size_t i = 0;; ++i) {
auto dd = cc.recv(); auto dd = cc.recv();
if (dd.size() < 2) return; if (dd.size() < 2) return;
// std::cout << "recving: " << i << "-[" << dd.size() << "]" << std::endl; //std::cout << "recv: " << i << "-[" << dd.size() << "]" << std::endl;
while (!cr.send(ipc::buff_t('a'))) { while (!cr.send(ipc::buff_t('a'))) {
std::this_thread::yield(); std::this_thread::yield();
} }
@ -383,8 +383,8 @@ void Unit::test_route_rtt() {
} }
sw.start(); sw.start();
for (std::size_t i = 0; i < LoopCount; ++i) { for (std::size_t i = 0; i < LoopCount; ++i) {
// std::cout << "sending: " << i << "-[" << datas__[i].size() << "]" << std::endl;
cc.send(datas__[i]); cc.send(datas__[i]);
//std::cout << "sent: " << i << "-[" << datas__[i].size() << "]" << std::endl;
/*auto dd = */cr.recv(); /*auto dd = */cr.recv();
// if (dd.size() != 1 || dd[0] != 'a') { // if (dd.size() != 1 || dd[0] != 'a') {
// QVERIFY(false); // QVERIFY(false);
@ -434,14 +434,17 @@ void Unit::test_channel_rtt() {
std::thread t1 {[&] { std::thread t1 {[&] {
ipc::channel cc { "my-ipc-channel" }; ipc::channel cc { "my-ipc-channel" };
bool recv_2 = false;
for (std::size_t i = 0;; ++i) { for (std::size_t i = 0;; ++i) {
auto dd = cc.recv(); auto dd = cc.recv();
if (dd.size() < 2) return; if (dd.size() < 2) return;
//std::cout << "recving: " << i << "-[" << dd.size() << "]" << std::endl; //if (i % 1000 == 0) {
while (!cc.send(ipc::buff_t('a'))) { // std::cout << "recv: " << i << "-[" << dd.size() << "]" << std::endl;
cc.wait_for_recv(1); //}
while (!recv_2) {
recv_2 = cc.wait_for_recv(2);
} }
//std::cout << "sent ack." << std::endl; cc.send(ipc::buff_t('a'));
} }
}}; }};
@ -450,7 +453,9 @@ void Unit::test_channel_rtt() {
cc.wait_for_recv(1); cc.wait_for_recv(1);
sw.start(); sw.start();
for (std::size_t i = 0; i < LoopCount; ++i) { for (std::size_t i = 0; i < LoopCount; ++i) {
//std::cout << "sending: " << i << "-[" << datas__[i].size() << "]" << std::endl; //if (i % 1000 == 0) {
// std::cout << "send: " << i << "-[" << datas__[i].size() << "]" << std::endl;
//}
cc.send(datas__[i]); cc.send(datas__[i]);
/*auto dd = */cc.recv(); /*auto dd = */cc.recv();
//if (dd.size() != 1 || dd.data<char>()[0] != 'a') { //if (dd.size() != 1 || dd.data<char>()[0] != 'a') {