Imported Upstream version 4.6.0
[debian/atlc] / tests / Test_threads_a.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 #include "config.h"
26
27 #ifdef HAVE_STDIO_H
28 #include <stdio.h>
29 #endif
30
31 #ifdef HAVE_STDIO_H
32 #include <stdlib.h>
33 #endif
34
35 #ifdef ENABLE_POSIX_THREADS
36 #include <pthread.h>
37
38 void *increment_a(void *arg);
39 void *increment_b(void *arg);
40 int  finalise(int, int);
41
42 int r1 = 0, r2 = 0, r3 = 0;
43
44 pthread_mutex_t r3_mutex;
45
46 #endif /* ifdef ENABLE_POSIX_THREADS */
47
48 int main()
49 {
50 #ifdef ENABLE_POSIX_THREADS
51   pthread_t       thread1, thread2;
52   int counter=0, i;
53
54   pthread_mutex_init(&r3_mutex, NULL);
55
56   r3 = 4;
57
58   for(i=1; i<10000; ++i)
59   {
60      if( pthread_create(&thread1, NULL, increment_a, (void *) &r1) != 0)
61      {
62         perror("Thread 1 not created properly");
63         exit(1);
64       }
65
66      if( pthread_create(&thread2, NULL, increment_b, (void *) &r2) != 0)
67      {
68         perror("Thread 2 not created properly");
69         exit(1);
70       }
71   
72      if(pthread_join(thread1, NULL) != 0)
73      {
74        perror("Thread 1 did not join properly");
75        exit (1);
76      }
77      if(pthread_join(thread2, NULL) != 0)
78      {
79        perror("Thread 2 did not join properly");
80        exit (1);
81      }
82
83      counter+=finalise(r1, r2);
84    }
85    if (counter == 399960000)
86      return 0;
87    else
88      return 1;
89 #else
90      return 77;
91 #endif
92 }
93
94 #ifdef ENABLE_POSIX_THREADS
95 void *increment_a(void *arg)
96 {
97   int  i,x;
98   int *pnum_times=(int *) arg;
99  
100   if( pthread_mutex_lock(&r3_mutex) != 0)
101   {
102     perror("pthread_mutex_lock failed");
103     exit(1);
104   }
105   if (r3 > 3) {
106         x = r3;
107         r3--;
108   } else {
109         x = 1;
110   }
111   if( pthread_mutex_unlock(&r3_mutex) != 0)
112   {
113     perror("pthread_mutex_unlock failed");
114     exit(1);
115   }
116   for (i = 0;  i < 4; i++) {
117     (*pnum_times)++;
118   }
119   return (NULL);
120 }
121
122 void *increment_b(void *arg)
123 {
124   int i,x;
125   int *pnum_times=(int *) arg;
126
127   if( pthread_mutex_lock(&r3_mutex) != 0)
128   {
129     perror("pthread_mutex_lock failed");
130     exit(1);
131   }
132   if (r3 > 3) {
133         x = r3;
134         r3--; 
135   } else {
136         x = 1;
137   }
138   if(pthread_mutex_unlock(&r3_mutex) != 0)
139   {
140     perror("pthread_mutex_unlock failed");
141     exit(1);
142   }
143   for (i = 0;  i < 4; i++) {
144     (*pnum_times)++;
145   }
146   return (NULL);
147 }
148
149 int  finalise(int one_times, int another_times)
150 {
151   int total;
152
153   total = one_times + another_times;
154   return(total);
155 }
156 #endif