[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
|
---|
| 33 | /* paranoid */ verify( ((uintptr_t)( mock_head(this) ) + offsetof( $thread, link )) == (uintptr_t)(&this.anchor) );
|
---|
| 34 | /* paranoid */ verify( &mock_head(this)->link.next == &this.anchor.next );
|
---|
| 35 | /* paranoid */ verify( &mock_head(this)->link.ts == &this.anchor.ts );
|
---|
| 36 | /* paranoid */ verify( mock_head(this)->link.next == 0p );
|
---|
| 37 | /* paranoid */ verify( mock_head(this)->link.ts == 0 );
|
---|
| 38 | /* paranoid */ verify( mock_head(this) == this.prev );
|
---|
| 39 | /* paranoid */ verify( __alignof__(__intrusive_lane_t) == 128 );
|
---|
| 40 | /* paranoid */ verify( __alignof__(this) == 128 );
|
---|
| 41 | /* paranoid */ verifyf( ((intptr_t)(&this) % 128) == 0, "Expected address to be aligned %p %% 128 == %zd", &this, ((intptr_t)(&this) % 128) );
|
---|
| 42 | }
|
---|
| 43 |
|
---|
| 44 | // Dtor is trivial
|
---|
| 45 | void ^?{}( __intrusive_lane_t & this ) {
|
---|
| 46 | // Make sure the list is empty
|
---|
| 47 | /* paranoid */ verify( this.anchor.next == 0p );
|
---|
| 48 | /* paranoid */ verify( this.anchor.ts == 0 );
|
---|
| 49 | /* paranoid */ verify( mock_head(this) == this.prev );
|
---|
| 50 | }
|
---|
| 51 |
|
---|
| 52 | // Push a thread onto this lane
|
---|
| 53 | // returns true of lane was empty before push, false otherwise
|
---|
| 54 | void push( __intrusive_lane_t & this, $thread * node ) {
|
---|
| 55 | /* paranoid */ verify( node->link.next == 0p );
|
---|
| 56 | /* paranoid */ verify( node->link.ts == 0 );
|
---|
| 57 | /* paranoid */ verify( this.prev->link.next == 0p );
|
---|
| 58 | /* paranoid */ verify( this.prev->link.ts == 0 );
|
---|
| 59 | if( this.anchor.next == 0p ) {
|
---|
[2b96031] | 60 | /* paranoid */ verify( this.anchor.next == 0p );
|
---|
| 61 | /* paranoid */ verify( this.anchor.ts == 0 );
|
---|
[f6fdfb14] | 62 | /* paranoid */ verify( this.prev == mock_head( this ) );
|
---|
| 63 | } else {
|
---|
[2b96031] | 64 | /* paranoid */ verify( this.anchor.next != 0p );
|
---|
| 65 | /* paranoid */ verify( this.anchor.ts != 0 );
|
---|
[f6fdfb14] | 66 | /* paranoid */ verify( this.prev != mock_head( this ) );
|
---|
[2b96031] | 67 | }
|
---|
| 68 |
|
---|
[f6fdfb14] | 69 | // Get the relevant nodes locally
|
---|
| 70 | this.prev->link.next = node;
|
---|
| 71 | this.prev->link.ts = rdtscl();
|
---|
| 72 | this.prev = node;
|
---|
| 73 | }
|
---|
| 74 |
|
---|
| 75 | // Pop a thread from this lane (must be non-empty)
|
---|
| 76 | // returns popped
|
---|
| 77 | // returns true of lane was empty before push, false otherwise
|
---|
| 78 | $thread * pop( __intrusive_lane_t & this ) {
|
---|
| 79 | /* paranoid */ verify( this.anchor.next != 0p );
|
---|
| 80 | /* paranoid */ verify( this.anchor.ts != 0 );
|
---|
| 81 |
|
---|
| 82 | // Get the relevant nodes locally
|
---|
| 83 | $thread * node = this.anchor.next;
|
---|
| 84 | this.anchor.next = node->link.next;
|
---|
| 85 | this.anchor.ts = node->link.ts;
|
---|
| 86 | bool is_empty = this.anchor.ts == 0;
|
---|
| 87 | node->link.next = 0p;
|
---|
| 88 | node->link.ts = 0;
|
---|
| 89 |
|
---|
| 90 | // Update head time stamp
|
---|
| 91 | if(is_empty) this.prev = mock_head( this );
|
---|
| 92 |
|
---|
| 93 | /* paranoid */ verify( node->link.next == 0p );
|
---|
| 94 | /* paranoid */ verify( node->link.ts == 0 );
|
---|
| 95 | return node;
|
---|
| 96 | }
|
---|
| 97 |
|
---|
| 98 | // Check whether or not list is empty
|
---|
| 99 | static inline bool is_empty(__intrusive_lane_t & this) {
|
---|
| 100 | return this.anchor.ts == 0;
|
---|
| 101 | }
|
---|
| 102 |
|
---|
| 103 | // Return the timestamp
|
---|
| 104 | static inline unsigned long long ts(__intrusive_lane_t & this) {
|
---|
| 105 | // Cannot verify here since it may not be locked
|
---|
| 106 | return this.anchor.ts;
|
---|
| 107 | }
|
---|
[9cc3a18] | 108 |
|
---|
| 109 | // Aligned timestamps which are used by the relaxed ready queue
|
---|
| 110 | struct __attribute__((aligned(128))) __timestamp_t {
|
---|
| 111 | volatile unsigned long long tv;
|
---|
| 112 | };
|
---|
| 113 |
|
---|
| 114 | void ?{}(__timestamp_t & this) { this.tv = 0; }
|
---|
| 115 | void ^?{}(__timestamp_t & this) {}
|
---|