1.23
2.0
main
test-branch
|
Last change
on this file since 1aaefd4 was ea777aa, checked in by Alex Wilton <awilton@…>, 3 years ago |
|
Moved examples, include, build_default.properties, common.xml, and README out from dev.civl.com into the root of the repo.
git-svn-id: svn://vsl.cis.udel.edu/civl/trunk@5704 fb995dde-84ed-4084-dfe6-e5aef3e2452c
|
-
Property mode
set to
100644
|
|
File size:
1.2 KB
|
| Line | |
|---|
| 1 | extern void __VERIFIER_error();
|
|---|
| 2 |
|
|---|
| 3 | #include <stdlib.h>
|
|---|
| 4 | #include <pthread.h>
|
|---|
| 5 |
|
|---|
| 6 | #define SIZE 128
|
|---|
| 7 | #define MAX 4
|
|---|
| 8 | #define NUM_THREADS 13
|
|---|
| 9 |
|
|---|
| 10 | int table[SIZE];
|
|---|
| 11 | pthread_mutex_t cas_mutex[SIZE];
|
|---|
| 12 |
|
|---|
| 13 | pthread_t tids[NUM_THREADS];
|
|---|
| 14 |
|
|---|
| 15 |
|
|---|
| 16 | int cas(int * tab, int h, int val, int new_val)
|
|---|
| 17 | {
|
|---|
| 18 | int ret_val = 0;
|
|---|
| 19 | pthread_mutex_lock(&cas_mutex[h]);
|
|---|
| 20 |
|
|---|
| 21 |
|
|---|
| 22 | if ( tab[h] == val ) {
|
|---|
| 23 | tab[h] = new_val;
|
|---|
| 24 | ret_val = 1;
|
|---|
| 25 | }
|
|---|
| 26 |
|
|---|
| 27 | pthread_mutex_unlock(&cas_mutex[h]);
|
|---|
| 28 |
|
|---|
| 29 |
|
|---|
| 30 | return ret_val;
|
|---|
| 31 | }
|
|---|
| 32 |
|
|---|
| 33 |
|
|---|
| 34 |
|
|---|
| 35 | void * thread_routine(void * arg)
|
|---|
| 36 | {
|
|---|
| 37 | int tid;
|
|---|
| 38 | int m = 0, w, h;
|
|---|
| 39 | tid = *((int *)arg);
|
|---|
| 40 |
|
|---|
| 41 | while(1){
|
|---|
| 42 | if ( m < MAX ){
|
|---|
| 43 | w = (++m) * 11 + tid;
|
|---|
| 44 | }
|
|---|
| 45 | else{
|
|---|
| 46 | pthread_exit(NULL);
|
|---|
| 47 | }
|
|---|
| 48 |
|
|---|
| 49 | h = (w * 7) % SIZE;
|
|---|
| 50 |
|
|---|
| 51 | if (h<0)
|
|---|
| 52 | {
|
|---|
| 53 | ERROR: __VERIFIER_error();
|
|---|
| 54 | ;
|
|---|
| 55 | }
|
|---|
| 56 |
|
|---|
| 57 | while ( cas(table, h, 0, w) == 0){
|
|---|
| 58 | h = (h+1) % SIZE;
|
|---|
| 59 | }
|
|---|
| 60 | }
|
|---|
| 61 |
|
|---|
| 62 | }
|
|---|
| 63 |
|
|---|
| 64 |
|
|---|
| 65 | int main()
|
|---|
| 66 | {
|
|---|
| 67 | int i, arg;
|
|---|
| 68 |
|
|---|
| 69 | for (i = 0; i < SIZE; i++)
|
|---|
| 70 | pthread_mutex_init(&cas_mutex[i], NULL);
|
|---|
| 71 |
|
|---|
| 72 | for (i = 0; i < NUM_THREADS; i++){
|
|---|
| 73 | arg=i;
|
|---|
| 74 | pthread_create(&tids[i], NULL, thread_routine, &arg);
|
|---|
| 75 | }
|
|---|
| 76 |
|
|---|
| 77 | for (i = 0; i < NUM_THREADS; i++){
|
|---|
| 78 | pthread_join(tids[i], NULL);
|
|---|
| 79 | }
|
|---|
| 80 |
|
|---|
| 81 | for (i = 0; i < SIZE; i++){
|
|---|
| 82 | pthread_mutex_destroy(&cas_mutex[i]);
|
|---|
| 83 | }
|
|---|
| 84 |
|
|---|
| 85 | }
|
|---|
| 86 |
|
|---|
Note:
See
TracBrowser
for help on using the repository browser.