Flutter Engine
The Flutter Engine
dart_microtask_queue.cc
Go to the documentation of this file.
1// Copyright 2013 The Flutter Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
6
8#include "tonic/dart_state.h"
10
11#ifdef OS_IOS
12#include <pthread.h>
13#endif // OS_IOS
14
15namespace tonic {
16namespace {
17
18#ifdef OS_IOS
19// iOS doesn't support the thread_local keyword.
20
21pthread_key_t g_queue_key;
22pthread_once_t g_queue_key_once = PTHREAD_ONCE_INIT;
23
24void MakeKey() {
25 pthread_key_create(&g_queue_key, nullptr);
26}
27
28void SetQueue(DartMicrotaskQueue* queue) {
29 pthread_once(&g_queue_key_once, MakeKey);
30 pthread_setspecific(g_queue_key, queue);
31}
32
33DartMicrotaskQueue* GetQueue() {
34 return static_cast<tonic::DartMicrotaskQueue*>(
35 pthread_getspecific(g_queue_key));
36}
37
38#else
39
40thread_local DartMicrotaskQueue* g_queue = nullptr;
41
42void SetQueue(DartMicrotaskQueue* queue) {
43 g_queue = queue;
44}
45
46DartMicrotaskQueue* GetQueue() {
47 return g_queue;
48}
49
50#endif
51
52} // namespace
53
55
57
59 SetQueue(new DartMicrotaskQueue());
60}
61
63 return GetQueue();
64}
65
67 queue_.emplace_back(DartState::Current(), callback);
68}
69
71 while (!queue_.empty()) {
72 MicrotaskQueue local;
73 std::swap(queue_, local);
74 for (const auto& callback : local) {
75 if (auto dart_state = callback.dart_state().lock()) {
76 DartState::Scope dart_scope(dart_state.get());
77 Dart_Handle result = Dart_InvokeClosure(callback.value(), 0, nullptr);
78 // If the Dart program has set a return code, then it is intending to
79 // shut down by way of a fatal error, and so there is no need to emit a
80 // log message.
81 if (!dart_state->has_set_return_code() || !Dart_IsError(result) ||
84 }
86 if (error != kNoError) {
87 last_error_ = error;
88 }
89 dart_state->MessageEpilogue(result);
91 return;
92 }
93 }
94 }
95}
96
99 SetQueue(nullptr);
100 delete this;
101}
102
104 return last_error_;
105}
106
107} // namespace tonic
void swap(sk_sp< T > &a, sk_sp< T > &b)
Definition: SkRefCnt.h:341
void ScheduleMicrotask(Dart_Handle callback)
static DartMicrotaskQueue * GetForCurrentThread()
DartErrorHandleType GetLastError()
static DartState * Current()
Definition: dart_state.cc:56
struct _Dart_Handle * Dart_Handle
Definition: dart_api.h:258
DART_EXPORT bool Dart_IsFatalError(Dart_Handle handle)
DART_EXPORT DART_WARN_UNUSED_RESULT Dart_Handle Dart_InvokeClosure(Dart_Handle closure, int number_of_arguments, Dart_Handle *arguments)
DART_EXPORT Dart_Isolate Dart_CurrentIsolate(void)
DART_EXPORT bool Dart_IsError(Dart_Handle handle)
VkQueue queue
Definition: main.cc:55
FlKeyEvent uint64_t FlKeyResponderAsyncCallback callback
const uint8_t uint32_t uint32_t GError ** error
GAsyncResult * result
DartErrorHandleType GetErrorHandleType(Dart_Handle handle)
Definition: dart_error.cc:56
DartErrorHandleType
Definition: dart_error.h:67
@ kNoError
Definition: dart_error.h:68
bool CheckAndHandleError(Dart_Handle handle)
Definition: dart_error.cc:33
#define TONIC_DCHECK
Definition: macros.h:32