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 
~ManualResetEventManualResetEvent34     ~ManualResetEvent()
35     {
36         close(underlying_);
37     }
38 
39     ManualResetEvent(const ManualResetEvent &) = delete;
40     ManualResetEvent(ManualResetEvent &&other) = default;
41 
RegisterForPollingManualResetEvent42     void RegisterForPolling(Epoller &poller) const
43     {
44         poller.RegisterMe(underlying_);
45     }
46 
IsItYoursManualResetEvent47     [[nodiscard]] bool IsItYours(FileDescriptor descriptor) const
48     {
49         return descriptor == underlying_;
50     }
51 
SetManualResetEvent52     void Set()
53     {
54         uint64_t u = 1;
55         write(underlying_, &u, sizeof(uint64_t));
56     }
57 
ResetManualResetEvent58     void Reset()
59     {
60         uint64_t u;
61         read(underlying_, &u, sizeof(uint64_t));
62     }
63 
64 private:
65     FileDescriptor underlying_;
66 };
67 
68 } // namespace OHOS::NetStack::HttpOverCurl
69 
70 #endif // COMMUNICATIONNETSTACK_MANUAL_RESET_EVENT_H
71