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> | |
14 #include <sys/event.h> // NOLINT | 13 #include <sys/event.h> // NOLINT |
15 #include <sys/socket.h> | 14 #include <sys/socket.h> |
16 | 15 |
17 #include "platform/hashmap.h" | 16 #include "platform/hashmap.h" |
18 | 17 |
19 | 18 |
20 namespace dart { | 19 namespace dart { |
21 namespace bin { | 20 namespace bin { |
22 | 21 |
23 class InterruptMessage { | 22 class InterruptMessage { |
24 public: | 23 public: |
25 intptr_t id; | 24 intptr_t id; |
26 Dart_Port dart_port; | 25 Dart_Port dart_port; |
27 int64_t data; | 26 int64_t data; |
28 }; | 27 }; |
29 | 28 |
30 | 29 |
31 class SocketData { | 30 class SocketData { |
32 public: | 31 public: |
33 explicit SocketData(intptr_t fd) | 32 explicit SocketData(intptr_t fd) |
34 : fd_(fd), | 33 : fd_(fd), |
35 port_(0), | 34 port_(0), |
36 mask_(0), | 35 mask_(0), |
37 tracked_by_kqueue_(false) { | 36 tracked_by_kqueue_(false), |
| 37 tokens_(8) { |
38 ASSERT(fd_ != -1); | 38 ASSERT(fd_ != -1); |
39 } | 39 } |
40 | 40 |
41 bool HasReadEvent(); | 41 bool HasReadEvent(); |
42 bool HasWriteEvent(); | 42 bool HasWriteEvent(); |
43 | 43 |
44 void ShutdownRead() { | |
45 shutdown(fd_, SHUT_RD); | |
46 } | |
47 | |
48 void ShutdownWrite() { | |
49 shutdown(fd_, SHUT_WR); | |
50 } | |
51 | |
52 void Close() { | 44 void Close() { |
53 port_ = 0; | 45 port_ = 0; |
54 mask_ = 0; | 46 mask_ = 0; |
55 close(fd_); | 47 close(fd_); |
56 fd_ = -1; | 48 fd_ = -1; |
57 } | 49 } |
58 | 50 |
59 bool IsListeningSocket() { return (mask_ & (1 << kListeningSocket)) != 0; } | 51 bool IsListeningSocket() { return (mask_ & (1 << kListeningSocket)) != 0; } |
60 bool IsPipe() { return (mask_ & (1 << kPipe)) != 0; } | |
61 | 52 |
62 void SetPortAndMask(Dart_Port port, intptr_t mask) { | 53 void SetPortAndMask(Dart_Port port, intptr_t mask) { |
63 ASSERT(fd_ != -1); | 54 ASSERT(fd_ != -1); |
64 port_ = port; | 55 port_ = port; |
65 mask_ = mask; | 56 mask_ = mask; |
66 } | 57 } |
67 | 58 |
68 intptr_t fd() { return fd_; } | 59 intptr_t fd() { return fd_; } |
69 Dart_Port port() { return port_; } | 60 Dart_Port port() { return port_; } |
70 intptr_t mask() { return mask_; } | 61 intptr_t mask() { return mask_; } |
71 bool tracked_by_kqueue() { return tracked_by_kqueue_; } | 62 bool tracked_by_kqueue() { return tracked_by_kqueue_; } |
72 void set_tracked_by_kqueue(bool value) { | 63 void set_tracked_by_kqueue(bool value) { |
73 tracked_by_kqueue_ = value; | 64 tracked_by_kqueue_ = value; |
74 } | 65 } |
75 | 66 |
| 67 // Returns true if the last token was taken. |
| 68 bool TakeToken() { |
| 69 tokens_--; |
| 70 return tokens_ == 0; |
| 71 } |
| 72 |
| 73 // Returns true if the tokens was 0 before adding. |
| 74 bool ReturnToken() { |
| 75 tokens_++; |
| 76 return tokens_ == 1; |
| 77 } |
| 78 |
76 private: | 79 private: |
77 intptr_t fd_; | 80 intptr_t fd_; |
78 Dart_Port port_; | 81 Dart_Port port_; |
79 intptr_t mask_; | 82 intptr_t mask_; |
80 bool tracked_by_kqueue_; | 83 bool tracked_by_kqueue_; |
| 84 int tokens_; |
81 }; | 85 }; |
82 | 86 |
83 | 87 |
84 class EventHandlerImplementation { | 88 class EventHandlerImplementation { |
85 public: | 89 public: |
86 EventHandlerImplementation(); | 90 EventHandlerImplementation(); |
87 ~EventHandlerImplementation(); | 91 ~EventHandlerImplementation(); |
88 | 92 |
89 // Gets the socket data structure for a given file | 93 // Gets the socket data structure for a given file |
90 // descriptor. Creates a new one if one is not found. | 94 // descriptor. Creates a new one if one is not found. |
91 SocketData* GetSocketData(intptr_t fd, bool* is_new); | 95 SocketData* GetSocketData(intptr_t fd); |
92 void Notify(intptr_t id, Dart_Port dart_port, int64_t data); | 96 void SendData(intptr_t id, Dart_Port dart_port, int64_t data); |
93 void Start(EventHandler* handler); | 97 void Start(EventHandler* handler); |
94 void Shutdown(); | 98 void Shutdown(); |
95 | 99 |
96 private: | 100 private: |
97 int64_t GetTimeout(); | 101 int64_t GetTimeout(); |
98 void HandleEvents(struct kevent* events, int size); | 102 void HandleEvents(struct kevent* events, int size); |
99 void HandleTimeout(); | 103 void HandleTimeout(); |
100 static void EventHandlerEntry(uword args); | 104 static void EventHandlerEntry(uword args); |
101 void WakeupHandler(intptr_t id, Dart_Port dart_port, int64_t data); | 105 void WakeupHandler(intptr_t id, Dart_Port dart_port, int64_t data); |
102 void HandleInterruptFd(); | 106 void HandleInterruptFd(); |
103 void SetPort(intptr_t fd, Dart_Port dart_port, intptr_t mask); | 107 void SetPort(intptr_t fd, Dart_Port dart_port, intptr_t mask); |
104 intptr_t GetEvents(struct kevent* event, SocketData* sd); | 108 intptr_t GetEvents(struct kevent* event, SocketData* sd); |
105 static void* GetHashmapKeyFromFd(intptr_t fd); | 109 static void* GetHashmapKeyFromFd(intptr_t fd); |
106 static uint32_t GetHashmapHashFromFd(intptr_t fd); | 110 static uint32_t GetHashmapHashFromFd(intptr_t fd); |
107 | 111 |
108 HashMap socket_map_; | 112 HashMap socket_map_; |
109 TimeoutQueue timeout_queue_; | 113 TimeoutQueue timeout_queue_; |
110 bool shutdown_; | 114 bool shutdown_; |
111 int interrupt_fds_[2]; | 115 int interrupt_fds_[2]; |
112 int kqueue_fd_; | 116 int kqueue_fd_; |
113 }; | 117 }; |
114 | 118 |
115 } // namespace bin | 119 } // namespace bin |
116 } // namespace dart | 120 } // namespace dart |
117 | 121 |
118 #endif // BIN_EVENTHANDLER_MACOS_H_ | 122 #endif // BIN_EVENTHANDLER_MACOS_H_ |
OLD | NEW |