Flutter Engine
The Flutter Engine
Classes | Static Public Member Functions | List of all members
dart::PortMap Class Reference

#include <port.h>

Inheritance diagram for dart::PortMap:
dart::AllStatic

Static Public Member Functions

static Dart_Port CreatePort (MessageHandler *handler)
 
static bool ClosePort (Dart_Port id, MessageHandler **message_handler=nullptr)
 
static void ClosePorts (MessageHandler *handler)
 
static bool PostMessage (std::unique_ptr< Message > message, bool before_events=false)
 
static IsolateGetIsolate (Dart_Port id)
 
static Dart_Port GetOriginId (Dart_Port id)
 
static bool IsReceiverInThisIsolateGroupOrClosed (Dart_Port receiver, IsolateGroup *group)
 
static void Init ()
 
static void Cleanup ()
 
static void PrintPortsForMessageHandler (MessageHandler *handler, JSONStream *stream)
 
static void DebugDumpForMessageHandler (MessageHandler *handler)
 

Detailed Description

Definition at line 24 of file port.h.

Member Function Documentation

◆ Cleanup()

void dart::PortMap::Cleanup ( )
static

Definition at line 261 of file port.cc.

261 {
262 ASSERT(ports_ != nullptr);
263 ASSERT(prng_ != nullptr);
264 for (auto it = ports_->begin(); it != ports_->end(); ++it) {
265 const auto& entry = *it;
266 ASSERT(entry.handler != nullptr);
267 delete entry.handler;
268 it.Delete();
269 }
270 ports_->Rebalance();
271
272 // Grab the mutex and delete the port set.
273 MutexLocker ml(mutex_);
274 delete prng_;
275 prng_ = nullptr;
276 delete ports_;
277 ports_ = nullptr;
278}
DART_FORCE_INLINE Iterator begin()
Definition: port_set.h:114
DART_FORCE_INLINE Iterator end()
Definition: port_set.h:124
void Rebalance()
Definition: port_set.h:165
#define ASSERT(E)

◆ ClosePort()

bool dart::PortMap::ClosePort ( Dart_Port  id,
MessageHandler **  message_handler = nullptr 
)
static

Definition at line 90 of file port.cc.

90 {
91 if (message_handler != nullptr) *message_handler = nullptr;
92
93 MessageHandler* handler = nullptr;
94 {
95 MutexLocker ml(mutex_);
96 if (ports_ == nullptr) {
97 return false;
98 }
99 auto it = ports_->TryLookup(port);
100 if (it == ports_->end()) {
101 return false;
102 }
103 Entry entry = *it;
104 handler = entry.handler;
105 ASSERT(handler != nullptr);
106
107#if defined(DEBUG)
108 handler->CheckAccess();
109#endif
110
111 // Delete the port entry before releasing the lock to avoid holding the lock
112 // while flushing the messages below.
113 it.Delete();
114 ports_->Rebalance();
115
116 // The MessageHandler::ports_ is only accessed by [PortMap], it is guarded
117 // by the [PortMap::mutex_] we already hold.
118 auto isolate_it = handler->ports_.TryLookup(port);
119 ASSERT(isolate_it != handler->ports_.end());
120 isolate_it.Delete();
121 handler->ports_.Rebalance();
122 }
123 handler->ClosePort(port);
124 if (message_handler != nullptr) *message_handler = handler;
125 return true;
126}
Iterator TryLookup(Dart_Port port)
Definition: port_set.h:156
DEF_SWITCHES_START aot vmservice shared library Name of the *so containing AOT compiled Dart assets for launching the service isolate vm snapshot The VM snapshot data that will be memory mapped as read only SnapshotAssetPath must be present isolate snapshot The isolate snapshot data that will be memory mapped as read only SnapshotAssetPath must be present cache dir Path to the cache directory This is different from the persistent_cache_path in embedder which is used for Skia shader cache icu native lib Path to the library file that exports the ICU data vm service The hostname IP address on which the Dart VM Service should be served If not defaults to or::depending on whether ipv6 is specified vm service port
Definition: switches.h:87
std::function< void(const T &message, const MessageReply< T > &reply)> MessageHandler

◆ ClosePorts()

void dart::PortMap::ClosePorts ( MessageHandler handler)
static

Definition at line 128 of file port.cc.

128 {
129 {
130 MutexLocker ml(mutex_);
131 if (ports_ == nullptr) {
132 return;
133 }
134 // The MessageHandler::ports_ is only accessed by [PortMap], it is guarded
135 // by the [PortMap::mutex_] we already hold.
136 for (auto isolate_it = handler->ports_.begin();
137 isolate_it != handler->ports_.end(); ++isolate_it) {
138 auto it = ports_->TryLookup((*isolate_it).port);
139 ASSERT(it != ports_->end());
140 Entry entry = *it;
141 ASSERT(entry.port == (*isolate_it).port);
142 ASSERT(entry.handler == handler);
143 it.Delete();
144 isolate_it.Delete();
145 }
146 ASSERT(handler->ports_.IsEmpty());
147 ports_->Rebalance();
148 }
149 handler->CloseAllPorts();
150}

◆ CreatePort()

Dart_Port dart::PortMap::CreatePort ( MessageHandler handler)
static

Definition at line 55 of file port.cc.

55 {
56 ASSERT(handler != nullptr);
57 MutexLocker ml(mutex_);
58 if (ports_ == nullptr) {
59 return ILLEGAL_PORT;
60 }
61
62#if defined(DEBUG)
63 handler->CheckAccess();
64#endif
65
66 const Dart_Port port = AllocatePort();
67
68 // The MessageHandler::ports_ is only accessed by [PortMap], it is guarded
69 // by the [PortMap::mutex_] we already hold.
70 MessageHandler::PortSetEntry isolate_entry;
71 isolate_entry.port = port;
72 handler->ports_.Insert(isolate_entry);
73
74 Entry entry;
75 entry.port = port;
76 entry.handler = handler;
77 ports_->Insert(entry);
78
79 if (FLAG_trace_isolates) {
81 "[+] Opening port: \n"
82 "\thandler: %s\n"
83 "\tport: %" Pd64 "\n",
84 handler->name(), entry.port);
85 }
86
87 return entry.port;
88}
static void static void PrintErr(const char *format,...) PRINTF_ATTRIBUTE(1
void Insert(const T &entry)
Definition: port_set.h:126
#define ILLEGAL_PORT
Definition: dart_api.h:1535
int64_t Dart_Port
Definition: dart_api.h:1525
#define Pd64
Definition: globals.h:416

◆ DebugDumpForMessageHandler()

void dart::PortMap::DebugDumpForMessageHandler ( MessageHandler handler)
static

Definition at line 305 of file port.cc.

305 {
306 SafepointMutexLocker ml(mutex_);
307 if (ports_ == nullptr) {
308 return;
309 }
310 Object& msg_handler = Object::Handle();
311 for (auto& entry : *ports_) {
312 if (entry.handler == handler) {
313 OS::PrintErr("Port = %" Pd64 "\n", entry.port);
314 msg_handler = DartLibraryCalls::LookupHandler(entry.port);
315 OS::PrintErr("Handler = %s\n", msg_handler.ToCString());
316 }
317 }
318}
static ObjectPtr LookupHandler(Dart_Port port_id)
Definition: dart_entry.cc:664
static Object & Handle()
Definition: object.h:407

◆ GetIsolate()

Isolate * dart::PortMap::GetIsolate ( Dart_Port  id)
static

Definition at line 181 of file port.cc.

181 {
182 MutexLocker ml(mutex_);
183 if (ports_ == nullptr) {
184 return nullptr;
185 }
186 auto it = ports_->TryLookup(id);
187 if (it == ports_->end()) {
188 // Port does not exist.
189 return nullptr;
190 }
191
192 MessageHandler* handler = (*it).handler;
193 return handler->isolate();
194}

◆ GetOriginId()

Dart_Port dart::PortMap::GetOriginId ( Dart_Port  id)
static

Definition at line 196 of file port.cc.

196 {
197 MutexLocker ml(mutex_);
198 if (ports_ == nullptr) {
199 return ILLEGAL_PORT;
200 }
201 auto it = ports_->TryLookup(id);
202 if (it == ports_->end()) {
203 // Port does not exist.
204 return ILLEGAL_PORT;
205 }
206
207 MessageHandler* handler = (*it).handler;
208 Isolate* isolate = handler->isolate();
209 if (isolate == nullptr) {
210 // Message handler is a native port instead of an isolate.
211 return ILLEGAL_PORT;
212 }
213 return isolate->origin_id();
214}

◆ Init()

void dart::PortMap::Init ( )
static

Definition at line 248 of file port.cc.

248 {
249 if (mutex_ == nullptr) {
250 mutex_ = new Mutex();
251 }
252 ASSERT(mutex_ != nullptr);
253 if (prng_ == nullptr) {
254 prng_ = new Random();
255 }
256 if (ports_ == nullptr) {
257 ports_ = new PortSet<Entry>();
258 }
259}

◆ IsReceiverInThisIsolateGroupOrClosed()

bool dart::PortMap::IsReceiverInThisIsolateGroupOrClosed ( Dart_Port  receiver,
IsolateGroup group 
)
static

Definition at line 228 of file port.cc.

229 {
230 MutexLocker ml(mutex_);
231 if (ports_ == nullptr) {
232 // Port was closed.
233 return true;
234 }
235 auto it = ports_->TryLookup(receiver);
236 if (it == ports_->end()) {
237 // Port was closed.
238 return true;
239 }
240 auto isolate = (*it).handler->isolate();
241 if (isolate == nullptr) {
242 // Port belongs to a native port instead of an isolate.
243 return false;
244 }
245 return isolate->group() == group;
246}

◆ PostMessage()

bool dart::PortMap::PostMessage ( std::unique_ptr< Message message,
bool  before_events = false 
)
static

Definition at line 152 of file port.cc.

153 {
154 MutexLocker ml(mutex_);
155 if (ports_ == nullptr) {
156 return false;
157 }
158 auto it = ports_->TryLookup(message->dest_port());
159 if (it == ports_->end()) {
160 // Ownership of external data remains with the poster.
161 message->DropFinalizers();
162 return false;
163 }
164 MessageHandler* handler = (*it).handler;
165 ASSERT(handler != nullptr);
166 handler->PostMessage(std::move(message), before_events);
167 return true;
168}
Win32Message message

◆ PrintPortsForMessageHandler()

void dart::PortMap::PrintPortsForMessageHandler ( MessageHandler handler,
JSONStream stream 
)
static

Definition at line 280 of file port.cc.

281 {
282#ifndef PRODUCT
283 JSONObject jsobj(stream);
284 jsobj.AddProperty("type", "_Ports");
285 Object& msg_handler = Object::Handle();
286 {
287 JSONArray ports(&jsobj, "ports");
288 SafepointMutexLocker ml(mutex_);
289 if (ports_ == nullptr) {
290 return;
291 }
292 for (auto& entry : *ports_) {
293 if (entry.handler == handler) {
294 JSONObject port(&ports);
295 port.AddProperty("type", "_Port");
296 port.AddPropertyF("name", "Isolate Port (%" Pd64 ")", entry.port);
297 msg_handler = DartLibraryCalls::LookupHandler(entry.port);
298 port.AddProperty("handler", msg_handler);
299 }
300 }
301 }
302#endif
303}

The documentation for this class was generated from the following files: