Last active
May 28, 2021 06:20
-
-
Save db7/2e368c8c8b1eaa8ea360e44bdb209306 to your computer and use it in GitHub Desktop.
Test if TSO is correctly enabled with TSOEnabler
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
| /******************************************************************************* | |
| * Compile and run: | |
| * gcc -DTSO=1 -o tso-test tso-test.c -lpthread && ./tso-test | |
| * gcc -DTSO=0 -o tso-test tso-test.c -lpthread && ./tso-test | |
| * | |
| * With TSO=0 the assertion should fail. | |
| * | |
| * Requires TSOEnabler: https://github.com/saagarjha/TSOEnabler | |
| ******************************************************************************/ | |
| #include <stdatomic.h> | |
| #include <pthread.h> | |
| #include <sys/sysctl.h> | |
| #include <assert.h> | |
| #define NITERS 1000000 // number of iterations | |
| atomic_int lock; | |
| #define lock_acquire() \ | |
| while(atomic_exchange_explicit(&lock, 1, memory_order_relaxed)) {} | |
| #define lock_release() atomic_store(&lock, 0) | |
| #ifndef TSO | |
| #error "compile with -DTSO=1 or -DTSO=0" | |
| #endif | |
| static void enable_tso() | |
| { | |
| int enable = TSO; | |
| size_t size = sizeof(enable); | |
| int err = sysctlbyname("kern.tso_enable", NULL, &size, &enable, size); | |
| assert(err == 0); | |
| } | |
| static unsigned int x; | |
| static void *run(void *arg) | |
| { | |
| (void) arg; | |
| enable_tso(); | |
| for (int i=0; i < NITERS; i++) { | |
| lock_acquire(); | |
| x++; | |
| lock_release(); | |
| } | |
| return 0; | |
| } | |
| int main() | |
| { | |
| pthread_t t; | |
| pthread_create(&t, 0, run, 0); | |
| run(0); | |
| pthread_join(t, 0); | |
| assert(x == 2*NITERS && "TSO is not enabled"); | |
| return 0; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment