shared_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_SHARED_MUTEX_HPP
#define PMEMOBJ_SHARED_MUTEX_HPP
#include "libpmemobj/thread.h"
#include "libpmemobj/tx_base.h"
namespace pmem
{
namespace obj
{
class shared_mutex {
public:
typedef PMEMrwlock *native_handle_type;
shared_mutex()
{
PMEMobjpool *pop;
if ((pop = pmemobj_pool_by_ptr(&plock)) == nullptr)
throw lock_error(1, std::generic_category(),
"Persistent shared mutex not from "
"persistent memory.");
pmemobj_rwlock_zero(pop, &plock);
}
~shared_mutex() = default;
void
lock()
{
PMEMobjpool *pop = pmemobj_pool_by_ptr(this);
if (int ret = pmemobj_rwlock_wrlock(pop, &this->plock))
throw lock_error(ret, std::system_category(),
"Failed to lock a "
"shared mutex.");
}
void
lock_shared()
{
PMEMobjpool *pop = pmemobj_pool_by_ptr(this);
if (int ret = pmemobj_rwlock_rdlock(pop, &this->plock))
throw lock_error(ret, std::system_category(),
"Failed to shared lock a "
"shared mutex.");
}
bool
try_lock()
{
PMEMobjpool *pop = pmemobj_pool_by_ptr(this);
int ret = pmemobj_rwlock_trywrlock(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"
" shared mutex.");
}
bool
try_lock_shared()
{
PMEMobjpool *pop = pmemobj_pool_by_ptr(this);
int ret = pmemobj_rwlock_tryrdlock(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"
" shared mutex.");
}
void
unlock()
{
PMEMobjpool *pop = pmemobj_pool_by_ptr(this);
(void)pmemobj_rwlock_unlock(pop, &this->plock);
}
void
unlock_shared()
{
this->unlock();
}
native_handle_type
native_handle() noexcept
{
return &this->plock;
}
enum pobj_tx_param
lock_type() const noexcept
{
return TX_PARAM_RWLOCK;
}
shared_mutex &operator=(const shared_mutex &) = delete;
shared_mutex(const shared_mutex &) = delete;
private:
PMEMrwlock plock;
};
} /* namespace obj */
} /* namespace pmem */
#endif /* PMEMOBJ_SHARED_MUTEX_HPP */
pmem::obj::shared_mutex::unlock
void unlock()
Unlocks the mutex.
Definition: shared_mutex.hpp:202
pmem::obj::shared_mutex::lock_type
enum pobj_tx_param lock_type() const noexcept
The type of lock needed for the transaction API.
Definition: shared_mutex.hpp:237
pmem::obj::shared_mutex::plock
PMEMrwlock plock
A POSIX style PMEM-resident shared_mutex.
Definition: shared_mutex.hpp:254
pmem::obj::shared_mutex::~shared_mutex
~shared_mutex()=default
Defaulted destructor.
pmem::obj::shared_mutex::native_handle_type
PMEMrwlock * native_handle_type
Implementation defined handle to the native type.
Definition: shared_mutex.hpp:62
pmem::obj::shared_mutex::lock_shared
void lock_shared()
Lock the mutex for shared access.
Definition: shared_mutex.hpp:124
pmem::obj::shared_mutex::unlock_shared
void unlock_shared()
Unlocks the mutex.
Definition: shared_mutex.hpp:215
pmem::obj::shared_mutex::shared_mutex
shared_mutex()
Default constructor.
Definition: shared_mutex.hpp:70
pmem::lock_error
Custom lock error class.
Definition: pexceptions.hpp:74
pmem::obj::shared_mutex
Persistent memory resident shared_mutex implementation.
Definition: shared_mutex.hpp:59
pmem::obj::shared_mutex::native_handle
native_handle_type native_handle() noexcept
Access a native handle to this shared mutex.
Definition: shared_mutex.hpp:226
pmem::obj::shared_mutex::try_lock
bool try_lock()
Try to lock the mutex for exclusive access, returns regardless if the lock succeeds.
Definition: shared_mutex.hpp:148
pmem
Definition: allocator.hpp:48
pmem::obj::shared_mutex::lock
void lock()
Lock the mutex for exclusive access.
Definition: shared_mutex.hpp:99
pmem::obj::shared_mutex::try_lock_shared
bool try_lock_shared()
Try to lock the mutex for shared access, returns regardless if the lock succeeds. ...
Definition: shared_mutex.hpp:180
pmem::obj::shared_mutex::operator=
shared_mutex & operator=(const shared_mutex &)=delete
Deleted assignment operator.