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 #ifndef INCLUDE_PERFETTO_BASE_EVENT_H_ 18 #define INCLUDE_PERFETTO_BASE_EVENT_H_ 19 20 #include "perfetto/base/build_config.h" 21 #include "perfetto/base/scoped_file.h" 22 23 #if PERFETTO_BUILDFLAG(PERFETTO_OS_LINUX) || \ 24 PERFETTO_BUILDFLAG(PERFETTO_OS_ANDROID) 25 #define PERFETTO_USE_EVENTFD() 1 26 #else 27 #define PERFETTO_USE_EVENTFD() 0 28 #endif 29 30 namespace perfetto { 31 namespace base { 32 33 // A waitable event that can be used with poll/select. 34 // This is really a wrapper around eventfd_create with a pipe-based fallback 35 // for other platforms where eventfd is not supported. 36 class Event { 37 public: 38 Event(); 39 ~Event(); 40 Event(Event&&) noexcept = default; 41 Event& operator=(Event&&) = default; 42 43 // The non-blocking file descriptor that can be polled to wait for the event. fd()44 int fd() const { return fd_.get(); } 45 46 // Can be called from any thread. 47 void Notify(); 48 49 // Can be called from any thread. If more Notify() are queued a Clear() call 50 // can clear all of them (up to 16 per call). 51 void Clear(); 52 53 private: 54 // The eventfd, when eventfd is supported, otherwise this is the read end of 55 // the pipe for fallback mode. 56 ScopedFile fd_; 57 58 #if !PERFETTO_USE_EVENTFD() 59 // The write end of the wakeup pipe. 60 ScopedFile write_fd_; 61 #endif 62 }; 63 64 } // namespace base 65 } // namespace perfetto 66 67 #endif // INCLUDE_PERFETTO_BASE_EVENT_H_ 68