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 |
(...skipping 12 matching lines...) Expand all Loading... |
23 | 23 |
24 | 24 |
25 enum PortDataFlags { | 25 enum PortDataFlags { |
26 kClosedRead = 0, | 26 kClosedRead = 0, |
27 kClosedWrite = 1, | 27 kClosedWrite = 1, |
28 }; | 28 }; |
29 | 29 |
30 | 30 |
31 class SocketData { | 31 class SocketData { |
32 public: | 32 public: |
33 explicit SocketData(intptr_t fd) : fd_(fd), port_(0), mask_(0), flags_(0) { | 33 explicit SocketData(intptr_t fd) |
| 34 : fd_(fd), |
| 35 port_(0), |
| 36 mask_(0), |
| 37 flags_(0), |
| 38 read_tracked_by_kqueue_(false), |
| 39 write_tracked_by_kqueue_(false) { |
34 ASSERT(fd_ != -1); | 40 ASSERT(fd_ != -1); |
35 } | 41 } |
36 | 42 |
37 intptr_t GetPollEvents(); | 43 bool HasReadEvent(); |
38 | 44 bool HasWriteEvent(); |
39 void Unregister() { | |
40 port_ = 0; | |
41 mask_ = 0; | |
42 } | |
43 | 45 |
44 void ShutdownRead() { | 46 void ShutdownRead() { |
45 shutdown(fd_, SHUT_RD); | 47 shutdown(fd_, SHUT_RD); |
46 MarkClosedRead(); | 48 MarkClosedRead(); |
47 } | 49 } |
48 | 50 |
49 void ShutdownWrite() { | 51 void ShutdownWrite() { |
50 shutdown(fd_, SHUT_WR); | 52 shutdown(fd_, SHUT_WR); |
51 MarkClosedWrite(); | 53 MarkClosedWrite(); |
52 } | 54 } |
53 | 55 |
54 void Close() { | 56 void Close() { |
55 Unregister(); | 57 port_ = 0; |
| 58 mask_ = 0; |
56 flags_ = 0; | 59 flags_ = 0; |
57 close(fd_); | 60 close(fd_); |
58 fd_ = -1; | 61 fd_ = -1; |
59 } | 62 } |
60 | 63 |
61 bool IsListeningSocket() { return (mask_ & (1 << kListeningSocket)) != 0; } | 64 bool IsListeningSocket() { return (mask_ & (1 << kListeningSocket)) != 0; } |
62 bool IsPipe() { return (mask_ & (1 << kPipe)) != 0; } | 65 bool IsPipe() { return (mask_ & (1 << kPipe)) != 0; } |
63 bool IsClosedRead() { return (flags_ & (1 << kClosedRead)) != 0; } | 66 bool IsClosedRead() { return (flags_ & (1 << kClosedRead)) != 0; } |
64 bool IsClosedWrite() { return (flags_ & (1 << kClosedWrite)) != 0; } | 67 bool IsClosedWrite() { return (flags_ & (1 << kClosedWrite)) != 0; } |
65 | 68 |
66 void MarkClosedRead() { flags_ |= (1 << kClosedRead); } | 69 void MarkClosedRead() { flags_ |= (1 << kClosedRead); } |
67 void MarkClosedWrite() { flags_ |= (1 << kClosedWrite); } | 70 void MarkClosedWrite() { flags_ |= (1 << kClosedWrite); } |
68 | 71 |
69 bool HasPollEvents() { return mask_ != 0; } | |
70 | |
71 void SetPortAndMask(Dart_Port port, intptr_t mask) { | 72 void SetPortAndMask(Dart_Port port, intptr_t mask) { |
72 ASSERT(fd_ != -1); | 73 ASSERT(fd_ != -1); |
73 port_ = port; | 74 port_ = port; |
74 mask_ = mask; | 75 mask_ = mask; |
75 } | 76 } |
76 | 77 |
77 intptr_t fd() { return fd_; } | 78 intptr_t fd() { return fd_; } |
78 Dart_Port port() { return port_; } | 79 Dart_Port port() { return port_; } |
79 intptr_t mask() { return mask_; } | 80 intptr_t mask() { return mask_; } |
| 81 bool read_tracked_by_kqueue() { return read_tracked_by_kqueue_; } |
| 82 void set_read_tracked_by_kqueue(bool value) { |
| 83 read_tracked_by_kqueue_ = value; |
| 84 } |
| 85 bool write_tracked_by_kqueue() { return write_tracked_by_kqueue_; } |
| 86 void set_write_tracked_by_kqueue(bool value) { |
| 87 write_tracked_by_kqueue_ = value; |
| 88 } |
80 | 89 |
81 private: | 90 private: |
82 intptr_t fd_; | 91 intptr_t fd_; |
83 Dart_Port port_; | 92 Dart_Port port_; |
84 intptr_t mask_; | 93 intptr_t mask_; |
85 intptr_t flags_; | 94 intptr_t flags_; |
| 95 bool read_tracked_by_kqueue_; |
| 96 bool write_tracked_by_kqueue_; |
86 }; | 97 }; |
87 | 98 |
88 | 99 |
89 class EventHandlerImplementation { | 100 class EventHandlerImplementation { |
90 public: | 101 public: |
91 EventHandlerImplementation(); | 102 EventHandlerImplementation(); |
92 ~EventHandlerImplementation(); | 103 ~EventHandlerImplementation(); |
93 | 104 |
94 // Gets the socket data structure for a given file | 105 // Gets the socket data structure for a given file |
95 // descriptor. Creates a new one of one is not found. | 106 // descriptor. Creates a new one if one is not found. |
96 SocketData* GetSocketData(intptr_t fd); | 107 SocketData* GetSocketData(intptr_t fd); |
97 void SendData(intptr_t id, Dart_Port dart_port, intptr_t data); | 108 void SendData(intptr_t id, Dart_Port dart_port, intptr_t data); |
98 void StartEventHandler(); | 109 void StartEventHandler(); |
99 | 110 |
100 private: | 111 private: |
101 intptr_t GetTimeout(); | 112 intptr_t GetTimeout(); |
102 bool GetInterruptMessage(InterruptMessage* msg); | 113 bool GetInterruptMessage(InterruptMessage* msg); |
103 struct pollfd* GetPollFds(intptr_t* size); | 114 void HandleEvents(struct kevent* events, int size); |
104 void HandleEvents(struct pollfd* pollfds, int pollfds_size, int result_size); | |
105 void HandleTimeout(); | 115 void HandleTimeout(); |
106 static void Poll(uword args); | 116 static void EventHandlerEntry(uword args); |
107 void WakeupHandler(intptr_t id, Dart_Port dart_port, int64_t data); | 117 void WakeupHandler(intptr_t id, Dart_Port dart_port, int64_t data); |
108 void HandleInterruptFd(); | 118 void HandleInterruptFd(); |
109 void SetPort(intptr_t fd, Dart_Port dart_port, intptr_t mask); | 119 void SetPort(intptr_t fd, Dart_Port dart_port, intptr_t mask); |
110 intptr_t GetPollEvents(struct pollfd* pollfd); | 120 intptr_t GetEvents(struct kevent* event, SocketData* sd); |
111 static void* GetHashmapKeyFromFd(intptr_t fd); | 121 static void* GetHashmapKeyFromFd(intptr_t fd); |
112 static uint32_t GetHashmapHashFromFd(intptr_t fd); | 122 static uint32_t GetHashmapHashFromFd(intptr_t fd); |
113 | 123 |
114 HashMap socket_map_; | 124 HashMap socket_map_; |
115 int64_t timeout_; // Time for next timeout. | 125 int64_t timeout_; // Time for next timeout. |
116 Dart_Port timeout_port_; | 126 Dart_Port timeout_port_; |
117 int interrupt_fds_[2]; | 127 int interrupt_fds_[2]; |
| 128 int kqueue_fd_; |
118 }; | 129 }; |
119 | 130 |
120 | 131 |
121 #endif // BIN_EVENTHANDLER_MACOS_H_ | 132 #endif // BIN_EVENTHANDLER_MACOS_H_ |
OLD | NEW |