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
|
/**
* @file racer.c
*
* @brief a very strenuous test of mutexes, condition variables, and semaphores
*
* @author Peter Nelson <pnelson>
*
* This creates <threads> number of threads. Each thread gets 1 line of the
* terminal. Every thread attempts to aquire the semaphore which was
* initialized with a value of <semaphores>. When a thread aquires the sem
* it prints out <runlength> number of characters, one at a time.
*
* There is an additional <pausetime> that can be used to cause each thread
* to call sleep of that value. Finally there is a "bad" thread that
* cycles misbehave states and then sleeps for <misbehavetime>.
*
* The expected default result is seeing a group of 12 threads run out for 5
* characters. Then the next group of 12 threades will run out for 5 characters.
* The bottom-right corner shows the current number of threads that are active
* (should always be <=12) and the current misbehave state.
*
* threads and semaphores should be set to 24 and 12 for best effect
* runlength should be set to 5-10 to be able to see how many are active,
* or set to 1 for the most strenuous
*
* Colors are pseudo-randomly rotated by continuously adding get_ticks() to
* the color value. No real point other than to be trippy.
*
* Arguments: program threads runlength semaphores pausetime misbehavetime
*/
#include <thread.h>
#include <syscall.h>
#include <simics.h>
#include <stdio.h>
#include <sem.h>
#include <mutex.h>
#include <stdlib.h>
#define STACK_SIZE 4096
#define MAX_MISBEHAVE 64
// Uses my static initializer, you might have to change this
mutex_t lock_print;
// Default values
int threads = 24;
int run = 5;
int sems = 12;
sem_t sem;
int pause = 0;
int bad_sleep = 50;
char alphabet[] = "abcdefghijklmnopqrstuvwxyz";
char empty[81];
mutex_t lock_running;
int running = 0;
char color = 0;
#define expect(exp) \
{ \
err = exp; \
if(err < 0) \
lprintf("%s:%u: returned %d", \
__FILE__, __LINE__, err); \
}
void *race(void *arg)
{
int err;
int col = -1;
int i;
int row = (int)arg;
mutex_lock(&lock_print);
color += get_ticks();
set_term_color(color);
expect(set_cursor_pos(row, 0));
printf("%s", &empty[0]);
mutex_unlock(&lock_print);
while(1)
{
sem_wait(&sem);
mutex_lock(&lock_running);
int r = ++running;
mutex_unlock(&lock_running);
if(r > sems)
lprintf("BAD! I have %d > %d threads running", r, sems);
mutex_lock(&lock_print);
expect(set_cursor_pos(24, 77));
printf("%.2d", running);
mutex_unlock(&lock_print);
for(i = 0; i < run; i++)
{
col++;
if(80 == col)
{
mutex_lock(&lock_print);
color += get_ticks();
set_term_color(color);
expect(set_cursor_pos(row, 0));
printf("%s", &empty[0]);
mutex_unlock(&lock_print);
col = 0;
}
mutex_lock(&lock_print);
color += get_ticks();
set_term_color(color);
expect(set_cursor_pos(row, col));
printf("%c", alphabet[i]);
mutex_unlock(&lock_print);
if(pause)
expect(sleep(pause));
thr_yield(-1);
}
mutex_lock(&lock_running);
running--;
mutex_unlock(&lock_running);
sem_signal(&sem);
}
}
void *bad(void *arg)
{
while(1)
{
int err;
int i = (int)arg;
for(i=-1; i<MAX_MISBEHAVE; i++)
{
lprintf("calling misbehave(%d)", i);
mutex_lock(&lock_print);
expect(set_cursor_pos(24, 73));
printf("% .2d", i);
mutex_unlock(&lock_print);
misbehave(i);
sleep(bad_sleep);
}
}
return arg;
}
int main( int argc, char *argv[] )
{
int i, err;
switch(argc) {
case 6:
i = atoi(argv[5]);
if(i >= 0)
bad_sleep = i;
/* FALLTHROUGH */
case 5:
i = atoi(argv[4]);
if(i >= 0)
pause = i;
/* FALLTHROUGH */
case 4:
i = atoi(argv[3]);
if(i > 0)
sems = i;
/* FALLTHROUGH */
case 3:
i = atoi(argv[2]);
if(i <= 26 && i > 0)
run = i;
/* FALLTHROUGH */
case 2:
i = atoi(argv[1]);
if(i > 1 && i <= 24)
threads = i;
/* FALLTHROUGH */
default:
break;
}
expect(thr_init(STACK_SIZE));
expect(mutex_init(&lock_print));
expect(mutex_init(&lock_running));
expect(sem_init(&sem, sems));
for(i=0; i<80; i++)
empty[i] = ' ';
empty[80] = '\0';
expect(thr_create(bad, (void*)i) >= 0);
for(i=0; i<threads; i++)
expect(thr_create(race, (void*)i) >= 0);
getchar();
expect(set_cursor_pos(24,0));
task_vanish(0);
}
|