source: libcfa/src/concurrency/ready_subqueue.hfa @ 45b9b21

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

Fix timestamp with new subqueue which was read after being cleared.

  • Property mode set to 100644
File size: 3.9 KB
Line 
1#pragma once
2
3#define __CFA_NO_SCHED_STATS__
4
5#include "containers/queueLockFree.hfa"
6
7// Intrusives lanes which are used by the relaxed ready queue
8struct __attribute__((aligned(128))) __intrusive_lane_t {
9        struct $thread * prev;
10
11        // spin lock protecting the queue
12        volatile bool lock;
13
14        __thread_desc_link anchor;
15};
16
17// Get the head pointer (one before the first element) from the anchor
18static inline $thread * mock_head(const __intrusive_lane_t & this) {
19        $thread * rhead = ($thread *)(
20                (uintptr_t)( &this.anchor ) - __builtin_offsetof( $thread, link )
21        );
22        return rhead;
23}
24
25// Ctor
26void ?{}( __intrusive_lane_t & this ) {
27        this.lock = false;
28        this.prev = mock_head(this);
29        this.anchor.next = 0p;
30        this.anchor.ts   = 0;
31
32        // We add a boat-load of assertions here because the anchor code is very fragile
33        /* paranoid */ verify( offsetof( $thread, link ) == offsetof(__intrusive_lane_t, anchor) );
34        /* paranoid */ verify( ((uintptr_t)( mock_head(this) ) + offsetof( $thread, link )) == (uintptr_t)(&this.anchor) );
35        /* paranoid */ verify( &mock_head(this)->link.next == &this.anchor.next );
36        /* paranoid */ verify( &mock_head(this)->link.ts   == &this.anchor.ts   );
37        /* paranoid */ verify( mock_head(this)->link.next == 0p );
38        /* paranoid */ verify( mock_head(this)->link.ts   == 0  );
39        /* paranoid */ verify( mock_head(this) == this.prev );
40        /* paranoid */ verify( __alignof__(__intrusive_lane_t) == 128 );
41        /* paranoid */ verify( __alignof__(this) == 128 );
42        /* paranoid */ verifyf( ((intptr_t)(&this) % 128) == 0, "Expected address to be aligned %p %% 128 == %zd", &this, ((intptr_t)(&this) % 128) );
43}
44
45// Dtor is trivial
46void ^?{}( __intrusive_lane_t & this ) {
47        // Make sure the list is empty
48        /* paranoid */ verify( this.anchor.next == 0p );
49        /* paranoid */ verify( this.anchor.ts   == 0  );
50        /* paranoid */ verify( mock_head(this)  == this.prev );
51}
52
53// Push a thread onto this lane
54// returns true of lane was empty before push, false otherwise
55static inline void push( __intrusive_lane_t & this, $thread * node ) {
56        /* paranoid */ verify( node->link.next == 0p );
57        /* paranoid */ verify( node->link.ts   == 0  );
58        /* paranoid */ verify( this.prev->link.next == 0p );
59        /* paranoid */ verify( this.prev->link.ts   == 0  );
60        if( this.anchor.next == 0p ) {
61                /* paranoid */ verify( this.anchor.next == 0p );
62                /* paranoid */ verify( this.anchor.ts   == 0  );
63                /* paranoid */ verify( this.prev == mock_head( this ) );
64        } else {
65                /* paranoid */ verify( this.anchor.next != 0p );
66                /* paranoid */ verify( this.anchor.ts   != 0  );
67                /* paranoid */ verify( this.prev != mock_head( this ) );
68        }
69
70        // Get the relevant nodes locally
71        this.prev->link.next = node;
72        this.prev->link.ts   = rdtscl();
73        this.prev = node;
74}
75
76// Pop a thread from this lane (must be non-empty)
77// returns popped
78// returns true of lane was empty before push, false otherwise
79static inline [* $thread, unsigned long long] pop( __intrusive_lane_t & this ) {
80        /* paranoid */ verify( this.anchor.next != 0p );
81        /* paranoid */ verify( this.anchor.ts   != 0  );
82
83        // Get the relevant nodes locally
84        unsigned long long ts = this.anchor.ts;
85        $thread * node = this.anchor.next;
86        this.anchor.next = node->link.next;
87        this.anchor.ts   = node->link.ts;
88        bool is_empty = this.anchor.ts == 0;
89        node->link.next = 0p;
90        node->link.ts   = 0;
91
92        // Update head time stamp
93        if(is_empty) this.prev = mock_head( this );
94
95        /* paranoid */ verify( node->link.next == 0p );
96        /* paranoid */ verify( node->link.ts   == 0  );
97        return [node, ts];
98}
99
100// Check whether or not list is empty
101static inline bool is_empty(__intrusive_lane_t & this) {
102        return this.anchor.ts == 0;
103}
104
105// Return the timestamp
106static inline unsigned long long ts(__intrusive_lane_t & this) {
107        // Cannot verify here since it may not be locked
108        return this.anchor.ts;
109}
110
111// Aligned timestamps which are used by the relaxed ready queue
112struct __attribute__((aligned(128))) __timestamp_t {
113        volatile unsigned long long tv;
114};
115
116void  ?{}(__timestamp_t & this) { this.tv = 0; }
117void ^?{}(__timestamp_t & this) {}
Note: See TracBrowser for help on using the repository browser.