source: CIVL/examples/pthread/indexer_true.cvl@ 50f834b

1.23 2.0 main test-branch
Last change on this file since 50f834b was f72f577, checked in by John Edenhofner <johneden@…>, 12 years ago

Added support for robust mutex, get/sets for pthread_mutexattr_t fields, pthread_mutex_trylock, and multiple new competition code examples

git-svn-id: svn://vsl.cis.udel.edu/civl/trunk@1073 fb995dde-84ed-4084-dfe6-e5aef3e2452c

  • Property mode set to 100644
File size: 1.7 KB
RevLine 
[f72f577]1/*****************************************************************************
2* SOURCE: This is a translation of a Pthread program from
3* the Pthread benchmarks of SV-COMP 2014.
4* https://svn.sosy-lab.org/software/sv-benchmarks/tags/svcomp14/
5* FILE: indexer_true.cvl
6* DESCRIPTION:
7* Command line execution:
8* civl verify -inputSIZE=8 -inputMAX=4 -inputNUM_THREADS=13 indexer_true.cvl
9******************************************************************************/
10#include <stdlib.h>
11#include "pthread.cvh"
12#include <civlc.h>
13
14$input int SIZE;
15$input int MAX;
16$input int NUM_THREADS;
17
18int table[SIZE];
19pthread_mutex_t cas_mutex[SIZE];
20
21pthread_t tids[NUM_THREADS];
22
23int cas(int * tab, int h, int val, int new_val)
24{
25 int ret_val = 0;
26 pthread_mutex_lock(&cas_mutex[h]);
27
28
29 if ( tab[h] == val ) {
30 tab[h] = new_val;
31 ret_val = 1;
32 }
33
34 pthread_mutex_unlock(&cas_mutex[h]);
35
36
37 return ret_val;
38}
39
40void * thread_routine(void * arg)
41{
42 int tid;
43 int m = 0, w, h;
44 tid = *((int *)arg);
45
46 while(1){
47 if ( m < MAX ){
48 w = (++m) * 11 + tid;
49 }
50 else{
51 pthread_exit(NULL, false, NULL, 0); //Different parameters
52 }
53
54 h = (w * 7) % SIZE;
55
56 if (h<0)
57 {
58 $assert($false);
59 }
60
61 while ( cas(table, h, 0, w) == 0){
62 h = (h+1) % SIZE;
63 }
64 }
65
66}
67
68int main()
69{
70 int i, arg;
71
72 for (i = 0; i < SIZE; i++)
73 pthread_mutex_init(&cas_mutex[i], NULL);
74
75 for (i = 0; i < NUM_THREADS; i++){
76 arg=i;
77 pthread_create(&tids[i], NULL, thread_routine, (void *)&arg);
78 }
79
80 for (i = 0; i < NUM_THREADS; i++){
81 pthread_join(tids[i], NULL);
82 }
83
84 for (i = 0; i < SIZE; i++){
85 pthread_mutex_destroy(&cas_mutex[i]);
86 }
87
88}
Note: See TracBrowser for help on using the repository browser.