Skip to content

Commit

Permalink
Extract value-barrier-norace example from silver searcher
Browse files Browse the repository at this point in the history
  • Loading branch information
sim642 committed Sep 20, 2023
1 parent 16aa15f commit 2121eb0
Showing 1 changed file with 54 additions and 0 deletions.
54 changes: 54 additions & 0 deletions tests/regression/03-practical/38-value-barrier-norace.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
// Race-free due to value-based barrier, main thread only writes before.
// Extracted from silver searcher.
#include <stdlib.h>
#include <stdbool.h>
#include <pthread.h>
#include <goblint.h>
extern int __VERIFIER_nondet_int();

bool ready = false;
pthread_mutex_t ready_mutex = PTHREAD_MUTEX_INITIALIZER;

int data = 0;

void *thread(void *arg) {
// wait for main thread to be ready
pthread_mutex_lock(&ready_mutex);
while (!ready) { // NORACE
pthread_mutex_unlock(&ready_mutex);
// busy loop for simplicity
pthread_mutex_lock(&ready_mutex);
}
pthread_mutex_unlock(&ready_mutex);

int x = data; // NORACE (main thread wrote before ready)
return NULL;
}

int main() {
int threads_total = __VERIFIER_nondet_int();
__goblint_assume(threads_total >= 0);

pthread_t *tids = malloc(threads_total * sizeof(pthread_t));

// create threads
for (int i = 0; i < threads_total; i++) {
pthread_create(&tids[i], NULL, &thread, NULL); // may fail but doesn't matter
}

data = __VERIFIER_nondet_int(); // NORACE (write before ready)

// become ready
pthread_mutex_lock(&ready_mutex);
ready = true; // NORACE
pthread_mutex_unlock(&ready_mutex);

// join threads
for (int i = 0; i < threads_total; i++) {
pthread_join(tids[i], NULL);
}

free(tids);

return 0;
}

0 comments on commit 2121eb0

Please sign in to comment.