Skip to content
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
105 changes: 66 additions & 39 deletions system/lib/pthread/proxying.c
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,6 @@ static em_proxying_queue system_proxying_queue = {
.capacity = 0,
};

static _Thread_local bool system_queue_in_use = false;

em_proxying_queue* emscripten_proxy_get_system_queue(void) {
return &system_proxying_queue;
}
Expand Down Expand Up @@ -80,56 +78,93 @@ static em_task_queue* get_tasks_for_thread(em_proxying_queue* q,
return NULL;
}

// Not thread safe.
static em_task_queue* get_or_add_tasks_for_thread(em_proxying_queue* q,
pthread_t thread) {
pthread_mutex_lock(&q->mutex);

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Perhaps we could add a comment the declaration of this mutex in the header file saying something like: "Note: Since this lock is used to control the low level system proxying it is critical that we do not perform any allocation while holding this lock". (or some such language).

em_task_queue* tasks = get_tasks_for_thread(q, thread);
pthread_mutex_unlock(&q->mutex);
if (tasks != NULL) {
return tasks;
}

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this if (get_tasks_for_thread) return tasks; pattern also seem to occur at the start of the loop below? Perhaps we somehow avoid duplicating up here?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It can be done, but the control flow ends up being harder to follow. It's simplest to keep this fast path before the loop separate.

// There were no tasks for the thread; initialize a new em_task_queue. If
// there are not enough queues, allocate more.
if (q->size == q->capacity) {

// There were no tasks for the thread; initialize a new em_task_queue outside
// the lock so allocations cannot deadlock with queue execution.
em_task_queue* new_tasks = em_task_queue_create(thread);
if (new_tasks == NULL) {
return NULL;
}

// Holds the newly allocated task_queues array before it is installed, or the
// old task_queues array after they are swapped so it can be freed outside
// the lock.
em_task_queue** queues_to_free = NULL;
pthread_mutex_lock(&q->mutex);
while (true) {
// Re-check in case another thread added the queue while unlocked.
tasks = get_tasks_for_thread(q, thread);
if (tasks != NULL) {
pthread_mutex_unlock(&q->mutex);
goto out;
}
if (q->size < q->capacity) {
// We have free capacity to add a new queue.
break;
}
// There is not capacity for more queues; reallocate outside the lock.
int new_capacity = q->capacity == 0 ? 1 : q->capacity * 2;
pthread_mutex_unlock(&q->mutex);
em_task_queue** new_task_queues =
realloc(q->task_queues, sizeof(em_task_queue*) * new_capacity);
realloc(queues_to_free, sizeof(em_task_queue*) * new_capacity);
if (new_task_queues == NULL) {
return NULL;
goto out;
}
queues_to_free = new_task_queues;
pthread_mutex_lock(&q->mutex);
if (q->capacity < new_capacity) {
// Copy existing queues to the new array.
if (q->task_queues != NULL) {
memcpy(
queues_to_free, q->task_queues, sizeof(em_task_queue*) * q->size);
}
// Install the new array and arrange for the old to be freed.
em_task_queue** old_task_queues = q->task_queues;
q->task_queues = queues_to_free;
q->capacity = new_capacity;
queues_to_free = old_task_queues;
}
q->task_queues = new_task_queues;
q->capacity = new_capacity;
}
// Initialize the next available task queue.
tasks = em_task_queue_create(thread);
if (tasks == NULL) {
return NULL;
// Add the new task queue.
q->task_queues[q->size++] = new_tasks;
pthread_mutex_unlock(&q->mutex);
tasks = new_tasks;
new_tasks = NULL;

out:
if (queues_to_free != NULL) {
free(queues_to_free);
}
if (new_tasks != NULL) {
em_task_queue_destroy(new_tasks);
}
q->task_queues[q->size++] = tasks;
return tasks;
}

static _Thread_local bool executing_system_queue = false;

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So executing_system_queue is like the old system_queue_in_use but a more narrow meaning?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, and in fact this is just going back to the name and functionality we had before #24565.


void emscripten_proxy_execute_queue(em_proxying_queue* q) {
assert(q != NULL);
assert(pthread_self());

// Below is a recursion and deadlock guard: The recursion guard is to avoid
// infinite recursion when we arrive here from the pthread_lock call below
// that executes the system queue. The per-task_queue recursion lock can't
// catch these recursions because it can only be checked after the lock has
// been acquired.
//
// This also guards against deadlocks when adding to the system queue. When
// the current thread is adding tasks, it locks the queue, but we can
// potentially try to execute the queue during the add (from emscripten_yield
// when malloc takes a lock). This will deadlock the thread, so only try to
// take the lock if the current thread is not using the queue. We then hope
// the queue is executed later when it is unlocked.
// Recursion guard to avoid infinite recursion when we arrive here from the
// pthread_lock call below that executes the system queue. The per-task_queue
// recursion lock can't catch these recursions because it can only be checked
// after the lock has been acquired.
bool is_system_queue = q == &system_proxying_queue;
if (is_system_queue) {
if (system_queue_in_use) {
if (executing_system_queue) {
return;
}
system_queue_in_use = true;
executing_system_queue = true;
}

pthread_mutex_lock(&q->mutex);
Expand All @@ -142,22 +177,13 @@ void emscripten_proxy_execute_queue(em_proxying_queue* q) {
}

if (is_system_queue) {
system_queue_in_use = false;
executing_system_queue = false;
}
}

static bool do_proxy(em_proxying_queue* q, pthread_t target_thread, task t) {
assert(q != NULL);
pthread_mutex_lock(&q->mutex);
bool is_system_queue = q == &system_proxying_queue;

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we keep is_system_queue.. even though it only has a single use? That keeps the diff smaller and make this condition easier to read ?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done.

if (is_system_queue) {
system_queue_in_use = true;
}
em_task_queue* tasks = get_or_add_tasks_for_thread(q, target_thread);
if (is_system_queue) {
system_queue_in_use = false;
}
pthread_mutex_unlock(&q->mutex);
if (tasks == NULL) {
return false;
}
Expand All @@ -168,6 +194,7 @@ static bool do_proxy(em_proxying_queue* q, pthread_t target_thread, task t) {
// special case in that we need to wake the target thread in case it is in
// `emscripten_futex_wait`. Additionally, the _dlopen_proxying_queue also
// requires a wakeup of the target thread after enqueuing work.
bool is_system_queue = q == &system_proxying_queue;
bool needs_notify =
#ifdef EMSCRIPTEN_DYNAMIC_LINKING
q == _dlopen_proxying_queue ||
Expand Down
10 changes: 5 additions & 5 deletions test/codesize/test_codesize_minimal_pthreads.json
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
{
"a.out.js": 6823,
"a.out.js.gz": 3353,
"a.out.nodebug.wasm": 19136,
"a.out.nodebug.wasm.gz": 8818,
"total": 25959,
"total_gz": 12171,
"a.out.nodebug.wasm": 19230,
"a.out.nodebug.wasm.gz": 8898,
"total": 26053,
"total_gz": 12251,
"sent": [
"a (memory)",
"b (exit)",
Expand Down Expand Up @@ -121,6 +121,7 @@
"$em_task_queue_cancel",
"$em_task_queue_create",
"$em_task_queue_dequeue",
"$em_task_queue_destroy",
"$em_task_queue_enqueue",
"$em_task_queue_execute",
"$em_task_queue_free",
Expand All @@ -135,7 +136,6 @@
"$emscripten_stack_set_limits",
"$emscripten_thread_mailbox_unref",
"$free_ctx",
"$get_or_add_tasks_for_thread",
"$get_tasks_for_thread",
"$init_active_ctxs",
"$init_file_lock",
Expand Down
10 changes: 5 additions & 5 deletions test/codesize/test_codesize_minimal_pthreads_memgrowth.json
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
{
"a.out.js": 7281,
"a.out.js.gz": 3572,
"a.out.nodebug.wasm": 19137,
"a.out.nodebug.wasm.gz": 8821,
"total": 26418,
"total_gz": 12393,
"a.out.nodebug.wasm": 19231,
"a.out.nodebug.wasm.gz": 8899,
"total": 26512,
"total_gz": 12471,
"sent": [
"a (memory)",
"b (exit)",
Expand Down Expand Up @@ -121,6 +121,7 @@
"$em_task_queue_cancel",
"$em_task_queue_create",
"$em_task_queue_dequeue",
"$em_task_queue_destroy",
"$em_task_queue_enqueue",
"$em_task_queue_execute",
"$em_task_queue_free",
Expand All @@ -135,7 +136,6 @@
"$emscripten_stack_set_limits",
"$emscripten_thread_mailbox_unref",
"$free_ctx",
"$get_or_add_tasks_for_thread",
"$get_tasks_for_thread",
"$init_active_ctxs",
"$init_file_lock",
Expand Down
Loading
Loading