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