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 <grpc/support/log.h>
43 :
44 : #include "src/core/iomgr/wakeup_fd_posix.h"
45 : #include "src/core/profiling/timers.h"
46 :
47 9513 : static void eventfd_create(grpc_wakeup_fd* fd_info) {
48 9513 : int efd = eventfd(0, EFD_NONBLOCK | EFD_CLOEXEC);
49 : /* TODO(klempner): Handle failure more gracefully */
50 9513 : GPR_ASSERT(efd >= 0);
51 9513 : fd_info->read_fd = efd;
52 9513 : fd_info->write_fd = -1;
53 9513 : }
54 :
55 318312 : static void eventfd_consume(grpc_wakeup_fd* fd_info) {
56 : eventfd_t value;
57 : int err;
58 : do {
59 318312 : err = eventfd_read(fd_info->read_fd, &value);
60 318337 : } while (err < 0 && errno == EINTR);
61 318337 : }
62 :
63 1116371 : static void eventfd_wakeup(grpc_wakeup_fd* fd_info) {
64 : int err;
65 : GPR_TIMER_BEGIN("eventfd_wakeup", 0);
66 : do {
67 1116371 : err = eventfd_write(fd_info->read_fd, 1);
68 1116394 : } while (err < 0 && errno == EINTR);
69 : GPR_TIMER_END("eventfd_wakeup", 0);
70 1116394 : }
71 :
72 9495 : static void eventfd_destroy(grpc_wakeup_fd* fd_info) {
73 9495 : if (fd_info->read_fd != 0) close(fd_info->read_fd);
74 9495 : }
75 :
76 3457 : static int eventfd_check_availability(void) {
77 : /* TODO(klempner): Actually check if eventfd is available */
78 3457 : return 1;
79 : }
80 :
81 : const grpc_wakeup_fd_vtable grpc_specialized_wakeup_fd_vtable = {
82 : eventfd_create, eventfd_consume, eventfd_wakeup, eventfd_destroy,
83 : eventfd_check_availability};
84 :
85 : #endif /* GPR_LINUX_EVENTFD */
|