quic/qbox
Loading...
Searching...
No Matches
semaphore.h
1/*
2 * Copyright (c) 2022-2023 Qualcomm Innovation Center, Inc. All Rights Reserved.
3 * Author: GreenSocs 2022
4 *
5 * SPDX-License-Identifier: BSD-3-Clause
6 */
7
8#ifndef SEMAPHORE_H
9#define SEMAPHORE_H
10
11namespace gs {
12
13#include <mutex>
14#include <condition_variable>
15
17{
18private:
19 std::mutex mutex_;
20 std::condition_variable condition_;
21 unsigned long count_ = 0; // Initialized as locked.
22
23public:
24 void notify()
25 {
26 std::lock_guard<decltype(mutex_)> lock(mutex_);
27 ++count_;
28 condition_.notify_one();
29 }
30
31 void wait()
32 {
33 std::unique_lock<decltype(mutex_)> lock(mutex_);
34 while (!count_) // Handle spurious wake-ups.
35 condition_.wait(lock);
36 --count_;
37 }
38
39 bool try_wait()
40 {
41 std::lock_guard<decltype(mutex_)> lock(mutex_);
42 if (count_) {
43 --count_;
44 return true;
45 }
46 return false;
47 }
48
49 semaphore() = default;
51 {
52 // don't block on destruction
53 notify();
54 }
55};
56} // namespace gs
57
58#endif // SEMAPHORE_H
Definition target.h:160
Definition semaphore.h:17
Tool which reads a Lua configuration file and sets parameters.
Definition biflow.cc:10