-
Notifications
You must be signed in to change notification settings - Fork 3.4k
Make longjmp/exceptions helpers thread-safe #12056
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Changes from 9 commits
Commits
Show all changes
29 commits
Select commit
Hold shift + click to select a range
5786570
Make asm.js threading globals thread-safe
kripken 0faaddd
make compiler-rt an MT library?
kripken 0ae338f
fix
kripken e4f69ed
Merge remote-tracking branch 'origin/master' into exc
kripken 3cba11a
Merge remote-tracking branch 'origin/master' into exc
kripken fccf48c
feedback: refactor into a side file
kripken a9741ea
add a test
kripken c4d680f
Merge remote-tracking branch 'origin/master' into exc
kripken daff2df
add a test that can't pass yet without a wasm backend fix [ci skip]
kripken 5a4ea53
Merge remote-tracking branch 'origin/master' into exc
kripken faa03bc
use threads.h [ci skip]
kripken 06ab316
add a compiler-rt mt variant [ci skip]
kripken b3db6a9
review feedback [ci skip]
kripken 7955ce2
Also build libc_rt_wasm with -mt when needed
kripken 96cbf74
fix test
kripken af889b0
Merge remote-tracking branch 'origin/master' into exc
kripken 0629608
Revert "Disable tests to allow LLVM to roll in (#12337)"
kripken 9e6193e
[ci skip]
kripken 586767e
Merge remote-tracking branch 'origin/master' into exc
kripken e55647a
revert the extra disabled test too [ci skip]
kripken c76b122
comment
kripken d42e148
embind and asan_js libraries now also need to be built with -mt
kripken 36c0ee0
-mt
kripken 05257b8
Revert "-mt"
kripken 5657df5
Revert "embind and asan_js libraries now also need to be built with -mt"
kripken b312b57
Revert "Also build libc_rt_wasm with -mt when needed"
kripken 44b8dda
fix comment
kripken 704a676
cleaner [ci skip]
kripken 84ab45b
remove unneeded change after the llvm change
kripken File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
/* | ||
* Copyright 2018 The Emscripten Authors. All rights reserved. | ||
* Emscripten is available under two separate licenses, the MIT license and the | ||
* University of Illinois/NCSA Open Source License. Both these licenses can be | ||
* found in the LICENSE file. | ||
*/ | ||
|
||
/* | ||
References to these longjmp- and exceptions-supporting things are generated | ||
in the llvm backend. | ||
|
||
Note that these might make more sense in compiler-rt, but they need to be | ||
built with multithreading support when relevant (to avoid races between | ||
threads that throw at the same time etc.), and compiler-rt is not built | ||
that way atm. | ||
*/ | ||
|
||
int _Thread_local __THREW__ = 0; | ||
int _Thread_local __threwValue = 0; | ||
|
||
void setThrew(int threw, int value) { | ||
if (__THREW__ == 0) { | ||
__THREW__ = threw; | ||
__threwValue = value; | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -30,4 +30,3 @@ long* _get_timezone() { | |
|
||
void __lock(void* ptr) {} | ||
void __unlock(void* ptr) {} | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
// Copyright 2019 The Emscripten Authors. All rights reserved. | ||
// Emscripten is available under two separate licenses, the MIT license and the | ||
// University of Illinois/NCSA Open Source License. Both these licenses can be | ||
// found in the LICENSE file. | ||
|
||
#include <stdio.h> | ||
#include <stdlib.h> | ||
#include <pthread.h> | ||
#include <assert.h> | ||
#include <emscripten.h> | ||
|
||
#include <atomic> | ||
|
||
#define NUM_THREADS 2 | ||
#define TOTAL 1000 | ||
#define THREAD_ADDS 750 | ||
#define MAIN_ADDS 5 | ||
|
||
static std::atomic<int> sum; | ||
static std::atomic<int> total; | ||
|
||
void *ThreadMain(void *arg) { | ||
for (int i = 0; i < TOTAL; i++) { | ||
try { | ||
// Throw two different types, to make sure we check throwing and landing | ||
// pad behavior. | ||
if (i & 3) { | ||
throw 3.14159f; | ||
} | ||
throw i; | ||
} catch (int x) { | ||
total += x; | ||
} catch (float f) { | ||
sum++; | ||
// wait for a change, so we see interleaved processing. | ||
int last = sum.load(); | ||
while (sum.load() == last) {} | ||
} | ||
} | ||
pthread_exit((void*)TOTAL); | ||
} | ||
|
||
pthread_t thread[NUM_THREADS]; | ||
|
||
void CreateThread(int i) | ||
{ | ||
pthread_attr_t attr; | ||
pthread_attr_init(&attr); | ||
pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_JOINABLE); | ||
static int counter = 1; | ||
kripken marked this conversation as resolved.
Show resolved
Hide resolved
|
||
int rc = pthread_create(&thread[i], &attr, ThreadMain, (void*)i); | ||
kripken marked this conversation as resolved.
Show resolved
Hide resolved
|
||
assert(rc == 0); | ||
pthread_attr_destroy(&attr); | ||
} | ||
|
||
void loop() { | ||
static int main_adds = 0; | ||
int worker_adds = sum.load() - main_adds; | ||
sum++; | ||
main_adds++; | ||
printf("main iter %d : %d\n", main_adds, worker_adds); | ||
if (worker_adds == NUM_THREADS * THREAD_ADDS && | ||
main_adds >= MAIN_ADDS) { | ||
printf("done: %d.\n", total.load()); | ||
emscripten_cancel_main_loop(); | ||
exit(0); | ||
} | ||
} | ||
|
||
int main() { | ||
// Create initial threads. | ||
for(int i = 0; i < NUM_THREADS; ++i) { | ||
printf("maek\n"); | ||
CreateThread(i); | ||
} | ||
|
||
emscripten_set_main_loop(loop, 0, 0); | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
done: 249000. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It looks like you can use the
thread_local
keyword if you includeI think that i slightly more readable and also the same keyword as C++ so less confusing.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Makes sense, done.