blob: dc170ad5b435cd53331bb5b3a0739b18a3c33991 [file] [log] [blame]
Ken Payson31caabd2016-08-06 21:27:29 -07001/*
2 *
Jan Tattermusch7897ae92017-06-07 22:57:36 +02003 * Copyright 2016 gRPC authors.
Ken Payson31caabd2016-08-06 21:27:29 -07004 *
Jan Tattermusch7897ae92017-06-07 22:57:36 +02005 * 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 Payson31caabd2016-08-06 21:27:29 -07008 *
Jan Tattermusch7897ae92017-06-07 22:57:36 +02009 * http://www.apache.org/licenses/LICENSE-2.0
Ken Payson31caabd2016-08-06 21:27:29 -070010 *
Jan Tattermusch7897ae92017-06-07 22:57:36 +020011 * 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 Payson31caabd2016-08-06 21:27:29 -070016 *
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 Paysone02c7ed2016-09-29 09:15:49 -070029 * poll() completion or a wakeup_fd() call.
Ken Payson31caabd2016-08-06 21:27:29 -070030 *
31 */
32
33#ifndef GRPC_CORE_LIB_IOMGR_WAKEUP_FD_CV_H
34#define GRPC_CORE_LIB_IOMGR_WAKEUP_FD_CV_H
35
Ken Payson82e4ec72016-10-13 12:26:01 -070036#include <grpc/support/sync.h>
Ken Payson31caabd2016-08-06 21:27:29 -070037
Ken Payson82e4ec72016-10-13 12:26:01 -070038#include "src/core/lib/iomgr/ev_posix.h"
39
Vijay Pai4a7fca52017-09-12 14:31:11 -070040#define GRPC_FD_TO_IDX(fd) (-(fd)-1)
41#define GRPC_IDX_TO_FD(idx) (-(idx)-1)
Ken Payson82e4ec72016-10-13 12:26:01 -070042
43typedef struct cv_node {
44 gpr_cv* cv;
45 struct cv_node* next;
Ken Payson16b39d62016-12-06 12:18:28 -080046 struct cv_node* prev;
Ken Payson82e4ec72016-10-13 12:26:01 -070047} cv_node;
48
49typedef struct fd_node {
50 int is_set;
51 cv_node* cvs;
52 struct fd_node* next_free;
53} fd_node;
54
55typedef struct cv_fd_table {
56 gpr_mu mu;
Ken Payson16b39d62016-12-06 12:18:28 -080057 gpr_refcount pollcount;
58 gpr_cv shutdown_cv;
Ken Payson82e4ec72016-10-13 12:26:01 -070059 fd_node* cvfds;
60 fd_node* free_fds;
61 unsigned int size;
62 grpc_poll_function_type poll;
63} cv_fd_table;
Ken Payson31caabd2016-08-06 21:27:29 -070064
65#endif /* GRPC_CORE_LIB_IOMGR_WAKEUP_FD_CV_H */