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_MACOS_H_ | 5 #ifndef BIN_EVENTHANDLER_MACOS_H_ |
6 #define BIN_EVENTHANDLER_MACOS_H_ | 6 #define BIN_EVENTHANDLER_MACOS_H_ |
7 | 7 |
8 #if !defined(BIN_EVENTHANDLER_H_) | 8 #if !defined(BIN_EVENTHANDLER_H_) |
9 #error Do not include eventhandler_macos.h directly; use eventhandler.h instead. | 9 #error Do not include eventhandler_macos.h directly; use eventhandler.h instead. |
10 #endif | 10 #endif |
11 | 11 |
12 #include <unistd.h> | 12 #include <unistd.h> |
| 13 #include <errno.h> |
13 #include <sys/event.h> // NOLINT | 14 #include <sys/event.h> // NOLINT |
14 #include <sys/socket.h> | 15 #include <sys/socket.h> |
15 | 16 |
16 #include "platform/hashmap.h" | 17 #include "platform/hashmap.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 : fd_(fd), | 34 : fd_(fd), |
40 port_(0), | 35 port_(0), |
41 mask_(0), | 36 mask_(0), |
42 flags_(0), | 37 tracked_by_kqueue_(false) { |
43 read_tracked_by_kqueue_(false), | |
44 write_tracked_by_kqueue_(false) { | |
45 ASSERT(fd_ != -1); | 38 ASSERT(fd_ != -1); |
46 } | 39 } |
47 | 40 |
48 bool HasReadEvent(); | 41 bool HasReadEvent(); |
49 bool HasWriteEvent(); | 42 bool HasWriteEvent(); |
50 | 43 |
51 void ShutdownRead() { | 44 void ShutdownRead() { |
52 shutdown(fd_, SHUT_RD); | 45 shutdown(fd_, SHUT_RD); |
53 MarkClosedRead(); | |
54 } | 46 } |
55 | 47 |
56 void ShutdownWrite() { | 48 void ShutdownWrite() { |
57 shutdown(fd_, SHUT_WR); | 49 shutdown(fd_, SHUT_WR); |
58 MarkClosedWrite(); | |
59 } | 50 } |
60 | 51 |
61 void Close() { | 52 void Close() { |
62 port_ = 0; | 53 port_ = 0; |
63 mask_ = 0; | 54 mask_ = 0; |
64 flags_ = 0; | |
65 close(fd_); | 55 close(fd_); |
66 fd_ = -1; | 56 fd_ = -1; |
67 } | 57 } |
68 | 58 |
69 bool IsListeningSocket() { return (mask_ & (1 << kListeningSocket)) != 0; } | 59 bool IsListeningSocket() { return (mask_ & (1 << kListeningSocket)) != 0; } |
70 bool IsPipe() { return (mask_ & (1 << kPipe)) != 0; } | 60 bool IsPipe() { return (mask_ & (1 << kPipe)) != 0; } |
71 bool IsClosedRead() { return (flags_ & (1 << kClosedRead)) != 0; } | |
72 bool IsClosedWrite() { return (flags_ & (1 << kClosedWrite)) != 0; } | |
73 | |
74 void MarkClosedRead() { flags_ |= (1 << kClosedRead); } | |
75 void MarkClosedWrite() { flags_ |= (1 << kClosedWrite); } | |
76 | 61 |
77 void SetPortAndMask(Dart_Port port, intptr_t mask) { | 62 void SetPortAndMask(Dart_Port port, intptr_t mask) { |
78 ASSERT(fd_ != -1); | 63 ASSERT(fd_ != -1); |
79 port_ = port; | 64 port_ = port; |
80 mask_ = mask; | 65 mask_ = mask; |
81 } | 66 } |
82 | 67 |
83 intptr_t fd() { return fd_; } | 68 intptr_t fd() { return fd_; } |
84 Dart_Port port() { return port_; } | 69 Dart_Port port() { return port_; } |
85 intptr_t mask() { return mask_; } | 70 intptr_t mask() { return mask_; } |
86 bool read_tracked_by_kqueue() { return read_tracked_by_kqueue_; } | 71 bool tracked_by_kqueue() { return tracked_by_kqueue_; } |
87 void set_read_tracked_by_kqueue(bool value) { | 72 void set_tracked_by_kqueue(bool value) { |
88 read_tracked_by_kqueue_ = value; | 73 tracked_by_kqueue_ = value; |
89 } | |
90 bool write_tracked_by_kqueue() { return write_tracked_by_kqueue_; } | |
91 void set_write_tracked_by_kqueue(bool value) { | |
92 write_tracked_by_kqueue_ = value; | |
93 } | 74 } |
94 | 75 |
95 private: | 76 private: |
96 intptr_t fd_; | 77 intptr_t fd_; |
97 Dart_Port port_; | 78 Dart_Port port_; |
98 intptr_t mask_; | 79 intptr_t mask_; |
99 intptr_t flags_; | 80 bool tracked_by_kqueue_; |
100 bool read_tracked_by_kqueue_; | |
101 bool write_tracked_by_kqueue_; | |
102 }; | 81 }; |
103 | 82 |
104 | 83 |
105 class EventHandlerImplementation { | 84 class EventHandlerImplementation { |
106 public: | 85 public: |
107 EventHandlerImplementation(); | 86 EventHandlerImplementation(); |
108 ~EventHandlerImplementation(); | 87 ~EventHandlerImplementation(); |
109 | 88 |
110 // Gets the socket data structure for a given file | 89 // Gets the socket data structure for a given file |
111 // descriptor. Creates a new one if one is not found. | 90 // descriptor. Creates a new one if one is not found. |
112 SocketData* GetSocketData(intptr_t fd); | 91 SocketData* GetSocketData(intptr_t fd, bool* is_new); |
113 void SendData(intptr_t id, Dart_Port dart_port, int64_t data); | 92 void SendData(intptr_t id, Dart_Port dart_port, int64_t data); |
114 void Start(EventHandler* handler); | 93 void Start(EventHandler* handler); |
115 void Shutdown(); | 94 void Shutdown(); |
116 | 95 |
117 private: | 96 private: |
118 int64_t GetTimeout(); | 97 int64_t GetTimeout(); |
119 void HandleEvents(struct kevent* events, int size); | 98 void HandleEvents(struct kevent* events, int size); |
120 void HandleTimeout(); | 99 void HandleTimeout(); |
121 static void EventHandlerEntry(uword args); | 100 static void EventHandlerEntry(uword args); |
122 void WakeupHandler(intptr_t id, Dart_Port dart_port, int64_t data); | 101 void WakeupHandler(intptr_t id, Dart_Port dart_port, int64_t data); |
123 void HandleInterruptFd(); | 102 void HandleInterruptFd(); |
124 void SetPort(intptr_t fd, Dart_Port dart_port, intptr_t mask); | 103 void SetPort(intptr_t fd, Dart_Port dart_port, intptr_t mask); |
125 intptr_t GetEvents(struct kevent* event, SocketData* sd); | 104 intptr_t GetEvents(struct kevent* event, SocketData* sd); |
126 static void* GetHashmapKeyFromFd(intptr_t fd); | 105 static void* GetHashmapKeyFromFd(intptr_t fd); |
127 static uint32_t GetHashmapHashFromFd(intptr_t fd); | 106 static uint32_t GetHashmapHashFromFd(intptr_t fd); |
128 | 107 |
129 HashMap socket_map_; | 108 HashMap socket_map_; |
130 TimeoutQueue timeout_queue_; | 109 TimeoutQueue timeout_queue_; |
131 bool shutdown_; | 110 bool shutdown_; |
132 int interrupt_fds_[2]; | 111 int interrupt_fds_[2]; |
133 int kqueue_fd_; | 112 int kqueue_fd_; |
134 }; | 113 }; |
135 | 114 |
136 } // namespace bin | 115 } // namespace bin |
137 } // namespace dart | 116 } // namespace dart |
138 | 117 |
139 #endif // BIN_EVENTHANDLER_MACOS_H_ | 118 #endif // BIN_EVENTHANDLER_MACOS_H_ |
OLD | NEW |