1 /* 2 * Copyright (C) 2022 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 #ifndef SYNCHRONIZE_EVENT_H 16 #define SYNCHRONIZE_EVENT_H 17 #include <pthread.h> 18 19 namespace OHOS { 20 namespace NFC { 21 class SynchronizeEvent { 22 public: 23 SynchronizeEvent(); 24 ~SynchronizeEvent(); 25 26 void Start(); 27 void End(); 28 void Wait(); 29 bool Wait(long ms); 30 void NotifyOne(); 31 32 private: 33 pthread_mutex_t lock_; 34 pthread_cond_t cond_; 35 }; 36 37 class SynchronizeGuard { 38 public: SynchronizeGuard(SynchronizeEvent & event)39 explicit SynchronizeGuard(SynchronizeEvent& event) : syncEvent_(event) 40 { 41 event.Start(); 42 }; 43 ~SynchronizeGuard()44 ~SynchronizeGuard() 45 { 46 syncEvent_.End(); 47 }; 48 49 private: 50 SynchronizeEvent& syncEvent_; 51 }; 52 } // namespace NFC 53 } // namespace OHOS 54 #endif // SYNCHRONIZE_EVENT_H 55