mirror of
https://github.com/trapexit/mergerfs.git
synced 2026-10-21 04:40:27 +00:00
101 lines
1.5 KiB
C++
101 lines
1.5 KiB
C++
#pragma once
|
|
|
|
#include <cerrno>
|
|
#include <cstdlib>
|
|
|
|
#include <pthread.h>
|
|
|
|
#ifndef PTHREAD_MUTEX_ADAPTIVE_NP
|
|
# define PTHREAD_MUTEX_ADAPTIVE_NP PTHREAD_MUTEX_NORMAL
|
|
#endif
|
|
|
|
|
|
static
|
|
inline
|
|
void
|
|
mutex_init(pthread_mutex_t &mutex_)
|
|
{
|
|
int rv;
|
|
pthread_mutexattr_t attr;
|
|
|
|
rv = pthread_mutexattr_init(&attr);
|
|
if(rv != 0)
|
|
std::abort();
|
|
|
|
rv = pthread_mutexattr_settype(&attr,PTHREAD_MUTEX_ADAPTIVE_NP);
|
|
if(rv != 0)
|
|
{
|
|
pthread_mutexattr_destroy(&attr);
|
|
std::abort();
|
|
}
|
|
|
|
rv = pthread_mutex_init(&mutex_,&attr);
|
|
if(rv != 0)
|
|
{
|
|
pthread_mutexattr_destroy(&attr);
|
|
std::abort();
|
|
}
|
|
|
|
rv = pthread_mutexattr_destroy(&attr);
|
|
if(rv != 0)
|
|
std::abort();
|
|
}
|
|
|
|
static
|
|
inline
|
|
void
|
|
mutex_lock(pthread_mutex_t &mutex_)
|
|
{
|
|
int rv;
|
|
|
|
rv = pthread_mutex_lock(&mutex_);
|
|
if(rv != 0)
|
|
std::abort();
|
|
}
|
|
|
|
static
|
|
inline
|
|
void
|
|
mutex_unlock(pthread_mutex_t &mutex_)
|
|
{
|
|
int rv;
|
|
|
|
rv = pthread_mutex_unlock(&mutex_);
|
|
if(rv != 0)
|
|
std::abort();
|
|
}
|
|
|
|
static
|
|
inline
|
|
void
|
|
mutex_destroy(pthread_mutex_t &mutex_)
|
|
{
|
|
int rv;
|
|
|
|
rv = pthread_mutex_destroy(&mutex_);
|
|
if(rv != 0)
|
|
std::abort();
|
|
}
|
|
|
|
class ScopedMutexLock
|
|
{
|
|
private:
|
|
pthread_mutex_t &_mutex;
|
|
|
|
public:
|
|
explicit
|
|
ScopedMutexLock(pthread_mutex_t &mutex_)
|
|
: _mutex(mutex_)
|
|
{
|
|
mutex_lock(_mutex);
|
|
}
|
|
|
|
~ScopedMutexLock() noexcept
|
|
{
|
|
mutex_unlock(_mutex);
|
|
}
|
|
|
|
ScopedMutexLock(const ScopedMutexLock&) = delete;
|
|
ScopedMutexLock& operator=(const ScopedMutexLock&) = delete;
|
|
};
|