summaryrefslogtreecommitdiff
path: root/gdb/testsuite/gdb.threads/step.c
blob: 1b18a4b07adcaed20a234d8d7eb42c7b8464848f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
/*    step.c for step.exp    */
#include <ipc.h>
#include <pthread.h>
#include <st.h>
#include <signal.h>
#include <stdio.h>

void alarm_handler ();
void alarm_handler1 ();
void alarm_handler2 ();
void thread1 ();
void thread2 ();

#define TIME_LIMIT 30


int count1 = 0;
int count2 = 0;

pthread_t tid1, tid2;
pthread_attr_t attr1, attr2;

pthread_mutex_t mut;
pthread_mutexattr_t mut_attr;

pthread_condattr_t cv_attr_a, cv_attr_b;
pthread_cond_t cv_a, cv_b;

struct cv_struct
  {
    char a;
    char b;
  }
test_struct;

main ()
{
  /*init la struct */
  test_struct.a = 0;
  test_struct.b = 1;

  /* create le mutex */
  if (pthread_mutexattr_create (&mut_attr) == -1)
    {
      perror ("mutexattr_create");
      exit (1);
    }


  if (pthread_mutex_init (&mut, mut_attr) == -1)
    {
      perror ("mutex_init");
      exit (1);
    }

  /* create 2 cv */
  if (pthread_condattr_create (&cv_attr_a) == -1)
    {
      perror ("condattr_create(1)");
      exit (1);
    }

  if (pthread_cond_init (&cv_a, cv_attr_a) == -1)
    {
      perror ("cond_init(1)");
      exit (1);
    }

  if (pthread_condattr_create (&cv_attr_b) == -1)
    {
      perror ("condattr_create(2)");
      exit (1);
    }

  if (pthread_cond_init (&cv_b, cv_attr_b) == -1)
    {
      perror ("cond_init(2)");
      exit (1);
    }

  /* create 2 threads of execution */
  if (pthread_attr_create (&attr1) == -1)
    {
      perror ("attr_create(1)");
      exit (1);
    }

  if (pthread_create (&tid1, attr1, thread1, &count1) == -1)
    {
      perror ("pthread_create(1)");
      exit (1);
    }

  if (pthread_attr_create (&attr2) == -1)
    {
      perror ("attr_create(2)");
      exit (1);
    }

  if (pthread_create (&tid2, attr2, thread2, &count2) == -1)
    {
      perror ("pthread_create(2)");
      exit (1);
    }

  /* set alarm to print out data and exit */
  signal (SIGALRM, alarm_handler);
  alarm (TIME_LIMIT);

  for (;;)
    pause ();
}

void 
thread1 (count)
     int *count;
{
  tid_t tid;

  tid = getstid ();
  printf ("Thread1 tid 0x%x  (%d) \n", tid, tid);
  printf ("Thread1 @tid=0x%x \n", &tid);
  signal (SIGALRM, alarm_handler1);

  for (;;)
    {
      if (pthread_mutex_lock (&mut) == -1)
	{
	  perror ("pthread_mutex_lock(1)");
	  pthread_exit ((void *) 0);
	}

      while (test_struct.a == 0)
	{
	  if (pthread_cond_wait (&cv_a, &mut) == -1)
	    {
	      perror ("pthread_cond_wait(1)");
	      pthread_exit ((void *) -1);
	    }
	}

      (*count)++;
      printf ("*******thread1 count %d\n", *count);

      test_struct.a = 0;

      test_struct.b = 1;
      pthread_cond_signal (&cv_b);

      if (pthread_mutex_unlock (&mut) == -1)
	{
	  perror ("pthread_mutex_unlock(1)");
	  pthread_exit ((void *) -1);
	}
    }
}

void 
thread2 (count)
     int *count;
{
  tid_t tid;

  tid = getstid ();
  printf ("Thread2 tid 0x%x  (%d) \n", tid, tid);
  printf ("Thread1 @tid=0x%x \n", &tid);
  signal (SIGALRM, alarm_handler2);

  for (;;)
    {
      if (pthread_mutex_lock (&mut) == -1)
	{
	  perror ("pthread_mutex_lock(2)");
	  pthread_exit ((void *) 0);
	}

      while (test_struct.b == 0)
	{
	  if (pthread_cond_wait (&cv_b, &mut) == -1)
	    {
	      perror ("pthread_cond_wait(2)");
	      pthread_exit ((void *) -1);
	    }
	}

      (*count)++;
      printf ("*******thread2 count %d\n", *count);

      test_struct.b = 0;

      test_struct.a = 1;
      pthread_cond_signal (&cv_a);

      if (pthread_mutex_unlock (&mut) == -1)
	{
	  perror ("pthread_mutex_unlock(2)");
	  pthread_exit ((void *) -1);
	}
    }
}


void 
alarm_handler ()
{
  printf ("\tcount1 (%d) \n\tcount2 (%d)\n", count1, count2);
  exit (0);
}

void 
alarm_handler1 ()
{
  printf ("ALARM thread 1\n");
}

void 
alarm_handler2 ()
{
  printf ("ALARM thread 2\n");
  pthread_exit ((void *) 0);
}