-
Notifications
You must be signed in to change notification settings - Fork 76
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Extract thread-alive-counter-outer example from nnn
- Loading branch information
Showing
2 changed files
with
50 additions
and
1 deletion.
There are no files selected for viewing
2 changes: 1 addition & 1 deletion
2
...on/03-practical/31-thread-alive-counter.c → ...practical/31-thread-alive-counter-inner.c
This file contains 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
49 changes: 49 additions & 0 deletions
49
tests/regression/03-practical/34-thread-alive-counter-outer.c
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
// Thread pool joining via threads alive counter incremented outside of thread. | ||
// Extracted from concrat/nnn. | ||
#include <pthread.h> | ||
#include <goblint.h> | ||
extern int __VERIFIER_nondet_int(); | ||
|
||
int threads_alive = 0; | ||
pthread_mutex_t threads_alive_mutex = PTHREAD_MUTEX_INITIALIZER; | ||
|
||
int data = 0; | ||
pthread_mutex_t data_mutex = PTHREAD_MUTEX_INITIALIZER; | ||
|
||
void *thread(void *arg) { | ||
pthread_mutex_lock(&data_mutex); | ||
data = __VERIFIER_nondet_int(); // NORACE | ||
pthread_mutex_unlock(&data_mutex); | ||
|
||
pthread_mutex_lock(&threads_alive_mutex); | ||
threads_alive--; // NORACE | ||
pthread_mutex_unlock(&threads_alive_mutex); | ||
return NULL; | ||
} | ||
|
||
int main() { | ||
int threads_total = __VERIFIER_nondet_int(); | ||
__goblint_assume(threads_total >= 0); | ||
|
||
// create threads | ||
for (int i = 0; i < threads_total; i++) { | ||
pthread_mutex_lock(&threads_alive_mutex); | ||
threads_alive++; // NORACE | ||
pthread_mutex_unlock(&threads_alive_mutex); | ||
|
||
pthread_t tid; | ||
pthread_create(&tid, NULL, &thread, NULL); // may fail but doesn't matter | ||
pthread_detach(tid); | ||
} | ||
|
||
// wait for all threads to stop | ||
pthread_mutex_lock(&threads_alive_mutex); | ||
while (threads_alive) { // NORACE | ||
pthread_mutex_unlock(&threads_alive_mutex); | ||
// busy loop for simplicity | ||
pthread_mutex_lock(&threads_alive_mutex); | ||
} | ||
pthread_mutex_unlock(&threads_alive_mutex); | ||
|
||
return data; // NORACE (all threads stopped) | ||
} |