Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: use monotonic time for performance object #265

Merged
merged 4 commits into from
Jan 18, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
142 changes: 73 additions & 69 deletions NativeScript/runtime/Runtime.h
Original file line number Diff line number Diff line change
@@ -1,91 +1,95 @@
#ifndef Runtime_h
#define Runtime_h

#include "libplatform/libplatform.h"
#include "Caches.h"
#include "Common.h"
#include "ModuleInternal.h"
#include "MetadataBuilder.h"
#include "ModuleInternal.h"
#include "SpinLock.h"
#include "Caches.h"
#include "libplatform/libplatform.h"

namespace tns {

class Runtime {
public:
Runtime();
~Runtime();
v8::Isolate* CreateIsolate();
void Init(v8::Isolate* isolate, bool isWorker = false);
void RunMainScript();
v8::Isolate* GetIsolate();

const int WorkerId();

void SetWorkerId(int workerId);
inline bool IsRuntimeWorker() {
return workerId_ > 0;
}

inline CFRunLoopRef RuntimeLoop() {
return runtimeLoop_;
}
public:
Runtime();
~Runtime();
v8::Isolate* CreateIsolate();
void Init(v8::Isolate* isolate, bool isWorker = false);
void RunMainScript();
v8::Isolate* GetIsolate();

void RunModule(const std::string moduleName);

void RunScript(const std::string script);
const int WorkerId();

static void Initialize();
void SetWorkerId(int workerId);
inline bool IsRuntimeWorker() { return workerId_ > 0; }

static Runtime* GetCurrentRuntime() {
return currentRuntime_;
}

static Runtime* GetRuntime(v8::Isolate* isolate);
inline CFRunLoopRef RuntimeLoop() { return runtimeLoop_; }

static bool IsWorker() {
if (currentRuntime_ == nullptr) {
return false;
}
void RunModule(const std::string moduleName);

return currentRuntime_->IsRuntimeWorker();
}
void RunScript(const std::string script);

static void Initialize();

static Runtime* GetCurrentRuntime() { return currentRuntime_; }

static Runtime* GetRuntime(v8::Isolate* isolate);

static std::shared_ptr<v8::Platform> GetPlatform() {
return platform_;
static bool IsWorker() {
if (currentRuntime_ == nullptr) {
return false;
}

static id GetAppConfigValue(std::string key);

static bool IsAlive(const v8::Isolate* isolate);
private:
static thread_local Runtime* currentRuntime_;
static std::shared_ptr<v8::Platform> platform_;
static std::vector<v8::Isolate*> isolates_;
static SpinMutex isolatesMutex_;
static bool v8Initialized_;
static std::atomic<int> nextIsolateId;

void DefineGlobalObject(v8::Local<v8::Context> context, bool isWorker);
void DefineCollectFunction(v8::Local<v8::Context> context);
void DefineNativeScriptVersion(v8::Isolate* isolate, v8::Local<v8::ObjectTemplate> globalTemplate);
void DefinePerformanceObject(v8::Isolate* isolate, v8::Local<v8::ObjectTemplate> globalTemplate);
void DefineTimeMethod(v8::Isolate* isolate, v8::Local<v8::ObjectTemplate> globalTemplate);
void DefineDrainMicrotaskMethod(v8::Isolate* isolate, v8::Local<v8::ObjectTemplate> globalTemplate);
void DefineDateTimeConfigurationChangeNotificationMethod(v8::Isolate* isolate, v8::Local<v8::ObjectTemplate> globalTemplate);

static void PerformanceNowCallback(const v8::FunctionCallbackInfo<v8::Value>& args);
v8::Isolate* isolate_;
std::unique_ptr<ModuleInternal> moduleInternal_;
int workerId_;
CFRunLoopRef runtimeLoop_;
// TODO: refactor this. This is only needed because, during program termination (UIApplicationMain not called)
// the Cache::Workers is released (static initialization order fiasco https://en.cppreference.com/w/cpp/language/siof)
// so it released the Cache::Workers shared_ptr and then releases the Runtime unique_ptr
// eventually we just need to refactor so that Runtime::Initialize is responsible for its initalization
// and lifecycle
std::shared_ptr<ConcurrentMap<int, std::shared_ptr<Caches::WorkerState>>> workerCache_;
return currentRuntime_->IsRuntimeWorker();
}

static std::shared_ptr<v8::Platform> GetPlatform() { return platform_; }

static id GetAppConfigValue(std::string key);

static bool IsAlive(const v8::Isolate* isolate);

private:
static thread_local Runtime* currentRuntime_;
static std::shared_ptr<v8::Platform> platform_;
static std::vector<v8::Isolate*> isolates_;
static SpinMutex isolatesMutex_;
static bool v8Initialized_;
static std::atomic<int> nextIsolateId;

void DefineGlobalObject(v8::Local<v8::Context> context, bool isWorker);
void DefineCollectFunction(v8::Local<v8::Context> context);
void DefineNativeScriptVersion(v8::Isolate* isolate,
v8::Local<v8::ObjectTemplate> globalTemplate);
void DefinePerformanceObject(v8::Isolate* isolate,
v8::Local<v8::ObjectTemplate> globalTemplate);
void DefineTimeMethod(v8::Isolate* isolate,
v8::Local<v8::ObjectTemplate> globalTemplate);
void DefineDrainMicrotaskMethod(v8::Isolate* isolate,
v8::Local<v8::ObjectTemplate> globalTemplate);
void DefineDateTimeConfigurationChangeNotificationMethod(
v8::Isolate* isolate, v8::Local<v8::ObjectTemplate> globalTemplate);

static void PerformanceNowCallback(
const v8::FunctionCallbackInfo<v8::Value>& args);
v8::Isolate* isolate_;
std::unique_ptr<ModuleInternal> moduleInternal_;
int workerId_;
CFRunLoopRef runtimeLoop_;
double startTime;
double realtimeOrigin;
// TODO: refactor this. This is only needed because, during program
// termination (UIApplicationMain not called) the Cache::Workers is released
// (static initialization order fiasco
// https://en.cppreference.com/w/cpp/language/siof) so it released the
// Cache::Workers shared_ptr and then releases the Runtime unique_ptr
// eventually we just need to refactor so that Runtime::Initialize is
// responsible for its initalization and lifecycle
std::shared_ptr<ConcurrentMap<int, std::shared_ptr<Caches::WorkerState>>>
workerCache_;
};

}
} // namespace tns

#endif /* Runtime_h */
Loading
Loading