Line data Source code
1 : /*
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 : #include <grpc/support/port_platform.h>
35 :
36 : #ifdef GPR_LINUX_EVENTFD
37 :
38 : #include <errno.h>
39 : #include <sys/eventfd.h>
40 : #include <unistd.h>
41 :
42 : #include "src/core/iomgr/wakeup_fd_posix.h"
43 : #include <grpc/support/log.h>
44 :
45 5287898 : static void eventfd_create(grpc_wakeup_fd* fd_info) {
46 5287898 : int efd = eventfd(0, EFD_NONBLOCK | EFD_CLOEXEC);
47 : /* TODO(klempner): Handle failure more gracefully */
48 5293403 : GPR_ASSERT(efd >= 0);
49 5293403 : fd_info->read_fd = efd;
50 5293403 : fd_info->write_fd = -1;
51 5293403 : }
52 :
53 484081 : static void eventfd_consume(grpc_wakeup_fd* fd_info) {
54 : eventfd_t value;
55 : int err;
56 : do {
57 484081 : err = eventfd_read(fd_info->read_fd, &value);
58 484073 : } while (err < 0 && errno == EINTR);
59 484073 : }
60 :
61 3043179 : static void eventfd_wakeup(grpc_wakeup_fd* fd_info) {
62 : int err;
63 : do {
64 3043179 : err = eventfd_write(fd_info->read_fd, 1);
65 3043079 : } while (err < 0 && errno == EINTR);
66 3043079 : }
67 :
68 5293602 : static void eventfd_destroy(grpc_wakeup_fd* fd_info) {
69 5293602 : if (fd_info->read_fd != 0) close(fd_info->read_fd);
70 5292628 : }
71 :
72 2506 : static int eventfd_check_availability(void) {
73 : /* TODO(klempner): Actually check if eventfd is available */
74 2506 : return 1;
75 : }
76 :
77 : const grpc_wakeup_fd_vtable grpc_specialized_wakeup_fd_vtable = {
78 : eventfd_create, eventfd_consume, eventfd_wakeup, eventfd_destroy,
79 : eventfd_check_availability};
80 :
81 : #endif /* GPR_LINUX_EVENTFD */
|