OLD | NEW |
1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file |
2 // for details. All rights reserved. Use of this source code is governed by a | 2 // for details. All rights reserved. Use of this source code is governed by a |
3 // BSD-style license that can be found in the LICENSE file. | 3 // BSD-style license that can be found in the LICENSE file. |
4 | 4 |
5 #include "platform/globals.h" | 5 #include "platform/globals.h" |
6 #if defined(TARGET_OS_LINUX) | 6 #if defined(TARGET_OS_LINUX) |
7 | 7 |
8 #include "bin/eventhandler.h" | 8 #include "bin/eventhandler.h" |
9 | 9 |
10 #include <errno.h> // NOLINT | 10 #include <errno.h> // NOLINT |
11 #include <pthread.h> // NOLINT | 11 #include <pthread.h> // NOLINT |
12 #include <stdio.h> // NOLINT | 12 #include <stdio.h> // NOLINT |
13 #include <string.h> // NOLINT | 13 #include <string.h> // NOLINT |
14 #include <sys/epoll.h> // NOLINT | 14 #include <sys/epoll.h> // NOLINT |
15 #include <sys/stat.h> // NOLINT | 15 #include <sys/stat.h> // NOLINT |
16 #include <sys/timerfd.h> // NOLINT | 16 #include <sys/timerfd.h> // NOLINT |
17 #include <unistd.h> // NOLINT | 17 #include <unistd.h> // NOLINT |
18 #include <fcntl.h> // NOLINT | 18 #include <fcntl.h> // NOLINT |
19 | 19 |
20 #include "bin/dartutils.h" | 20 #include "bin/dartutils.h" |
21 #include "bin/fdutils.h" | 21 #include "bin/fdutils.h" |
22 #include "bin/log.h" | 22 #include "bin/log.h" |
23 #include "bin/socket.h" | 23 #include "bin/socket.h" |
24 #include "platform/hashmap.h" | 24 #include "platform/signal_blocker.h" |
25 #include "platform/thread.h" | 25 #include "platform/thread.h" |
26 #include "platform/utils.h" | 26 #include "platform/utils.h" |
27 | 27 |
28 | 28 |
29 namespace dart { | 29 namespace dart { |
30 namespace bin { | 30 namespace bin { |
31 | 31 |
32 static const int kTimerId = -1; | 32 static const int kTimerId = -1; |
33 | 33 |
34 | 34 |
35 static void AddToEpollInstance(intptr_t epoll_fd_, | 35 static void AddToEpollInstance(intptr_t epoll_fd_, |
36 int fd, Dart_Port port, | 36 int fd, Dart_Port port, |
37 int mask) { | 37 int mask) { |
38 struct epoll_event event; | 38 struct epoll_event event; |
39 event.events = EPOLLET | EPOLLRDHUP; | 39 event.events = EPOLLET | EPOLLRDHUP; |
40 if ((mask & (1 << kInEvent)) != 0) event.events |= EPOLLIN; | 40 if ((mask & (1 << kInEvent)) != 0) event.events |= EPOLLIN; |
41 if ((mask & (1 << kOutEvent)) != 0) event.events |= EPOLLOUT; | 41 if ((mask & (1 << kOutEvent)) != 0) event.events |= EPOLLOUT; |
42 // Be sure we don't collide with the TIMER_BIT. | 42 // Be sure we don't collide with the TIMER_BIT. |
43 if (port == ILLEGAL_PORT) { | 43 if (port == ILLEGAL_PORT) { |
44 FATAL("Illigal port sent to event handler"); | 44 FATAL("Illigal port sent to event handler"); |
45 } | 45 } |
46 event.data.u64 = port; | 46 event.data.u64 = port; |
47 int status = TEMP_FAILURE_RETRY(epoll_ctl(epoll_fd_, | 47 int status = epoll_ctl(epoll_fd_, EPOLL_CTL_ADD, fd, &event); |
48 EPOLL_CTL_ADD, | |
49 fd, | |
50 &event)); | |
51 if (status == -1) { | 48 if (status == -1) { |
52 // Epoll does not accept the file descriptor. It could be due to | 49 // Epoll does not accept the file descriptor. It could be due to |
53 // already closed file descriptor, or unuspported devices, such | 50 // already closed file descriptor, or unuspported devices, such |
54 // as /dev/null. In such case, mark the file descriptor as closed, | 51 // as /dev/null. In such case, mark the file descriptor as closed, |
55 // so dart will handle it accordingly. | 52 // so dart will handle it accordingly. |
56 DartUtils::PostInt32(port, 1 << kCloseEvent); | 53 DartUtils::PostInt32(port, 1 << kCloseEvent); |
57 } | 54 } |
58 } | 55 } |
59 | 56 |
60 | 57 |
61 EventHandlerImplementation::EventHandlerImplementation() : shutdown_(false) { | 58 EventHandlerImplementation::EventHandlerImplementation() : shutdown_(false) { |
62 // The initial size passed to epoll_create is ignore on newer (>= | 59 // The initial size passed to epoll_create is ignore on newer (>= |
63 // 2.6.8) Linux versions | 60 // 2.6.8) Linux versions |
64 static const int kEpollInitialSize = 64; | 61 static const int kEpollInitialSize = 64; |
65 epoll_fd_ = TEMP_FAILURE_RETRY(epoll_create(kEpollInitialSize)); | 62 epoll_fd_ = epoll_create(kEpollInitialSize); |
66 if (epoll_fd_ == -1) { | 63 if (epoll_fd_ == -1) { |
67 FATAL("Failed creating epoll file descriptor"); | 64 FATAL("Failed creating epoll file descriptor"); |
68 } | 65 } |
69 FDUtils::SetCloseOnExec(epoll_fd_); | 66 FDUtils::SetCloseOnExec(epoll_fd_); |
70 timer_fd_ = TEMP_FAILURE_RETRY(timerfd_create(CLOCK_REALTIME, TFD_CLOEXEC)); | 67 timer_fd_ = timerfd_create(CLOCK_REALTIME, TFD_CLOEXEC); |
71 if (epoll_fd_ == -1) { | 68 if (epoll_fd_ == -1) { |
72 FATAL("Failed creating timerfd file descriptor"); | 69 FATAL("Failed creating timerfd file descriptor"); |
73 } | 70 } |
74 // Register the timer_fd_ with the epoll instance. | 71 // Register the timer_fd_ with the epoll instance. |
75 struct epoll_event event; | 72 struct epoll_event event; |
76 event.events = EPOLLIN; | 73 event.events = EPOLLIN; |
77 event.data.u64 = ILLEGAL_PORT; // Use ILLEGAL_PORT to identify timer-fd. | 74 event.data.u64 = ILLEGAL_PORT; // Use ILLEGAL_PORT to identify timer-fd. |
78 int status = TEMP_FAILURE_RETRY(epoll_ctl(epoll_fd_, | 75 int status = epoll_ctl(epoll_fd_, EPOLL_CTL_ADD, timer_fd_, &event); |
79 EPOLL_CTL_ADD, | |
80 timer_fd_, | |
81 &event)); | |
82 if (status == -1) { | 76 if (status == -1) { |
83 FATAL2( | 77 FATAL2( |
84 "Failed adding timerfd fd(%i) to epoll instance: %i", timer_fd_, errno); | 78 "Failed adding timerfd fd(%i) to epoll instance: %i", timer_fd_, errno); |
85 } | 79 } |
86 } | 80 } |
87 | 81 |
88 | 82 |
89 EventHandlerImplementation::~EventHandlerImplementation() { | 83 EventHandlerImplementation::~EventHandlerImplementation() { |
90 TEMP_FAILURE_RETRY(close(epoll_fd_)); | 84 TEMP_FAILURE_RETRY(close(epoll_fd_)); |
91 TEMP_FAILURE_RETRY(close(timer_fd_)); | 85 TEMP_FAILURE_RETRY(close(timer_fd_)); |
(...skipping 137 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
229 // Add to epoll - this is the first time we see it. | 223 // Add to epoll - this is the first time we see it. |
230 AddToEpollInstance(epoll_fd_, id, dart_port, data); | 224 AddToEpollInstance(epoll_fd_, id, dart_port, data); |
231 } | 225 } |
232 } | 226 } |
233 } | 227 } |
234 | 228 |
235 } // namespace bin | 229 } // namespace bin |
236 } // namespace dart | 230 } // namespace dart |
237 | 231 |
238 #endif // defined(TARGET_OS_LINUX) | 232 #endif // defined(TARGET_OS_LINUX) |
OLD | NEW |