Skip to content

Commit 1fd8cb8

Browse files
committed
Add comments. Remove pthread and just send the task to self.
1 parent 1b8ce30 commit 1fd8cb8

File tree

1 file changed

+10
-17
lines changed

1 file changed

+10
-17
lines changed

test/pthread/test_pthread_proxy_deadlock.c

Lines changed: 10 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,6 @@
1111
#include <emscripten/proxying.h>
1212
#include <emscripten/threading.h>
1313

14-
bool should_quit = false;
15-
pthread_t looper;
16-
1714
// In the actual implementation of malloc the system queue may be executed
1815
// non-deterministically if malloc is waiting on a mutex. This wraps malloc and
1916
// executes the system queue during every allocation to make the behavior
@@ -26,22 +23,18 @@ void *malloc(size_t size) {
2623
return ptr;
2724
}
2825

29-
void run_on_looper(void* arg) {
30-
emscripten_out("run_on_looper\n");
31-
should_quit = true;
32-
}
33-
34-
void* looper_main(void* arg) {
35-
while (!should_quit) {
36-
emscripten_proxy_execute_queue(emscripten_proxy_get_system_queue());
37-
sched_yield();
38-
}
39-
return NULL;
26+
void task(void* arg) {
27+
emscripten_out("task\n");
4028
}
4129

4230
int main() {
43-
pthread_create(&looper, NULL, looper_main, NULL);
44-
emscripten_proxy_async(emscripten_proxy_get_system_queue(), looper, run_on_looper, NULL);
45-
pthread_join(looper, NULL);
31+
// Tests for a deadlock scenario that can occur when sending a task.
32+
// The sequence of events is:
33+
// 1. Sending a task locks the queue.
34+
// 2. Allocating a new task queue calls malloc.
35+
// 3. Malloc then attempts to execute and lock the already-locked queue,
36+
// causing a deadlock.
37+
// This test ensures our implementation prevents this re-entrant lock.
38+
emscripten_proxy_async(emscripten_proxy_get_system_queue(), pthread_self(), task, NULL);
4639
emscripten_out("done\n");
4740
}

0 commit comments

Comments
 (0)