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 = read(event_handle_.get(), &value, sizeof(value));
78 if (ret <= 0 && errno != EAGAIN)
79 PERFETTO_DFATAL("EventFd::Clear()");
80 }
81
82 #else
83
EventFd()84 EventFd::EventFd() {
85 // Make the pipe non-blocking so that we never block the waking thread (either
86 // the main thread or another one) when scheduling a wake-up.
87 Pipe pipe = Pipe::Create(Pipe::kBothNonBlock);
88 event_handle_ = ScopedPlatformHandle(std::move(pipe.rd).release());
89 write_fd_ = std::move(pipe.wr);
90 }
91
Notify()92 void EventFd::Notify() {
93 const uint64_t value = 1;
94 ssize_t ret = write(write_fd_.get(), &value, sizeof(uint8_t));
95 if (ret <= 0 && errno != EAGAIN)
96 PERFETTO_DFATAL("EventFd::Notify()");
97 }
98
Clear()99 void EventFd::Clear() {
100 // Drain the byte(s) written to the wake-up pipe. We can potentially read
101 // more than one byte if several wake-ups have been scheduled.
102 char buffer[16];
103 ssize_t ret = read(event_handle_.get(), &buffer[0], sizeof(buffer));
104 if (ret <= 0 && errno != EAGAIN)
105 PERFETTO_DFATAL("EventFd::Clear()");
106 }
107 #endif
108
109 } // namespace base
110 } // namespace perfetto
111