1 /*
2  * Copyright (c) 2021 Huawei Device Co., Ltd.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
16 #include "platform/event/include/i_event.h"
17 
18 #include <condition_variable>
19 #include <memory>
20 #include <mutex>
21 #include <string>
22 #include <thread>
23 
24 #include "utils/constants/constants.h"
25 #include "utils/inf_cast_impl.h"
26 
27 namespace OHOS {
28 namespace AI {
29 class Event {
30 public:
Event()31     Event() : flag_(false)
32     {
33     }
34 
~Event()35     ~Event()
36     {
37     }
38 
Wait() const39     inline void Wait() const
40     {
41         std::unique_lock<std::mutex> lock(mutex_);
42         condition_.wait(lock, [&]()->bool { return flag_; });
43     }
44 
Wait(const int milliSeconds) const45     inline bool Wait(const int milliSeconds) const
46     {
47         using milliseconds_type = std::chrono::duration<int, std::milli>;
48         std::unique_lock<std::mutex> lock(mutex_);
49         return condition_.wait_for(lock, milliseconds_type(milliSeconds), [&]()->bool { return flag_; });
50     }
51 
Signal()52     inline bool Signal()
53     {
54         mutex_.lock();
55         bool signalled = flag_;
56         flag_ = true;
57         mutex_.unlock();
58         condition_.notify_all();
59         return !signalled;
60     }
61 
Reset()62     inline bool Reset()
63     {
64         mutex_.lock();
65         bool signalled = flag_;
66         flag_ = false;
67         mutex_.unlock();
68         return signalled;
69     }
70 
IsSet() const71     inline bool IsSet() const
72     {
73         return flag_;
74     }
75 
76 private:
77     bool flag_;
78     mutable std::mutex mutex_;
79     mutable std::condition_variable condition_;
80 };
81 
82 DEFINE_IMPL_CLASS_CAST(EventCast, IEvent, Event);
83 
84 class EventDeleter {
85 public:
operator ()(IEvent * p) const86     void operator ()(IEvent *p) const
87     {
88         EventCast::Destroy(p);
89     }
90 };
91 
MakeShared()92 std::shared_ptr<IEvent> IEvent::MakeShared()
93 {
94     std::shared_ptr<IEvent> sp(EventCast::Create(), EventDeleter());
95     return sp;
96 }
97 
Wait(const int milliSeconds) const98 bool IEvent::Wait(const int milliSeconds) const
99 {
100     if (milliSeconds <= 0) {
101         EventCast::Ref(this).Wait();
102         return true;
103     }
104     return EventCast::Ref(this).Wait(milliSeconds);
105 }
106 
Signal()107 bool IEvent::Signal()
108 {
109     return EventCast::Ref(this).Signal();
110 }
111 
Reset()112 bool IEvent::Reset()
113 {
114     return EventCast::Ref(this).Reset();
115 }
116 
IsSet() const117 bool IEvent::IsSet() const
118 {
119     return EventCast::Ref(this).IsSet();
120 }
121 } // namespace AI
122 } // namespace OHOS
123