SWC-DB  v0.5.12 C++ documentations
SWC-DB© (Super Wide Column Database) - High Performance Scalable Database (https://github.com/kashirin-alex/swc-db)
Semaphore.cc
Go to the documentation of this file.
1 /*
2  * SWC-DB© Copyright since 2019 Alex Kashirin <kashirin.alex@gmail.com>
3  * License details at <https://github.com/kashirin-alex/swc-db/#license>
4  */
5 
6 
7 #include "swcdb/core/Semaphore.h"
8 
9 
10 namespace SWC { namespace Core {
11 
12 
13 Semaphore::Semaphore(size_t sz, size_t pre_acquire) noexcept
14  : m_mutex(), m_cv(), m_sz(sz), m_count(pre_acquire) {
15 }
16 
17 Semaphore::~Semaphore() noexcept { }
18 
21  return m_sz - m_count;
22 }
23 
26  return m_count;
27 }
28 
30  Core::UniqueLock lock_wait(m_mutex);
31  if(m_count >= m_sz)
32  m_cv.wait(lock_wait, [this] {return m_count < m_sz;});
33  ++m_count;
34 }
35 
38  --m_count;
39  m_cv.notify_all();
40 }
41 
43  Core::UniqueLock lock_wait(m_mutex);
44  if(m_count >= sz)
45  m_cv.wait(lock_wait, [this, sz] {return m_count < sz;});
46  ++m_count;
47 }
48 
49 void Semaphore::wait_until_under(size_t sz) {
50  Core::UniqueLock lock_wait(m_mutex);
51  if(m_count >= sz)
52  m_cv.wait(lock_wait, [this, sz] {return m_count < sz;});
53 }
54 
56  Core::UniqueLock lock_wait(m_mutex);
57  if(m_count >= m_sz)
58  m_cv.wait(lock_wait, [this] {return m_count < m_sz;});
59  return m_sz - m_count;
60 }
61 
63  Core::UniqueLock lock_wait(m_mutex);
64  if(m_count)
65  m_cv.wait(lock_wait, [this] {return !m_count;});
66 }
67 
68 }}
SWC::Core::Semaphore::m_count
size_t m_count
Definition: Semaphore.h:49
SWC::Core::UniqueLock
Definition: MutexLock.h:68
SWC::Core::ScopedLock
Definition: MutexLock.h:41
SWC::Core::Semaphore::acquire
void acquire()
Definition: Semaphore.cc:29
SWC::Core::Semaphore::available
size_t available()
Definition: Semaphore.cc:19
SWC::Core::Semaphore::wait_all
void wait_all()
Definition: Semaphore.cc:62
SWC::Core::Semaphore::Semaphore
Semaphore(size_t sz=1, size_t pre_acquire=0) noexcept
Definition: Semaphore.cc:13
SWC
The SWC-DB C++ namespace 'SWC'.
Definition: main.cc:12
SWC::Core::Semaphore::~Semaphore
~Semaphore() noexcept
Definition: Semaphore.cc:17
SWC::Core::Semaphore::wait_until_under
void wait_until_under(size_t sz)
Definition: Semaphore.cc:49
SWC::Core::Semaphore::wait_until_under_and_acquire
void wait_until_under_and_acquire(size_t sz)
Definition: Semaphore.cc:42
Semaphore.h
SWC::Core::Semaphore::m_sz
const size_t m_sz
Definition: Semaphore.h:48
SWC::Core::Semaphore::wait_available
size_t wait_available()
Definition: Semaphore.cc:55
SWC::Core::Semaphore::has_pending
bool has_pending()
Definition: Semaphore.cc:24
SWC::Core::Semaphore::release
void release()
Definition: Semaphore.cc:36
SWC::Core::Semaphore::m_cv
std::condition_variable m_cv
Definition: Semaphore.h:47
SWC::Core::Semaphore::m_mutex
std::mutex m_mutex
Definition: Semaphore.h:46