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