1 // Copyright 2022 The gRPC Authors 2 // 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 #include <grpc/support/port_platform.h> 15 16 #include <memory> 17 18 #include "absl/status/status.h" 19 #include "absl/status/statusor.h" 20 #include "src/core/lib/event_engine/posix_engine/wakeup_fd_eventfd.h" 21 #include "src/core/lib/event_engine/posix_engine/wakeup_fd_pipe.h" 22 #include "src/core/lib/event_engine/posix_engine/wakeup_fd_posix.h" 23 #include "src/core/lib/iomgr/port.h" 24 25 namespace grpc_event_engine { 26 namespace experimental { 27 28 #ifdef GRPC_POSIX_WAKEUP_FD 29 NotSupported()30absl::StatusOr<std::unique_ptr<WakeupFd>> NotSupported() { 31 return absl::NotFoundError("Wakeup-fd is not supported on this system"); 32 } 33 34 namespace { 35 absl::StatusOr<std::unique_ptr<WakeupFd>> (*g_wakeup_fd_fn)() = 36 []() -> absl::StatusOr<std::unique_ptr<WakeupFd>> (*)() { 37 #ifndef GRPC_POSIX_NO_SPECIAL_WAKEUP_FD 38 if (EventFdWakeupFd::IsSupported()) { 39 return &EventFdWakeupFd::CreateEventFdWakeupFd; 40 } 41 #endif // GRPC_POSIX_NO_SPECIAL_WAKEUP_FD 42 if (PipeWakeupFd::IsSupported()) { 43 return &PipeWakeupFd::CreatePipeWakeupFd; 44 } 45 return NotSupported; 46 }(); 47 } // namespace 48 SupportsWakeupFd()49bool SupportsWakeupFd() { return g_wakeup_fd_fn != NotSupported; } 50 CreateWakeupFd()51absl::StatusOr<std::unique_ptr<WakeupFd>> CreateWakeupFd() { 52 return g_wakeup_fd_fn(); 53 } 54 55 #else // GRPC_POSIX_WAKEUP_FD 56 57 bool SupportsWakeupFd() { return false; } 58 59 absl::StatusOr<std::unique_ptr<WakeupFd>> CreateWakeupFd() { 60 return absl::NotFoundError("Wakeup-fd is not supported on this system"); 61 } 62 63 #endif // GRPC_POSIX_WAKEUP_FD 64 65 } // namespace experimental 66 } // namespace grpc_event_engine 67