Skip to content

Cleanup pthread tests. NFC. #13211

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
merged 2 commits into from
Jan 11, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions system/include/emscripten/threading.h
Original file line number Diff line number Diff line change
Expand Up @@ -393,21 +393,21 @@ void emscripten_thread_sleep(double msecs);
// Sets the profiler status of the calling thread. This is a no-op if thread
// profiling is not active.
// This is an internal function and generally not intended for user code.
// When thread profiler is not enabled (not building with --threadprofiling),
// When thread profiler is not enabled (not building with --threadprofiler),
// this is a no-op.
void emscripten_set_current_thread_status(EM_THREAD_STATUS newStatus);

// Sets the profiler status of the calling thread, but only if it was in the
// expected status beforehand.
// This is an internal function and generally not intended for user code.
// When thread profiler is not enabled (not building with --threadprofiling),
// When thread profiler is not enabled (not building with --threadprofiler),
// this is a no-op.
void emscripten_conditional_set_current_thread_status(EM_THREAD_STATUS expectedStatus, EM_THREAD_STATUS newStatus);

// Sets the name of the given thread. Pass pthread_self() as the thread ID to
// set the name of the calling thread.
// The name parameter is a UTF-8 encoded string which is truncated to 32 bytes.
// When thread profiler is not enabled (not building with --threadprofiling),
// When thread profiler is not enabled (not building with --threadprofiler),
// this is a no-op.
void emscripten_set_thread_name(pthread_t threadId, const char *name);

Expand Down
2 changes: 1 addition & 1 deletion system/lib/libc/musl/src/internal/pthread_impl.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ struct pthread {
// by direct pointer arithmetic in worker.js.
int threadStatus; // 0: thread not exited, 1: exited.
int threadExitCode; // Thread exit code.
void *profilerBlock; // If --threadprofiling is enabled, this pointer is allocated to contain internal information about the thread state for profiling purposes.
void *profilerBlock; // If --threadprofiler is enabled, this pointer is allocated to contain internal information about the thread state for profiling purposes.
#endif

struct pthread *self;
Expand Down
14 changes: 5 additions & 9 deletions tests/core/pthread/create.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,8 @@ static std::atomic<int> sum;

void *ThreadMain(void *arg) {
for (int i = 0; i < TOTAL; i++) {
sum++;
// wait for a change, so we see interleaved processing.
int last = sum.load();
int last = ++sum;
while (sum.load() == last) {}
}
pthread_exit((void*)TOTAL);
Expand All @@ -30,24 +29,20 @@ pthread_t thread[NUM_THREADS];

void CreateThread(int i)
{
static int counter = 1;
int rc = pthread_create(&thread[i], nullptr, ThreadMain, (void*)i);
assert(rc == 0);
}

void mainn() {
static int main_adds = 0;
int worker_adds = sum.load() - main_adds;
sum++;
main_adds++;
int worker_adds = sum++ - main_adds++;
printf("main iter %d : %d\n", main_adds, worker_adds);
if (worker_adds == NUM_THREADS * TOTAL) {
printf("done!\n");
#ifndef ALLOW_SYNC
emscripten_cancel_main_loop();
#else
exit(0);
emscripten_cancel_main_loop();
#endif
exit(0);
}
}

Expand All @@ -64,4 +59,5 @@ int main() {
#else
while (1) mainn();
#endif
return 0;
}
10 changes: 4 additions & 6 deletions tests/core/pthread/exceptions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,8 @@ void *ThreadMain(void *arg) {
} catch (int x) {
total += x;
} catch (float f) {
sum++;
// wait for a change, so we see interleaved processing.
int last = sum.load();
int last = ++sum;
while (sum.load() == last) {}
}
}
Expand All @@ -50,9 +49,7 @@ void CreateThread(int i)

void loop() {
static int main_adds = 0;
int worker_adds = sum.load() - main_adds;
sum++;
main_adds++;
int worker_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) {
Expand All @@ -65,9 +62,10 @@ void loop() {
int main() {
// Create initial threads.
for(int i = 0; i < NUM_THREADS; ++i) {
printf("maek\n");
printf("make\n");
CreateThread(i);
}

emscripten_set_main_loop(loop, 0, 0);
return 0;
}
1 change: 1 addition & 0 deletions tests/test_core.py
Original file line number Diff line number Diff line change
Expand Up @@ -8069,6 +8069,7 @@ def test_fpic_static(self):

@node_pthreads
def test_pthread_create(self):
self.set_setting('EXIT_RUNTIME')
self.do_run_in_out_file_test('tests', 'core', 'pthread', 'create.cpp')

@node_pthreads
Expand Down