• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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_QNX)
26 #include <unistd.h>
27 #elif PERFETTO_BUILDFLAG(PERFETTO_OS_LINUX) || \
28     PERFETTO_BUILDFLAG(PERFETTO_OS_ANDROID)
29 #include <sys/eventfd.h>
30 #include <unistd.h>
31 #else  // Mac, Fuchsia and other non-Linux UNIXes
32 #include <unistd.h>
33 #endif
34 
35 #include "perfetto/base/logging.h"
36 #include "perfetto/ext/base/event_fd.h"
37 #include "perfetto/ext/base/pipe.h"
38 #include "perfetto/ext/base/utils.h"
39 
40 namespace perfetto {
41 namespace base {
42 
43 EventFd::~EventFd() = default;
44 
45 #if PERFETTO_BUILDFLAG(PERFETTO_OS_WIN)
EventFd()46 EventFd::EventFd() {
47   event_handle_.reset(
48       CreateEventA(/*lpEventAttributes=*/nullptr, /*bManualReset=*/true,
49                    /*bInitialState=*/false, /*bInitialState=*/nullptr));
50 }
51 
Notify()52 void EventFd::Notify() {
53   if (!SetEvent(event_handle_.get()))  // 0: fail, !0: success, unlike UNIX.
54     PERFETTO_DFATAL("EventFd::Notify()");
55 }
56 
Clear()57 void EventFd::Clear() {
58   if (!ResetEvent(event_handle_.get()))  // 0: fail, !0: success, unlike UNIX.
59     PERFETTO_DFATAL("EventFd::Clear()");
60 }
61 
62 #elif PERFETTO_BUILDFLAG(PERFETTO_OS_LINUX_BUT_NOT_QNX) || \
63     PERFETTO_BUILDFLAG(PERFETTO_OS_ANDROID)
64 
EventFd()65 EventFd::EventFd() {
66   event_handle_.reset(eventfd(/*initval=*/0, EFD_CLOEXEC | EFD_NONBLOCK));
67   PERFETTO_CHECK(event_handle_);
68 }
69 
Notify()70 void EventFd::Notify() {
71   const uint64_t value = 1;
72   ssize_t ret = write(event_handle_.get(), &value, sizeof(value));
73   if (ret <= 0 && errno != EAGAIN)
74     PERFETTO_DFATAL("EventFd::Notify()");
75 }
76 
Clear()77 void EventFd::Clear() {
78   uint64_t value;
79   ssize_t ret =
80       PERFETTO_EINTR(read(event_handle_.get(), &value, sizeof(value)));
81   if (ret <= 0 && errno != EAGAIN)
82     PERFETTO_DFATAL("EventFd::Clear()");
83 }
84 
85 #else
86 
EventFd()87 EventFd::EventFd() {
88   // Make the pipe non-blocking so that we never block the waking thread (either
89   // the main thread or another one) when scheduling a wake-up.
90   Pipe pipe = Pipe::Create(Pipe::kBothNonBlock);
91   event_handle_ = ScopedPlatformHandle(std::move(pipe.rd).release());
92   write_fd_ = std::move(pipe.wr);
93 }
94 
Notify()95 void EventFd::Notify() {
96   const uint64_t value = 1;
97   ssize_t ret = write(write_fd_.get(), &value, sizeof(uint8_t));
98   if (ret <= 0 && errno != EAGAIN)
99     PERFETTO_DFATAL("EventFd::Notify()");
100 }
101 
Clear()102 void EventFd::Clear() {
103   // Drain the byte(s) written to the wake-up pipe. We can potentially read
104   // more than one byte if several wake-ups have been scheduled.
105   char buffer[16];
106   ssize_t ret =
107       PERFETTO_EINTR(read(event_handle_.get(), &buffer[0], sizeof(buffer)));
108   if (ret <= 0 && errno != EAGAIN)
109     PERFETTO_DFATAL("EventFd::Clear()");
110 }
111 #endif
112 
113 }  // namespace base
114 }  // namespace perfetto
115