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 |
---|
8 | struct __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 | #if !defined(__CFA_NO_STATISTICS__) |
---|
17 | unsigned cnt; |
---|
18 | #endif |
---|
19 | }; |
---|
20 | |
---|
21 | // Get the head pointer (one before the first element) from the anchor |
---|
22 | static inline $thread * mock_head(const __intrusive_lane_t & this) { |
---|
23 | $thread * rhead = ($thread *)( |
---|
24 | (uintptr_t)( &this.anchor ) - __builtin_offsetof( $thread, link ) |
---|
25 | ); |
---|
26 | return rhead; |
---|
27 | } |
---|
28 | |
---|
29 | // Ctor |
---|
30 | void ?{}( __intrusive_lane_t & this ) { |
---|
31 | this.lock = false; |
---|
32 | this.prev = mock_head(this); |
---|
33 | this.anchor.next = 0p; |
---|
34 | this.anchor.ts = 0; |
---|
35 | #if !defined(__CFA_NO_STATISTICS__) |
---|
36 | this.cnt = 0; |
---|
37 | #endif |
---|
38 | |
---|
39 | // We add a boat-load of assertions here because the anchor code is very fragile |
---|
40 | /* paranoid */ _Static_assert( offsetof( $thread, link ) == offsetof(__intrusive_lane_t, anchor) ); |
---|
41 | /* paranoid */ verify( offsetof( $thread, link ) == offsetof(__intrusive_lane_t, anchor) ); |
---|
42 | /* paranoid */ verify( ((uintptr_t)( mock_head(this) ) + offsetof( $thread, link )) == (uintptr_t)(&this.anchor) ); |
---|
43 | /* paranoid */ verify( &mock_head(this)->link.next == &this.anchor.next ); |
---|
44 | /* paranoid */ verify( &mock_head(this)->link.ts == &this.anchor.ts ); |
---|
45 | /* paranoid */ verify( mock_head(this)->link.next == 0p ); |
---|
46 | /* paranoid */ verify( mock_head(this)->link.ts == 0 ); |
---|
47 | /* paranoid */ verify( mock_head(this) == this.prev ); |
---|
48 | /* paranoid */ verify( __alignof__(__intrusive_lane_t) == 128 ); |
---|
49 | /* paranoid */ verify( __alignof__(this) == 128 ); |
---|
50 | /* paranoid */ verifyf( ((intptr_t)(&this) % 128) == 0, "Expected address to be aligned %p %% 128 == %zd", &this, ((intptr_t)(&this) % 128) ); |
---|
51 | } |
---|
52 | |
---|
53 | // Dtor is trivial |
---|
54 | void ^?{}( __intrusive_lane_t & this ) { |
---|
55 | // Make sure the list is empty |
---|
56 | /* paranoid */ verify( this.anchor.next == 0p ); |
---|
57 | /* paranoid */ verify( this.anchor.ts == 0 ); |
---|
58 | /* paranoid */ verify( mock_head(this) == this.prev ); |
---|
59 | } |
---|
60 | |
---|
61 | // Push a thread onto this lane |
---|
62 | // returns true of lane was empty before push, false otherwise |
---|
63 | static inline void push( __intrusive_lane_t & this, $thread * node ) { |
---|
64 | /* paranoid */ verify( this.lock ); |
---|
65 | /* paranoid */ verify( node->link.next == 0p ); |
---|
66 | /* paranoid */ verify( node->link.ts == 0 ); |
---|
67 | /* paranoid */ verify( this.prev->link.next == 0p ); |
---|
68 | /* paranoid */ verify( this.prev->link.ts == 0 ); |
---|
69 | if( this.anchor.next == 0p ) { |
---|
70 | /* paranoid */ verify( this.anchor.next == 0p ); |
---|
71 | /* paranoid */ verify( this.anchor.ts == 0 ); |
---|
72 | /* paranoid */ verify( this.prev == mock_head( this ) ); |
---|
73 | } else { |
---|
74 | /* paranoid */ verify( this.anchor.next != 0p ); |
---|
75 | /* paranoid */ verify( this.anchor.ts != 0 ); |
---|
76 | /* paranoid */ verify( this.prev != mock_head( this ) ); |
---|
77 | } |
---|
78 | |
---|
79 | // Get the relevant nodes locally |
---|
80 | this.prev->link.next = node; |
---|
81 | this.prev->link.ts = rdtscl(); |
---|
82 | this.prev = node; |
---|
83 | #if !defined(__CFA_NO_STATISTICS__) |
---|
84 | this.cnt++; |
---|
85 | #endif |
---|
86 | } |
---|
87 | |
---|
88 | // Pop a thread from this lane (must be non-empty) |
---|
89 | // returns popped |
---|
90 | // returns true of lane was empty before push, false otherwise |
---|
91 | static inline [* $thread, unsigned long long] pop( __intrusive_lane_t & this ) { |
---|
92 | /* paranoid */ verify( this.lock ); |
---|
93 | /* paranoid */ verify( this.anchor.next != 0p ); |
---|
94 | /* paranoid */ verify( this.anchor.ts != 0 ); |
---|
95 | |
---|
96 | // Get the relevant nodes locally |
---|
97 | unsigned long long ts = this.anchor.ts; |
---|
98 | $thread * node = this.anchor.next; |
---|
99 | this.anchor.next = node->link.next; |
---|
100 | this.anchor.ts = node->link.ts; |
---|
101 | bool is_empty = this.anchor.ts == 0; |
---|
102 | node->link.next = 0p; |
---|
103 | node->link.ts = 0; |
---|
104 | #if !defined(__CFA_NO_STATISTICS__) |
---|
105 | this.cnt--; |
---|
106 | #endif |
---|
107 | |
---|
108 | // Update head time stamp |
---|
109 | if(is_empty) this.prev = mock_head( this ); |
---|
110 | |
---|
111 | /* paranoid */ verify( node->link.next == 0p ); |
---|
112 | /* paranoid */ verify( node->link.ts == 0 ); |
---|
113 | return [node, ts]; |
---|
114 | } |
---|
115 | |
---|
116 | // Check whether or not list is empty |
---|
117 | static inline bool is_empty(__intrusive_lane_t & this) { |
---|
118 | return this.anchor.ts == 0; |
---|
119 | } |
---|
120 | |
---|
121 | // Return the timestamp |
---|
122 | static inline unsigned long long ts(__intrusive_lane_t & this) { |
---|
123 | // Cannot verify here since it may not be locked |
---|
124 | return this.anchor.ts; |
---|
125 | } |
---|