Skip to content

Commit 7654279

Browse files
authored
Merge pull request #5243 from TannerRogalsky/fix_main_thread_locking
Fix crash when calling pthread_mutex_lock on the main thread
2 parents 519de4c + 3d2305b commit 7654279

File tree

2 files changed

+7
-2
lines changed

2 files changed

+7
-2
lines changed

src/library_pthread.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,10 @@ var LibraryPThread = {
2525
// structure is 'alive'.
2626
{{{ makeSetValue('PThread.mainThreadBlock', C_STRUCTS.pthread.self, 'PThread.mainThreadBlock', 'i32') }}};
2727

28+
// pthread struct robust_list head should point to itself.
29+
var headPtr = PThread.mainThreadBlock + {{{ C_STRUCTS.pthread.robust_list }}};
30+
{{{ makeSetValue('headPtr', 0, 'headPtr', 'i32') }}};
31+
2832
// Allocate memory for thread-local storage.
2933
var tlsMemory = allocate({{{ cDefine('PTHREAD_KEYS_MAX') }}} * 4, "i32*", ALLOC_STATIC);
3034
for (var i = 0; i < {{{ cDefine('PTHREAD_KEYS_MAX') }}}; ++i) HEAPU32[tlsMemory/4+i] = 0;

tests/pthread/test_pthread_mutex.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -95,15 +95,16 @@ int main()
9595
pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_RECURSIVE);
9696
pthread_mutex_init(&lock, &attr);
9797

98+
pthread_mutex_lock(&lock);
99+
pthread_mutex_unlock(&lock);
100+
98101
if (emscripten_has_threading_support()) {
99102
// Create new threads in parallel.
100103
for(int i = 0; i < NUM_THREADS; ++i)
101104
CreateThread(i, threadNum++);
102105

103106
emscripten_set_main_loop(WaitToJoin, 0, 0);
104107
} else {
105-
pthread_mutex_lock(&lock);
106-
pthread_mutex_unlock(&lock);
107108
#ifdef REPORT_RESULT
108109
int result = 50;
109110
REPORT_RESULT();

0 commit comments

Comments
 (0)