rippled
Loading...
Searching...
No Matches
semaphore.h
1//------------------------------------------------------------------------------
2/*
3 This file is part of rippled: https://github.com/ripple/rippled
4 Copyright (c) 2012, 2013 Ripple Labs Inc.
5
6 Permission to use, copy, modify, and/or distribute this software for any
7 purpose with or without fee is hereby granted, provided that the above
8 copyright notice and this permission notice appear in all copies.
9
10 THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11 WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12 MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13 ANY SPECIAL , DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14 WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15 ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16 OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17*/
18//==============================================================================
19
48#ifndef RIPPLE_CORE_SEMAPHORE_H_INCLUDED
49#define RIPPLE_CORE_SEMAPHORE_H_INCLUDED
50
51#include <condition_variable>
52#include <mutex>
53
54namespace ripple {
55
56template <class Mutex, class CondVar>
58{
59private:
60 Mutex m_mutex;
61 CondVar m_cond;
63
64public:
66
70 explicit basic_semaphore(size_type count = 0) : m_count(count)
71 {
72 }
73
75 void
77 {
79 ++m_count;
80 m_cond.notify_one();
81 }
82
84 void
86 {
88 while (m_count == 0)
89 m_cond.wait(lock);
90 --m_count;
91 }
92
96 bool
98 {
100 if (m_count == 0)
101 return false;
102 --m_count;
103 return true;
104 }
105};
106
108
109} // namespace ripple
110
111#endif
void wait()
Block until notify is called.
Definition semaphore.h:85
basic_semaphore(size_type count=0)
Create the semaphore, with an optional initial count.
Definition semaphore.h:70
bool try_wait()
Perform a non-blocking wait.
Definition semaphore.h:97
void notify()
Increment the count and unblock one waiting thread.
Definition semaphore.h:76
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition algorithm.h:25