Skip to content

Commit

Permalink
fix: use serial queues and revert string copy changes (#156)
Browse files Browse the repository at this point in the history
  • Loading branch information
edusperoni authored Mar 30, 2022
1 parent 4f28eb0 commit e8681ff
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 24 deletions.
2 changes: 1 addition & 1 deletion NativeScript/NativeScript-Prefix.pch
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#ifndef NativeScript_Prefix_pch
#define NativeScript_Prefix_pch

#define NATIVESCRIPT_VERSION "8.2.2"
#define NATIVESCRIPT_VERSION "8.2.3-alpha.0"

#ifdef DEBUG
#define SIZEOF_OFF_T 8
Expand Down
5 changes: 1 addition & 4 deletions NativeScript/inspector/InspectorServer.mm
Original file line number Diff line number Diff line change
Expand Up @@ -78,10 +78,7 @@
if (size) {
NSString* payload = [[NSString alloc] initWithData:(NSData*)data encoding:NSUTF16LittleEndianStringEncoding];

if (payload) {
std::string result = [payload UTF8String];
onMessage(result);
}
onMessage([payload UTF8String]);

#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Warc-retain-cycles"
Expand Down
3 changes: 2 additions & 1 deletion NativeScript/inspector/JsV8InspectorClient.h
Original file line number Diff line number Diff line change
Expand Up @@ -44,14 +44,15 @@ class JsV8InspectorClient : V8InspectorClient, V8Inspector::Channel {
std::vector<std::string> messages_;
bool runningNestedLoops_;
dispatch_queue_t messagesQueue_;
dispatch_queue_t messageLoopQueue_;
dispatch_semaphore_t messageArrived_;
std::function<void (std::string)> sender_;
bool isWaitingForDebugger_;

void enableInspector(int argc, char** argv);
void notify(std::unique_ptr<StringBuffer> message);
void onFrontendConnected(std::function<void (std::string)> sender);
void onFrontendMessageReceived(std::string &message);
void onFrontendMessageReceived(std::string message);
template <class TypeName>
static v8::Local<TypeName> PersistentToLocal(v8::Isolate* isolate, const v8::Persistent<TypeName>& persistent);
std::string PumpMessage();
Expand Down
52 changes: 35 additions & 17 deletions NativeScript/inspector/JsV8InspectorClient.mm
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,8 @@
: runtime_(runtime),
messages_(),
runningNestedLoops_(false) {
this->messagesQueue_ = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
this->messagesQueue_ = dispatch_queue_create("NativeScript.v8.inspector.message_queue", DISPATCH_QUEUE_SERIAL);
this->messageLoopQueue_ = dispatch_queue_create("NativeScript.v8.inspector.message_loop_queue", DISPATCH_QUEUE_SERIAL);
this->messageArrived_ = dispatch_semaphore_create(0);
}

Expand All @@ -101,22 +102,27 @@
this->disconnect();
}

void JsV8InspectorClient::onFrontendMessageReceived(std::string &message) {
__block std::string strCopy = message;
void JsV8InspectorClient::onFrontendMessageReceived(std::string message) {
dispatch_sync(this->messagesQueue_, ^{
this->messages_.push_back(strCopy);
this->messages_.push_back(message);
dispatch_semaphore_signal(messageArrived_);
});

tns::ExecuteOnMainThread([this, message]() {
dispatch_sync(this->messagesQueue_, ^{
while (this->messages_.size() > 0) {
std::string message = this->PumpMessage();
dispatch_sync(this->messageLoopQueue_, ^{
// prevent execution if we're already pumping messages
if (runningNestedLoops_ && !terminated_) {
return;
};
std::string message;
do {
message = this->PumpMessage();
if (!message.empty()) {
this->dispatchMessage(message);
}
}
} while (!message.empty());
});

});
}

Expand Down Expand Up @@ -163,12 +169,20 @@
}

void JsV8InspectorClient::runMessageLoopOnPause(int contextGroupId) {
if (runningNestedLoops_) {
__block auto loopsRunning = false;
dispatch_sync(this->messageLoopQueue_, ^{
loopsRunning = runningNestedLoops_;
terminated_ = false;
if (runningNestedLoops_) {
return;
}
this->runningNestedLoops_ = true;
});

if (loopsRunning) {
return;
}

terminated_ = false;
this->runningNestedLoops_ = true;

bool shouldWait = false;
while (!terminated_) {
std::string message = this->PumpMessage();
Expand All @@ -183,16 +197,20 @@
Isolate* isolate = runtime_->GetIsolate();
platform::PumpMessageLoop(platform.get(), isolate, platform::MessageLoopBehavior::kDoNotWait);
if(shouldWait && !terminated_) {
dispatch_semaphore_wait(messageArrived_, dispatch_time(DISPATCH_TIME_NOW, 1000000)); // 1ms in ns
dispatch_semaphore_wait(messageArrived_, dispatch_time(DISPATCH_TIME_NOW, 1 * NSEC_PER_MSEC)); // 1ms
}
}

terminated_ = false;
runningNestedLoops_ = false;

dispatch_sync(this->messageLoopQueue_, ^{
terminated_ = false;
runningNestedLoops_ = false;
});
}

void JsV8InspectorClient::quitMessageLoopOnPause() {
terminated_ = true;
dispatch_sync(this->messageLoopQueue_, ^{
terminated_ = true;
});
}

void JsV8InspectorClient::sendResponse(int callId, std::unique_ptr<StringBuffer> message) {
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "@nativescript/ios",
"description": "NativeScript Runtime for iOS",
"version": "8.2.2",
"version": "8.2.3-alpha.0",
"keywords": [
"NativeScript",
"iOS",
Expand Down

0 comments on commit e8681ff

Please sign in to comment.