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