-
-
Notifications
You must be signed in to change notification settings - Fork 31.7k
worker: refactor thread id management #25796
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -13,6 +13,7 @@ | |
|
||
using node::options_parser::kDisallowedInEnvironment; | ||
using v8::ArrayBuffer; | ||
using v8::Boolean; | ||
using v8::Context; | ||
using v8::Function; | ||
using v8::FunctionCallbackInfo; | ||
|
@@ -33,9 +34,6 @@ namespace worker { | |
|
||
namespace { | ||
|
||
uint64_t next_thread_id = 1; | ||
Mutex next_thread_id_mutex; | ||
|
||
#if NODE_USE_V8_PLATFORM && HAVE_INSPECTOR | ||
void StartWorkerInspector(Environment* child, const std::string& url) { | ||
child->inspector_agent()->Start(url, | ||
|
@@ -74,17 +72,7 @@ Worker::Worker(Environment* env, | |
const std::string& url, | ||
std::shared_ptr<PerIsolateOptions> per_isolate_opts) | ||
: AsyncWrap(env, wrap, AsyncWrap::PROVIDER_WORKER), url_(url) { | ||
// Generate a new thread id. | ||
{ | ||
Mutex::ScopedLock next_thread_id_lock(next_thread_id_mutex); | ||
thread_id_ = next_thread_id++; | ||
} | ||
|
||
Debug(this, "Creating worker with id %llu", thread_id_); | ||
wrap->Set(env->context(), | ||
env->thread_id_string(), | ||
Number::New(env->isolate(), | ||
static_cast<double>(thread_id_))).FromJust(); | ||
Debug(this, "Creating new worker instance at %p", static_cast<void*>(this)); | ||
|
||
// Set up everything that needs to be set up in the parent environment. | ||
parent_port_ = MessagePort::New(env, env->context()); | ||
|
@@ -130,7 +118,7 @@ Worker::Worker(Environment* env, | |
CHECK_NE(env_, nullptr); | ||
env_->set_abort_on_uncaught_exception(false); | ||
env_->set_worker_context(this); | ||
env_->set_thread_id(thread_id_); | ||
thread_id_ = env_->thread_id(); | ||
|
||
env_->Start(env->profiler_idle_notifier_started()); | ||
env_->ProcessCliArgs(std::vector<std::string>{}, | ||
|
@@ -142,7 +130,15 @@ Worker::Worker(Environment* env, | |
// The new isolate won't be bothered on this thread again. | ||
isolate_->DiscardThreadSpecificMetadata(); | ||
|
||
Debug(this, "Set up worker with id %llu", thread_id_); | ||
wrap->Set(env->context(), | ||
env->thread_id_string(), | ||
Number::New(env->isolate(), static_cast<double>(thread_id_))) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Now come to think of it, maybe this should be a BigInt? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @nodejs/workers Thoughts? BigInt seems like it might be a bit much, but since it’s really just an identifier, that might be okay? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. That sounds OK to me There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. In order to be relevant that'd require spawning more than 9,007,199,254,740,991 threads. If we can spawn 1000 threads every seconds (a lot) and our program does nothing but spawn threads and we disregard any other limit or cleanup - it would take us ~285,616 years to exhaust that number of threads and overflow.
If our machine is super fast and we spawn 100,000 threads per second instead, it would still take us over 2000 years. I certainly hope no one is going to run a Node.js server for that long. I think a double is fine 😅 |
||
.FromJust(); | ||
|
||
Debug(this, | ||
"Set up worker at %p with id %llu", | ||
static_cast<void*>(this), | ||
thread_id_); | ||
} | ||
|
||
bool Worker::is_stopped() const { | ||
|
@@ -562,11 +558,17 @@ void InitWorker(Local<Object> target, | |
|
||
env->SetMethod(target, "getEnvMessagePort", GetEnvMessagePort); | ||
|
||
auto thread_id_string = FIXED_ONE_BYTE_STRING(env->isolate(), "threadId"); | ||
target->Set(env->context(), | ||
thread_id_string, | ||
Number::New(env->isolate(), | ||
static_cast<double>(env->thread_id()))).FromJust(); | ||
target | ||
->Set(env->context(), | ||
env->thread_id_string(), | ||
Number::New(env->isolate(), static_cast<double>(env->thread_id()))) | ||
.FromJust(); | ||
|
||
target | ||
->Set(env->context(), | ||
FIXED_ONE_BYTE_STRING(env->isolate(), "isMainThread"), | ||
Boolean::New(env->isolate(), env->is_main_thread())) | ||
.FromJust(); | ||
} | ||
|
||
} // anonymous namespace | ||
|
Uh oh!
There was an error while loading. Please reload this page.