1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
// Copyright (c) 2012-2017 VideoStitch SAS
// Copyright (c) 2018 stitchEm
#ifndef SEMAPHORE_HPP_
#define SEMAPHORE_HPP_
#include "../config.hpp"
#include <mutex>
#include <condition_variable>
namespace VideoStitch {
/** \class Semaphore
* \brief Custom Semaphore implementation.
* On notify we increase internal counter.
* On wait - we return if the counter is greater then 0 otherwise - we wait for that to happen.
* On wait_for - we return if the counter is greater then 0 otherwise - we wait for that to happen or until timeout is
* expired. In the first case we return true, in the latter case - false.
*/
class VS_EXPORT Semaphore {
// NOTE: only the thread that locked the mutex can unlock it.
public:
/**
* @brief Create Semaphore with given initial counter
* @param initialCount - initial value of Semaphore counter.
*/
explicit Semaphore(int initialCount = 1);
/**
* @brief Increase Semaphore counter.
*/
void notify();
/**
* @brief Return if the counter is greater then 0 and decrease counter. Otherwise wait until counter is greater then
* 0.
*/
void wait();
/**
* @brief Return if the counter is greater then 0 and decrease counter. Otherwise wait until counter is greater then 0
* or timeout is expired.
* @return False if timeout expired. True otherwise.
*/
bool wait_for(unsigned timeOutMs);
private:
volatile int count;
std::mutex mutex;
std::condition_variable condition;
};
} // namespace VideoStitch
#endif