Imported Upstream version 4.6.0
[debian/atlc] / tests / Test_threads_b.c
1 /*
2 atlc - arbitrary transmission line calculator, for the analysis of
3 transmission lines are directional couplers. 
4
5 Copyright (C) 2002. Dr. David Kirkby, PhD (G8WRB).
6
7 This program is free software; you can redistribute it and/or
8 modify it under the terms of the GNU General Public License
9 as published by the Free Software Foundation; either package_version 2
10 of the License, or (at your option) any later package_version.
11
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 GNU General Public License for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307,
20 USA.
21
22 Dr. David Kirkby, e-mail drkirkby@ntlworld.com 
23
24 */
25
26 /* This test program starts by puttting 10 into a 100x1000 array
27 then incrments it by 5 and decrements it by 2, one thousand 
28 times. The incrment and decrement operations are prevented from
29 operlapping by a mutex variable. */
30
31 /* If we #define DISABLE_MUTEX, there is no locking
32 on the array and so any thread can overwrite at any time
33 so we would expect jibberish output */
34
35 #include "config.h"
36
37 #ifdef HAVE_STDIO_H
38 #include <stdio.h>
39 #endif
40
41 #ifdef HAVE_STDIO_H
42 #include <stdlib.h>
43 #endif
44
45 #ifdef ENABLE_POSIX_THREADS
46 #include <pthread.h>
47
48 void *increment(void *y);
49 void *decrement(void *z);
50
51 int  check_array();
52
53 int r1 = 0, r2 = 0;
54
55 pthread_mutex_t array_mutex;
56
57 int array[1000][1000];
58
59 #endif /* ifdef ENABLE_POSIX_THREADS */
60
61 int main()
62 {
63 #ifdef ENABLE_POSIX_THREADS
64   pthread_t       thread1, thread2;
65   int i, j, ret;
66   /* Put 10 in each array element */
67   for(i=0; i<1000; ++i)
68     for(j=0; j<1000; ++j)
69       array[i][j]=10;
70
71   pthread_mutex_init(&array_mutex, NULL);
72 #ifdef ENABLE_POSIX_THREADS
73 #ifdef HAVE_PTHREAD_SETCONCURRENCY     
74   pthread_setconcurrency(6);
75 #endif 
76 #endif 
77   for(i=1; i<=100; ++i)
78   {
79      if( pthread_create(&thread1, NULL, increment, (void *) &r1) != 0)
80      {
81         perror("Thread 1 not created properly");
82         exit(1);
83       }
84
85      if( pthread_create(&thread2, NULL, decrement, (void *) &r2) != 0)
86      {
87         perror("Thread 2 not created properly");
88         exit(1);
89       }
90   
91      if(pthread_join(thread1, NULL) != 0)
92      {
93        perror("Thread 1 did not join properly");
94        exit (1);
95      }
96      if(pthread_join(thread2, NULL) != 0)
97      {
98        perror("Thread 2 did not join properly");
99        exit (1);
100      }
101    }
102    ret=check_array(); /* Returns 0 or 1 */
103    return(ret); 
104 #else
105      return 77;
106 #endif
107   exit(0);
108 }
109
110 #ifdef ENABLE_POSIX_THREADS
111 void *increment(void *pnum_times)
112 {
113   int i, j;
114   if( pthread_mutex_lock(&array_mutex) != 0)
115   {
116     perror("pthread_mutex_lock failed");
117     exit(1);
118   }
119   /* Increment each value in the array by 5, so 
120   we can check the values later. */
121   for(i=0; i<1000; ++i)
122     for(j=0; j<1000; ++j)
123       array[i][j]+=5;
124   if( pthread_mutex_unlock(&array_mutex) != 0)
125   {
126     perror("pthread_mutex_unlock failed");
127     exit(1);
128   }
129   return(0);
130
131 }
132
133 void *decrement(void *pnum_times)
134 {
135   int i,j;
136
137
138 #ifndef DISABLE_MUTEX 
139   if( pthread_mutex_lock(&array_mutex) != 0)
140   {
141     perror("pthread_mutex_lock failed");
142     exit(1);
143   }
144 #endif
145
146   /* Decrement each value in the array by 2, so 
147   we can check the values later. */
148   for(i=0; i<1000; ++i)
149     for(j=0; j<1000; ++j)
150       array[i][j]-=2;
151
152 #ifndef DISABLE_MUTEX 
153   if(pthread_mutex_unlock(&array_mutex) != 0)
154   {
155     perror("pthread_mutex_unlock failed");
156     exit(1);
157   }
158 #endif
159   return 0;
160 }
161
162 int check_array()
163 {
164   int i,j;
165   for(i=0; i<1000; ++i)
166   {
167     for(j=0; j<1000; ++j)
168     {
169       /* Numers start at 10, but get incremented by 
170       5 by 2, which is 3 . Loop runs 2000 times,
171       so resuls should be 2000*(5-2) + 10 = 6010 */
172
173       if (array[i][j] != 310)
174       {
175         fprintf(stderr,"array[%d][%d]=%d\n",i,j,array[i][j]);
176         return(1);
177       }
178     }
179   }
180   return(0);
181 }
182 #endif