blob: 71d32d97ba778da4b01fb5ea36ec482daec19a4b [file] [log] [blame]
David Klempner78dc6cd2015-01-26 15:02:51 -08001/*
2 *
Craig Tiller6169d5f2016-03-31 07:46:18 -07003 * Copyright 2015, Google Inc.
David Klempner78dc6cd2015-01-26 15:02:51 -08004 * 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 */
Craig Tiller9a4dddd2016-03-25 17:08:13 -070062#ifndef GRPC_CORE_LIB_IOMGR_WAKEUP_FD_POSIX_H
63#define GRPC_CORE_LIB_IOMGR_WAKEUP_FD_POSIX_H
David Klempner78dc6cd2015-01-26 15:02:51 -080064
Craig Tiller4f1d0f32016-05-06 17:12:37 -070065#include "src/core/lib/iomgr/error.h"
66
Craig Tillera82950e2015-09-22 12:33:20 -070067void grpc_wakeup_fd_global_init(void);
68void grpc_wakeup_fd_global_destroy(void);
David Klempner78dc6cd2015-01-26 15:02:51 -080069
Craig Tillere1a03a62015-02-02 07:46:55 -080070/* Force using the fallback implementation. This is intended for testing
71 * purposes only.*/
Craig Tillera82950e2015-09-22 12:33:20 -070072void grpc_wakeup_fd_global_init_force_fallback(void);
Craig Tillere1a03a62015-02-02 07:46:55 -080073
Ken Paysoncd7d0472016-10-11 12:24:20 -070074int grpc_has_wakeup_fd(void);
Ken Payson82e4ec72016-10-13 12:26:01 -070075int grpc_cv_wakeup_fds_enabled(void);
Ken Paysoncd7d0472016-10-11 12:24:20 -070076void grpc_enable_cv_wakeup_fds(int enable);
77
Craig Tiller5ddbb9d2015-07-29 15:58:11 -070078typedef struct grpc_wakeup_fd grpc_wakeup_fd;
Craig Tillere1a03a62015-02-02 07:46:55 -080079
Craig Tillera82950e2015-09-22 12:33:20 -070080typedef struct grpc_wakeup_fd_vtable {
Craig Tiller4f1d0f32016-05-06 17:12:37 -070081 grpc_error* (*init)(grpc_wakeup_fd* fd_info);
82 grpc_error* (*consume)(grpc_wakeup_fd* fd_info);
83 grpc_error* (*wakeup)(grpc_wakeup_fd* fd_info);
Craig Tillera82950e2015-09-22 12:33:20 -070084 void (*destroy)(grpc_wakeup_fd* fd_info);
Craig Tillere1a03a62015-02-02 07:46:55 -080085 /* Must be called before calling any other functions */
Craig Tillera82950e2015-09-22 12:33:20 -070086 int (*check_availability)(void);
Craig Tillere1a03a62015-02-02 07:46:55 -080087} grpc_wakeup_fd_vtable;
88
Craig Tillera82950e2015-09-22 12:33:20 -070089struct grpc_wakeup_fd {
Craig Tillere1a03a62015-02-02 07:46:55 -080090 int read_fd;
91 int write_fd;
92};
93
Craig Tillerf218c8b2015-12-10 14:57:56 -080094extern int grpc_allow_specialized_wakeup_fd;
Ken Payson31caabd2016-08-06 21:27:29 -070095extern int grpc_allow_pipe_wakeup_fd;
Craig Tillerf218c8b2015-12-10 14:57:56 -080096
Craig Tillere1a03a62015-02-02 07:46:55 -080097#define GRPC_WAKEUP_FD_GET_READ_FD(fd_info) ((fd_info)->read_fd)
David Klempner78dc6cd2015-01-26 15:02:51 -080098
Craig Tiller4f1d0f32016-05-06 17:12:37 -070099grpc_error* grpc_wakeup_fd_init(grpc_wakeup_fd* fd_info) GRPC_MUST_USE_RESULT;
100grpc_error* grpc_wakeup_fd_consume_wakeup(grpc_wakeup_fd* fd_info)
101 GRPC_MUST_USE_RESULT;
102grpc_error* grpc_wakeup_fd_wakeup(grpc_wakeup_fd* fd_info) GRPC_MUST_USE_RESULT;
Craig Tillera82950e2015-09-22 12:33:20 -0700103void grpc_wakeup_fd_destroy(grpc_wakeup_fd* fd_info);
David Klempner78dc6cd2015-01-26 15:02:51 -0800104
David Klempner78dc6cd2015-01-26 15:02:51 -0800105/* Defined in some specialized implementation's .c file, or by
106 * wakeup_fd_nospecial.c if no such implementation exists. */
Craig Tillere1a03a62015-02-02 07:46:55 -0800107extern const grpc_wakeup_fd_vtable grpc_specialized_wakeup_fd_vtable;
David Klempner78dc6cd2015-01-26 15:02:51 -0800108
Craig Tiller9a4dddd2016-03-25 17:08:13 -0700109#endif /* GRPC_CORE_LIB_IOMGR_WAKEUP_FD_POSIX_H */