Skip to content

Commit aebe57a

Browse files
committed
use previous gaurd
1 parent 63484be commit aebe57a

File tree

1 file changed

+24
-24
lines changed

1 file changed

+24
-24
lines changed

system/lib/pthread/proxying.c

Lines changed: 24 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,6 @@
2121
struct em_proxying_queue {
2222
// Protects all accesses to em_task_queues, size, and capacity.
2323
pthread_mutex_t mutex;
24-
// If the mutex is locked this is the thread that is using it.
25-
pthread_t active_thread;
2624
// `size` task queue pointers stored in an array of size `capacity`.
2725
em_task_queue** task_queues;
2826
int size;
@@ -32,12 +30,13 @@ struct em_proxying_queue {
3230
// The system proxying queue.
3331
static em_proxying_queue system_proxying_queue = {
3432
.mutex = PTHREAD_MUTEX_INITIALIZER,
35-
.active_thread = NULL,
3633
.task_queues = NULL,
3734
.size = 0,
3835
.capacity = 0,
3936
};
4037

38+
static _Thread_local int system_queue_in_use = 0;
39+
4140
em_proxying_queue* emscripten_proxy_get_system_queue(void) {
4241
return &system_proxying_queue;
4342
}
@@ -50,7 +49,6 @@ em_proxying_queue* em_proxying_queue_create(void) {
5049
}
5150
*q = (em_proxying_queue){
5251
.mutex = PTHREAD_MUTEX_INITIALIZER,
53-
.active_thread = NULL,
5452
.task_queues = NULL,
5553
.size = 0,
5654
.capacity = 0,
@@ -114,27 +112,24 @@ void emscripten_proxy_execute_queue(em_proxying_queue* q) {
114112
assert(q != NULL);
115113
assert(pthread_self());
116114

117-
// Recursion guard to avoid infinite recursion when we arrive here from the
118-
// pthread_lock call below that executes the system queue. The per-task_queue
119-
// recursion lock can't catch these recursions because it can only be checked
120-
// after the lock has been acquired.
121-
static _Thread_local int executing_system_queue = 0;
115+
// Below is a recursion and deadlock guard:
116+
// The recursion guard is to avoid infinite recursion when we arrive here from
117+
// the pthread_lock call below that executes the system queue. The
118+
// per-task_queue recursion lock can't catch these recursions because it can
119+
// only be checked after the lock has been acquired.
120+
//
121+
// This also guards against deadlocks when adding to the system queue. When
122+
// the current thread is adding tasks, it locks the queue, but we can
123+
// potentially try to execute the queue during the add (from
124+
// emscripten_yield). This will deadlock the thread, so only try to take the
125+
// lock if the current thread is not using the queue. We then hope the
126+
// queue is executed later when it is unlocked.
122127
int is_system_queue = q == &system_proxying_queue;
123128
if (is_system_queue) {
124-
if (executing_system_queue) {
129+
if (system_queue_in_use) {
125130
return;
126131
}
127-
executing_system_queue = 1;
128-
}
129-
130-
// When the current thread is adding tasks it locks the queue, but we can
131-
// potentially try to execute the queue during the add (from
132-
// emscripten_yield). This will deadlock the thread, so only try to take the
133-
// lock if the current thread is not adding to the queue. We then hope the
134-
// queue is executed later when it is unlocked.
135-
// XXX: This could leave to starvation if we never process the queue.
136-
if (q->active_thread == pthread_self()) {
137-
return;
132+
system_queue_in_use = 1;
138133
}
139134

140135
pthread_mutex_lock(&q->mutex);
@@ -147,16 +142,21 @@ void emscripten_proxy_execute_queue(em_proxying_queue* q) {
147142
}
148143

149144
if (is_system_queue) {
150-
executing_system_queue = 0;
145+
system_queue_in_use = 0;
151146
}
152147
}
153148

154149
static int do_proxy(em_proxying_queue* q, pthread_t target_thread, task t) {
155150
assert(q != NULL);
156151
pthread_mutex_lock(&q->mutex);
157-
q->active_thread = pthread_self();
152+
int is_system_queue = q == &system_proxying_queue;
153+
if (is_system_queue) {
154+
system_queue_in_use = 1;
155+
}
158156
em_task_queue* tasks = get_or_add_tasks_for_thread(q, target_thread);
159-
q->active_thread = NULL;
157+
if (is_system_queue) {
158+
system_queue_in_use = 0;
159+
}
160160
pthread_mutex_unlock(&q->mutex);
161161
if (tasks == NULL) {
162162
return 0;

0 commit comments

Comments
 (0)