Skip to content

Commit

Permalink
refactor jsapi - command listener
Browse files Browse the repository at this point in the history
  • Loading branch information
hyj1991 committed Oct 27, 2022
1 parent df9b7f2 commit 5ed0399
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 29 deletions.
29 changes: 9 additions & 20 deletions src/commands/listener.cc
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,11 @@
#include "commands/parser.h"
#include "environment_data.h"
#include "logger.h"
#include "nan.h"
#include "platform/platform.h"
#include "uv.h"
#include "xpf_mutex-inl.h"

namespace xprofiler {
using Nan::False;
using Nan::FunctionCallbackInfo;
using Nan::ThrowTypeError;
using Nan::True;
using v8::Value;

namespace per_process {
Mutex command_listener_mutex;
Expand All @@ -24,25 +18,20 @@ static void CreateCommandsListenerThread(void* unused) {
CreateIpcServer(ParseCmd);
}

void RunCommandsListener(const FunctionCallbackInfo<Value>& info) {
int StartCommandsListener(EnvironmentData* env_data) {
Mutex::ScopedLock lock(per_process::command_listener_mutex);
if (per_process::command_listener_thread_created) {
info.GetReturnValue().Set(True());
return;
return 0;
}
int rc = 0;

// init commands listener thread
rc = uv_thread_create(&per_process::uv_commands_listener_thread,
CreateCommandsListenerThread, nullptr);
if (rc != 0) {
ThrowTypeError("xprofiler: create uv commands listener thread failed!");
info.GetReturnValue().Set(False());
return;
int rc = uv_thread_create(&per_process::uv_commands_listener_thread,
CreateCommandsListenerThread, nullptr);
if (rc == 0) {
InfoT("init", env_data->thread_id(),
"commands listener: listener thread created.");
}
EnvironmentData* env_data = EnvironmentData::GetCurrent(info);
InfoT("init", env_data->thread_id(),
"commands listener: listener thread created.");

info.GetReturnValue().Set(True());
return rc;
}
} // namespace xprofiler
11 changes: 3 additions & 8 deletions src/commands/listener.h
Original file line number Diff line number Diff line change
@@ -1,14 +1,9 @@
#ifndef XPROFILER_SRC_COMMANDS_LISTENER_H
#define XPROFILER_SRC_COMMANDS_LISTENER_H

#include "nan.h"
#include "environment_data.h"

namespace xprofiler {
using Nan::FunctionCallbackInfo;
using v8::Value;

// javascript-accessible
void RunCommandsListener(const FunctionCallbackInfo<Value>& info);
} // namespace xprofiler
int StartCommandsListener(EnvironmentData* env_data);
}

#endif /* XPROFILER_SRC_COMMANDS_LISTENER_H */
21 changes: 21 additions & 0 deletions src/jsapi/export_thread_listener.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
#include "include/export_thread_listener.h"

#include "commands/listener.h"
#include "environment_data.h"

namespace xprofiler {
using Nan::False;
using Nan::FunctionCallbackInfo;
using Nan::ThrowTypeError;
using Nan::True;
using v8::Value;

void RunCommandsListener(const FunctionCallbackInfo<Value>& info) {
EnvironmentData* env_data = EnvironmentData::GetCurrent(info);
int rc = StartCommandsListener(env_data);
if (rc != 0) {
ThrowTypeError("xprofiler: create uv commands listener thread failed!");
}
info.GetReturnValue().Set(rc == 0 ? True() : False());
}
} // namespace xprofiler
10 changes: 10 additions & 0 deletions src/jsapi/include/export_thread_listener.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
#ifndef XPROFILER_SRC_JSAPI_THREAD_LISTENER_H
#define XPROFILER_SRC_JSAPI_THREAD_LISTENER_H

#include "nan.h"

namespace xprofiler {
void RunCommandsListener(const Nan::FunctionCallbackInfo<v8::Value>& info);
}

#endif /* XPROFILER_SRC_JSAPI_THREAD_LISTENER_H */
2 changes: 1 addition & 1 deletion src/xprofiler.cc
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
#include "commands/listener.h"
#include "jsapi/include/export_configure.h"
#include "jsapi/include/export_environment.h"
#include "jsapi/include/export_hooks.h"
#include "jsapi/include/export_http.h"
#include "jsapi/include/export_logger.h"
#include "jsapi/include/export_thread_listener.h"
#include "jsapi/include/export_thread_logbypass.h"
#include "library/common.h"
#include "nan.h"
Expand Down

0 comments on commit 5ed0399

Please sign in to comment.