blob: fe71b5abe911cc11f5d18967882c39a32557dae7 [file] [log] [blame]
David Klempner78dc6cd2015-01-26 15:02:51 -08001/*
2 *
3 * Copyright 2015, 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 abstracts the concept of a file descriptor for the purpose of
36 * waking up a thread in select()/poll()/epoll_wait()/etc.
37
38 * The poll() family of system calls provide a way for a thread to block until
39 * there is activity on one (or more) of a set of file descriptors. An
40 * application may wish to wake up this thread to do non file related work. The
41 * typical way to do this is to add a pipe to the set of file descriptors, then
42 * write to the pipe to wake up the thread in poll().
43 *
44 * Linux has a lighter weight eventfd specifically designed for this purpose.
45 * wakeup_fd abstracts the difference between the two.
46 *
47 * Setup:
48 * 1. Before calling anything, call global_init() at least once.
49 * 1. Call grpc_wakeup_fd_create() to get a wakeup_fd.
50 * 2. Add the result of GRPC_WAKEUP_FD_FD to the set of monitored file
51 * descriptors for the poll() style API you are using. Monitor the file
52 * descriptor for readability.
53 * 3. To tear down, call grpc_wakeup_fd_destroy(). This closes the underlying
54 * file descriptor.
55 *
56 * Usage:
57 * 1. To wake up a polling thread, call grpc_wakeup_fd_wakeup() on a wakeup_fd
58 * it is monitoring.
59 * 2. If the polling thread was awakened by a wakeup_fd event, call
60 * grpc_wakeup_fd_consume_wakeup() on it.
61 */
Nicolas "Pixel" Noble1ff52d52015-03-01 05:24:36 +010062#ifndef GRPC_INTERNAL_CORE_IOMGR_WAKEUP_FD_POSIX_H
63#define GRPC_INTERNAL_CORE_IOMGR_WAKEUP_FD_POSIX_H
David Klempner78dc6cd2015-01-26 15:02:51 -080064
Craig Tillera82950e2015-09-22 12:33:20 -070065void grpc_wakeup_fd_global_init(void);
66void grpc_wakeup_fd_global_destroy(void);
David Klempner78dc6cd2015-01-26 15:02:51 -080067
Craig Tillere1a03a62015-02-02 07:46:55 -080068/* Force using the fallback implementation. This is intended for testing
69 * purposes only.*/
Craig Tillera82950e2015-09-22 12:33:20 -070070void grpc_wakeup_fd_global_init_force_fallback(void);
Craig Tillere1a03a62015-02-02 07:46:55 -080071
Craig Tiller5ddbb9d2015-07-29 15:58:11 -070072typedef struct grpc_wakeup_fd grpc_wakeup_fd;
Craig Tillere1a03a62015-02-02 07:46:55 -080073
Craig Tillera82950e2015-09-22 12:33:20 -070074typedef struct grpc_wakeup_fd_vtable {
75 void (*init)(grpc_wakeup_fd* fd_info);
76 void (*consume)(grpc_wakeup_fd* fd_info);
77 void (*wakeup)(grpc_wakeup_fd* fd_info);
78 void (*destroy)(grpc_wakeup_fd* fd_info);
Craig Tillere1a03a62015-02-02 07:46:55 -080079 /* Must be called before calling any other functions */
Craig Tillera82950e2015-09-22 12:33:20 -070080 int (*check_availability)(void);
Craig Tillere1a03a62015-02-02 07:46:55 -080081} grpc_wakeup_fd_vtable;
82
Craig Tillera82950e2015-09-22 12:33:20 -070083struct grpc_wakeup_fd {
Craig Tillere1a03a62015-02-02 07:46:55 -080084 int read_fd;
85 int write_fd;
86};
87
88#define GRPC_WAKEUP_FD_GET_READ_FD(fd_info) ((fd_info)->read_fd)
David Klempner78dc6cd2015-01-26 15:02:51 -080089
Craig Tillera82950e2015-09-22 12:33:20 -070090void grpc_wakeup_fd_init(grpc_wakeup_fd* fd_info);
91void grpc_wakeup_fd_consume_wakeup(grpc_wakeup_fd* fd_info);
92void grpc_wakeup_fd_wakeup(grpc_wakeup_fd* fd_info);
93void grpc_wakeup_fd_destroy(grpc_wakeup_fd* fd_info);
David Klempner78dc6cd2015-01-26 15:02:51 -080094
David Klempner78dc6cd2015-01-26 15:02:51 -080095/* Defined in some specialized implementation's .c file, or by
96 * wakeup_fd_nospecial.c if no such implementation exists. */
Craig Tillere1a03a62015-02-02 07:46:55 -080097extern const grpc_wakeup_fd_vtable grpc_specialized_wakeup_fd_vtable;
David Klempner78dc6cd2015-01-26 15:02:51 -080098
Craig Tillerd6c98df2015-08-18 09:33:44 -070099#endif /* GRPC_INTERNAL_CORE_IOMGR_WAKEUP_FD_POSIX_H */