source: tests/concurrency/park/contention.cfa @ 08e0d65

Last change on this file since 08e0d65 was c26bea2a, checked in by Peter A. Buhr <pabuhr@…>, 18 months ago

first attempt at renaming directory tests/concurrent to tests/concurrency to harmonize with other concurrency directory names

  • Property mode set to 100644
File size: 1.1 KB
Line 
1#include <kernel.hfa>
2#include <thread.hfa>
3
4__thread drand48_data buffer = { 0 };
5int myrand() {
6        long int result;
7        lrand48_r(&buffer, &result);
8        return result;
9}
10
11
12thread Thread {};
13void ^?{}(Thread & mutex this) {}
14
15enum Constants { blocked_size = 20 };
16Thread * volatile blocked[blocked_size];
17
18void main( Thread & this ) {
19        for(int i = 0; i < 1000; i++) {
20                int idx = myrand() % blocked_size;
21                if(blocked[idx]) {
22                        Thread * thrd = __atomic_exchange_n(&blocked[idx], 0p, __ATOMIC_SEQ_CST);
23                        unpark( *thrd );
24                } else {
25                        Thread * thrd = __atomic_exchange_n(&blocked[idx], &this, __ATOMIC_SEQ_CST);
26                        unpark( *thrd );
27                        park();
28                }
29        }
30        printf("Done\n");
31}
32
33// Extra thread to avoid deadlocking
34thread Unparker {};
35
36void main( Unparker & this ) {
37        while(true) {
38                waitfor( ^?{} : this ) {
39                        break;
40                } or else {
41                        int idx = myrand() % blocked_size;
42                        Thread * thrd = __atomic_exchange_n(&blocked[idx], 0p, __ATOMIC_SEQ_CST);
43                        unpark( *thrd );
44                        yield( myrand() % 20 );
45                }
46        }
47        printf("Done Unparker\n");
48}
49
50
51int main() {
52        for(i ; blocked_size) {
53                blocked[i] = 0p;
54        }
55
56        processor p[3];
57
58        Unparker u;
59        {
60                Thread t[20];
61        }
62}
Note: See TracBrowser for help on using the repository browser.