Skip to content
This repository has been archived by the owner on Apr 22, 2023. It is now read-only.

Commit

Permalink
isolates: make main thread reap subordinate threads
Browse files Browse the repository at this point in the history
  • Loading branch information
bnoordhuis committed Dec 13, 2011
1 parent 0a0322a commit 6228172
Showing 1 changed file with 18 additions and 15 deletions.
33 changes: 18 additions & 15 deletions src/node.cc
Expand Up @@ -97,7 +97,7 @@ extern char **environ;


#include <node_vars.h>
#include "atomic_ops.h"
#include <node_atomic.h>

// We do the following to minimize the detal between v0.6 branch. We want to
// use the variables as they were being used before.
Expand Down Expand Up @@ -1837,26 +1837,14 @@ static unsigned long NewThreadId() {
}


// TODO break up in definition and implementation and move to separate files
struct ThreadInfo {
AtomicQueueEntry queue_entry_;
unsigned long thread_id_;
uv_thread_t thread_;
char** argv_;
int argc_;

ThreadInfo(int argc, char** argv) {
thread_id_ = NewThreadId();

argc_ = argc;
argv_ = new char*[argc_ + 1];

for (int i = 0; i < argc_; ++i) {
size_t size = 1 + strlen(argv[i]);
argv_[i] = new char[size];
memcpy(argv_[i], argv[i], size);
}
argv_[argc_] = NULL;
}

ThreadInfo(Handle<Array> args) {
argc_ = args->Length();
argv_ = new char*[argc_ + 1];
Expand All @@ -1868,17 +1856,27 @@ struct ThreadInfo {
memcpy(argv_[i], *str, size);
}
argv_[argc_] = NULL;

active_threads_.Insert(this);
}


~ThreadInfo() {
active_threads_.Remove(this);

for (int i = 0; i < argc_; ++i) {
delete[] argv_[i];
}
delete[] argv_;
}

static AtomicQueue<ThreadInfo> active_threads_;
};


AtomicQueue<ThreadInfo> ThreadInfo::active_threads_;


static void RunIsolate(void* arg) {
ThreadInfo* ti = reinterpret_cast<ThreadInfo*>(arg);

Expand Down Expand Up @@ -2762,6 +2760,11 @@ int Start(int argc, char *argv[]) {
StartThread(NewThreadId(), isolate, argc, argv);
isolate->Dispose();

while (ThreadInfo* ti = ThreadInfo::active_threads_.Pop()) {
if (uv_thread_join(&ti->thread_))
abort();
}

#ifndef NDEBUG
// Clean up.
V8::Dispose();
Expand Down

0 comments on commit 6228172

Please sign in to comment.