-
Notifications
You must be signed in to change notification settings - Fork 750
Implement sync primitives instead of using pthread ones #2028
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
Changes from all commits
c83b8b4
fcea51c
4f5ff0c
1227d42
c379e63
6b2999f
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -368,9 +368,9 @@ jobs: | |
| mkdir wasi-libc | ||
| cd wasi-libc | ||
| git init | ||
| # "Rename thread_spawn import" commit on main branch | ||
| # "Fix a_store operation in atomic.h" commit on main branch | ||
| git fetch https://github.com/WebAssembly/wasi-libc \ | ||
| 8f5275796a82f8ecfd0833a4f3f444fa37ed4546 | ||
| 1dfe5c302d1c5ab621f7abf04620fae92700fd22 | ||
| git checkout FETCH_HEAD | ||
| make -j \ | ||
| AR=/opt/wasi-sdk/bin/llvm-ar \ | ||
|
|
@@ -532,9 +532,9 @@ jobs: | |
| mkdir wasi-libc | ||
| cd wasi-libc | ||
| git init | ||
| # "Rename thread_spawn import" commit on main branch | ||
| # "Fix a_store operation in atomic.h" commit on main branch | ||
| git fetch https://github.com/WebAssembly/wasi-libc \ | ||
| 8f5275796a82f8ecfd0833a4f3f444fa37ed4546 | ||
| 1dfe5c302d1c5ab621f7abf04620fae92700fd22 | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same as above |
||
| git checkout FETCH_HEAD | ||
| make -j \ | ||
| AR=/opt/wasi-sdk/bin/llvm-ar \ | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -6,11 +6,16 @@ | |
| #include <stdlib.h> | ||
| #include <stdio.h> | ||
| #include <assert.h> | ||
| #include <pthread.h> | ||
| #include <stdbool.h> | ||
| #include <unistd.h> | ||
| #include <limits.h> | ||
|
|
||
| #if USE_CUSTOM_SYNC_PRIMITIVES != 0 | ||
| #include "sync_primitives.h" | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'd like to use this primitives to easier find the other errors, but do we want to use it as default one though? Pros:
Cons:
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes, had better use the int old;
do old = *p;
while (a_cas(p, old, v) != old);
return old;Here
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
So should we merge this one as it is then? Until we get it fixed in wasi-libc.
Nice catch, that's probably what the thread sanitizer is complaining about for the load/store data races. It may be the same for other operations as well, I see the |
||
| #else | ||
| #include <pthread.h> | ||
| #endif | ||
|
|
||
| #include "wasi_thread_start.h" | ||
|
|
||
| typedef enum { | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,91 @@ | ||
| /* | ||
| * Copyright (C) 2023 Amazon.com Inc. or its affiliates. All rights reserved. | ||
| * SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception | ||
| */ | ||
|
|
||
| #include <stdbool.h> | ||
|
|
||
| /* Mutex */ | ||
|
|
||
| typedef int pthread_mutex_t; | ||
|
|
||
| int | ||
| pthread_mutex_init(pthread_mutex_t *mutex, void *unused) | ||
| { | ||
| *mutex = 0; | ||
| return 0; | ||
| } | ||
|
|
||
| int | ||
| pthread_mutex_destroy(pthread_mutex_t *mutex) | ||
| { | ||
| return 0; | ||
| } | ||
|
|
||
| static bool | ||
| try_pthread_mutex_lock(pthread_mutex_t *mutex) | ||
| { | ||
| int expected = 0; | ||
| return __atomic_compare_exchange_n(mutex, &expected, 1, false, | ||
| __ATOMIC_SEQ_CST, __ATOMIC_SEQ_CST); | ||
| } | ||
|
|
||
| int | ||
| pthread_mutex_lock(pthread_mutex_t *mutex) | ||
| { | ||
| while (!try_pthread_mutex_lock(mutex)) | ||
| __builtin_wasm_memory_atomic_wait32(mutex, 1, -1); | ||
| return 0; | ||
| } | ||
|
|
||
| int | ||
| pthread_mutex_unlock(pthread_mutex_t *mutex) | ||
| { | ||
| __atomic_store_n(mutex, 0, __ATOMIC_SEQ_CST); | ||
| __builtin_wasm_memory_atomic_notify(mutex, 1); | ||
| return 0; | ||
| } | ||
|
|
||
| /* Barrier */ | ||
|
|
||
| typedef struct { | ||
| int count; | ||
| int num_threads; | ||
| int mutex; | ||
| int ready; | ||
| } pthread_barrier_t; | ||
|
|
||
| int | ||
| pthread_barrier_init(pthread_barrier_t *barrier, void *unused, int num_threads) | ||
| { | ||
| barrier->count = 0; | ||
| barrier->num_threads = num_threads; | ||
eloparco marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| barrier->ready = 0; | ||
| pthread_mutex_init(&barrier->mutex, NULL); | ||
|
|
||
| return 0; | ||
| } | ||
|
|
||
| int | ||
| pthread_barrier_wait(pthread_barrier_t *barrier) | ||
| { | ||
| bool no_wait = false; | ||
| int count; | ||
|
|
||
| pthread_mutex_lock(&barrier->mutex); | ||
| count = barrier->count++; | ||
| if (barrier->count >= barrier->num_threads) { | ||
| no_wait = true; | ||
| barrier->count = 0; | ||
| } | ||
| pthread_mutex_unlock(&barrier->mutex); | ||
|
|
||
| if (no_wait) { | ||
| __atomic_store_n(&barrier->ready, 1, __ATOMIC_SEQ_CST); | ||
| __builtin_wasm_memory_atomic_notify(&barrier->ready, count); | ||
| return 0; | ||
| } | ||
|
|
||
| __builtin_wasm_memory_atomic_wait32(&barrier->ready, 0, -1); | ||
| return 0; | ||
| } | ||
This file was deleted.
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.
Had better also compile wasi-libc in Ubuntu-22.04 instead using the pre-release wasi-sdk-thread version since there is issue in it:
Merge L337 to L346, and remove L366 and
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.
I thought of that, but it would mean that now we remove the usage wasi-sdk 20 pre-release from the ci, basically reverting all the changes previously added in #2021. When wasi-sdk 20 final release comes out we will have to add those changes again.
I was leaving the wasi-sdk 20 part as it is since in practice we didn't encounter problems in the main branch, the hanging issues, only reproducible by running each test many times, always passed unnoticed in the CI.
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.
Got it, so let's keep it.