source: libcfa/src/concurrency/select.hfa @ 4066bd2

ADTast-experimental
Last change on this file since 4066bd2 was 339e30a, checked in by caparsons <caparson@…>, 23 months ago

added new future and added rudimentary select statement support for it

  • Property mode set to 100644
File size: 1.8 KB
Line 
1#include "containers/list.hfa"
2#include <stdint.h>
3#include <kernel.hfa>
4#include <locks.hfa>
5
6struct select_node {
7    thread$ * blocked_thread;
8    void ** race_flag;
9    inline dlink(select_node);
10};
11P9_EMBEDDED( select_node, dlink(select_node) )
12
13void ?{}( select_node & this ) {
14    this.blocked_thread = 0p;
15    this.race_flag = 0p;
16}
17
18void ?{}( select_node & this, thread$ * blocked_thread ) {
19    this.blocked_thread = blocked_thread;
20    this.race_flag = 0p;
21}
22
23void ?{}( select_node & this, thread$ * blocked_thread, void ** race_flag ) {
24    this.blocked_thread = blocked_thread;
25    this.race_flag = race_flag;
26}
27
28void ^?{}( select_node & this ) {}
29
30
31//-----------------------------------------------------------------------------
32// is_selectable
33trait 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
44static 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}
Note: See TracBrowser for help on using the repository browser.