1 | /** |
2 | * Copyright 2021 Alibaba, Inc. and its affiliates. All Rights Reserved. |
3 | * |
4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
5 | * you may not use this file except in compliance with the License. |
6 | * You may obtain a copy of the License at |
7 | * |
8 | * http://www.apache.org/licenses/LICENSE-2.0 |
9 | * |
10 | * Unless required by applicable law or agreed to in writing, software |
11 | * distributed under the License is distributed on an "AS IS" BASIS, |
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
13 | * See the License for the specific language governing permissions and |
14 | * limitations under the License. |
15 | |
16 | * \author Haichao.chc |
17 | * \date Oct 2020 |
18 | * \brief Simple condition wait and notify class |
19 | */ |
20 | |
21 | #pragma once |
22 | |
23 | #include <chrono> |
24 | #include <condition_variable> |
25 | #include <mutex> |
26 | |
27 | namespace proxima { |
28 | namespace be { |
29 | |
30 | /* |
31 | * Simple wait notifier for multi-threads message sync |
32 | */ |
33 | class WaitNotifier { |
34 | public: |
35 | //! Constructor |
36 | WaitNotifier() = default; |
37 | |
38 | //! Destructor |
39 | ~WaitNotifier() = default; |
40 | |
41 | public: |
42 | // Wait for condition var notify |
43 | void wait(); |
44 | |
45 | // Wait for deserted time |
46 | void wait_until(const std::chrono::system_clock::time_point &tm_point); |
47 | |
48 | // Wait for some duration time |
49 | void wait_for(const std::chrono::system_clock::duration &tm_duration); |
50 | |
51 | // Notify once |
52 | void notify(); |
53 | |
54 | private: |
55 | bool notified_{false}; |
56 | std::mutex mutex_; |
57 | std::condition_variable cv_; |
58 | }; |
59 | |
60 | } // namespace be |
61 | } // end namespace proxima |
62 | |