-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathSemaphore.cpp
More file actions
57 lines (49 loc) · 1.66 KB
/
Semaphore.cpp
File metadata and controls
57 lines (49 loc) · 1.66 KB
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
56
57
///////////////////////////////////////////////////////////////////////////////
// FILE: Semaphore.cpp
// PROJECT: Micro-Manager
// SUBSYSTEM: MMCore
//-----------------------------------------------------------------------------
// DESCRIPTION: Synchronization primitive with counter.
//
// AUTHOR: Tomas Hanak, tomas.hanak@teledyne.com, 03/03/2021
// Andrej Bencur, andrej.bencur@teledyne.com, 03/03/2021
//
// COPYRIGHT: Teledyne Digital Imaging US, Inc., 2021
//
// LICENSE: This file is distributed under the "Lesser GPL" (LGPL) license.
// License text is included with the source distribution.
//
// This file is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty
// of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
//
// IN NO EVENT SHALL THE COPYRIGHT OWNER OR
// CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
// INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES.
#include "Semaphore.h"
#include <mutex>
namespace mmcore {
namespace internal {
Semaphore::Semaphore()
{
}
Semaphore::Semaphore(size_t initCount)
: count_(initCount)
{
}
void Semaphore::Wait(size_t count)
{
std::unique_lock<std::mutex> lock(mx_);
cv_.wait(lock, [&]() { return count_ >= count; });
count_ -= count;
}
void Semaphore::Release(size_t count)
{
{
std::lock_guard<std::mutex> lock(mx_);
count_ += count;
}
cv_.notify_all();
}
} // namespace internal
} // namespace mmcore