| 15 | volatile int lock = 0; // spinlock "mutex" variable |
| 16 | |
| 17 | void *ThreadMain(void *arg) { |
| 18 | printf("Thread started.\n"); |
| 19 | for (int i = 0; i < 100; ++i) { |
| 20 | #ifdef USE_EMSCRIPTEN_INTRINSICS |
| 21 | while(emscripten_atomic_exchange_u32((void*)&lock, 1)) |
| 22 | #else |
| 23 | while(__sync_lock_test_and_set(&lock, 1)) |
| 24 | #endif |
| 25 | { |
| 26 | /*nop*/; |
| 27 | } |
| 28 | int c = counter; |
| 29 | usleep(5 * 1000); // Create contention on the lock. |
| 30 | ++c; |
| 31 | counter = c; |
| 32 | #ifdef USE_EMSCRIPTEN_INTRINSICS |
| 33 | emscripten_atomic_store_u32((void*)&lock, 0); |
| 34 | #else |
| 35 | __sync_lock_release(&lock); |
| 36 | #endif |
| 37 | } |
| 38 | printf("Thread done.\n"); |
| 39 | pthread_exit(0); |
| 40 | } |
| 41 | |
| 42 | #define NUM_THREADS 8 |
| 43 |
nothing calls this directly
no test coverage detected