source: tests/concurrent/park/contention.cfa @ 50b8885

ADTarm-ehast-experimentalenumforall-pointer-decayjacob/cs343-translationjenkins-sandboxnew-astnew-ast-unique-exprpthread-emulationqualifiedEnum
Last change on this file since 50b8885 was 3381ed7, checked in by Thierry Delisle <tdelisle@…>, 4 years ago

Added park/unpark primitives thread and removed BlockInternal?.
Converted monitors to use park unpark.
Intrusive Queue now mark next field when thread is inside queue.
Added several asserts to kernel and monitor.
Added a few tests for park and unpark.

  • Property mode set to 100644
File size: 1.1 KB
Line 
1#include <kernel.hfa>
2#include <thread.hfa>
3
4thread_local 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.