source: libcfa/src/concurrency/ready_subqueue.hfa@ 9e2341b4

ADT arm-eh ast-experimental enum forall-pointer-decay jacob/cs343-translation new-ast-unique-expr pthread-emulation qualifiedEnum
Last change on this file since 9e2341b4 was d3ba775, checked in by Thierry Delisle <tdelisle@…>, 4 years ago

More clean-up after new subqueue

  • Property mode set to 100644
File size: 3.8 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
55void 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
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
100static inline bool is_empty(__intrusive_lane_t & this) {
101 return this.anchor.ts == 0;
102}
103
104// Return the timestamp
105static 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}
109
110// Aligned timestamps which are used by the relaxed ready queue
111struct __attribute__((aligned(128))) __timestamp_t {
112 volatile unsigned long long tv;
113};
114
115void ?{}(__timestamp_t & this) { this.tv = 0; }
116void ^?{}(__timestamp_t & this) {}
Note: See TracBrowser for help on using the repository browser.