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 #include "event_notifier.h"
16
17 #include <cerrno>
18 #include <climits>
19 #include <cstring>
20 #include <fcntl.h>
21 #include <sys/eventfd.h>
22 #include <unistd.h>
23 #include "logging.h"
24
25 #ifndef EFD_SEMAPHORE
26 #define EFD_SEMAPHORE 1
27 #endif
28
Create(unsigned int initValue,unsigned int mask)29 EventNotifierPtr EventNotifier::Create(unsigned int initValue, unsigned int mask)
30 {
31 return std::make_shared<EventNotifier>(initValue, mask);
32 }
33
CreateWithFd(int fd)34 EventNotifierPtr EventNotifier::CreateWithFd(int fd)
35 {
36 return std::make_shared<EventNotifier>(fd);
37 }
38
EventNotifier(unsigned int initValue,unsigned int mask)39 EventNotifier::EventNotifier(unsigned int initValue, unsigned int mask) : fd_(-1), flags_(O_CLOEXEC)
40 {
41 if (mask & NONBLOCK) {
42 flags_ |= O_NONBLOCK;
43 }
44 if (mask & SEMAPHORE) {
45 flags_ |= EFD_SEMAPHORE;
46 }
47 fd_ = eventfd(initValue, flags_);
48 CHECK_TRUE(fd_ >= 0, NO_RETVAL, "create eventfd FAILED, %d", errno);
49 HILOG_DEBUG(LOG_CORE, "EventNotifier create eventfd %d done!", fd_);
50 }
51
EventNotifier(int fd)52 EventNotifier::EventNotifier(int fd) : fd_(fd), flags_(0)
53 {
54 int flags = fcntl(fd_, F_GETFL);
55 CHECK_TRUE(flags >= 0, NO_RETVAL, "get flags of fd %d FAILED, %d", fd, errno);
56 HILOG_DEBUG(LOG_CORE, "EventNotifier bind eventfd %d done!", fd_);
57 }
58
~EventNotifier()59 EventNotifier::~EventNotifier()
60 {
61 HILOG_DEBUG(LOG_CORE, "EventNotifier close eventfd %d", fd_);
62 close(fd_);
63 }
64
GetFd() const65 int EventNotifier::GetFd() const
66 {
67 return fd_;
68 }
69
IsNonBlocking() const70 bool EventNotifier::IsNonBlocking() const
71 {
72 return flags_ & O_NONBLOCK;
73 }
74
IsSemaphore() const75 bool EventNotifier::IsSemaphore() const
76 {
77 return flags_ & EFD_SEMAPHORE;
78 }
79
Take() const80 uint64_t EventNotifier::Take() const
81 {
82 uint64_t value = UINT64_MAX;
83 int retval = TEMP_FAILURE_RETRY(read(fd_, &value, sizeof(value)));
84 CHECK_TRUE(retval == sizeof(value), 0, "read value from eventfd %d failed, %d!", fd_, errno);
85 return value;
86 }
87
Post(uint64_t value) const88 bool EventNotifier::Post(uint64_t value) const
89 {
90 int retval = TEMP_FAILURE_RETRY(write(fd_, &value, sizeof(value)));
91 CHECK_TRUE(retval == sizeof(value), false, "write value to eventfd %d failed, %d!", fd_, errno);
92 return true;
93 }
94