Ken Payson | 31caabd | 2016-08-06 21:27:29 -0700 | [diff] [blame] | 1 | /* |
| 2 | * |
Jan Tattermusch | 7897ae9 | 2017-06-07 22:57:36 +0200 | [diff] [blame] | 3 | * Copyright 2016 gRPC authors. |
Ken Payson | 31caabd | 2016-08-06 21:27:29 -0700 | [diff] [blame] | 4 | * |
Jan Tattermusch | 7897ae9 | 2017-06-07 22:57:36 +0200 | [diff] [blame] | 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 |
Ken Payson | 31caabd | 2016-08-06 21:27:29 -0700 | [diff] [blame] | 8 | * |
Jan Tattermusch | 7897ae9 | 2017-06-07 22:57:36 +0200 | [diff] [blame] | 9 | * http://www.apache.org/licenses/LICENSE-2.0 |
Ken Payson | 31caabd | 2016-08-06 21:27:29 -0700 | [diff] [blame] | 10 | * |
Jan Tattermusch | 7897ae9 | 2017-06-07 22:57:36 +0200 | [diff] [blame] | 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. |
Ken Payson | 31caabd | 2016-08-06 21:27:29 -0700 | [diff] [blame] | 16 | * |
| 17 | */ |
| 18 | |
| 19 | /* |
| 20 | * wakeup_fd_cv uses condition variables to implement wakeup fds. |
| 21 | * |
| 22 | * It is intended for use only in cases when eventfd() and pipe() are not |
| 23 | * available. It can only be used with the "poll" engine. |
| 24 | * |
| 25 | * Implementation: |
| 26 | * A global table of cv wakeup fds is mantained. A cv wakeup fd is a negative |
| 27 | * file descriptor. poll() is then run in a background thread with only the |
| 28 | * real socket fds while we wait on a condition variable trigged by either the |
Ken Payson | e02c7ed | 2016-09-29 09:15:49 -0700 | [diff] [blame] | 29 | * poll() completion or a wakeup_fd() call. |
Ken Payson | 31caabd | 2016-08-06 21:27:29 -0700 | [diff] [blame] | 30 | * |
| 31 | */ |
| 32 | |
| 33 | #ifndef GRPC_CORE_LIB_IOMGR_WAKEUP_FD_CV_H |
| 34 | #define GRPC_CORE_LIB_IOMGR_WAKEUP_FD_CV_H |
| 35 | |
Ken Payson | 82e4ec7 | 2016-10-13 12:26:01 -0700 | [diff] [blame] | 36 | #include <grpc/support/sync.h> |
Ken Payson | 31caabd | 2016-08-06 21:27:29 -0700 | [diff] [blame] | 37 | |
Ken Payson | 82e4ec7 | 2016-10-13 12:26:01 -0700 | [diff] [blame] | 38 | #include "src/core/lib/iomgr/ev_posix.h" |
| 39 | |
Vijay Pai | 4a7fca5 | 2017-09-12 14:31:11 -0700 | [diff] [blame] | 40 | #define GRPC_FD_TO_IDX(fd) (-(fd)-1) |
| 41 | #define GRPC_IDX_TO_FD(idx) (-(idx)-1) |
Ken Payson | 82e4ec7 | 2016-10-13 12:26:01 -0700 | [diff] [blame] | 42 | |
| 43 | typedef struct cv_node { |
| 44 | gpr_cv* cv; |
| 45 | struct cv_node* next; |
Ken Payson | 16b39d6 | 2016-12-06 12:18:28 -0800 | [diff] [blame] | 46 | struct cv_node* prev; |
Ken Payson | 82e4ec7 | 2016-10-13 12:26:01 -0700 | [diff] [blame] | 47 | } cv_node; |
| 48 | |
| 49 | typedef struct fd_node { |
| 50 | int is_set; |
| 51 | cv_node* cvs; |
| 52 | struct fd_node* next_free; |
| 53 | } fd_node; |
| 54 | |
| 55 | typedef struct cv_fd_table { |
| 56 | gpr_mu mu; |
Ken Payson | 16b39d6 | 2016-12-06 12:18:28 -0800 | [diff] [blame] | 57 | gpr_refcount pollcount; |
| 58 | gpr_cv shutdown_cv; |
Ken Payson | 82e4ec7 | 2016-10-13 12:26:01 -0700 | [diff] [blame] | 59 | fd_node* cvfds; |
| 60 | fd_node* free_fds; |
| 61 | unsigned int size; |
| 62 | grpc_poll_function_type poll; |
| 63 | } cv_fd_table; |
Ken Payson | 31caabd | 2016-08-06 21:27:29 -0700 | [diff] [blame] | 64 | |
| 65 | #endif /* GRPC_CORE_LIB_IOMGR_WAKEUP_FD_CV_H */ |