1 | #include "containers/list.hfa" |
---|
2 | #include <stdint.h> |
---|
3 | #include <kernel.hfa> |
---|
4 | #include <locks.hfa> |
---|
5 | |
---|
6 | struct select_node { |
---|
7 | thread$ * blocked_thread; |
---|
8 | void ** race_flag; |
---|
9 | inline dlink(select_node); |
---|
10 | }; |
---|
11 | P9_EMBEDDED( select_node, dlink(select_node) ) |
---|
12 | |
---|
13 | void ?{}( select_node & this ) { |
---|
14 | this.blocked_thread = 0p; |
---|
15 | this.race_flag = 0p; |
---|
16 | } |
---|
17 | |
---|
18 | void ?{}( select_node & this, thread$ * blocked_thread ) { |
---|
19 | this.blocked_thread = blocked_thread; |
---|
20 | this.race_flag = 0p; |
---|
21 | } |
---|
22 | |
---|
23 | void ?{}( select_node & this, thread$ * blocked_thread, void ** race_flag ) { |
---|
24 | this.blocked_thread = blocked_thread; |
---|
25 | this.race_flag = race_flag; |
---|
26 | } |
---|
27 | |
---|
28 | void ^?{}( select_node & this ) {} |
---|
29 | |
---|
30 | |
---|
31 | //----------------------------------------------------------------------------- |
---|
32 | // is_selectable |
---|
33 | trait is_selectable(T & | sized(T)) { |
---|
34 | // For registering a select on a selectable concurrency primitive |
---|
35 | // return 0p if primitive not accessible yet |
---|
36 | // return 1p if primitive gets acquired |
---|
37 | // return 2p if primitive is accessible but some other primitive won the race |
---|
38 | // C_TODO: add enum for return values |
---|
39 | void * register_select( T &, select_node & ); |
---|
40 | |
---|
41 | void unregister_select( T &, select_node & ); |
---|
42 | }; |
---|
43 | |
---|
44 | static inline bool install_select_winner( select_node & this, void * primitive_ptr ) with(this) { |
---|
45 | // temporary needed for atomic instruction |
---|
46 | void * cmp_flag = 0p; |
---|
47 | |
---|
48 | // if we dont win the selector race we need to potentially |
---|
49 | // ignore this node and move to the next one so we return accordingly |
---|
50 | if ( *race_flag != 0p || |
---|
51 | !__atomic_compare_exchange_n( |
---|
52 | race_flag, |
---|
53 | &cmp_flag, |
---|
54 | primitive_ptr, |
---|
55 | false, |
---|
56 | __ATOMIC_SEQ_CST, |
---|
57 | __ATOMIC_SEQ_CST |
---|
58 | ) |
---|
59 | ) return false; // lost race and some other node triggered select |
---|
60 | return true; // won race so this node is what the select proceeds with |
---|
61 | } |
---|