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/semaphore/include/simple_event_notifier.h"
17
18namespace OHOS {
19namespace AI {
20template<class T>
21SimpleEventNotifier<T>::SimpleEventNotifier(FPDestruct destruct)
22    : value_(nullptr), destruct_(destruct), producer_(ISemaphore::MakeShared(0))
23{
24}
25
26template<class T>
27SimpleEventNotifier<T>::~SimpleEventNotifier()
28{
29    CHK_RET_NONE(value_ == nullptr || destruct_ == nullptr);
30    destruct_(value_);
31}
32
33template<class T>
34void SimpleEventNotifier<T>::AddToBack(T *item)
35{
36    value_ = item;
37    producer_->Signal();
38}
39
40template<class T>
41bool SimpleEventNotifier<T>::GetFromFront(int timeOut, T *&item)
42{
43    bool ret = producer_->Wait(timeOut);
44    if (ret) {
45        item = value_;
46        value_ = nullptr;
47    }
48    return ret;
49}
50} // namespace AI
51} // namespace OHOS