2 * Copyright (c) 1998 - 2000 Kungliga Tekniska Högskolan
3 * (Royal Institute of Technology, Stockholm, Sweden).
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
17 * 3. Neither the name of the Institute nor the names of its contributors
18 * may be used to endorse or promote products derived from this software
19 * without specific prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
37 * Checks if lwp seems to work, and demostrates how to use lwp. Give
38 * multiple commands on the command line to run several tests at the
54 static void Producer(void *foo
) ;
55 static void Consumer(void *foo
) ;
57 #ifndef AFS_LWP_MINSTACKSIZE
58 #define LWPTEST_STACKSIZE (16*1024)
60 #define LWPTEST_STACKSIZE AFS_LWP_MINSTACKSIZE
64 * Classic Producer Consumer thread testing
66 * Observe the use of SignalNoYield, you can't signal anything
67 * that isn't sleeping.
70 static char pcfoo
= 'a' - 1;
76 LWP_WaitProcess((char *)Producer
);
80 printf("[producer] creating %c\n", pcfoo
);
81 LWP_NoYieldSignal ((char *)Consumer
);
88 LWP_NoYieldSignal ((char *)Producer
);
90 LWP_WaitProcess((char *)Consumer
);
91 printf("[consumer] eating %c\n", pcfoo
);
92 LWP_NoYieldSignal ((char *)Producer
);
99 PROCESS consumer
, producer
;
101 if (LWP_CreateProcess (Producer
, LWPTEST_STACKSIZE
, 1,
102 NULL
, "producer", &consumer
))
103 errx (1, "Cannot create producer process");
105 if (LWP_CreateProcess (Consumer
, LWPTEST_STACKSIZE
, 1,
106 NULL
, "consumer", &producer
))
107 errx (1, "Cannot create consumer process");
112 * Test the LWP_Sleep() function
118 printf("[mrsleepy] I'm going to bed\n");
121 printf("[mrsleepy] yawn\n");
126 putMrSleeptoBed(void)
130 if (LWP_CreateProcess (MrSleepy
, LWPTEST_STACKSIZE
, 1,
131 NULL
, "mrsleepy", &mrsleepy
))
132 errx (1, "Cannot create consumer process");
137 * The Producer Consumer thingy done over a pipe
140 int pipa
[2] = { -1, -1 };
143 SelectProducer(void *foo
)
145 char *str
= (char *) foo
;
154 printf("[selprodu] %s\n", str
);
159 SelectConsumer(void *foo
)
167 if (pipa
[0] >= FD_SETSIZE
)
168 errx (1, "fd too large");
170 FD_SET(pipa
[0], &readset
);
171 IOMGR_Select(pipa
[0] + 1, &readset
, NULL
, NULL
, NULL
);
172 len
= read(pipa
[0], str
, 199);
176 printf("[selcomsu] %s\n", str
);
181 startSelectPC (char *progname
)
190 if (LWP_CreateProcess (SelectConsumer
, LWPTEST_STACKSIZE
, 1,
191 NULL
, "selconsu", &consumer
))
192 errx (1, "Cannot create select consumer process");
201 if (dup2(pipa
[1], 0) == -1)
204 execl(progname
, "testlwp", "selectproducer", NULL
);
219 printf("[onstrike] I'll never quit, block, block, block\n");
220 IOMGR_Select(0, NULL
, NULL
, NULL
, NULL
);
221 printf("[onstrike] Bah, you will never pay enough\n");
226 freeEnterprise(void *foo
)
228 PROCESS
*pid
= (PROCESS
*) foo
;
232 printf("[enterpri] Raise salery\n");
240 static PROCESS worker
, enterprise
;
242 if (LWP_CreateProcess (onStrike
, LWPTEST_STACKSIZE
, 1,
243 NULL
, "worker", &worker
))
244 errx (1, "Cannot create worker process");
246 if (LWP_CreateProcess (freeEnterprise
, LWPTEST_STACKSIZE
, 1,
247 (void *)&worker
, "enterprise", &enterprise
))
248 errx (1, "Cannot create enterprise process");
252 deadlock_write (void)
257 ObtainWriteLock(&lock
);
258 ObtainWriteLock(&lock
);
267 ObtainWriteLock(&lock
);
268 ObtainReadLock(&lock
);
272 deadlock_read2 (void)
277 ObtainReadLock(&lock
);
278 ObtainWriteLock(&lock
);
282 * the terms overrun and underrun stack are used somewhat wrong here,
283 * in what direction do your stack grow
287 * when testing if the stack thingy work, it might not be so smart to
288 * use the stack (automatic variables) for storage :)
291 int stack_i
, stack_printed
= 0;
294 overrun_stack (void *arg
)
297 int stack_i
, stack_printed
= 0;
299 for (stack_i
= 10; stack_i
> -LWPTEST_STACKSIZE
* 2; stack_i
--) {
300 if (stack_i
< -LWPTEST_STACKSIZE
- 100 && !stack_printed
) {
301 printf("hum overrun stack now\n");
310 underrun_stack (void *arg
)
314 for (stack_i
= 0; stack_i
< LWPTEST_STACKSIZE
* 2; stack_i
++) {
315 if (stack_i
> LWPTEST_STACKSIZE
+ 100&& !stack_printed
) {
316 printf("hum underrun stack now\n");
329 usage(char *progname
)
331 fprintf(stderr
, "usage: %s cmd ...\nWhere cmd is one of:\n", progname
);
333 "pc\t\tProducer Consumer test\n"
334 "sleep\t\tSleeptest\n"
335 "selectconsumer\tSelect consumer\n"
336 "selectproducer\t(special case, just print a string on stdout repeatally)\n"
337 "cancel\t\tTest iomgr cancel\n"
338 "deadlock-write\tdeadlockdetection\n"
339 "deadlock-read\tdeadlockdetection\n"
340 "deadlock-read2\tdeadlockdetection\n"
341 "overrun-stack\tover run the stack\n"
342 "underrun-stack\tunder run the stack\n"
343 "version\t\tPrint version\n");
345 printf("Use several of these tests together to test their interopability\n");
349 int main(int argc
, char **argv
)
352 char *progname
= strdup(argv
[0]);
354 if (progname
== NULL
)
360 printf("starting LWP support\n");
361 if (LWP_InitializeProcessSupport(LWP_NORMAL_PRIORITY
, &pid
))
362 errx(1, "LWP_InitializeProcessSupport()\n");
364 printf("starting IOMGR support\n");
365 if (IOMGR_Initialize())
366 errx(1, "IOMGR_Initialize()\n");
369 if (strcasecmp("pc", argv
[1]) == 0) {
371 } else if (strcasecmp("sleep", argv
[1]) == 0) {
373 } else if (strcasecmp("selectproducer", argv
[1]) == 0) {
374 SelectProducer(NULL
);
375 exit(1); /* Special case */
376 } else if (strcasecmp("selectconsumer", argv
[1]) == 0) {
377 startSelectPC (progname
);
378 } else if (strcasecmp("cancel", argv
[1]) == 0) {
380 } else if (strcasecmp("deadlock-write", argv
[1]) == 0) {
382 } else if (strcasecmp("deadlock-read", argv
[1]) == 0) {
384 } else if (strcasecmp("deadlock-read2", argv
[1]) == 0) {
386 } else if (strcasecmp("overrun-stack", argv
[1]) == 0) {
388 if (LWP_CreateProcess (overrun_stack
, LWPTEST_STACKSIZE
, 1,
389 NULL
, "overunner", &tpid
))
390 errx (1, "Cannot create stack overrunner process");
391 } else if (strcasecmp("underrun-stack", argv
[1]) == 0) {
393 if (LWP_CreateProcess (underrun_stack
, LWPTEST_STACKSIZE
, 1,
394 NULL
, "underrunner", &tpid
))
395 errx (1, "Cannot create stack underrunner process");
396 } else if (strcasecmp("version", argv
[1]) == 0) {
401 printf("unknown command %s\n", argv
[1]);
408 LWP_WaitProcess((char *) main
);