|
82 | 82 | : runtime_(runtime),
|
83 | 83 | messages_(),
|
84 | 84 | runningNestedLoops_(false) {
|
85 |
| - this->messagesQueue_ = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0); |
| 85 | + this->messagesQueue_ = dispatch_queue_create("NativeScript.v8.inspector.message_queue", DISPATCH_QUEUE_SERIAL); |
| 86 | + this->messageLoopQueue_ = dispatch_queue_create("NativeScript.v8.inspector.message_loop_queue", DISPATCH_QUEUE_SERIAL); |
86 | 87 | this->messageArrived_ = dispatch_semaphore_create(0);
|
87 | 88 | }
|
88 | 89 |
|
|
101 | 102 | this->disconnect();
|
102 | 103 | }
|
103 | 104 |
|
104 |
| -void JsV8InspectorClient::onFrontendMessageReceived(std::string &message) { |
105 |
| - __block std::string strCopy = message; |
| 105 | +void JsV8InspectorClient::onFrontendMessageReceived(std::string message) { |
106 | 106 | dispatch_sync(this->messagesQueue_, ^{
|
107 |
| - this->messages_.push_back(strCopy); |
| 107 | + this->messages_.push_back(message); |
108 | 108 | dispatch_semaphore_signal(messageArrived_);
|
109 | 109 | });
|
110 | 110 |
|
111 | 111 | tns::ExecuteOnMainThread([this, message]() {
|
112 |
| - dispatch_sync(this->messagesQueue_, ^{ |
113 |
| - while (this->messages_.size() > 0) { |
114 |
| - std::string message = this->PumpMessage(); |
| 112 | + dispatch_sync(this->messageLoopQueue_, ^{ |
| 113 | + // prevent execution if we're already pumping messages |
| 114 | + if (runningNestedLoops_ && !terminated_) { |
| 115 | + return; |
| 116 | + }; |
| 117 | + std::string message; |
| 118 | + do { |
| 119 | + message = this->PumpMessage(); |
115 | 120 | if (!message.empty()) {
|
116 | 121 | this->dispatchMessage(message);
|
117 | 122 | }
|
118 |
| - } |
| 123 | + } while (!message.empty()); |
119 | 124 | });
|
| 125 | + |
120 | 126 | });
|
121 | 127 | }
|
122 | 128 |
|
|
163 | 169 | }
|
164 | 170 |
|
165 | 171 | void JsV8InspectorClient::runMessageLoopOnPause(int contextGroupId) {
|
166 |
| - if (runningNestedLoops_) { |
| 172 | + __block auto loopsRunning = false; |
| 173 | + dispatch_sync(this->messageLoopQueue_, ^{ |
| 174 | + loopsRunning = runningNestedLoops_; |
| 175 | + terminated_ = false; |
| 176 | + if (runningNestedLoops_) { |
| 177 | + return; |
| 178 | + } |
| 179 | + this->runningNestedLoops_ = true; |
| 180 | + }); |
| 181 | + |
| 182 | + if (loopsRunning) { |
167 | 183 | return;
|
168 | 184 | }
|
169 |
| - |
170 |
| - terminated_ = false; |
171 |
| - this->runningNestedLoops_ = true; |
| 185 | + |
172 | 186 | bool shouldWait = false;
|
173 | 187 | while (!terminated_) {
|
174 | 188 | std::string message = this->PumpMessage();
|
|
183 | 197 | Isolate* isolate = runtime_->GetIsolate();
|
184 | 198 | platform::PumpMessageLoop(platform.get(), isolate, platform::MessageLoopBehavior::kDoNotWait);
|
185 | 199 | if(shouldWait && !terminated_) {
|
186 |
| - dispatch_semaphore_wait(messageArrived_, dispatch_time(DISPATCH_TIME_NOW, 1000000)); // 1ms in ns |
| 200 | + dispatch_semaphore_wait(messageArrived_, dispatch_time(DISPATCH_TIME_NOW, 1 * NSEC_PER_MSEC)); // 1ms |
187 | 201 | }
|
188 | 202 | }
|
189 |
| - |
190 |
| - terminated_ = false; |
191 |
| - runningNestedLoops_ = false; |
| 203 | + |
| 204 | + dispatch_sync(this->messageLoopQueue_, ^{ |
| 205 | + terminated_ = false; |
| 206 | + runningNestedLoops_ = false; |
| 207 | + }); |
192 | 208 | }
|
193 | 209 |
|
194 | 210 | void JsV8InspectorClient::quitMessageLoopOnPause() {
|
195 |
| - terminated_ = true; |
| 211 | + dispatch_sync(this->messageLoopQueue_, ^{ |
| 212 | + terminated_ = true; |
| 213 | + }); |
196 | 214 | }
|
197 | 215 |
|
198 | 216 | void JsV8InspectorClient::sendResponse(int callId, std::unique_ptr<StringBuffer> message) {
|
|
0 commit comments