mutex.hpp
Go to the documentation of this file.
/*
* Copyright 2016-2017, Intel Corporation
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* * Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in
* the documentation and/or other materials provided with the
* distribution.
*
* * Neither the name of the copyright holder nor the names of its
* contributors may be used to endorse or promote products derived
* from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#ifndef PMEMOBJ_MUTEX_HPP
#define PMEMOBJ_MUTEX_HPP
#include "libpmemobj++/detail/pexceptions.hpp"
#include "libpmemobj/thread.h"
#include "libpmemobj/tx_base.h"
namespace pmem
{
namespace obj
{
class mutex {
public:
typedef PMEMmutex *native_handle_type;
mutex()
{
PMEMobjpool *pop;
if ((pop = pmemobj_pool_by_ptr(&plock)) == nullptr)
throw lock_error(1, std::generic_category(),
"Persistent mutex not from persistent"
"memory.");
pmemobj_mutex_zero(pop, &plock);
}
~mutex() = default;
void
lock()
{
PMEMobjpool *pop = pmemobj_pool_by_ptr(this);
if (int ret = pmemobj_mutex_lock(pop, &this->plock))
throw lock_error(ret, std::system_category(),
"Failed to lock a mutex.");
}
bool
try_lock()
{
PMEMobjpool *pop = pmemobj_pool_by_ptr(this);
int ret = pmemobj_mutex_trylock(pop, &this->plock);
if (ret == 0)
return true;
else if (ret == EBUSY)
return false;
else
throw lock_error(ret, std::system_category(),
"Failed to lock a mutex.");
}
void
unlock()
{
PMEMobjpool *pop = pmemobj_pool_by_ptr(this);
(void)pmemobj_mutex_unlock(pop, &this->plock);
}
native_handle_type
native_handle() noexcept
{
return &this->plock;
}
enum pobj_tx_param
lock_type() const noexcept
{
return TX_PARAM_MUTEX;
}
mutex &operator=(const mutex &) = delete;
mutex(const mutex &) = delete;
private:
PMEMmutex plock;
};
} /* namespace obj */
} /* namespace pmem */
#endif /* PMEMOBJ_MUTEX_HPP */
pmem::obj::mutex::native_handle_type
PMEMmutex * native_handle_type
Implementation defined handle to the native type.
Definition: mutex.hpp:63
pmem::obj::mutex::mutex
mutex()
Default constructor.
Definition: mutex.hpp:70
pmem::obj::mutex::lock_type
enum pobj_tx_param lock_type() const noexcept
The type of lock needed for the transaction API.
Definition: mutex.hpp:166
pmem::obj::mutex::~mutex
~mutex()=default
Defaulted destructor.
pmem::obj::mutex::native_handle
native_handle_type native_handle() noexcept
Access a native handle to this condition variable.
Definition: mutex.hpp:155
pexceptions.hpp
Custom exceptions.
pmem::obj::mutex::plock
PMEMmutex plock
A POSIX style PMEM-resident mutex.
Definition: mutex.hpp:183
pmem::obj::mutex
Persistent memory resident mutex implementation.
Definition: mutex.hpp:60
pmem::obj::mutex::lock
void lock()
Locks the mutex, blocks if already locked.
Definition: mutex.hpp:98
pmem::obj::mutex::operator=
mutex & operator=(const mutex &)=delete
Deleted assignment operator.
pmem::lock_error
Custom lock error class.
Definition: pexceptions.hpp:74
pmem::obj::mutex::try_lock
bool try_lock()
Tries to lock the mutex, returns regardless if the lock succeeds.
Definition: mutex.hpp:121
pmem::obj::mutex::unlock
void unlock()
Unlocks a previously locked mutex.
Definition: mutex.hpp:143
pmem
Definition: allocator.hpp:48