[13c5e19] | 1 | #pragma once |
---|
| 2 | |
---|
| 3 | #define __CFA_NO_SCHED_STATS__ |
---|
| 4 | |
---|
[7a2972b9] | 5 | #include "containers/queueLockFree.hfa" |
---|
| 6 | |
---|
[f6fdfb14] | 7 | // Intrusives lanes which are used by the relaxed ready queue |
---|
| 8 | struct __attribute__((aligned(128))) __intrusive_lane_t { |
---|
| 9 | struct $thread * prev; |
---|
[2b96031] | 10 | |
---|
[f6fdfb14] | 11 | // spin lock protecting the queue |
---|
| 12 | volatile bool lock; |
---|
[13c5e19] | 13 | |
---|
[f6fdfb14] | 14 | __thread_desc_link anchor; |
---|
| 15 | }; |
---|
[2b96031] | 16 | |
---|
[f6fdfb14] | 17 | // Get the head pointer (one before the first element) from the anchor |
---|
| 18 | static 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 |
---|
| 26 | void ?{}( __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 |
---|
[d3ba775] | 33 | /* paranoid */ verify( offsetof( $thread, link ) == offsetof(__intrusive_lane_t, anchor) ); |
---|
[f6fdfb14] | 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 |
---|
| 46 | void ^?{}( __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 |
---|
[f302d80] | 55 | static inline void push( __intrusive_lane_t & this, $thread * node ) { |
---|
[f6fdfb14] | 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 ) { |
---|
[2b96031] | 61 | /* paranoid */ verify( this.anchor.next == 0p ); |
---|
| 62 | /* paranoid */ verify( this.anchor.ts == 0 ); |
---|
[f6fdfb14] | 63 | /* paranoid */ verify( this.prev == mock_head( this ) ); |
---|
| 64 | } else { |
---|
[2b96031] | 65 | /* paranoid */ verify( this.anchor.next != 0p ); |
---|
| 66 | /* paranoid */ verify( this.anchor.ts != 0 ); |
---|
[f6fdfb14] | 67 | /* paranoid */ verify( this.prev != mock_head( this ) ); |
---|
[2b96031] | 68 | } |
---|
| 69 | |
---|
[f6fdfb14] | 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 |
---|
[f302d80] | 79 | static inline [* $thread, unsigned long long] pop( __intrusive_lane_t & this ) { |
---|
[f6fdfb14] | 80 | /* paranoid */ verify( this.anchor.next != 0p ); |
---|
| 81 | /* paranoid */ verify( this.anchor.ts != 0 ); |
---|
| 82 | |
---|
| 83 | // Get the relevant nodes locally |
---|
[f302d80] | 84 | unsigned long long ts = this.anchor.ts; |
---|
[f6fdfb14] | 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 ); |
---|
[f302d80] | 97 | return [node, ts]; |
---|
[f6fdfb14] | 98 | } |
---|
| 99 | |
---|
| 100 | // Check whether or not list is empty |
---|
| 101 | static inline bool is_empty(__intrusive_lane_t & this) { |
---|
| 102 | return this.anchor.ts == 0; |
---|
| 103 | } |
---|
| 104 | |
---|
| 105 | // Return the timestamp |
---|
| 106 | static 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 | } |
---|
[9cc3a18] | 110 | |
---|
| 111 | // Aligned timestamps which are used by the relaxed ready queue |
---|
| 112 | struct __attribute__((aligned(128))) __timestamp_t { |
---|
| 113 | volatile unsigned long long tv; |
---|
| 114 | }; |
---|
| 115 | |
---|
| 116 | void ?{}(__timestamp_t & this) { this.tv = 0; } |
---|
| 117 | void ^?{}(__timestamp_t & this) {} |
---|