1 /* 2 * Copyright (c) 2024 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 COMMUNICATIONNETSTACK_MANUAL_RESET_EVENT_H 17 #define COMMUNICATIONNETSTACK_MANUAL_RESET_EVENT_H 18 19 #include <sys/epoll.h> 20 #include <sys/eventfd.h> 21 22 #include <unistd.h> 23 24 #include "epoller.h" 25 26 namespace OHOS::NetStack::HttpOverCurl { 27 28 struct ManualResetEvent { ManualResetEventManualResetEvent29 ManualResetEvent() 30 { 31 underlying_ = eventfd(0, 0); 32 } 33 34 #ifdef HTTP_HANDOVER_FEATURE ManualResetEventManualResetEvent35 explicit ManualResetEvent(bool isSemaphore) 36 { 37 if (isSemaphore) { 38 underlying_ = eventfd(0, EFD_SEMAPHORE); 39 } else { 40 underlying_ = eventfd(0, 0); 41 } 42 } 43 #endif 44 ~ManualResetEventManualResetEvent45 ~ManualResetEvent() 46 { 47 close(underlying_); 48 } 49 50 ManualResetEvent(const ManualResetEvent &) = delete; 51 ManualResetEvent(ManualResetEvent &&other) = default; 52 RegisterForPollingManualResetEvent53 void RegisterForPolling(Epoller &poller) const 54 { 55 poller.RegisterMe(underlying_); 56 } 57 IsItYoursManualResetEvent58 [[nodiscard]] bool IsItYours(FileDescriptor descriptor) const 59 { 60 return descriptor == underlying_; 61 } 62 SetManualResetEvent63 void Set() 64 { 65 uint64_t u = 1; 66 write(underlying_, &u, sizeof(uint64_t)); 67 } 68 ResetManualResetEvent69 void Reset() 70 { 71 uint64_t u; 72 read(underlying_, &u, sizeof(uint64_t)); 73 } 74 75 private: 76 FileDescriptor underlying_; 77 }; 78 79 } // namespace OHOS::NetStack::HttpOverCurl 80 81 #endif // COMMUNICATIONNETSTACK_MANUAL_RESET_EVENT_H 82