• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #include "src/core/lib/iomgr/wakeup_fd_cv.h"
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 int has_real_wakeup_fd = 1;
36 int cv_wakeup_fds_enabled = 0;
37 
grpc_wakeup_fd_global_init(void)38 void grpc_wakeup_fd_global_init(void) {
39   if (grpc_allow_specialized_wakeup_fd &&
40       grpc_specialized_wakeup_fd_vtable.check_availability()) {
41     wakeup_fd_vtable = &grpc_specialized_wakeup_fd_vtable;
42   } else if (grpc_allow_pipe_wakeup_fd &&
43              grpc_pipe_wakeup_fd_vtable.check_availability()) {
44     wakeup_fd_vtable = &grpc_pipe_wakeup_fd_vtable;
45   } else {
46     has_real_wakeup_fd = 0;
47   }
48 }
49 
grpc_wakeup_fd_global_destroy(void)50 void grpc_wakeup_fd_global_destroy(void) { wakeup_fd_vtable = nullptr; }
51 
grpc_has_wakeup_fd(void)52 int grpc_has_wakeup_fd(void) { return has_real_wakeup_fd; }
53 
grpc_cv_wakeup_fds_enabled(void)54 int grpc_cv_wakeup_fds_enabled(void) { return cv_wakeup_fds_enabled; }
55 
grpc_enable_cv_wakeup_fds(int enable)56 void grpc_enable_cv_wakeup_fds(int enable) { cv_wakeup_fds_enabled = enable; }
57 
grpc_wakeup_fd_init(grpc_wakeup_fd * fd_info)58 grpc_error* grpc_wakeup_fd_init(grpc_wakeup_fd* fd_info) {
59   if (cv_wakeup_fds_enabled) {
60     return grpc_cv_wakeup_fd_vtable.init(fd_info);
61   }
62   return wakeup_fd_vtable->init(fd_info);
63 }
64 
grpc_wakeup_fd_consume_wakeup(grpc_wakeup_fd * fd_info)65 grpc_error* grpc_wakeup_fd_consume_wakeup(grpc_wakeup_fd* fd_info) {
66   if (cv_wakeup_fds_enabled) {
67     return grpc_cv_wakeup_fd_vtable.consume(fd_info);
68   }
69   return wakeup_fd_vtable->consume(fd_info);
70 }
71 
grpc_wakeup_fd_wakeup(grpc_wakeup_fd * fd_info)72 grpc_error* grpc_wakeup_fd_wakeup(grpc_wakeup_fd* fd_info) {
73   if (cv_wakeup_fds_enabled) {
74     return grpc_cv_wakeup_fd_vtable.wakeup(fd_info);
75   }
76   return wakeup_fd_vtable->wakeup(fd_info);
77 }
78 
grpc_wakeup_fd_destroy(grpc_wakeup_fd * fd_info)79 void grpc_wakeup_fd_destroy(grpc_wakeup_fd* fd_info) {
80   if (cv_wakeup_fds_enabled) {
81     grpc_cv_wakeup_fd_vtable.destroy(fd_info);
82   } else {
83     wakeup_fd_vtable->destroy(fd_info);
84   }
85 }
86 
87 #endif /* GRPC_POSIX_WAKEUP_FD */
88