Skip to content

Commit

Permalink
feat: use messaging object to pass message to workers (#233)
Browse files Browse the repository at this point in the history
  • Loading branch information
edusperoni authored May 30, 2024
1 parent bb441af commit 7ded0c3
Show file tree
Hide file tree
Showing 13 changed files with 772 additions and 44 deletions.
8 changes: 4 additions & 4 deletions NativeScript/runtime/ConcurrentQueue.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ void ConcurrentQueue::Initialize(CFRunLoopRef runLoop, void (*performWork)(void*
CFRunLoopAddSource(this->runLoop_, this->runLoopTasksSource_, kCFRunLoopCommonModes);
}

void ConcurrentQueue::Push(std::string message) {
void ConcurrentQueue::Push(std::shared_ptr<worker::Message> message) {
if (this->runLoopTasksSource_ != nullptr && !CFRunLoopSourceIsValid(this->runLoopTasksSource_)) {
return;
}
Expand All @@ -27,12 +27,12 @@ void ConcurrentQueue::Push(std::string message) {
this->SignalAndWakeUp();
}

std::vector<std::string> ConcurrentQueue::PopAll() {
std::vector<std::shared_ptr<worker::Message>> ConcurrentQueue::PopAll() {
std::unique_lock<std::mutex> mlock(this->mutex_);
std::vector<std::string> messages;
std::vector<std::shared_ptr<worker::Message>> messages;

while (!this->messagesQueue_.empty()) {
std::string message = this->messagesQueue_.front();
std::shared_ptr<worker::Message> message = this->messagesQueue_.front();
this->messagesQueue_.pop();
messages.push_back(message);
}
Expand Down
7 changes: 4 additions & 3 deletions NativeScript/runtime/ConcurrentQueue.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,18 @@
#include <string>
#include <queue>
#include <mutex>
#include "Message.hpp"

namespace tns {

struct ConcurrentQueue {
public:
void Initialize(CFRunLoopRef runLoop, void (*performWork)(void*), void* info);
void Push(std::string message);
std::vector<std::string> PopAll();
void Push(std::shared_ptr<worker::Message> message);
std::vector<std::shared_ptr<worker::Message>> PopAll();
void Terminate();
private:
std::queue<std::string> messagesQueue_;
std::queue<std::shared_ptr<worker::Message>> messagesQueue_;
CFRunLoopSourceRef runLoopTasksSource_ = nullptr;
CFRunLoopRef runLoop_ = nullptr;
bool terminated = false;
Expand Down
6 changes: 3 additions & 3 deletions NativeScript/runtime/DataWrapper.h
Original file line number Diff line number Diff line change
Expand Up @@ -654,12 +654,12 @@ class ExtVectorWrapper: public BaseDataWrapper {

class WorkerWrapper: public BaseDataWrapper {
public:
WorkerWrapper(v8::Isolate* mainIsolate, std::function<void (v8::Isolate*, v8::Local<v8::Object> thiz, std::string)> onMessage);
WorkerWrapper(v8::Isolate* mainIsolate, std::function<void (v8::Isolate*, v8::Local<v8::Object> thiz, std::shared_ptr<worker::Message>)> onMessage);

void Start(std::shared_ptr<v8::Persistent<v8::Value>> poWorker, std::function<v8::Isolate* ()> func);
void CallOnErrorHandlers(v8::TryCatch& tc);
void PassUncaughtExceptionFromWorkerToMain(v8::Local<v8::Context> context, v8::TryCatch& tc, bool async = true);
void PostMessage(std::string message);
void PostMessage(std::shared_ptr<worker::Message> message);
void Close();
void Terminate();

Expand Down Expand Up @@ -691,7 +691,7 @@ class WorkerWrapper: public BaseDataWrapper {
std::atomic<bool> isTerminating_;
bool isDisposed_;
bool isWeak_;
std::function<void (v8::Isolate*, v8::Local<v8::Object> thiz, std::string)> onMessage_;
std::function<void (v8::Isolate*, v8::Local<v8::Object> thiz, std::shared_ptr<worker::Message>)> onMessage_;
std::shared_ptr<v8::Persistent<v8::Value>> poWorker_;
ConcurrentQueue queue_;
static std::atomic<int> nextId_;
Expand Down
51 changes: 51 additions & 0 deletions NativeScript/runtime/Helpers.h
Original file line number Diff line number Diff line change
Expand Up @@ -345,6 +345,57 @@ void SetConstructorFunction(v8::Isolate* isolate,
SetConstructorFunctionFlag::SET_CLASS_NAME);


template <int N>
inline v8::Local<v8::String> FIXED_ONE_BYTE_STRING(
v8::Isolate* isolate,
const char(&data)[N]) {
return OneByteString(isolate, data, N - 1);
}

template <std::size_t N>
inline v8::Local<v8::String> FIXED_ONE_BYTE_STRING(
v8::Isolate* isolate,
const std::array<char, N>& arr) {
return OneByteString(isolate, arr.data(), N - 1);
}

class PersistentToLocal {
public:
// If persistent.IsWeak() == false, then do not call persistent.Reset()
// while the returned Local<T> is still in scope, it will destroy the
// reference to the object.
template <class TypeName>
static inline v8::Local<TypeName> Default(
v8::Isolate* isolate,
const v8::PersistentBase<TypeName>& persistent) {
if (persistent.IsWeak()) {
return PersistentToLocal::Weak(isolate, persistent);
} else {
return PersistentToLocal::Strong(persistent);
}
}

// Unchecked conversion from a non-weak Persistent<T> to Local<T>,
// use with care!
//
// Do not call persistent.Reset() while the returned Local<T> is still in
// scope, it will destroy the reference to the object.
template <class TypeName>
static inline v8::Local<TypeName> Strong(
const v8::PersistentBase<TypeName>& persistent) {
// DCHECK(!persistent.IsWeak());
return *reinterpret_cast<v8::Local<TypeName>*>(
const_cast<v8::PersistentBase<TypeName>*>(&persistent));
}

template <class TypeName>
static inline v8::Local<TypeName> Weak(
v8::Isolate* isolate,
const v8::PersistentBase<TypeName>& persistent) {
return v8::Local<TypeName>::New(isolate, persistent);
}
};

}

#endif /* Helpers_h */
Loading

0 comments on commit 7ded0c3

Please sign in to comment.