OSSIM - Open Source Software Image Map  Version 1.9.0 (20180803)
RWLock.cpp
Go to the documentation of this file.
1 #include <ossim/base/RWLock.h>
2 
4  m_refCounter(0),
5  MIN_INT(std::numeric_limits<int>::min())
6 {
7 
8 }
9 
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 }
31 
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 }
39 
41 {
42  m_refCounter.store(0, std::memory_order_release);
43  m_waitConditional.notify_all();
44 }
45 
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 }
58 
60 {
61  return m_refCounter.fetch_add(1, std::memory_order_acquire) >= 0;
62 }
63 
65 {
66  m_refCounter.fetch_sub(1, std::memory_order_release);
67  m_waitConditional.notify_one();
68 }
void lockWrite()
Definition: RWLock.cpp:10
void unlockRead()
Definition: RWLock.cpp:64
bool tryLockRead()
Definition: RWLock.cpp:59
void lockRead()
Definition: RWLock.cpp:46
bool tryLockWrite()
Definition: RWLock.cpp:32
void unlockWrite()
Definition: RWLock.cpp:40
#define min(a, b)
Definition: auxiliary.h:75