1 //
2 //
3 // Copyright 2015 gRPC authors.
4 //
5 // Licensed under the Apache License, Version 2.0 (the "License");
6 // you may not use this file except in compliance with the License.
7 // You may obtain a copy of the License at
8 //
9 //     http://www.apache.org/licenses/LICENSE-2.0
10 //
11 // Unless required by applicable law or agreed to in writing, software
12 // distributed under the License is distributed on an "AS IS" BASIS,
13 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 // See the License for the specific language governing permissions and
15 // limitations under the License.
16 //
17 //
18 
19 #include <grpc/support/port_platform.h>
20 
21 #include "src/core/lib/iomgr/port.h"
22 
23 #ifdef GRPC_POSIX_WAKEUP_FD
24 
25 #include <stddef.h>
26 
27 #include "src/core/lib/iomgr/wakeup_fd_pipe.h"
28 #include "src/core/lib/iomgr/wakeup_fd_posix.h"
29 
30 static const grpc_wakeup_fd_vtable* wakeup_fd_vtable = nullptr;
31 
32 int grpc_allow_specialized_wakeup_fd = 1;
33 int grpc_allow_pipe_wakeup_fd = 1;
34 
35 static int has_real_wakeup_fd = 1;
36 
37 static gpr_once once_init_wakeup_fd = GPR_ONCE_INIT;
38 
grpc_wakeup_fd_global_init(void)39 void grpc_wakeup_fd_global_init(void) {
40   gpr_once_init(&once_init_wakeup_fd, []() {
41     if (grpc_allow_specialized_wakeup_fd &&
42         grpc_specialized_wakeup_fd_vtable.check_availability()) {
43       wakeup_fd_vtable = &grpc_specialized_wakeup_fd_vtable;
44     } else if (grpc_allow_pipe_wakeup_fd &&
45                grpc_pipe_wakeup_fd_vtable.check_availability()) {
46       wakeup_fd_vtable = &grpc_pipe_wakeup_fd_vtable;
47     } else {
48       has_real_wakeup_fd = 0;
49     }
50   });
51 }
52 
grpc_wakeup_fd_global_destroy(void)53 void grpc_wakeup_fd_global_destroy(void) {}
54 
grpc_has_wakeup_fd(void)55 int grpc_has_wakeup_fd(void) { return has_real_wakeup_fd; }
56 
grpc_wakeup_fd_init(grpc_wakeup_fd * fd_info)57 grpc_error_handle grpc_wakeup_fd_init(grpc_wakeup_fd* fd_info) {
58   return wakeup_fd_vtable->init(fd_info);
59 }
60 
grpc_wakeup_fd_consume_wakeup(grpc_wakeup_fd * fd_info)61 grpc_error_handle grpc_wakeup_fd_consume_wakeup(grpc_wakeup_fd* fd_info) {
62   return wakeup_fd_vtable->consume(fd_info);
63 }
64 
grpc_wakeup_fd_wakeup(grpc_wakeup_fd * fd_info)65 grpc_error_handle grpc_wakeup_fd_wakeup(grpc_wakeup_fd* fd_info) {
66   return wakeup_fd_vtable->wakeup(fd_info);
67 }
68 
grpc_wakeup_fd_destroy(grpc_wakeup_fd * fd_info)69 void grpc_wakeup_fd_destroy(grpc_wakeup_fd* fd_info) {
70   wakeup_fd_vtable->destroy(fd_info);
71 }
72 
73 #endif  // GRPC_POSIX_WAKEUP_FD
74