Flutter Engine
The Flutter Engine
Loading...
Searching...
No Matches
eventhandler_fuchsia.h
Go to the documentation of this 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
3// BSD-style license that can be found in the LICENSE file.
4
5#ifndef RUNTIME_BIN_EVENTHANDLER_FUCHSIA_H_
6#define RUNTIME_BIN_EVENTHANDLER_FUCHSIA_H_
7
8#if !defined(RUNTIME_BIN_EVENTHANDLER_H_)
9#error Do not include eventhandler_fuchsia.h directly; use eventhandler.h.
10#endif
11
12#include <errno.h>
13#include <lib/fdio/unsafe.h>
14#include <sys/socket.h>
15#include <unistd.h>
16#include <zircon/status.h>
17#include <zircon/syscalls.h>
18#include <zircon/syscalls/object.h>
19#include <zircon/syscalls/port.h>
20
22#include "bin/thread.h"
24
25namespace dart {
26namespace bin {
27
28class DescriptorInfo;
29
30class IOHandle : public ReferenceCounted<IOHandle> {
31 public:
32 explicit IOHandle(intptr_t fd)
34 mutex_(),
35 write_events_enabled_(true),
36 read_events_enabled_(true),
37 close_events_enabled_(true),
38 fd_(fd),
39 handle_(ZX_HANDLE_INVALID),
40 wait_key_(0),
41 fdio_(fdio_unsafe_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 intptr_t AvailableBytes();
51
52 // Called from the EventHandler thread.
53 void Close();
54 uint32_t MaskToEpollEvents(intptr_t mask);
55 // If port is ZX_HANDLE_INVALID, AsyncWait uses the port from the previous
56 // call with a valid port handle.
57 bool AsyncWait(zx_handle_t port, uint32_t events, uint64_t key);
58 void CancelWait(zx_handle_t port, uint64_t key);
59 uint32_t WaitEnd(zx_signals_t observed);
60 intptr_t ToggleEvents(intptr_t event_mask);
61
62 static intptr_t EpollEventsToMask(intptr_t events);
63
64 private:
65 ~IOHandle() {
66 if (fdio_ != nullptr) {
67 fdio_unsafe_release(fdio_);
68 }
69 }
70
71 bool AsyncWaitLocked(zx_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 bool close_events_enabled_;
78
79 // Bytes remaining to be read from the socket. Read events should only be
80 // re-enabled when this drops to zero.
81 intptr_t available_bytes_;
82
83 // TODO(zra): Add flag to enable/disable peer closed signal?
84 intptr_t fd_;
85 zx_handle_t handle_;
86 zx_handle_t port_;
87 uint64_t wait_key_;
88 fdio_t* fdio_;
89
90 friend class ReferenceCounted<IOHandle>;
92};
93
95 public:
96 explicit DescriptorInfo(intptr_t fd) : DescriptorInfoBase(fd) {
97 IOHandle* handle = reinterpret_cast<IOHandle*>(fd);
98 handle->Retain();
99 }
100
101 virtual ~DescriptorInfo() {
102 IOHandle* handle = reinterpret_cast<IOHandle*>(fd_);
103 handle->Release();
104 }
105
106 virtual void Close() {
107 IOHandle* handle = reinterpret_cast<IOHandle*>(fd_);
108 handle->Close();
109 }
110
111 IOHandle* io_handle() const { return reinterpret_cast<IOHandle*>(fd_); }
112
113 private:
115};
116
117class DescriptorInfoSingle : public DescriptorInfoSingleMixin<DescriptorInfo> {
118 public:
122
123 private:
125};
126
128 : public DescriptorInfoMultipleMixin<DescriptorInfo> {
129 public:
133
134 private:
136};
137
139 public:
142
143 void UpdatePort(intptr_t old_mask, DescriptorInfo* di);
144
145 // Gets the socket data structure for a given file
146 // descriptor. Creates a new one if one is not found.
147 DescriptorInfo* GetDescriptorInfo(intptr_t fd, bool is_listening);
148 void SendData(intptr_t id, Dart_Port dart_port, int64_t data);
149 void Start(EventHandler* handler);
150 void Shutdown();
151
152 private:
153 static constexpr uint64_t kInterruptPacketKey = 1;
154
155 static void Poll(uword args);
156 static void* GetHashmapKeyFromFd(intptr_t fd);
157 static uint32_t GetHashmapHashFromFd(intptr_t fd);
158 static void AddToPort(zx_handle_t port_handle, DescriptorInfo* di);
159 static void RemoveFromPort(zx_handle_t port_handle, DescriptorInfo* di);
160
161 int64_t GetTimeout() const;
162 void HandlePacket(zx_port_packet_t* pkt);
163 void HandleTimeout();
164 void WakeupHandler(intptr_t id, Dart_Port dart_port, int64_t data);
165 intptr_t GetPollEvents(intptr_t events);
166 void HandleInterrupt(InterruptMessage* msg);
167
168 SimpleHashMap socket_map_;
169 TimeoutQueue timeout_queue_;
170 bool shutdown_;
171 zx_handle_t port_handle_;
172
174};
175
176} // namespace bin
177} // namespace dart
178
179#endif // RUNTIME_BIN_EVENTHANDLER_FUCHSIA_H_
void SendData(intptr_t id, Dart_Port dart_port, int64_t data)
void UpdatePort(intptr_t old_mask, DescriptorInfo *di)
void Start(EventHandler *handler)
DescriptorInfo * GetDescriptorInfo(intptr_t fd, bool is_listening)
bool AsyncWait(zx_handle_t port, uint32_t events, uint64_t key)
intptr_t Write(const void *buffer, intptr_t num_bytes)
uint32_t WaitEnd(zx_signals_t observed)
static intptr_t EpollEventsToMask(intptr_t events)
intptr_t AvailableBytes()
intptr_t ToggleEvents(intptr_t event_mask)
intptr_t Read(void *buffer, intptr_t num_bytes)
intptr_t Accept(struct sockaddr *addr, socklen_t *addrlen)
void CancelWait(zx_handle_t port, uint64_t key)
uint32_t MaskToEpollEvents(intptr_t mask)
int64_t Dart_Port
Definition dart_api.h:1524
G_BEGIN_DECLS G_MODULE_EXPORT FlValue * args
static const uint8_t buffer[]
uintptr_t uword
Definition globals.h:501
static int8_t data[kExtLength]
#define DISALLOW_COPY_AND_ASSIGN(TypeName)
Definition globals.h:581