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 mxio_(__mxio_fd_to_io(fd)) {} | |
41 | |
42 intptr_t fd() const { return fd_; } | |
43 | |
44 // Called from SocketBase::{Read(), Write()} and ServerSocket::Accept() on | |
45 // the Dart thread. | |
46 intptr_t Read(void* buffer, intptr_t num_bytes); | |
47 intptr_t Write(const void* buffer, intptr_t num_bytes); | |
48 intptr_t Accept(struct sockaddr* addr, socklen_t* addrlen); | |
49 | |
50 // Called from the EventHandler thread. | |
51 void Close(); | |
52 uint32_t MaskToEpollEvents(intptr_t mask); | |
53 bool AsyncWait(mx_handle_t port, uint32_t events, uint64_t key); | |
54 void CancelWait(mx_handle_t port, uint64_t key); | |
55 uint32_t WaitEnd(mx_signals_t observed); | |
56 intptr_t ToggleEvents(intptr_t event_mask); | |
57 | |
58 static intptr_t EpollEventsToMask(intptr_t events); | |
59 | |
60 private: | |
61 ~IOHandle() { | |
62 if (mxio_ != NULL) { | |
63 __mxio_release(mxio_); | |
64 } | |
65 delete mutex_; | |
66 } | |
67 | |
68 mx_status_t ReadySignalsLocked(mx_signals_t* ready_signals); | |
69 | |
70 // Mutex that protects the state here. | |
71 Mutex* mutex_; | |
72 bool write_events_enabled_; | |
73 bool read_events_enabled_; | |
74 // TODO(zra): Add flag to enable/disable peer closed signal? | |
75 intptr_t fd_; | |
76 mx_handle_t handle_; | |
77 mxio_t* mxio_; | |
78 | |
79 friend class ReferenceCounted<IOHandle>; | |
80 DISALLOW_COPY_AND_ASSIGN(IOHandle); | |
81 }; | |
82 | |
22 class DescriptorInfo : public DescriptorInfoBase { | 83 class DescriptorInfo : public DescriptorInfoBase { |
23 public: | 84 public: |
24 explicit DescriptorInfo(intptr_t fd) : DescriptorInfoBase(fd) {} | 85 explicit DescriptorInfo(intptr_t fd) : DescriptorInfoBase(fd) { |
86 IOHandle* handle = reinterpret_cast<IOHandle*>(fd); | |
87 handle->Retain(); | |
88 } | |
25 | 89 |
26 virtual ~DescriptorInfo() {} | 90 virtual ~DescriptorInfo() { |
27 | 91 IOHandle* handle = reinterpret_cast<IOHandle*>(fd_); |
28 intptr_t GetPollEvents(); | 92 handle->Release(); |
93 } | |
29 | 94 |
30 virtual void Close() { | 95 virtual void Close() { |
31 // Should be VOID_TEMP_FAILURE_RETRY | 96 IOHandle* handle = reinterpret_cast<IOHandle*>(fd_); |
32 VOID_NO_RETRY_EXPECTED(close(fd_)); | 97 handle->Close(); |
33 fd_ = -1; | |
34 } | 98 } |
35 | 99 |
100 IOHandle* io_handle() const { return reinterpret_cast<IOHandle*>(fd_); } | |
101 | |
36 private: | 102 private: |
37 DISALLOW_COPY_AND_ASSIGN(DescriptorInfo); | 103 DISALLOW_COPY_AND_ASSIGN(DescriptorInfo); |
38 }; | 104 }; |
39 | 105 |
40 class DescriptorInfoSingle : public DescriptorInfoSingleMixin<DescriptorInfo> { | 106 class DescriptorInfoSingle : public DescriptorInfoSingleMixin<DescriptorInfo> { |
41 public: | 107 public: |
42 explicit DescriptorInfoSingle(intptr_t fd) | 108 explicit DescriptorInfoSingle(intptr_t fd) |
43 : DescriptorInfoSingleMixin(fd, false) {} | 109 : DescriptorInfoSingleMixin(fd, false) {} |
44 virtual ~DescriptorInfoSingle() {} | 110 virtual ~DescriptorInfoSingle() {} |
45 | 111 |
(...skipping 10 matching lines...) Expand all Loading... | |
56 | 122 |
57 private: | 123 private: |
58 DISALLOW_COPY_AND_ASSIGN(DescriptorInfoMultiple); | 124 DISALLOW_COPY_AND_ASSIGN(DescriptorInfoMultiple); |
59 }; | 125 }; |
60 | 126 |
61 class EventHandlerImplementation { | 127 class EventHandlerImplementation { |
62 public: | 128 public: |
63 EventHandlerImplementation(); | 129 EventHandlerImplementation(); |
64 ~EventHandlerImplementation(); | 130 ~EventHandlerImplementation(); |
65 | 131 |
66 void UpdateEpollInstance(intptr_t old_mask, DescriptorInfo* di); | 132 void UpdatePort(intptr_t old_mask, DescriptorInfo* di); |
67 | 133 |
68 // Gets the socket data structure for a given file | 134 // Gets the socket data structure for a given file |
69 // descriptor. Creates a new one if one is not found. | 135 // descriptor. Creates a new one if one is not found. |
70 DescriptorInfo* GetDescriptorInfo(intptr_t fd, bool is_listening); | 136 DescriptorInfo* GetDescriptorInfo(intptr_t fd, bool is_listening); |
71 void SendData(intptr_t id, Dart_Port dart_port, int64_t data); | 137 void SendData(intptr_t id, Dart_Port dart_port, int64_t data); |
72 void Start(EventHandler* handler); | 138 void Start(EventHandler* handler); |
73 void Shutdown(); | 139 void Shutdown(); |
74 | 140 |
75 private: | 141 private: |
142 static const uint64_t kInterruptPacketKey = 0xda47da47; | |
abarth
2017/06/19 19:02:45
You might as well just use the value 0x1 for this
zra
2017/06/19 21:04:28
Done.
| |
143 | |
76 static void Poll(uword args); | 144 static void Poll(uword args); |
77 static void* GetHashmapKeyFromFd(intptr_t fd); | 145 static void* GetHashmapKeyFromFd(intptr_t fd); |
78 static uint32_t GetHashmapHashFromFd(intptr_t fd); | 146 static uint32_t GetHashmapHashFromFd(intptr_t fd); |
147 static void AddToPort(mx_handle_t port_handle, DescriptorInfo* di); | |
148 static void RemoveFromPort(mx_handle_t port_handle, DescriptorInfo* di); | |
79 | 149 |
80 int64_t GetTimeout() const; | 150 int64_t GetTimeout() const; |
81 void HandleEvents(struct epoll_event* events, int size); | 151 void HandlePacket(mx_port_packet_t* pkt); |
82 void HandleTimeout(); | 152 void HandleTimeout(); |
83 void WakeupHandler(intptr_t id, Dart_Port dart_port, int64_t data); | 153 void WakeupHandler(intptr_t id, Dart_Port dart_port, int64_t data); |
84 intptr_t GetPollEvents(intptr_t events, DescriptorInfo* di); | 154 intptr_t GetPollEvents(intptr_t events); |
85 void HandleInterruptFd(); | 155 void HandleInterrupt(InterruptMessage* msg); |
86 | 156 |
87 HashMap socket_map_; | 157 HashMap socket_map_; |
88 TimeoutQueue timeout_queue_; | 158 TimeoutQueue timeout_queue_; |
89 bool shutdown_; | 159 bool shutdown_; |
90 int interrupt_fds_[2]; | 160 mx_handle_t port_handle_; |
91 int epoll_fd_; | |
92 | 161 |
93 DISALLOW_COPY_AND_ASSIGN(EventHandlerImplementation); | 162 DISALLOW_COPY_AND_ASSIGN(EventHandlerImplementation); |
94 }; | 163 }; |
95 | 164 |
96 } // namespace bin | 165 } // namespace bin |
97 } // namespace dart | 166 } // namespace dart |
98 | 167 |
99 #endif // RUNTIME_BIN_EVENTHANDLER_FUCHSIA_H_ | 168 #endif // RUNTIME_BIN_EVENTHANDLER_FUCHSIA_H_ |
OLD | NEW |