mirror of
https://github.com/trapexit/mergerfs.git
synced 2026-11-30 03:47:02 -05:00
Use debug mutex more
This commit is contained in:
parent
3f70e66ee3
commit
a2860c3f1f
26 changed files with 364 additions and 1156 deletions
|
|
@ -29,8 +29,8 @@ fuse_msgbuf_t *msgbuf_alloc();
|
|||
fuse_msgbuf_t *msgbuf_alloc_page_aligned();
|
||||
void msgbuf_free(fuse_msgbuf_t *msgbuf);
|
||||
|
||||
void msgbuf_clear();
|
||||
void msgbuf_gc();
|
||||
void msgbuf_gc_10percent();
|
||||
|
||||
u64 msgbuf_alloc_count();
|
||||
u64 msgbuf_avail_count();
|
||||
|
|
|
|||
|
|
@ -5,3 +5,48 @@
|
|||
#else
|
||||
#include "mutex_ndebug.hpp"
|
||||
#endif
|
||||
|
||||
typedef pthread_mutex_t mutex_t;
|
||||
|
||||
// Simple mutex_t wrapper to provide RAII
|
||||
// Because macros are needed to get location in the code a lock is
|
||||
// called not making it std::mutex compatible.
|
||||
class Mutex
|
||||
{
|
||||
private:
|
||||
mutex_t _mutex;
|
||||
|
||||
public:
|
||||
Mutex()
|
||||
{
|
||||
mutex_init(_mutex);
|
||||
}
|
||||
|
||||
~Mutex()
|
||||
{
|
||||
mutex_destroy(_mutex);
|
||||
}
|
||||
|
||||
operator mutex_t&()
|
||||
{
|
||||
return _mutex;
|
||||
}
|
||||
};
|
||||
|
||||
class LockGuard
|
||||
{
|
||||
private:
|
||||
mutex_t &_mutex;
|
||||
|
||||
public:
|
||||
LockGuard(mutex_t &mutex_)
|
||||
: _mutex(mutex_)
|
||||
{
|
||||
mutex_lock(_mutex);
|
||||
}
|
||||
|
||||
~LockGuard()
|
||||
{
|
||||
mutex_unlock(_mutex);
|
||||
}
|
||||
};
|
||||
|
|
|
|||
|
|
@ -2,19 +2,19 @@
|
|||
|
||||
#include "fmt/core.h"
|
||||
|
||||
#include <ctime>
|
||||
#include <cstdlib>
|
||||
|
||||
#include <pthread.h>
|
||||
#include <time.h>
|
||||
|
||||
#ifndef PTHREAD_MUTEX_ADAPTIVE_NP
|
||||
# define PTHREAD_MUTEX_ADAPTIVE_NP PTHREAD_MUTEX_NORMAL
|
||||
#endif
|
||||
|
||||
#define mutex_init(M) _mutex_init(M,__FILE__,__func__,__LINE__)
|
||||
#define mutex_destroy(M) _mutex_destroy(M,__FILE__,__func__,__LINE__)
|
||||
#define mutex_lock(M) _mutex_lock(M,__FILE__,__func__,__LINE__)
|
||||
#define mutex_unlock(M) _mutex_unlock(M,__FILE__,__func__,__LINE__)
|
||||
#define mutex_init(M) _mutex_init((M),__FILE__,__func__,__LINE__)
|
||||
#define mutex_destroy(M) _mutex_destroy((M),__FILE__,__func__,__LINE__)
|
||||
#define mutex_lock(M) _mutex_lock((M),__FILE__,__func__,__LINE__)
|
||||
#define mutex_unlock(M) _mutex_unlock((M),__FILE__,__func__,__LINE__)
|
||||
|
||||
|
||||
static
|
||||
|
|
@ -34,13 +34,13 @@ _print_error_and_abort(const char *func1_,
|
|||
file_,
|
||||
func2_,
|
||||
linenum_);
|
||||
abort();
|
||||
std::abort();
|
||||
}
|
||||
|
||||
static
|
||||
inline
|
||||
void
|
||||
_mutex_init(pthread_mutex_t *mutex_,
|
||||
_mutex_init(pthread_mutex_t &mutex_,
|
||||
const char *file_,
|
||||
const char *func_,
|
||||
const int linenum_)
|
||||
|
|
@ -51,7 +51,7 @@ _mutex_init(pthread_mutex_t *mutex_,
|
|||
pthread_mutexattr_init(&attr);
|
||||
pthread_mutexattr_settype(&attr,PTHREAD_MUTEX_ADAPTIVE_NP);
|
||||
|
||||
rv = pthread_mutex_init(mutex_,&attr);
|
||||
rv = pthread_mutex_init(&mutex_,&attr);
|
||||
if(rv != 0)
|
||||
_print_error_and_abort(__func__,rv,file_,func_,linenum_);
|
||||
|
||||
|
|
@ -61,14 +61,14 @@ _mutex_init(pthread_mutex_t *mutex_,
|
|||
static
|
||||
inline
|
||||
void
|
||||
_mutex_destroy(pthread_mutex_t *mutex_,
|
||||
_mutex_destroy(pthread_mutex_t &mutex_,
|
||||
const char *file_,
|
||||
const char *func_,
|
||||
const int linenum_)
|
||||
{
|
||||
int rv;
|
||||
|
||||
rv = pthread_mutex_destroy(mutex_);
|
||||
rv = pthread_mutex_destroy(&mutex_);
|
||||
if(rv != 0)
|
||||
_print_error_and_abort(__func__,rv,file_,func_,linenum_);
|
||||
}
|
||||
|
|
@ -76,7 +76,7 @@ _mutex_destroy(pthread_mutex_t *mutex_,
|
|||
static
|
||||
inline
|
||||
void
|
||||
_mutex_lock(pthread_mutex_t *mutex_,
|
||||
_mutex_lock(pthread_mutex_t &mutex_,
|
||||
const char *file_,
|
||||
const char *func_,
|
||||
const int linenum_)
|
||||
|
|
@ -95,7 +95,7 @@ _mutex_lock(pthread_mutex_t *mutex_,
|
|||
timeout.tv_nsec -= 1000000000;
|
||||
}
|
||||
|
||||
rv = pthread_mutex_timedlock(mutex_,&timeout);
|
||||
rv = pthread_mutex_timedlock(&mutex_,&timeout);
|
||||
switch(rv)
|
||||
{
|
||||
case 0:
|
||||
|
|
@ -119,14 +119,14 @@ _mutex_lock(pthread_mutex_t *mutex_,
|
|||
static
|
||||
inline
|
||||
void
|
||||
_mutex_unlock(pthread_mutex_t *mutex_,
|
||||
_mutex_unlock(pthread_mutex_t &mutex_,
|
||||
const char *file_,
|
||||
const char *func_,
|
||||
const int linenum_)
|
||||
{
|
||||
int rv;
|
||||
|
||||
rv = pthread_mutex_unlock(mutex_);
|
||||
rv = pthread_mutex_unlock(&mutex_);
|
||||
if(rv != 0)
|
||||
_print_error_and_abort(__func__,rv,file_,func_,linenum_);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -12,7 +12,7 @@
|
|||
static
|
||||
inline
|
||||
void
|
||||
mutex_init(pthread_mutex_t *mutex_)
|
||||
mutex_init(pthread_mutex_t &mutex_)
|
||||
{
|
||||
int rv;
|
||||
pthread_mutexattr_t attr;
|
||||
|
|
@ -20,9 +20,9 @@ mutex_init(pthread_mutex_t *mutex_)
|
|||
pthread_mutexattr_init(&attr);
|
||||
pthread_mutexattr_settype(&attr,PTHREAD_MUTEX_ADAPTIVE_NP);
|
||||
|
||||
rv = pthread_mutex_init(mutex_,&attr);
|
||||
rv = pthread_mutex_init(&mutex_,&attr);
|
||||
if(rv != 0)
|
||||
abort();
|
||||
std::abort();
|
||||
|
||||
pthread_mutexattr_destroy(&attr);
|
||||
}
|
||||
|
|
@ -30,35 +30,35 @@ mutex_init(pthread_mutex_t *mutex_)
|
|||
static
|
||||
inline
|
||||
void
|
||||
mutex_lock(pthread_mutex_t *mutex_)
|
||||
mutex_lock(pthread_mutex_t &mutex_)
|
||||
{
|
||||
int rv;
|
||||
|
||||
rv = pthread_mutex_lock(mutex_);
|
||||
rv = pthread_mutex_lock(&mutex_);
|
||||
if(rv != 0)
|
||||
abort();
|
||||
std::abort();
|
||||
}
|
||||
|
||||
static
|
||||
inline
|
||||
void
|
||||
mutex_unlock(pthread_mutex_t *mutex_)
|
||||
mutex_unlock(pthread_mutex_t &mutex_)
|
||||
{
|
||||
int rv;
|
||||
|
||||
rv = pthread_mutex_unlock(mutex_);
|
||||
rv = pthread_mutex_unlock(&mutex_);
|
||||
if(rv != 0)
|
||||
abort();
|
||||
std::abort();
|
||||
}
|
||||
|
||||
static
|
||||
inline
|
||||
void
|
||||
mutex_destroy(pthread_mutex_t *mutex_)
|
||||
mutex_destroy(pthread_mutex_t &mutex_)
|
||||
{
|
||||
int rv;
|
||||
|
||||
rv = pthread_mutex_destroy(mutex_);
|
||||
rv = pthread_mutex_destroy(&mutex_);
|
||||
if(rv != 0)
|
||||
abort();
|
||||
std::abort();
|
||||
}
|
||||
|
|
|
|||
|
|
@ -16,14 +16,43 @@
|
|||
|
||||
#pragma once
|
||||
|
||||
#include "mutex.hpp"
|
||||
|
||||
#include <algorithm>
|
||||
#include <atomic>
|
||||
#include <memory>
|
||||
#include <mutex>
|
||||
#include <new>
|
||||
#include <utility>
|
||||
#include <type_traits>
|
||||
|
||||
template<typename T>
|
||||
struct DefaultAllocator
|
||||
{
|
||||
void*
|
||||
allocate(size_t size_, size_t align_)
|
||||
{
|
||||
return ::operator new(size_, std::align_val_t{align_});
|
||||
}
|
||||
|
||||
void
|
||||
deallocate(void *ptr_, size_t /*size_*/, size_t align_)
|
||||
{
|
||||
::operator delete(ptr_, std::align_val_t{align_});
|
||||
}
|
||||
};
|
||||
|
||||
struct DefaultShouldPool
|
||||
{
|
||||
template<typename T>
|
||||
bool operator()(T*) const noexcept
|
||||
{
|
||||
return true;
|
||||
}
|
||||
};
|
||||
|
||||
template<typename T,
|
||||
typename Allocator = DefaultAllocator,
|
||||
typename ShouldPool = DefaultShouldPool>
|
||||
class ObjPool
|
||||
{
|
||||
private:
|
||||
|
|
@ -45,11 +74,34 @@ private:
|
|||
}
|
||||
|
||||
private:
|
||||
std::mutex _mtx;
|
||||
mutex_t _mtx;
|
||||
Node *_head = nullptr;
|
||||
std::atomic<size_t> _pooled_count{0};
|
||||
std::atomic<size_t> _pool_count{0};
|
||||
Allocator _allocator;
|
||||
ShouldPool _should_pool;
|
||||
|
||||
public:
|
||||
template<typename AllocPred,
|
||||
typename = std::enable_if_t<
|
||||
!std::is_same_v<std::decay_t<AllocPred>, ObjPool> &&
|
||||
!std::is_same_v<std::decay_t<AllocPred>, Allocator>
|
||||
>>
|
||||
explicit
|
||||
ObjPool(AllocPred&& allocator_pred_)
|
||||
: _allocator(allocator_pred_)
|
||||
{}
|
||||
|
||||
template<typename ShouldPoolPred,
|
||||
typename = std::enable_if_t<
|
||||
!std::is_same_v<std::decay_t<ShouldPoolPred>, ObjPool> &&
|
||||
!std::is_same_v<std::decay_t<ShouldPoolPred>, ShouldPool>
|
||||
>>
|
||||
explicit
|
||||
ObjPool(Allocator allocator_, ShouldPoolPred&& should_pool_pred_)
|
||||
: _allocator(allocator_),
|
||||
_should_pool(std::forward<ShouldPoolPred>(should_pool_pred_))
|
||||
{}
|
||||
|
||||
ObjPool() = default;
|
||||
ObjPool(const ObjPool&) = delete;
|
||||
ObjPool& operator=(const ObjPool&) = delete;
|
||||
|
|
@ -65,26 +117,31 @@ private:
|
|||
Node*
|
||||
_pop_node()
|
||||
{
|
||||
std::lock_guard<std::mutex> lock(_mtx);
|
||||
mutex_lock(&_mtx);
|
||||
|
||||
Node *node = _head;
|
||||
if(node)
|
||||
{
|
||||
_head = node->next;
|
||||
_pooled_count.fetch_sub(1, std::memory_order_relaxed);
|
||||
_pool_count.fetch_sub(1, std::memory_order_relaxed);
|
||||
}
|
||||
|
||||
mutex_unlock(&_mtx);
|
||||
|
||||
return node;
|
||||
}
|
||||
|
||||
void
|
||||
_push_node(Node *node_)
|
||||
{
|
||||
std::lock_guard<std::mutex> lock(_mtx);
|
||||
mutex_lock(_mtx);
|
||||
|
||||
node_->next = _head;
|
||||
_head = node_;
|
||||
_pooled_count.fetch_add(1, std::memory_order_relaxed);
|
||||
|
||||
mutex_unlock(_mtx);
|
||||
|
||||
_pool_count.fetch_add(1, std::memory_order_relaxed);
|
||||
}
|
||||
|
||||
public:
|
||||
|
|
@ -93,24 +150,26 @@ public:
|
|||
{
|
||||
Node *head;
|
||||
|
||||
{
|
||||
std::lock_guard<std::mutex> lock(_mtx);
|
||||
head = _head;
|
||||
_head = nullptr;
|
||||
_pooled_count.store(0, std::memory_order_relaxed);
|
||||
}
|
||||
mutex_lock(_mtx);
|
||||
|
||||
head = _head;
|
||||
_head = nullptr;
|
||||
|
||||
mutex_unlock(_mtx);
|
||||
|
||||
_pool_count.store(0, std::memory_order_relaxed);
|
||||
|
||||
while(head)
|
||||
{
|
||||
Node *next = head->next;
|
||||
::operator delete(head, std::align_val_t{alignof(T)});
|
||||
_allocator.deallocate(head, sizeof(Node), alignof(Node));
|
||||
head = next;
|
||||
}
|
||||
}
|
||||
|
||||
template<typename... Args>
|
||||
T*
|
||||
alloc(Args&&... args_)
|
||||
alloc(size_t size_ = sizeof(T), Args&&... args_)
|
||||
{
|
||||
void *mem;
|
||||
Node *node;
|
||||
|
|
@ -118,7 +177,7 @@ public:
|
|||
node = _pop_node();
|
||||
mem = (node ?
|
||||
static_cast<void*>(node) :
|
||||
::operator new(sizeof(T), std::align_val_t{alignof(T)}));
|
||||
_allocator.allocate(size_, alignof(T)));
|
||||
|
||||
try
|
||||
{
|
||||
|
|
@ -126,32 +185,38 @@ public:
|
|||
}
|
||||
catch(...)
|
||||
{
|
||||
_push_node(static_cast<Node*>(mem));
|
||||
if(!node)
|
||||
_allocator.deallocate(mem, size_, alignof(T));
|
||||
throw;
|
||||
}
|
||||
}
|
||||
|
||||
void
|
||||
free(T *obj_) noexcept
|
||||
free(T *obj_, size_t size_ = sizeof(T)) noexcept
|
||||
{
|
||||
if(not obj_)
|
||||
return;
|
||||
|
||||
bool should_pool = _should_pool(obj_);
|
||||
|
||||
obj_->~T();
|
||||
|
||||
_push_node(to_node(obj_));
|
||||
if(should_pool)
|
||||
_push_node(to_node(obj_));
|
||||
else
|
||||
_allocator.deallocate(obj_, size_, alignof(T));
|
||||
}
|
||||
|
||||
size_t
|
||||
size() const noexcept
|
||||
{
|
||||
return _pooled_count.load(std::memory_order_relaxed);
|
||||
return _pool_count.load(std::memory_order_relaxed);
|
||||
}
|
||||
|
||||
void
|
||||
gc() noexcept
|
||||
{
|
||||
size_t count = _pooled_count.load(std::memory_order_relaxed);
|
||||
size_t count = _pool_count.load(std::memory_order_relaxed);
|
||||
size_t to_free = std::max(count / 10, size_t{1});
|
||||
|
||||
for(size_t i = 0; i < to_free; ++i)
|
||||
|
|
@ -159,7 +224,7 @@ public:
|
|||
Node *node = _pop_node();
|
||||
if(not node)
|
||||
break;
|
||||
::operator delete(node, std::align_val_t{alignof(T)});
|
||||
_allocator.deallocate(node, sizeof(Node), alignof(Node));
|
||||
}
|
||||
}
|
||||
};
|
||||
|
|
|
|||
369
libfuse/include/scope_guard.hpp
Normal file
369
libfuse/include/scope_guard.hpp
Normal file
|
|
@ -0,0 +1,369 @@
|
|||
// _____ _____ _ _____
|
||||
// / ____| / ____| | | / ____|_ _
|
||||
// | (___ ___ ___ _ __ ___ | | __ _ _ __ _ _ __ __| | | | _| |_ _| |_
|
||||
// \___ \ / __/ _ \| '_ \ / _ \ | | |_ | | | |/ _` | '__/ _` | | | |_ _|_ _|
|
||||
// ____) | (_| (_) | |_) | __/ | |__| | |_| | (_| | | | (_| | | |____|_| |_|
|
||||
// |_____/ \___\___/| .__/ \___| \_____|\__,_|\__,_|_| \__,_| \_____|
|
||||
// | | https://github.com/Neargye/scope_guard
|
||||
// |_| version 0.9.1
|
||||
//
|
||||
// Licensed under the MIT License <http://opensource.org/licenses/MIT>.
|
||||
// SPDX-License-Identifier: MIT
|
||||
// Copyright (c) 2018 - 2021 Daniil Goncharov <neargye@gmail.com>.
|
||||
//
|
||||
// Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
// of this software and associated documentation files (the "Software"), to deal
|
||||
// in the Software without restriction, including without limitation the rights
|
||||
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
// copies of the Software, and to permit persons to whom the Software is
|
||||
// furnished to do so, subject to the following conditions:
|
||||
//
|
||||
// The above copyright notice and this permission notice shall be included in all
|
||||
// copies or substantial portions of the Software.
|
||||
//
|
||||
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
// SOFTWARE.
|
||||
|
||||
#ifndef NEARGYE_SCOPE_GUARD_HPP
|
||||
#define NEARGYE_SCOPE_GUARD_HPP
|
||||
|
||||
#define SCOPE_GUARD_VERSION_MAJOR 0
|
||||
#define SCOPE_GUARD_VERSION_MINOR 9
|
||||
#define SCOPE_GUARD_VERSION_PATCH 1
|
||||
|
||||
#include <type_traits>
|
||||
#if (defined(_MSC_VER) && _MSC_VER >= 1900) || ((defined(__clang__) || defined(__GNUC__)) && __cplusplus >= 201700L)
|
||||
#include <exception>
|
||||
#endif
|
||||
|
||||
// scope_guard throwable settings:
|
||||
// SCOPE_GUARD_NO_THROW_CONSTRUCTIBLE requires nothrow constructible action.
|
||||
// SCOPE_GUARD_MAY_THROW_ACTION action may throw exceptions.
|
||||
// SCOPE_GUARD_NO_THROW_ACTION requires noexcept action.
|
||||
// SCOPE_GUARD_SUPPRESS_THROW_ACTION exceptions during action will be suppressed.
|
||||
// SCOPE_GUARD_CATCH_HANDLER exceptions handler. If SCOPE_GUARD_SUPPRESS_THROW_ACTIONS is not defined, it will do nothing.
|
||||
|
||||
#if !defined(SCOPE_GUARD_MAY_THROW_ACTION) && !defined(SCOPE_GUARD_NO_THROW_ACTION) && !defined(SCOPE_GUARD_SUPPRESS_THROW_ACTION)
|
||||
# define SCOPE_GUARD_MAY_THROW_ACTION
|
||||
#elif (defined(SCOPE_GUARD_MAY_THROW_ACTION) + defined(SCOPE_GUARD_NO_THROW_ACTION) + defined(SCOPE_GUARD_SUPPRESS_THROW_ACTION)) > 1
|
||||
# error Only one of SCOPE_GUARD_MAY_THROW_ACTION and SCOPE_GUARD_NO_THROW_ACTION and SCOPE_GUARD_SUPPRESS_THROW_ACTION may be defined.
|
||||
#endif
|
||||
|
||||
#if !defined(SCOPE_GUARD_CATCH_HANDLER)
|
||||
# define SCOPE_GUARD_CATCH_HANDLER /* Suppress exception.*/
|
||||
#endif
|
||||
|
||||
namespace scope_guard {
|
||||
|
||||
namespace detail {
|
||||
|
||||
#if defined(SCOPE_GUARD_SUPPRESS_THROW_ACTION) && (defined(__cpp_exceptions) || defined(__EXCEPTIONS) || (_HAS_EXCEPTIONS))
|
||||
# define NEARGYE_NOEXCEPT(...) noexcept
|
||||
# define NEARGYE_TRY try {
|
||||
# define NEARGYE_CATCH } catch (...) { SCOPE_GUARD_CATCH_HANDLER }
|
||||
#else
|
||||
# define NEARGYE_NOEXCEPT(...) noexcept(__VA_ARGS__)
|
||||
# define NEARGYE_TRY
|
||||
# define NEARGYE_CATCH
|
||||
#endif
|
||||
|
||||
#define NEARGYE_MOV(...) static_cast<typename std::remove_reference<decltype(__VA_ARGS__)>::type&&>(__VA_ARGS__)
|
||||
#define NEARGYE_FWD(...) static_cast<decltype(__VA_ARGS__)&&>(__VA_ARGS__)
|
||||
|
||||
// NEARGYE_NODISCARD encourages the compiler to issue a warning if the return value is discarded.
|
||||
#if !defined(NEARGYE_NODISCARD)
|
||||
# if defined(__clang__)
|
||||
# if (__clang_major__ * 10 + __clang_minor__) >= 39 && __cplusplus >= 201703L
|
||||
# define NEARGYE_NODISCARD [[nodiscard]]
|
||||
# else
|
||||
# define NEARGYE_NODISCARD __attribute__((__warn_unused_result__))
|
||||
# endif
|
||||
# elif defined(__GNUC__)
|
||||
# if __GNUC__ >= 7 && __cplusplus >= 201703L
|
||||
# define NEARGYE_NODISCARD [[nodiscard]]
|
||||
# else
|
||||
# define NEARGYE_NODISCARD __attribute__((__warn_unused_result__))
|
||||
# endif
|
||||
# elif defined(_MSC_VER)
|
||||
# if _MSC_VER >= 1911 && defined(_MSVC_LANG) && _MSVC_LANG >= 201703L
|
||||
# define NEARGYE_NODISCARD [[nodiscard]]
|
||||
# elif defined(_Check_return_)
|
||||
# define NEARGYE_NODISCARD _Check_return_
|
||||
# else
|
||||
# define NEARGYE_NODISCARD
|
||||
# endif
|
||||
# else
|
||||
# define NEARGYE_NODISCARD
|
||||
# endif
|
||||
#endif
|
||||
|
||||
#if defined(_MSC_VER) && _MSC_VER < 1900
|
||||
inline int uncaught_exceptions() noexcept {
|
||||
return *(reinterpret_cast<int*>(static_cast<char*>(static_cast<void*>(_getptd())) + (sizeof(void*) == 8 ? 0x100 : 0x90)));
|
||||
}
|
||||
#elif (defined(__clang__) || defined(__GNUC__)) && __cplusplus < 201700L
|
||||
struct __cxa_eh_globals;
|
||||
extern "C" __cxa_eh_globals* __cxa_get_globals() noexcept;
|
||||
inline int uncaught_exceptions() noexcept {
|
||||
return static_cast<int>(*(reinterpret_cast<unsigned int*>(static_cast<char*>(static_cast<void*>(__cxa_get_globals())) + sizeof(void*))));
|
||||
}
|
||||
#else
|
||||
inline int uncaught_exceptions() noexcept {
|
||||
return std::uncaught_exceptions();
|
||||
}
|
||||
#endif
|
||||
|
||||
class on_exit_policy {
|
||||
bool execute_;
|
||||
|
||||
public:
|
||||
explicit on_exit_policy(bool execute) noexcept : execute_{execute} {}
|
||||
|
||||
void dismiss() noexcept {
|
||||
execute_ = false;
|
||||
}
|
||||
|
||||
bool should_execute() const noexcept {
|
||||
return execute_;
|
||||
}
|
||||
};
|
||||
|
||||
class on_fail_policy {
|
||||
int ec_;
|
||||
|
||||
public:
|
||||
explicit on_fail_policy(bool execute) noexcept : ec_{execute ? uncaught_exceptions() : -1} {}
|
||||
|
||||
void dismiss() noexcept {
|
||||
ec_ = -1;
|
||||
}
|
||||
|
||||
bool should_execute() const noexcept {
|
||||
return ec_ != -1 && ec_ < uncaught_exceptions();
|
||||
}
|
||||
};
|
||||
|
||||
class on_success_policy {
|
||||
int ec_;
|
||||
|
||||
public:
|
||||
explicit on_success_policy(bool execute) noexcept : ec_{execute ? uncaught_exceptions() : -1} {}
|
||||
|
||||
void dismiss() noexcept {
|
||||
ec_ = -1;
|
||||
}
|
||||
|
||||
bool should_execute() const noexcept {
|
||||
return ec_ != -1 && ec_ >= uncaught_exceptions();
|
||||
}
|
||||
};
|
||||
|
||||
template <typename T, typename = void>
|
||||
struct is_noarg_returns_void_action
|
||||
: std::false_type {};
|
||||
|
||||
template <typename T>
|
||||
struct is_noarg_returns_void_action<T, decltype((std::declval<T>())())>
|
||||
: std::true_type {};
|
||||
|
||||
template <typename T, bool = is_noarg_returns_void_action<T>::value>
|
||||
struct is_nothrow_invocable_action
|
||||
: std::false_type {};
|
||||
|
||||
template <typename T>
|
||||
struct is_nothrow_invocable_action<T, true>
|
||||
: std::integral_constant<bool, noexcept((std::declval<T>())())> {};
|
||||
|
||||
template <typename F, typename P>
|
||||
class scope_guard {
|
||||
using A = typename std::decay<F>::type;
|
||||
|
||||
static_assert(is_noarg_returns_void_action<A>::value,
|
||||
"scope_guard requires no-argument action, that returns void.");
|
||||
static_assert(std::is_same<P, on_exit_policy>::value || std::is_same<P, on_fail_policy>::value || std::is_same<P, on_success_policy>::value,
|
||||
"scope_guard requires on_exit_policy, on_fail_policy or on_success_policy.");
|
||||
#if defined(SCOPE_GUARD_NO_THROW_ACTION)
|
||||
static_assert(is_nothrow_invocable_action<A>::value,
|
||||
"scope_guard requires noexcept invocable action.");
|
||||
#endif
|
||||
#if defined(SCOPE_GUARD_NO_THROW_CONSTRUCTIBLE)
|
||||
static_assert(std::is_nothrow_move_constructible<A>::value,
|
||||
"scope_guard requires nothrow constructible action.");
|
||||
#endif
|
||||
|
||||
P policy_;
|
||||
A action_;
|
||||
|
||||
void* operator new(std::size_t) = delete;
|
||||
void operator delete(void*) = delete;
|
||||
|
||||
public:
|
||||
scope_guard() = delete;
|
||||
scope_guard(const scope_guard&) = delete;
|
||||
scope_guard& operator=(const scope_guard&) = delete;
|
||||
scope_guard& operator=(scope_guard&&) = delete;
|
||||
|
||||
scope_guard(scope_guard&& other) noexcept(std::is_nothrow_move_constructible<A>::value)
|
||||
: policy_{false},
|
||||
action_{NEARGYE_MOV(other.action_)} {
|
||||
policy_ = NEARGYE_MOV(other.policy_);
|
||||
other.policy_.dismiss();
|
||||
}
|
||||
|
||||
scope_guard(const A& action) = delete;
|
||||
scope_guard(A& action) = delete;
|
||||
|
||||
explicit scope_guard(A&& action) noexcept(std::is_nothrow_move_constructible<A>::value)
|
||||
: policy_{true},
|
||||
action_{NEARGYE_MOV(action)} {}
|
||||
|
||||
void dismiss() noexcept {
|
||||
policy_.dismiss();
|
||||
}
|
||||
|
||||
~scope_guard() NEARGYE_NOEXCEPT(is_nothrow_invocable_action<A>::value) {
|
||||
if (policy_.should_execute()) {
|
||||
NEARGYE_TRY
|
||||
action_();
|
||||
NEARGYE_CATCH
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
template <typename F>
|
||||
using scope_exit = scope_guard<F, on_exit_policy>;
|
||||
|
||||
template <typename F, typename std::enable_if<is_noarg_returns_void_action<F>::value, int>::type = 0>
|
||||
NEARGYE_NODISCARD scope_exit<F> make_scope_exit(F&& action) noexcept(noexcept(scope_exit<F>{NEARGYE_FWD(action)})) {
|
||||
return scope_exit<F>{NEARGYE_FWD(action)};
|
||||
}
|
||||
|
||||
template <typename F>
|
||||
using scope_fail = scope_guard<F, on_fail_policy>;
|
||||
|
||||
template <typename F, typename std::enable_if<is_noarg_returns_void_action<F>::value, int>::type = 0>
|
||||
NEARGYE_NODISCARD scope_fail<F> make_scope_fail(F&& action) noexcept(noexcept(scope_fail<F>{NEARGYE_FWD(action)})) {
|
||||
return scope_fail<F>{NEARGYE_FWD(action)};
|
||||
}
|
||||
|
||||
template <typename F>
|
||||
using scope_success = scope_guard<F, on_success_policy>;
|
||||
|
||||
template <typename F, typename std::enable_if<is_noarg_returns_void_action<F>::value, int>::type = 0>
|
||||
NEARGYE_NODISCARD scope_success<F> make_scope_success(F&& action) noexcept(noexcept(scope_success<F>{NEARGYE_FWD(action)})) {
|
||||
return scope_success<F>{NEARGYE_FWD(action)};
|
||||
}
|
||||
|
||||
struct scope_exit_tag {};
|
||||
|
||||
template <typename F, typename std::enable_if<is_noarg_returns_void_action<F>::value, int>::type = 0>
|
||||
scope_exit<F> operator<<(scope_exit_tag, F&& action) noexcept(noexcept(scope_exit<F>{NEARGYE_FWD(action)})) {
|
||||
return scope_exit<F>{NEARGYE_FWD(action)};
|
||||
}
|
||||
|
||||
struct scope_fail_tag {};
|
||||
|
||||
template <typename F, typename std::enable_if<is_noarg_returns_void_action<F>::value, int>::type = 0>
|
||||
scope_fail<F> operator<<(scope_fail_tag, F&& action) noexcept(noexcept(scope_fail<F>{NEARGYE_FWD(action)})) {
|
||||
return scope_fail<F>{NEARGYE_FWD(action)};
|
||||
}
|
||||
|
||||
struct scope_success_tag {};
|
||||
|
||||
template <typename F, typename std::enable_if<is_noarg_returns_void_action<F>::value, int>::type = 0>
|
||||
scope_success<F> operator<<(scope_success_tag, F&& action) noexcept(noexcept(scope_success<F>{NEARGYE_FWD(action)})) {
|
||||
return scope_success<F>{NEARGYE_FWD(action)};
|
||||
}
|
||||
|
||||
#undef NEARGYE_MOV
|
||||
#undef NEARGYE_FWD
|
||||
#undef NEARGYE_NOEXCEPT
|
||||
#undef NEARGYE_TRY
|
||||
#undef NEARGYE_CATCH
|
||||
#undef NEARGYE_NODISCARD
|
||||
|
||||
} // namespace scope_guard::detail
|
||||
|
||||
using detail::make_scope_exit;
|
||||
using detail::make_scope_fail;
|
||||
using detail::make_scope_success;
|
||||
|
||||
} // namespace scope_guard
|
||||
|
||||
// NEARGYE_MAYBE_UNUSED suppresses compiler warnings on unused entities, if any.
|
||||
#if !defined(NEARGYE_MAYBE_UNUSED)
|
||||
# if defined(__clang__)
|
||||
# if (__clang_major__ * 10 + __clang_minor__) >= 39 && __cplusplus >= 201703L
|
||||
# define NEARGYE_MAYBE_UNUSED [[maybe_unused]]
|
||||
# else
|
||||
# define NEARGYE_MAYBE_UNUSED __attribute__((__unused__))
|
||||
# endif
|
||||
# elif defined(__GNUC__)
|
||||
# if __GNUC__ >= 7 && __cplusplus >= 201703L
|
||||
# define NEARGYE_MAYBE_UNUSED [[maybe_unused]]
|
||||
# else
|
||||
# define NEARGYE_MAYBE_UNUSED __attribute__((__unused__))
|
||||
# endif
|
||||
# elif defined(_MSC_VER)
|
||||
# if _MSC_VER >= 1911 && defined(_MSVC_LANG) && _MSVC_LANG >= 201703L
|
||||
# define NEARGYE_MAYBE_UNUSED [[maybe_unused]]
|
||||
# else
|
||||
# define NEARGYE_MAYBE_UNUSED __pragma(warning(suppress : 4100 4101 4189))
|
||||
# endif
|
||||
# else
|
||||
# define NEARGYE_MAYBE_UNUSED
|
||||
# endif
|
||||
#endif
|
||||
|
||||
#if !defined(NEARGYE_STR_CONCAT)
|
||||
# define NEARGYE_STR_CONCAT_(s1, s2) s1##s2
|
||||
# define NEARGYE_STR_CONCAT(s1, s2) NEARGYE_STR_CONCAT_(s1, s2)
|
||||
#endif
|
||||
|
||||
#if !defined(NEARGYE_COUNTER)
|
||||
# if defined(__COUNTER__)
|
||||
# define NEARGYE_COUNTER __COUNTER__
|
||||
# elif defined(__LINE__)
|
||||
# define NEARGYE_COUNTER __LINE__
|
||||
# endif
|
||||
#endif
|
||||
|
||||
#if defined(SCOPE_GUARD_NO_THROW_ACTION)
|
||||
# define NEARGYE_MAKE_SCOPE_GUARD_ACTION [&]() noexcept -> void
|
||||
#else
|
||||
# define NEARGYE_MAKE_SCOPE_GUARD_ACTION [&]() -> void
|
||||
#endif
|
||||
|
||||
#define NEARGYE_MAKE_SCOPE_EXIT ::scope_guard::detail::scope_exit_tag{} << NEARGYE_MAKE_SCOPE_GUARD_ACTION
|
||||
#define NEARGYE_MAKE_SCOPE_FAIL ::scope_guard::detail::scope_fail_tag{} << NEARGYE_MAKE_SCOPE_GUARD_ACTION
|
||||
#define NEARGYE_MAKE_SCOPE_SUCCESS ::scope_guard::detail::scope_success_tag{} << NEARGYE_MAKE_SCOPE_GUARD_ACTION
|
||||
|
||||
#define NEARGYE_SCOPE_GUARD_WITH_(g, i) for (int i = 1; i--; g)
|
||||
#define NEARGYE_SCOPE_GUARD_WITH(g) NEARGYE_SCOPE_GUARD_WITH_(g, NEARGYE_STR_CONCAT(NEARGYE_INTERNAL_OBJECT_, NEARGYE_COUNTER))
|
||||
|
||||
// SCOPE_EXIT executing action on scope exit.
|
||||
#define MAKE_SCOPE_EXIT(name) auto name = NEARGYE_MAKE_SCOPE_EXIT
|
||||
#define SCOPE_EXIT NEARGYE_MAYBE_UNUSED const MAKE_SCOPE_EXIT(NEARGYE_STR_CONCAT(NEARGYE_SCOPE_EXIT_, NEARGYE_COUNTER))
|
||||
#define WITH_SCOPE_EXIT(guard) NEARGYE_SCOPE_GUARD_WITH(NEARGYE_MAKE_SCOPE_EXIT{ guard })
|
||||
|
||||
// SCOPE_FAIL executing action on scope exit when an exception has been thrown before scope exit.
|
||||
#define MAKE_SCOPE_FAIL(name) auto name = NEARGYE_MAKE_SCOPE_FAIL
|
||||
#define SCOPE_FAIL NEARGYE_MAYBE_UNUSED const MAKE_SCOPE_FAIL(NEARGYE_STR_CONCAT(NEARGYE_SCOPE_FAIL_, NEARGYE_COUNTER))
|
||||
#define WITH_SCOPE_FAIL(guard) NEARGYE_SCOPE_GUARD_WITH(NEARGYE_MAKE_SCOPE_FAIL{ guard })
|
||||
|
||||
// SCOPE_SUCCESS executing action on scope exit when no exceptions have been thrown before scope exit.
|
||||
#define MAKE_SCOPE_SUCCESS(name) auto name = NEARGYE_MAKE_SCOPE_SUCCESS
|
||||
#define SCOPE_SUCCESS NEARGYE_MAYBE_UNUSED const MAKE_SCOPE_SUCCESS(NEARGYE_STR_CONCAT(NEARGYE_SCOPE_SUCCESS_, NEARGYE_COUNTER))
|
||||
#define WITH_SCOPE_SUCCESS(guard) NEARGYE_SCOPE_GUARD_WITH(NEARGYE_MAKE_SCOPE_SUCCESS{ guard })
|
||||
|
||||
// DEFER executing action on scope exit.
|
||||
#define MAKE_DEFER(name) MAKE_SCOPE_EXIT(name)
|
||||
#define DEFER SCOPE_EXIT
|
||||
#define WITH_DEFER(guard) WITH_SCOPE_EXIT(guard)
|
||||
|
||||
#endif // NEARGYE_SCOPE_GUARD_HPP
|
||||
|
|
@ -4,13 +4,14 @@
|
|||
#include "moodycamel/lightweightsemaphore.h"
|
||||
#include "invocable.h"
|
||||
|
||||
#include "mutex.hpp"
|
||||
|
||||
#include <algorithm>
|
||||
#include <atomic>
|
||||
#include <csignal>
|
||||
#include <cstring>
|
||||
#include <future>
|
||||
#include <memory>
|
||||
#include <mutex>
|
||||
#include <stdexcept>
|
||||
#include <string>
|
||||
#include <thread>
|
||||
|
|
@ -91,7 +92,7 @@ private:
|
|||
private:
|
||||
std::string const _name;
|
||||
std::vector<pthread_t> _threads;
|
||||
mutable std::mutex _threads_mutex;
|
||||
mutable Mutex _threads_mutex;
|
||||
};
|
||||
|
||||
|
||||
|
|
@ -230,7 +231,8 @@ ThreadPool::add_thread(const std::string name_)
|
|||
pthread_setname_np(t,name.c_str());
|
||||
|
||||
{
|
||||
std::lock_guard<std::mutex> lg(_threads_mutex);
|
||||
LockGuard lg(_threads_mutex);
|
||||
|
||||
_threads.push_back(t);
|
||||
}
|
||||
|
||||
|
|
@ -243,7 +245,8 @@ int
|
|||
ThreadPool::remove_thread(void)
|
||||
{
|
||||
{
|
||||
std::lock_guard<std::mutex> lg(_threads_mutex);
|
||||
LockGuard lg(_threads_mutex);
|
||||
|
||||
if(_threads.size() <= 1)
|
||||
return -EINVAL;
|
||||
}
|
||||
|
|
@ -258,7 +261,7 @@ ThreadPool::remove_thread(void)
|
|||
promise.set_value(t);
|
||||
|
||||
{
|
||||
std::lock_guard<std::mutex> lg(_threads_mutex);
|
||||
LockGuard lg(_threads_mutex);
|
||||
|
||||
for(auto i = _threads.begin(); i != _threads.end(); ++i)
|
||||
{
|
||||
|
|
@ -285,8 +288,9 @@ int
|
|||
ThreadPool::set_threads(std::size_t const count_)
|
||||
{
|
||||
int diff;
|
||||
|
||||
{
|
||||
std::lock_guard<std::mutex> lg(_threads_mutex);
|
||||
LockGuard lg(_threads_mutex);
|
||||
|
||||
diff = ((int)count_ - (int)_threads.size());
|
||||
}
|
||||
|
|
@ -304,7 +308,7 @@ inline
|
|||
void
|
||||
ThreadPool::shutdown(void)
|
||||
{
|
||||
std::lock_guard<std::mutex> lg(_threads_mutex);
|
||||
LockGuard lg(_threads_mutex);
|
||||
|
||||
for(pthread_t tid : _threads)
|
||||
pthread_cancel(tid);
|
||||
|
|
@ -375,7 +379,7 @@ inline
|
|||
std::vector<pthread_t>
|
||||
ThreadPool::threads() const
|
||||
{
|
||||
std::lock_guard<std::mutex> lg(_threads_mutex);
|
||||
LockGuard lg(_threads_mutex);
|
||||
|
||||
return _threads;
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue