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 #ifndef BIN_EVENTHANDLER_LINUX_H_ | 5 #ifndef BIN_EVENTHANDLER_LINUX_H_ |
6 #define BIN_EVENTHANDLER_LINUX_H_ | 6 #define BIN_EVENTHANDLER_LINUX_H_ |
7 | 7 |
8 #if !defined(BIN_EVENTHANDLER_H_) | 8 #if !defined(BIN_EVENTHANDLER_H_) |
9 #error Do not include eventhandler_linux.h directly; use eventhandler.h instead. | 9 #error Do not include eventhandler_linux.h directly; use eventhandler.h instead. |
10 #endif | 10 #endif |
11 | 11 |
12 #include <unistd.h> | 12 #include <unistd.h> |
13 #include <sys/epoll.h> | 13 #include <sys/epoll.h> |
14 #include <sys/socket.h> | 14 #include <sys/socket.h> |
15 | 15 |
16 #include "platform/hashmap.h" | 16 #include "platform/hashmap.h" |
| 17 #include "platform/thread.h" |
17 | 18 |
18 | 19 |
19 namespace dart { | 20 namespace dart { |
20 namespace bin { | 21 namespace bin { |
21 | 22 |
22 class InterruptMessage { | 23 class InterruptMessage { |
23 public: | 24 public: |
24 intptr_t id; | 25 intptr_t id; |
25 Dart_Port dart_port; | 26 Dart_Port dart_port; |
26 int64_t data; | 27 int64_t data; |
27 }; | 28 }; |
28 | 29 |
29 | 30 |
30 enum PortDataFlags { | |
31 kClosedRead = 0, | |
32 kClosedWrite = 1, | |
33 }; | |
34 | |
35 | |
36 class SocketData { | 31 class SocketData { |
37 public: | 32 public: |
38 explicit SocketData(intptr_t fd) | 33 explicit SocketData(intptr_t fd) |
39 : tracked_by_epoll_(false), fd_(fd), port_(0), mask_(0), flags_(0) { | 34 : tracked_by_epoll_(false), fd_(fd), port_(0), mask_(0) { |
40 ASSERT(fd_ != -1); | 35 ASSERT(fd_ != -1); |
41 } | 36 } |
42 | 37 |
43 intptr_t GetPollEvents(); | |
44 | |
45 void ShutdownRead() { | 38 void ShutdownRead() { |
46 shutdown(fd_, SHUT_RD); | 39 shutdown(fd_, SHUT_RD); |
47 MarkClosedRead(); | |
48 } | 40 } |
49 | 41 |
50 void ShutdownWrite() { | 42 void ShutdownWrite() { |
51 shutdown(fd_, SHUT_WR); | 43 shutdown(fd_, SHUT_WR); |
52 MarkClosedWrite(); | |
53 } | 44 } |
54 | 45 |
55 void Close() { | 46 void Close() { |
56 port_ = 0; | 47 port_ = 0; |
57 mask_ = 0; | 48 mask_ = 0; |
58 flags_ = 0; | |
59 close(fd_); | 49 close(fd_); |
60 fd_ = -1; | 50 fd_ = -1; |
61 } | 51 } |
62 | 52 |
63 bool IsListeningSocket() { return (mask_ & (1 << kListeningSocket)) != 0; } | 53 bool IsListeningSocket() { return (mask_ & (1 << kListeningSocket)) != 0; } |
64 bool IsPipe() { return (mask_ & (1 << kPipe)) != 0; } | 54 bool IsPipe() { return (mask_ & (1 << kPipe)) != 0; } |
65 bool IsClosedRead() { return (flags_ & (1 << kClosedRead)) != 0; } | |
66 bool IsClosedWrite() { return (flags_ & (1 << kClosedWrite)) != 0; } | |
67 | |
68 void MarkClosedRead() { flags_ |= (1 << kClosedRead); } | |
69 void MarkClosedWrite() { flags_ |= (1 << kClosedWrite); } | |
70 | 55 |
71 void SetPortAndMask(Dart_Port port, intptr_t mask) { | 56 void SetPortAndMask(Dart_Port port, intptr_t mask) { |
72 ASSERT(fd_ != -1); | 57 ASSERT(fd_ != -1); |
73 port_ = port; | 58 port_ = port; |
74 mask_ = mask; | 59 mask_ = mask; |
75 } | 60 } |
76 | 61 |
77 intptr_t fd() { return fd_; } | 62 intptr_t fd() { return fd_; } |
78 Dart_Port port() { return port_; } | 63 Dart_Port port() { return port_; } |
79 intptr_t mask() { return mask_; } | 64 intptr_t mask() { return mask_; } |
80 bool tracked_by_epoll() { return tracked_by_epoll_; } | 65 bool tracked_by_epoll() { return tracked_by_epoll_; } |
81 void set_tracked_by_epoll(bool value) { tracked_by_epoll_ = value; } | 66 void set_tracked_by_epoll(bool value) { tracked_by_epoll_ = value; } |
82 | 67 |
83 private: | 68 private: |
84 bool tracked_by_epoll_; | 69 bool tracked_by_epoll_; |
85 intptr_t fd_; | 70 intptr_t fd_; |
86 Dart_Port port_; | 71 Dart_Port port_; |
87 intptr_t mask_; | 72 intptr_t mask_; |
88 intptr_t flags_; | |
89 }; | 73 }; |
90 | 74 |
91 | 75 |
92 class EventHandlerImplementation { | 76 class EventHandlerImplementation { |
93 public: | 77 public: |
94 EventHandlerImplementation(); | 78 EventHandlerImplementation(); |
95 ~EventHandlerImplementation(); | 79 ~EventHandlerImplementation(); |
96 | 80 |
97 // Gets the socket data structure for a given file | 81 // Gets the socket data structure for a given file |
98 // descriptor. Creates a new one if one is not found. | 82 // descriptor. Creates a new one if one is not found. |
99 SocketData* GetSocketData(intptr_t fd); | 83 SocketData* GetSocketData(intptr_t fd, bool* is_new); |
100 void SendData(intptr_t id, Dart_Port dart_port, int64_t data); | 84 void SendData(intptr_t id, Dart_Port dart_port, int64_t data); |
101 void Start(EventHandler* handler); | 85 void Start(EventHandler* handler); |
102 void Shutdown(); | 86 void Shutdown(); |
103 | 87 |
104 private: | 88 private: |
105 void HandleEvents(struct epoll_event* events, int size); | 89 void HandleEvents(struct epoll_event* events, int size); |
106 static void Poll(uword args); | 90 static void Poll(uword args); |
107 void WakeupHandler(intptr_t id, Dart_Port dart_port, int64_t data); | 91 void WakeupHandler(intptr_t id, Dart_Port dart_port, int64_t data); |
108 void HandleInterruptFd(); | 92 void HandleInterruptFd(); |
109 void SetPort(intptr_t fd, Dart_Port dart_port, intptr_t mask); | 93 void SetPort(intptr_t fd, Dart_Port dart_port, intptr_t mask); |
110 intptr_t GetPollEvents(intptr_t events, SocketData* sd); | 94 intptr_t GetPollEvents(intptr_t events, SocketData* sd); |
111 static void* GetHashmapKeyFromFd(intptr_t fd); | 95 static void* GetHashmapKeyFromFd(intptr_t fd); |
112 static uint32_t GetHashmapHashFromFd(intptr_t fd); | 96 static uint32_t GetHashmapHashFromFd(intptr_t fd); |
113 | 97 |
114 HashMap socket_map_; | 98 HashMap socket_map_; |
115 TimeoutQueue timeout_queue_; | 99 TimeoutQueue timeout_queue_; |
116 bool shutdown_; | 100 bool shutdown_; |
117 int interrupt_fds_[2]; | 101 int interrupt_fds_[2]; |
118 int epoll_fd_; | 102 int epoll_fd_; |
119 int timer_fd_; | 103 int timer_fd_; |
120 }; | 104 }; |
121 | 105 |
122 } // namespace bin | 106 } // namespace bin |
123 } // namespace dart | 107 } // namespace dart |
124 | 108 |
125 #endif // BIN_EVENTHANDLER_LINUX_H_ | 109 #endif // BIN_EVENTHANDLER_LINUX_H_ |
OLD | NEW |