Skip to content

Commit

Permalink
Merge pull request #5243 from TannerRogalsky/fix_main_thread_locking
Browse files Browse the repository at this point in the history
Fix crash when calling pthread_mutex_lock on the main thread
  • Loading branch information
juj authored Jun 23, 2017
2 parents 519de4c + 3d2305b commit 7654279
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 2 deletions.
4 changes: 4 additions & 0 deletions src/library_pthread.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,10 @@ var LibraryPThread = {
// structure is 'alive'.
{{{ makeSetValue('PThread.mainThreadBlock', C_STRUCTS.pthread.self, 'PThread.mainThreadBlock', 'i32') }}};

// pthread struct robust_list head should point to itself.
var headPtr = PThread.mainThreadBlock + {{{ C_STRUCTS.pthread.robust_list }}};
{{{ makeSetValue('headPtr', 0, 'headPtr', 'i32') }}};

// Allocate memory for thread-local storage.
var tlsMemory = allocate({{{ cDefine('PTHREAD_KEYS_MAX') }}} * 4, "i32*", ALLOC_STATIC);
for (var i = 0; i < {{{ cDefine('PTHREAD_KEYS_MAX') }}}; ++i) HEAPU32[tlsMemory/4+i] = 0;
Expand Down
5 changes: 3 additions & 2 deletions tests/pthread/test_pthread_mutex.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -95,15 +95,16 @@ int main()
pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_RECURSIVE);
pthread_mutex_init(&lock, &attr);

pthread_mutex_lock(&lock);
pthread_mutex_unlock(&lock);

if (emscripten_has_threading_support()) {
// Create new threads in parallel.
for(int i = 0; i < NUM_THREADS; ++i)
CreateThread(i, threadNum++);

emscripten_set_main_loop(WaitToJoin, 0, 0);
} else {
pthread_mutex_lock(&lock);
pthread_mutex_unlock(&lock);
#ifdef REPORT_RESULT
int result = 50;
REPORT_RESULT();
Expand Down

0 comments on commit 7654279

Please sign in to comment.