OLD | NEW |
1 // Copyright (c) 2016, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2016, 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 RUNTIME_BIN_EVENTHANDLER_FUCHSIA_H_ | 5 #ifndef RUNTIME_BIN_EVENTHANDLER_FUCHSIA_H_ |
6 #define RUNTIME_BIN_EVENTHANDLER_FUCHSIA_H_ | 6 #define RUNTIME_BIN_EVENTHANDLER_FUCHSIA_H_ |
7 | 7 |
8 #if !defined(RUNTIME_BIN_EVENTHANDLER_H_) | 8 #if !defined(RUNTIME_BIN_EVENTHANDLER_H_) |
9 #error Do not include eventhandler_fuchsia.h directly; use eventhandler.h. | 9 #error Do not include eventhandler_fuchsia.h directly; use eventhandler.h. |
10 #endif | 10 #endif |
11 | 11 |
12 #include <errno.h> | 12 #include <errno.h> |
| 13 #include <magenta/status.h> |
| 14 #include <magenta/syscalls.h> |
| 15 #include <magenta/syscalls/object.h> |
| 16 #include <magenta/syscalls/port.h> |
| 17 #include <mxio/private.h> |
13 #include <sys/epoll.h> | 18 #include <sys/epoll.h> |
14 #include <sys/socket.h> | 19 #include <sys/socket.h> |
15 #include <unistd.h> | 20 #include <unistd.h> |
16 | 21 |
| 22 #include "bin/reference_counting.h" |
| 23 #include "bin/thread.h" |
17 #include "platform/signal_blocker.h" | 24 #include "platform/signal_blocker.h" |
18 | 25 |
19 namespace dart { | 26 namespace dart { |
20 namespace bin { | 27 namespace bin { |
21 | 28 |
| 29 class DescriptorInfo; |
| 30 |
| 31 class IOHandle : public ReferenceCounted<IOHandle> { |
| 32 public: |
| 33 explicit IOHandle(intptr_t fd) |
| 34 : ReferenceCounted(), |
| 35 mutex_(new Mutex()), |
| 36 write_events_enabled_(true), |
| 37 read_events_enabled_(true), |
| 38 fd_(fd), |
| 39 handle_(MX_HANDLE_INVALID), |
| 40 wait_key_(0), |
| 41 mxio_(__mxio_fd_to_io(fd)) {} |
| 42 |
| 43 intptr_t fd() const { return fd_; } |
| 44 |
| 45 // Called from SocketBase::{Read(), Write()} and ServerSocket::Accept() on |
| 46 // the Dart thread. |
| 47 intptr_t Read(void* buffer, intptr_t num_bytes); |
| 48 intptr_t Write(const void* buffer, intptr_t num_bytes); |
| 49 intptr_t Accept(struct sockaddr* addr, socklen_t* addrlen); |
| 50 |
| 51 // Called from the EventHandler thread. |
| 52 void Close(); |
| 53 uint32_t MaskToEpollEvents(intptr_t mask); |
| 54 // If port is MX_HANDLE_INVALID, AsyncWait uses the port from the previous |
| 55 // call with a valid port handle. |
| 56 bool AsyncWait(mx_handle_t port, uint32_t events, uint64_t key); |
| 57 void CancelWait(mx_handle_t port, uint64_t key); |
| 58 uint32_t WaitEnd(mx_signals_t observed); |
| 59 intptr_t ToggleEvents(intptr_t event_mask); |
| 60 |
| 61 static intptr_t EpollEventsToMask(intptr_t events); |
| 62 |
| 63 private: |
| 64 ~IOHandle() { |
| 65 if (mxio_ != NULL) { |
| 66 __mxio_release(mxio_); |
| 67 } |
| 68 delete mutex_; |
| 69 } |
| 70 |
| 71 bool AsyncWaitLocked(mx_handle_t port, uint32_t events, uint64_t key); |
| 72 |
| 73 // Mutex that protects the state here. |
| 74 Mutex* mutex_; |
| 75 bool write_events_enabled_; |
| 76 bool read_events_enabled_; |
| 77 // TODO(zra): Add flag to enable/disable peer closed signal? |
| 78 intptr_t fd_; |
| 79 mx_handle_t handle_; |
| 80 mx_handle_t port_; |
| 81 uint64_t wait_key_; |
| 82 mxio_t* mxio_; |
| 83 |
| 84 friend class ReferenceCounted<IOHandle>; |
| 85 DISALLOW_COPY_AND_ASSIGN(IOHandle); |
| 86 }; |
| 87 |
22 class DescriptorInfo : public DescriptorInfoBase { | 88 class DescriptorInfo : public DescriptorInfoBase { |
23 public: | 89 public: |
24 explicit DescriptorInfo(intptr_t fd) : DescriptorInfoBase(fd) {} | 90 explicit DescriptorInfo(intptr_t fd) : DescriptorInfoBase(fd) { |
| 91 IOHandle* handle = reinterpret_cast<IOHandle*>(fd); |
| 92 handle->Retain(); |
| 93 } |
25 | 94 |
26 virtual ~DescriptorInfo() {} | 95 virtual ~DescriptorInfo() { |
27 | 96 IOHandle* handle = reinterpret_cast<IOHandle*>(fd_); |
28 intptr_t GetPollEvents(); | 97 handle->Release(); |
| 98 } |
29 | 99 |
30 virtual void Close() { | 100 virtual void Close() { |
31 // Should be VOID_TEMP_FAILURE_RETRY | 101 IOHandle* handle = reinterpret_cast<IOHandle*>(fd_); |
32 VOID_NO_RETRY_EXPECTED(close(fd_)); | 102 handle->Close(); |
33 fd_ = -1; | |
34 } | 103 } |
35 | 104 |
| 105 IOHandle* io_handle() const { return reinterpret_cast<IOHandle*>(fd_); } |
| 106 |
36 private: | 107 private: |
37 DISALLOW_COPY_AND_ASSIGN(DescriptorInfo); | 108 DISALLOW_COPY_AND_ASSIGN(DescriptorInfo); |
38 }; | 109 }; |
39 | 110 |
40 class DescriptorInfoSingle : public DescriptorInfoSingleMixin<DescriptorInfo> { | 111 class DescriptorInfoSingle : public DescriptorInfoSingleMixin<DescriptorInfo> { |
41 public: | 112 public: |
42 explicit DescriptorInfoSingle(intptr_t fd) | 113 explicit DescriptorInfoSingle(intptr_t fd) |
43 : DescriptorInfoSingleMixin(fd, false) {} | 114 : DescriptorInfoSingleMixin(fd, false) {} |
44 virtual ~DescriptorInfoSingle() {} | 115 virtual ~DescriptorInfoSingle() {} |
45 | 116 |
(...skipping 10 matching lines...) Expand all Loading... |
56 | 127 |
57 private: | 128 private: |
58 DISALLOW_COPY_AND_ASSIGN(DescriptorInfoMultiple); | 129 DISALLOW_COPY_AND_ASSIGN(DescriptorInfoMultiple); |
59 }; | 130 }; |
60 | 131 |
61 class EventHandlerImplementation { | 132 class EventHandlerImplementation { |
62 public: | 133 public: |
63 EventHandlerImplementation(); | 134 EventHandlerImplementation(); |
64 ~EventHandlerImplementation(); | 135 ~EventHandlerImplementation(); |
65 | 136 |
66 void UpdateEpollInstance(intptr_t old_mask, DescriptorInfo* di); | 137 void UpdatePort(intptr_t old_mask, DescriptorInfo* di); |
67 | 138 |
68 // Gets the socket data structure for a given file | 139 // Gets the socket data structure for a given file |
69 // descriptor. Creates a new one if one is not found. | 140 // descriptor. Creates a new one if one is not found. |
70 DescriptorInfo* GetDescriptorInfo(intptr_t fd, bool is_listening); | 141 DescriptorInfo* GetDescriptorInfo(intptr_t fd, bool is_listening); |
71 void SendData(intptr_t id, Dart_Port dart_port, int64_t data); | 142 void SendData(intptr_t id, Dart_Port dart_port, int64_t data); |
72 void Start(EventHandler* handler); | 143 void Start(EventHandler* handler); |
73 void Shutdown(); | 144 void Shutdown(); |
74 | 145 |
75 private: | 146 private: |
| 147 static const uint64_t kInterruptPacketKey = 1; |
| 148 |
76 static void Poll(uword args); | 149 static void Poll(uword args); |
77 static void* GetHashmapKeyFromFd(intptr_t fd); | 150 static void* GetHashmapKeyFromFd(intptr_t fd); |
78 static uint32_t GetHashmapHashFromFd(intptr_t fd); | 151 static uint32_t GetHashmapHashFromFd(intptr_t fd); |
| 152 static void AddToPort(mx_handle_t port_handle, DescriptorInfo* di); |
| 153 static void RemoveFromPort(mx_handle_t port_handle, DescriptorInfo* di); |
79 | 154 |
80 int64_t GetTimeout() const; | 155 int64_t GetTimeout() const; |
81 void HandleEvents(struct epoll_event* events, int size); | 156 void HandlePacket(mx_port_packet_t* pkt); |
82 void HandleTimeout(); | 157 void HandleTimeout(); |
83 void WakeupHandler(intptr_t id, Dart_Port dart_port, int64_t data); | 158 void WakeupHandler(intptr_t id, Dart_Port dart_port, int64_t data); |
84 intptr_t GetPollEvents(intptr_t events, DescriptorInfo* di); | 159 intptr_t GetPollEvents(intptr_t events); |
85 void HandleInterruptFd(); | 160 void HandleInterrupt(InterruptMessage* msg); |
86 | 161 |
87 HashMap socket_map_; | 162 HashMap socket_map_; |
88 TimeoutQueue timeout_queue_; | 163 TimeoutQueue timeout_queue_; |
89 bool shutdown_; | 164 bool shutdown_; |
90 int interrupt_fds_[2]; | 165 mx_handle_t port_handle_; |
91 int epoll_fd_; | |
92 | 166 |
93 DISALLOW_COPY_AND_ASSIGN(EventHandlerImplementation); | 167 DISALLOW_COPY_AND_ASSIGN(EventHandlerImplementation); |
94 }; | 168 }; |
95 | 169 |
96 } // namespace bin | 170 } // namespace bin |
97 } // namespace dart | 171 } // namespace dart |
98 | 172 |
99 #endif // RUNTIME_BIN_EVENTHANDLER_FUCHSIA_H_ | 173 #endif // RUNTIME_BIN_EVENTHANDLER_FUCHSIA_H_ |
OLD | NEW |