• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
24 #include "logging.h"
25 
26 #ifndef EFD_SEMAPHORE
27 #define EFD_SEMAPHORE 1
28 #endif
29 
Create(unsigned int initValue,unsigned int mask)30 EventNotifierPtr EventNotifier::Create(unsigned int initValue, unsigned int mask)
31 {
32     return std::make_shared<EventNotifier>(initValue, mask);
33 }
34 
CreateWithFd(int fd)35 EventNotifierPtr EventNotifier::CreateWithFd(int fd)
36 {
37     return std::make_shared<EventNotifier>(fd);
38 }
39 
EventNotifier(unsigned int initValue,unsigned int mask)40 EventNotifier::EventNotifier(unsigned int initValue, unsigned int mask) : fd_(-1), flags_(O_CLOEXEC)
41 {
42     if (mask & NONBLOCK) {
43         flags_ |= O_NONBLOCK;
44     }
45     if (mask & SEMAPHORE) {
46         flags_ |= EFD_SEMAPHORE;
47     }
48     fd_ = eventfd(initValue, flags_);
49     CHECK_TRUE(fd_ >= 0, NO_RETVAL, "create eventfd FAILED, %d", errno);
50     HILOG_DEBUG(LOG_CORE, "EventNotifier create eventfd %d done!", fd_);
51 }
52 
EventNotifier(int fd)53 EventNotifier::EventNotifier(int fd) : fd_(fd), flags_(0)
54 {
55     int flags = fcntl(fd_, F_GETFL);
56     CHECK_TRUE(flags >= 0, NO_RETVAL, "get flags of fd %d FAILED, %d", fd, errno);
57     HILOG_DEBUG(LOG_CORE, "EventNotifier bind eventfd %d done!", fd_);
58 }
59 
~EventNotifier()60 EventNotifier::~EventNotifier()
61 {
62     HILOG_DEBUG(LOG_CORE, "EventNotifier close eventfd %d", fd_);
63     close(fd_);
64 }
65 
GetFd() const66 int EventNotifier::GetFd() const
67 {
68     return fd_;
69 }
70 
IsNonBlocking() const71 bool EventNotifier::IsNonBlocking() const
72 {
73     return flags_ & O_NONBLOCK;
74 }
75 
IsSemaphore() const76 bool EventNotifier::IsSemaphore() const
77 {
78     return flags_ & EFD_SEMAPHORE;
79 }
80 
Take() const81 uint64_t EventNotifier::Take() const
82 {
83     uint64_t value = UINT64_MAX;
84     int retval = TEMP_FAILURE_RETRY(read(fd_, &value, sizeof(value)));
85     CHECK_TRUE(retval == sizeof(value), false, "read value from eventfd %d failed, %d!", fd_, errno);
86     return value;
87 }
88 
Post(uint64_t value) const89 bool EventNotifier::Post(uint64_t value) const
90 {
91     int retval = TEMP_FAILURE_RETRY(write(fd_, &value, sizeof(value)));
92     CHECK_TRUE(retval == sizeof(value), false, "write value to eventfd %d failed, %d!", fd_, errno);
93     return true;
94 }
95