[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
|
---|
| 55 | 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 ) {
|
---|
[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
|
---|
| 79 | $thread * 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 | $thread * node = this.anchor.next;
|
---|
| 85 | this.anchor.next = node->link.next;
|
---|
| 86 | this.anchor.ts = node->link.ts;
|
---|
| 87 | bool is_empty = this.anchor.ts == 0;
|
---|
| 88 | node->link.next = 0p;
|
---|
| 89 | node->link.ts = 0;
|
---|
| 90 |
|
---|
| 91 | // Update head time stamp
|
---|
| 92 | if(is_empty) this.prev = mock_head( this );
|
---|
| 93 |
|
---|
| 94 | /* paranoid */ verify( node->link.next == 0p );
|
---|
| 95 | /* paranoid */ verify( node->link.ts == 0 );
|
---|
| 96 | return node;
|
---|
| 97 | }
|
---|
| 98 |
|
---|
| 99 | // Check whether or not list is empty
|
---|
| 100 | static inline bool is_empty(__intrusive_lane_t & this) {
|
---|
| 101 | return this.anchor.ts == 0;
|
---|
| 102 | }
|
---|
| 103 |
|
---|
| 104 | // Return the timestamp
|
---|
| 105 | static inline unsigned long long ts(__intrusive_lane_t & this) {
|
---|
| 106 | // Cannot verify here since it may not be locked
|
---|
| 107 | return this.anchor.ts;
|
---|
| 108 | }
|
---|
[9cc3a18] | 109 |
|
---|
| 110 | // Aligned timestamps which are used by the relaxed ready queue
|
---|
| 111 | struct __attribute__((aligned(128))) __timestamp_t {
|
---|
| 112 | volatile unsigned long long tv;
|
---|
| 113 | };
|
---|
| 114 |
|
---|
| 115 | void ?{}(__timestamp_t & this) { this.tv = 0; }
|
---|
| 116 | void ^?{}(__timestamp_t & this) {}
|
---|