blob: ac16be1750a7e6295281d3a2f1df1331aaa395c3 [file] [log] [blame]
Ken Payson31caabd2016-08-06 21:27:29 -07001/*
2 *
3 * Copyright 2016, Google Inc.
4 * All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions are
8 * met:
9 *
10 * * Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * * Redistributions in binary form must reproduce the above
13 * copyright notice, this list of conditions and the following disclaimer
14 * in the documentation and/or other materials provided with the
15 * distribution.
16 * * Neither the name of Google Inc. nor the names of its
17 * contributors may be used to endorse or promote products derived from
18 * this software without specific prior written permission.
19 *
20 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
23 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
24 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
25 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
26 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
28 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
30 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 *
32 */
33
34/*
35 * wakeup_fd_cv uses condition variables to implement wakeup fds.
36 *
37 * It is intended for use only in cases when eventfd() and pipe() are not
38 * available. It can only be used with the "poll" engine.
39 *
40 * Implementation:
41 * A global table of cv wakeup fds is mantained. A cv wakeup fd is a negative
42 * file descriptor. poll() is then run in a background thread with only the
43 * real socket fds while we wait on a condition variable trigged by either the
Ken Paysone02c7ed2016-09-29 09:15:49 -070044 * poll() completion or a wakeup_fd() call.
Ken Payson31caabd2016-08-06 21:27:29 -070045 *
46 */
47
48#ifndef GRPC_CORE_LIB_IOMGR_WAKEUP_FD_CV_H
49#define GRPC_CORE_LIB_IOMGR_WAKEUP_FD_CV_H
50
Ken Payson82e4ec72016-10-13 12:26:01 -070051#include <grpc/support/sync.h>
Ken Payson31caabd2016-08-06 21:27:29 -070052
Ken Payson82e4ec72016-10-13 12:26:01 -070053#include "src/core/lib/iomgr/ev_posix.h"
54
55#define FD_TO_IDX(fd) (-(fd)-1)
56#define IDX_TO_FD(idx) (-(idx)-1)
57
58typedef struct cv_node {
59 gpr_cv* cv;
60 struct cv_node* next;
61} cv_node;
62
63typedef struct fd_node {
64 int is_set;
65 cv_node* cvs;
66 struct fd_node* next_free;
67} fd_node;
68
69typedef struct cv_fd_table {
70 gpr_mu mu;
71 int pollcount;
72 int shutdown;
73 gpr_cv shutdown_complete;
74 fd_node* cvfds;
75 fd_node* free_fds;
76 unsigned int size;
77 grpc_poll_function_type poll;
78} cv_fd_table;
Ken Payson31caabd2016-08-06 21:27:29 -070079
80#endif /* GRPC_CORE_LIB_IOMGR_WAKEUP_FD_CV_H */