clear codes

This commit is contained in:
mutouyun 2019-02-12 13:46:43 +08:00
parent f7f06ab052
commit 1cf69038bb
4 changed files with 24 additions and 59 deletions

View File

@ -2,18 +2,8 @@
#include <pthread.h>
#include <cstring>
#include <atomic>
#include <string>
#include <utility>
#include <tuple>
#include "def.h"
#include "log.h"
#include "shm.h"
#include "rw_lock.h"
#include "id_pool.h"
#include "pool_alloc.h"
#include "platform/detail.h"
@ -117,7 +107,7 @@ public:
class semaphore {
mutex lock_;
condition cond_;
long counter_ = 0;
long volatile counter_ = 0;
public:
bool open() {

View File

@ -18,7 +18,8 @@ namespace ipc {
namespace detail {
class waiter {
std::atomic<long> counter_ { 0 };
long volatile counter_ = 0;
spin_lock lock_;
public:
using handle_t = HANDLE;
@ -37,51 +38,32 @@ public:
::CloseHandle(h);
}
template <typename F>
static bool multi_wait_if(std::tuple<waiter*, handle_t> const * all, std::size_t size, F&& pred) {
if (all == nullptr || size == 0) {
return false;
}
if (!std::forward<F>(pred)()) return true;
auto hs = static_cast<handle_t*>(mem::alloc(sizeof(handle_t) * size));
IPC_UNUSED_ auto guard = unique_ptr(hs, [size](void* p) { mem::free(p, sizeof(handle_t) * size); });
std::size_t i = 0;
for (; i < size; ++i) {
auto& info = all[i];
if ((std::get<0>(all[i]) == nullptr) ||
(std::get<1>(all[i]) == invalid())) continue;
std::get<0>(info)->counter_.fetch_add(1, std::memory_order_relaxed);
hs[i] = std::get<1>(all[i]);
}
std::atomic_thread_fence(std::memory_order_release);
return ::WaitForMultipleObjects(static_cast<DWORD>(i), hs, FALSE, INFINITE) != WAIT_FAILED;
}
template <typename F>
bool wait_if(handle_t h, F&& pred) {
if (h == invalid()) return false;
{
IPC_UNUSED_ auto guard = ipc::detail::unique_lock(lock_);
if (!std::forward<F>(pred)()) return true;
counter_.fetch_add(1, std::memory_order_relaxed);
std::atomic_thread_fence(std::memory_order_release);
++ counter_;
}
return ::WaitForSingleObject(h, INFINITE) == WAIT_OBJECT_0;
}
void notify(handle_t h) {
if (h == invalid()) return;
for (unsigned k = 0;;) {
auto c = counter_.load(std::memory_order_acquire);
if (c == 0) return;
if (counter_.compare_exchange_weak(c, c - 1, std::memory_order_release)) {
break;
}
ipc::yield(k);
}
IPC_UNUSED_ auto guard = ipc::detail::unique_lock(lock_);
if (counter_ == 0) return;
-- counter_;
::ReleaseSemaphore(h, 1, NULL);
}
void broadcast(handle_t h) {
if (h == invalid()) return;
::ReleaseSemaphore(h, counter_.exchange(0, std::memory_order_acquire), NULL);
IPC_UNUSED_ auto guard = ipc::detail::unique_lock(lock_);
if (counter_ == 0) return;
long all_count = counter_;
counter_ = 0;
::ReleaseSemaphore(h, all_count, NULL);
}
};

View File

@ -1,10 +1,7 @@
#pragma once
#include <string>
#include <tuple>
#include <type_traits>
#include "pool_alloc.h"
#include <atomic>
#if defined(WIN64) || defined(_WIN64) || defined(__WIN64__) || \
defined(WIN32) || defined(_WIN32) || defined(__WIN32__) || defined(__NT__) || \
@ -26,10 +23,6 @@ private:
waiter_t* w_ = nullptr;
waiter_t::handle_t h_ = waiter_t::invalid();
auto to_w_info() {
return std::make_tuple(w_, h_);
}
public:
waiter_wrapper() = default;
explicit waiter_wrapper(waiter_t* w) {