source: tests/concurrent/park/contention.cfa @ a77496cb

ADTarm-ehast-experimentalenumforall-pointer-decayjacob/cs343-translationnew-ast-unique-exprpthread-emulationqualifiedEnum
Last change on this file since a77496cb was ae66348, checked in by Thierry Delisle <tdelisle@…>, 4 years ago

Threads in debug now keep track of last function to park/unpark it

  • Property mode set to 100644
File size: 1.2 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 __cfaabi_dbg_ctx2 );
24                } else {
25                        Thread * thrd = __atomic_exchange_n(&blocked[idx], &this, __ATOMIC_SEQ_CST);
26                        unpark( *thrd __cfaabi_dbg_ctx2 );
27                        park( __cfaabi_dbg_ctx );
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 __cfaabi_dbg_ctx2 );
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.