1 /* 2 * 3 * Copyright 2016 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 "src/core/lib/iomgr/wakeup_fd_cv.h" 26 27 #include <errno.h> 28 #include <string.h> 29 30 #include <grpc/support/alloc.h> 31 #include <grpc/support/log.h> 32 #include <grpc/support/sync.h> 33 #include <grpc/support/time.h> 34 35 #include "src/core/lib/gpr/useful.h" 36 #include "src/core/lib/gprpp/thd.h" 37 38 #define MAX_TABLE_RESIZE 256 39 40 extern grpc_cv_fd_table g_cvfds; 41 42 static grpc_error* cv_fd_init(grpc_wakeup_fd* fd_info) { 43 unsigned int i, newsize; 44 int idx; 45 gpr_mu_lock(&g_cvfds.mu); 46 if (!g_cvfds.free_fds) { 47 newsize = GPR_MIN(g_cvfds.size * 2, g_cvfds.size + MAX_TABLE_RESIZE); 48 g_cvfds.cvfds = static_cast<grpc_fd_node*>( 49 gpr_realloc(g_cvfds.cvfds, sizeof(grpc_fd_node) * newsize)); 50 for (i = g_cvfds.size; i < newsize; i++) { 51 g_cvfds.cvfds[i].is_set = 0; 52 g_cvfds.cvfds[i].cvs = nullptr; 53 g_cvfds.cvfds[i].next_free = g_cvfds.free_fds; 54 g_cvfds.free_fds = &g_cvfds.cvfds[i]; 55 } 56 g_cvfds.size = newsize; 57 } 58 59 idx = static_cast<int>(g_cvfds.free_fds - g_cvfds.cvfds); 60 g_cvfds.free_fds = g_cvfds.free_fds->next_free; 61 g_cvfds.cvfds[idx].cvs = nullptr; 62 g_cvfds.cvfds[idx].is_set = 0; 63 fd_info->read_fd = GRPC_IDX_TO_FD(idx); 64 fd_info->write_fd = -1; 65 gpr_mu_unlock(&g_cvfds.mu); 66 return GRPC_ERROR_NONE; 67 } 68 69 static grpc_error* cv_fd_wakeup(grpc_wakeup_fd* fd_info) { 70 grpc_cv_node* cvn; 71 gpr_mu_lock(&g_cvfds.mu); 72 g_cvfds.cvfds[GRPC_FD_TO_IDX(fd_info->read_fd)].is_set = 1; 73 cvn = g_cvfds.cvfds[GRPC_FD_TO_IDX(fd_info->read_fd)].cvs; 74 while (cvn) { 75 gpr_cv_signal(cvn->cv); 76 cvn = cvn->next; 77 } 78 gpr_mu_unlock(&g_cvfds.mu); 79 return GRPC_ERROR_NONE; 80 } 81 82 static grpc_error* cv_fd_consume(grpc_wakeup_fd* fd_info) { 83 gpr_mu_lock(&g_cvfds.mu); 84 g_cvfds.cvfds[GRPC_FD_TO_IDX(fd_info->read_fd)].is_set = 0; 85 gpr_mu_unlock(&g_cvfds.mu); 86 return GRPC_ERROR_NONE; 87 } 88 89 static void cv_fd_destroy(grpc_wakeup_fd* fd_info) { 90 if (fd_info->read_fd == 0) { 91 return; 92 } 93 gpr_mu_lock(&g_cvfds.mu); 94 // Assert that there are no active pollers 95 GPR_ASSERT(!g_cvfds.cvfds[GRPC_FD_TO_IDX(fd_info->read_fd)].cvs); 96 g_cvfds.cvfds[GRPC_FD_TO_IDX(fd_info->read_fd)].next_free = g_cvfds.free_fds; 97 g_cvfds.free_fds = &g_cvfds.cvfds[GRPC_FD_TO_IDX(fd_info->read_fd)]; 98 gpr_mu_unlock(&g_cvfds.mu); 99 } 100 101 static int cv_check_availability(void) { return 1; } 102 103 const grpc_wakeup_fd_vtable grpc_cv_wakeup_fd_vtable = { 104 cv_fd_init, cv_fd_consume, cv_fd_wakeup, cv_fd_destroy, 105 cv_check_availability}; 106 107 #endif /* GRPC_POSIX_WAKUP_FD */ 108