fix tables
[nobug.git] / tests / test_nobug_threadstress.c
blob203907745007613e55968d158032d2aeb6ac0827
1 #define TESTTHREADS 200
3 #include "test.h"
4 #include "nobug.h"
6 #include <stdlib.h>
7 #include <unistd.h>
8 #include <stdint.h>
9 #include <pthread.h>
11 struct testthread
13 pthread_t thread;
14 NOBUG_RESOURCE_HANDLE(rh);
17 void*
18 threadfn(void* nop)
20 NOBUG_THREAD_ID_SET("thread");
22 NOBUG_RESOURCE_USER(rh);
23 NOBUG_RESOURCE_USER_INIT(rh);
24 NOBUG_RESOURCE_ENTER(NOBUG_ON, ((struct testthread*)nop)->rh, "threadfn", NOBUG_RESOURCE_WAITING, rh) { }
26 ECHO("starting");
28 unsigned int seed = (uintptr_t)nop;
31 int r1 = (rand_r(&seed)%100)*1000;
32 if (r1>20000)
34 ECHO("first sleeping for %d usec", r1);
35 usleep(r1);
37 else
39 ECHO("first busy for %d iterations", r1*500);
40 for (volatile int i=0; i<r1*500; ++i)
42 volatile int j = i*i;
43 j /= 2;
47 NOBUG_RESOURCE_STATE (NOBUG_ON, NOBUG_RESOURCE_EXCLUSIVE, rh) {}
49 int r2 = (rand_r(&seed)%100)*1000;
50 if (r2>20000)
52 ECHO("second sleeping for %d usec", r2);
53 usleep(r2);
55 else
57 ECHO("second busy for %d iterations", r2*500);
58 for (volatile int i=0; i<r2*500; ++i)
60 volatile int j = i*i;
61 j /= 2;
65 int r3 = (rand_r(&seed)%100)*1000;
66 if (r3>20000)
68 ECHO("third sleeping for %d usec", r3);
69 usleep(r3);
71 else
73 ECHO("third busy for %d iterations", r3*500);
74 for (volatile int i=0; i<r3*500; ++i)
76 volatile int j = i*i;
77 j /= 2;
81 ECHO("ending");
83 NOBUG_RESOURCE_LEAVE(NOBUG_ON, rh) { }
85 return nop;
89 TESTS_BEGIN
90 NOBUG_THREAD_ID_SET("main");
91 ECHO ("testing");
94 TEST(many_threads)
96 struct testthread threads[TESTTHREADS];
98 for (int i = 0; i<TESTTHREADS; ++i)
100 NOBUG_RESOURCE_HANDLE_INIT(threads[i].rh);
101 NOBUG_RESOURCE_ANNOUNCE(NOBUG_ON, "thread", "test", &threads[i], threads[i].rh)
103 if (!pthread_create(&threads[i].thread, NULL, threadfn, &threads[i]))
105 ECHO("created thread");
107 else
109 ECHO("thread created failed");
110 exit(1);
115 for (int i = 0; i<TESTTHREADS; ++i)
117 void* ret;
119 NOBUG_RESOURCE_FORGET(NOBUG_ON, threads[i].rh)
121 if (!pthread_join(threads[i].thread, &ret))
123 ECHO("thread joined");
124 CHECK (ret == &threads[i]);
126 else
128 ECHO("thread join failed");
129 exit(1);
137 TESTS_END