21
21
struct em_proxying_queue {
22
22
// Protects all accesses to em_task_queues, size, and capacity.
23
23
pthread_mutex_t mutex ;
24
- // If the mutex is locked this is the thread that is using it.
25
- pthread_t active_thread ;
26
24
// `size` task queue pointers stored in an array of size `capacity`.
27
25
em_task_queue * * task_queues ;
28
26
int size ;
@@ -32,12 +30,13 @@ struct em_proxying_queue {
32
30
// The system proxying queue.
33
31
static em_proxying_queue system_proxying_queue = {
34
32
.mutex = PTHREAD_MUTEX_INITIALIZER ,
35
- .active_thread = NULL ,
36
33
.task_queues = NULL ,
37
34
.size = 0 ,
38
35
.capacity = 0 ,
39
36
};
40
37
38
+ static _Thread_local int system_queue_in_use = 0 ;
39
+
41
40
em_proxying_queue * emscripten_proxy_get_system_queue (void ) {
42
41
return & system_proxying_queue ;
43
42
}
@@ -50,7 +49,6 @@ em_proxying_queue* em_proxying_queue_create(void) {
50
49
}
51
50
* q = (em_proxying_queue ){
52
51
.mutex = PTHREAD_MUTEX_INITIALIZER ,
53
- .active_thread = NULL ,
54
52
.task_queues = NULL ,
55
53
.size = 0 ,
56
54
.capacity = 0 ,
@@ -114,27 +112,24 @@ void emscripten_proxy_execute_queue(em_proxying_queue* q) {
114
112
assert (q != NULL );
115
113
assert (pthread_self ());
116
114
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.
122
127
int is_system_queue = q == & system_proxying_queue ;
123
128
if (is_system_queue ) {
124
- if (executing_system_queue ) {
129
+ if (system_queue_in_use ) {
125
130
return ;
126
131
}
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 ;
138
133
}
139
134
140
135
pthread_mutex_lock (& q -> mutex );
@@ -147,16 +142,21 @@ void emscripten_proxy_execute_queue(em_proxying_queue* q) {
147
142
}
148
143
149
144
if (is_system_queue ) {
150
- executing_system_queue = 0 ;
145
+ system_queue_in_use = 0 ;
151
146
}
152
147
}
153
148
154
149
static int do_proxy (em_proxying_queue * q , pthread_t target_thread , task t ) {
155
150
assert (q != NULL );
156
151
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
+ }
158
156
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
+ }
160
160
pthread_mutex_unlock (& q -> mutex );
161
161
if (tasks == NULL ) {
162
162
return 0 ;
0 commit comments