Commit | Line | Data |
---|---|---|
c906108c SS |
1 | /* BeginSourceFile more_steps.c |
2 | ||
3 | This file creates a lot of threads which then execute | |
4 | in parallel, so that wdb can be tested on handling | |
5 | simultaneous thread events. | |
6 | ||
7 | To compile: | |
8 | ||
9 | cc -Ae +DA1.0 -g -o more_steps -lpthread more_steps.c | |
10 | ||
11 | To run: | |
12 | ||
13 | more_threads | |
14 | */ | |
15 | ||
16 | #include <unistd.h> | |
17 | #include <stdlib.h> | |
18 | #include <stdio.h> | |
19 | #include <assert.h> | |
20 | #include <pthread.h> | |
21 | ||
22 | #define TRUE 1 | |
23 | #define FALSE 0 | |
24 | #define N_THREADS 3 | |
25 | #define PHASES 3 | |
26 | ||
27 | typedef enum { | |
28 | ZERO, | |
29 | ONE, | |
30 | TWO, | |
31 | THREE | |
32 | } phase_t; | |
33 | ||
34 | /* Uncomment to turn on debugging output */ | |
35 | /* #define DEBUG */ | |
36 | ||
37 | /* Locks. | |
38 | */ | |
39 | int lock_one; /* Main W, others R */ | |
40 | int lock_two; /* ditto */ | |
41 | int lock_end[ N_THREADS ]; /* Main R, others R[i] */ | |
42 | int phase[ N_THREADS ]; | |
43 | ||
44 | /* Routine for each thread to run. | |
45 | */ | |
46 | void *spin( vp ) | |
47 | void * vp; | |
48 | { | |
49 | int me = (int) vp; | |
50 | int i; | |
51 | ||
52 | lock_end[ me ] = TRUE; | |
53 | ||
54 | phase[ me ] = ONE; | |
55 | ||
56 | while( lock_one ); | |
57 | ||
58 | phase[ me ] = TWO; | |
59 | ||
60 | while( lock_two ); | |
61 | ||
62 | phase[ me ] = THREE; | |
63 | ||
64 | lock_end[ me ] = FALSE; | |
65 | } | |
66 | ||
67 | void | |
68 | do_pass() | |
69 | { | |
70 | int i; | |
71 | pthread_t t[ N_THREADS ]; | |
72 | int err; | |
73 | int done; | |
74 | ||
75 | /* Start N_THREADS threads, then join them so | |
76 | * that they are terminated. | |
77 | */ | |
78 | for( i = 0; i < N_THREADS; i++ ) { | |
79 | err = pthread_create( &t[i], NULL, spin, (void *)i ); | |
80 | if( err != 0 ) { | |
81 | printf( "== Start/stop, error in thread %d create\n", i ); | |
82 | } | |
83 | } | |
84 | ||
85 | /* Do phase 1. | |
86 | */ | |
87 | lock_one = FALSE; | |
88 | ||
89 | /* Do phase 2. | |
90 | */ | |
91 | lock_two = FALSE; | |
92 | ||
93 | /* Be done. | |
94 | */ | |
95 | done = 0; | |
96 | while( !done ) { | |
97 | ||
98 | /* Be optimistic. | |
99 | */ | |
100 | done = 1; | |
101 | for( i = 0; i < N_THREADS; i++ ) { | |
102 | if( lock_end[i] ) { | |
103 | /* Thread "i" is not ready yet. | |
104 | */ | |
105 | done = 0; | |
106 | break; | |
107 | } | |
108 | } | |
109 | } | |
110 | ||
111 | /* Finish up | |
112 | */ | |
113 | for( i = 0; i < N_THREADS; i++ ) { | |
114 | err = pthread_join(t[i], NULL ); /* Line 105 */ | |
115 | if( err != 0 ) { /* Line 106 */ | |
116 | printf( "== Start/stop, error in thread %d join\n", i ); | |
117 | } | |
118 | } | |
119 | ||
120 | i = 10; /* Line 109. Null line for setting bpts on. */ | |
121 | } | |
122 | ||
123 | main( argc, argv ) | |
124 | int argc; | |
125 | char **argv; | |
126 | { | |
127 | int i; | |
128 | ||
129 | /* Init | |
130 | */ | |
131 | lock_one = TRUE; | |
132 | lock_two = TRUE; | |
133 | for( i = 0; i < N_THREADS; i++ ) { | |
134 | lock_end[i] = TRUE; | |
135 | phase[i] = ZERO; | |
136 | } | |
137 | ||
138 | do_pass(); | |
139 | return(0); | |
140 | } |