1 /*
2 * Copyright (C) 2018 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17 #include "perfetto/base/build_config.h"
18
19 #include <errno.h>
20 #include <stdint.h>
21
22 #if PERFETTO_BUILDFLAG(PERFETTO_OS_WIN)
23 #include <Windows.h>
24 #include <synchapi.h>
25 #elif PERFETTO_BUILDFLAG(PERFETTO_OS_LINUX) || \
26 PERFETTO_BUILDFLAG(PERFETTO_OS_ANDROID)
27 #include <sys/eventfd.h>
28 #include <unistd.h>
29 #else // Mac, Fuchsia and other non-Linux UNIXes
30 #include <unistd.h>
31 #endif
32
33 #include "perfetto/base/logging.h"
34 #include "perfetto/ext/base/event_fd.h"
35 #include "perfetto/ext/base/pipe.h"
36 #include "perfetto/ext/base/utils.h"
37
38 namespace perfetto {
39 namespace base {
40
41 EventFd::~EventFd() = default;
42
43 #if PERFETTO_BUILDFLAG(PERFETTO_OS_WIN)
EventFd()44 EventFd::EventFd() {
45 event_handle_.reset(
46 CreateEventA(/*lpEventAttributes=*/nullptr, /*bManualReset=*/true,
47 /*bInitialState=*/false, /*bInitialState=*/nullptr));
48 }
49
Notify()50 void EventFd::Notify() {
51 if (!SetEvent(event_handle_.get())) // 0: fail, !0: success, unlike UNIX.
52 PERFETTO_DFATAL("EventFd::Notify()");
53 }
54
Clear()55 void EventFd::Clear() {
56 if (!ResetEvent(event_handle_.get())) // 0: fail, !0: success, unlike UNIX.
57 PERFETTO_DFATAL("EventFd::Clear()");
58 }
59
60 #elif PERFETTO_BUILDFLAG(PERFETTO_OS_LINUX) || \
61 PERFETTO_BUILDFLAG(PERFETTO_OS_ANDROID)
62
EventFd()63 EventFd::EventFd() {
64 event_handle_.reset(eventfd(/*initval=*/0, EFD_CLOEXEC | EFD_NONBLOCK));
65 PERFETTO_CHECK(event_handle_);
66 }
67
Notify()68 void EventFd::Notify() {
69 const uint64_t value = 1;
70 ssize_t ret = write(event_handle_.get(), &value, sizeof(value));
71 if (ret <= 0 && errno != EAGAIN)
72 PERFETTO_DFATAL("EventFd::Notify()");
73 }
74
Clear()75 void EventFd::Clear() {
76 uint64_t value;
77 ssize_t ret =
78 PERFETTO_EINTR(read(event_handle_.get(), &value, sizeof(value)));
79 if (ret <= 0 && errno != EAGAIN)
80 PERFETTO_DFATAL("EventFd::Clear()");
81 }
82
83 #else
84
EventFd()85 EventFd::EventFd() {
86 // Make the pipe non-blocking so that we never block the waking thread (either
87 // the main thread or another one) when scheduling a wake-up.
88 Pipe pipe = Pipe::Create(Pipe::kBothNonBlock);
89 event_handle_ = ScopedPlatformHandle(std::move(pipe.rd).release());
90 write_fd_ = std::move(pipe.wr);
91 }
92
Notify()93 void EventFd::Notify() {
94 const uint64_t value = 1;
95 ssize_t ret = write(write_fd_.get(), &value, sizeof(uint8_t));
96 if (ret <= 0 && errno != EAGAIN)
97 PERFETTO_DFATAL("EventFd::Notify()");
98 }
99
Clear()100 void EventFd::Clear() {
101 // Drain the byte(s) written to the wake-up pipe. We can potentially read
102 // more than one byte if several wake-ups have been scheduled.
103 char buffer[16];
104 ssize_t ret =
105 PERFETTO_EINTR(read(event_handle_.get(), &buffer[0], sizeof(buffer)));
106 if (ret <= 0 && errno != EAGAIN)
107 PERFETTO_DFATAL("EventFd::Clear()");
108 }
109 #endif
110
111 } // namespace base
112 } // namespace perfetto
113