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 #ifndef SIMPLE_EVENT_NOTIFIER_H 17 #define SIMPLE_EVENT_NOTIFIER_H 18 19 #include "platform/semaphore/include/i_semaphore.h" 20 21 namespace OHOS { 22 namespace AI { 23 template<class T> 24 class SimpleEventNotifier { 25 public: 26 using FPDestruct = void(*) (T *&t); 27 explicit SimpleEventNotifier(FPDestruct destruct = nullptr); 28 ~SimpleEventNotifier(); 29 30 /** 31 * Return the execution result to the waiting party 32 * 33 * @param [in] item The task which wait for notification 34 */ 35 void AddToBack(T *item); 36 37 /** 38 * Wait for notice within the time period. 39 * 40 * @param [in] timeOut The range of timeout 41 * @param [out] item The result which is calculated from task 42 * @return Returns true if the operation is successful, returns false otherwise. 43 */ 44 bool GetFromFront(int timeOut, T *&item); 45 46 private: 47 T *value_; 48 FPDestruct destruct_; 49 std::shared_ptr<ISemaphore> producer_; 50 }; 51 } // namespace AI 52 } // namespace OHOS 53 54 #include "platform/semaphore/include/simple_event_notifier.inl" 55 56 #endif // SIMPLE_EVENT_NOTIFIER_H