OSSIM - Open Source Software Image Map  Version 1.9.0 (20180803)
Public Member Functions | Private Attributes | List of all members
ossim::RWLock Class Reference

Code was derived from https://gist.github.com/mshockwave. More...

#include <RWLock.h>

Public Member Functions

 RWLock ()
 
void lockWrite ()
 
bool tryLockWrite ()
 
void unlockWrite ()
 
void lockRead ()
 
bool tryLockRead ()
 
void unlockRead ()
 

Private Attributes

std::mutex m_waitMutex
 
std::condition_variable m_waitConditional
 
std::atomic_int m_refCounter
 
const int MIN_INT
 

Detailed Description

Code was derived from https://gist.github.com/mshockwave.

Has a pure c++11 implementation for read/write locks allowing one to choose the locking technique to use.

At the bottom we added typedefs so you do not have to specify the template values.

Example:

// enter a section that just needs read only access
{
ossim::ScopeReadLock lock(mutex);
}
// enter some section that requires write
{
ossim::ScopeWriteLock lock(mutex);
}

Definition at line 38 of file RWLock.h.

Constructor & Destructor Documentation

◆ RWLock()

ossim::RWLock::RWLock ( )

Definition at line 3 of file RWLock.cpp.

3  :
4  m_refCounter(0),
6 {
7 
8 }
const int MIN_INT
Definition: RWLock.h:43
std::atomic_int m_refCounter
Definition: RWLock.h:42
#define min(a, b)
Definition: auxiliary.h:75

Member Function Documentation

◆ lockRead()

void ossim::RWLock::lockRead ( )

Definition at line 46 of file RWLock.cpp.

Referenced by ossim::ScopeReadLock::ScopeReadLock().

47 {
48  if(m_refCounter.fetch_add(1, std::memory_order_acquire) < 0){
49  m_refCounter.fetch_sub(1, std::memory_order_release);
50 
51  std::unique_lock<std::mutex> lk(m_waitMutex);
52  m_waitConditional.wait(lk, [this]{
53  return m_refCounter.fetch_add(1, std::memory_order_acquire) >= 0;
54  });
55  lk.unlock();
56  }
57 }
std::mutex m_waitMutex
Definition: RWLock.h:40
std::atomic_int m_refCounter
Definition: RWLock.h:42
std::condition_variable m_waitConditional
Definition: RWLock.h:41

◆ lockWrite()

void ossim::RWLock::lockWrite ( )

Definition at line 10 of file RWLock.cpp.

Referenced by ossim::ScopeWriteLock::ScopeWriteLock().

11 {
12  int expected = 0;
13  if(!m_refCounter.compare_exchange_strong(expected, MIN_INT,
14  std::memory_order_acquire,
15  std::memory_order_relaxed)){
16  expected = 0;
17 
18  std::unique_lock<std::mutex> lk(m_waitMutex);
19  m_waitConditional.wait(lk, [this,&expected] {
20  if(!m_refCounter.compare_exchange_strong(expected, MIN_INT,
21  std::memory_order_acquire,
22  std::memory_order_relaxed)){
23  expected = 0;
24  return false;
25  }
26  return true;
27  });
28  lk.unlock();
29  }
30 }
std::mutex m_waitMutex
Definition: RWLock.h:40
const int MIN_INT
Definition: RWLock.h:43
std::atomic_int m_refCounter
Definition: RWLock.h:42
std::condition_variable m_waitConditional
Definition: RWLock.h:41

◆ tryLockRead()

bool ossim::RWLock::tryLockRead ( )

Definition at line 59 of file RWLock.cpp.

60 {
61  return m_refCounter.fetch_add(1, std::memory_order_acquire) >= 0;
62 }
std::atomic_int m_refCounter
Definition: RWLock.h:42

◆ tryLockWrite()

bool ossim::RWLock::tryLockWrite ( )

Definition at line 32 of file RWLock.cpp.

33 {
34  int expected = 0;
35  return m_refCounter.compare_exchange_strong(expected, MIN_INT,
36  std::memory_order_acquire,
37  std::memory_order_relaxed);
38 }
const int MIN_INT
Definition: RWLock.h:43
std::atomic_int m_refCounter
Definition: RWLock.h:42

◆ unlockRead()

void ossim::RWLock::unlockRead ( )

Definition at line 64 of file RWLock.cpp.

Referenced by ossim::ScopeReadLock::~ScopeReadLock().

65 {
66  m_refCounter.fetch_sub(1, std::memory_order_release);
67  m_waitConditional.notify_one();
68 }
std::atomic_int m_refCounter
Definition: RWLock.h:42
std::condition_variable m_waitConditional
Definition: RWLock.h:41

◆ unlockWrite()

void ossim::RWLock::unlockWrite ( )

Definition at line 40 of file RWLock.cpp.

Referenced by ossim::ScopeWriteLock::~ScopeWriteLock().

41 {
42  m_refCounter.store(0, std::memory_order_release);
43  m_waitConditional.notify_all();
44 }
std::atomic_int m_refCounter
Definition: RWLock.h:42
std::condition_variable m_waitConditional
Definition: RWLock.h:41

Member Data Documentation

◆ m_refCounter

std::atomic_int ossim::RWLock::m_refCounter
private

Definition at line 42 of file RWLock.h.

◆ m_waitConditional

std::condition_variable ossim::RWLock::m_waitConditional
private

Definition at line 41 of file RWLock.h.

◆ m_waitMutex

std::mutex ossim::RWLock::m_waitMutex
private

Definition at line 40 of file RWLock.h.

◆ MIN_INT

const int ossim::RWLock::MIN_INT
private

Definition at line 43 of file RWLock.h.


The documentation for this class was generated from the following files: