[629c95a] | 1 | //
|
---|
| 2 | // Cforall Version 1.0.0 Copyright (C) 2021 University of Waterloo
|
---|
| 3 | //
|
---|
| 4 | // The contents of this file are covered under the licence agreement in the
|
---|
| 5 | // file "LICENCE" distributed with Cforall.
|
---|
| 6 | //
|
---|
| 7 | // channel.hfa -- LIBCFATHREAD
|
---|
| 8 | // Runtime locks that used with the runtime thread system.
|
---|
| 9 | //
|
---|
| 10 | // Author : Colby Alexander Parsons
|
---|
| 11 | // Created On : Thu Jan 21 19:46:50 2022
|
---|
| 12 | // Last Modified By :
|
---|
| 13 | // Last Modified On :
|
---|
| 14 | // Update Count :
|
---|
| 15 | //
|
---|
| 16 |
|
---|
[5e4a830] | 17 | #pragma once
|
---|
| 18 |
|
---|
[4a962d8] | 19 | #include <locks.hfa>
|
---|
[d30e3eb] | 20 | #include <list.hfa>
|
---|
[beeff61e] | 21 | #include "select.hfa"
|
---|
[a45e21c] | 22 |
|
---|
| 23 | // returns true if woken due to shutdown
|
---|
| 24 | // blocks thread on list and releases passed lock
|
---|
[beeff61e] | 25 | static inline bool block( dlist( select_node ) & queue, void * elem_ptr, go_mutex & lock ) {
|
---|
| 26 | select_node sn{ active_thread(), elem_ptr };
|
---|
| 27 | insert_last( queue, sn );
|
---|
[a45e21c] | 28 | unlock( lock );
|
---|
| 29 | park();
|
---|
[beeff61e] | 30 | return sn.extra == 0p;
|
---|
| 31 | }
|
---|
| 32 |
|
---|
| 33 | // Waituntil support (un)register_select helper routine
|
---|
| 34 | // Sets select node avail if not special OR case and then unlocks
|
---|
| 35 | static inline void __set_avail_then_unlock( select_node & node, go_mutex & mutex_lock ) {
|
---|
| 36 | if ( node.park_counter ) __make_select_node_available( node );
|
---|
| 37 | unlock( mutex_lock );
|
---|
[a45e21c] | 38 | }
|
---|
| 39 |
|
---|
| 40 | // void * used for some fields since exceptions don't work with parametric polymorphism currently
|
---|
| 41 | exception channel_closed {
|
---|
| 42 | // on failed insert elem is a ptr to the element attempting to be inserted
|
---|
| 43 | // on failed remove elem ptr is 0p
|
---|
| 44 | // on resumption of a failed insert this elem will be inserted
|
---|
| 45 | // so a user may modify it in the resumption handler
|
---|
| 46 | void * elem;
|
---|
| 47 |
|
---|
| 48 | // pointer to chan that is closed
|
---|
| 49 | void * closed_chan;
|
---|
| 50 | };
|
---|
| 51 | vtable(channel_closed) channel_closed_vt;
|
---|
| 52 |
|
---|
[ded6c2a6] | 53 | static inline bool is_insert( channel_closed & e ) { return e.elem != 0p; }
|
---|
| 54 | static inline bool is_remove( channel_closed & e ) { return e.elem == 0p; }
|
---|
[3eeeb88] | 55 |
|
---|
[a45e21c] | 56 | // #define CHAN_STATS // define this to get channel stats printed in dtor
|
---|
| 57 |
|
---|
[4a962d8] | 58 | forall( T ) {
|
---|
[d30e3eb] | 59 |
|
---|
[a45e21c] | 60 | struct __attribute__((aligned(128))) channel {
|
---|
| 61 | size_t size, front, back, count;
|
---|
[4a962d8] | 62 | T * buffer;
|
---|
[beeff61e] | 63 | dlist( select_node ) prods, cons; // lists of blocked threads
|
---|
[629c95a] | 64 | go_mutex mutex_lock; // MX lock
|
---|
| 65 | bool closed; // indicates channel close/open
|
---|
[a45e21c] | 66 | #ifdef CHAN_STATS
|
---|
[88b49bb] | 67 | size_t p_blocks, p_ops, c_blocks, c_ops; // counts total ops and ops resulting in a blocked thd
|
---|
[a45e21c] | 68 | #endif
|
---|
[4a962d8] | 69 | };
|
---|
[7a2c6b18] | 70 | static inline void ?{}( channel(T) & this, channel(T) this2 ) = void;
|
---|
| 71 | static inline void ?=?( channel(T) & this, channel(T) this2 ) = void;
|
---|
[4a962d8] | 72 |
|
---|
| 73 | static inline void ?{}( channel(T) &c, size_t _size ) with(c) {
|
---|
| 74 | size = _size;
|
---|
| 75 | front = back = count = 0;
|
---|
[beeff61e] | 76 | if ( size != 0 ) buffer = aalloc( size );
|
---|
[4a962d8] | 77 | prods{};
|
---|
| 78 | cons{};
|
---|
| 79 | mutex_lock{};
|
---|
[a45e21c] | 80 | closed = false;
|
---|
| 81 | #ifdef CHAN_STATS
|
---|
[88b49bb] | 82 | p_blocks = 0;
|
---|
| 83 | p_ops = 0;
|
---|
| 84 | c_blocks = 0;
|
---|
| 85 | c_ops = 0;
|
---|
[a45e21c] | 86 | #endif
|
---|
[4a962d8] | 87 | }
|
---|
| 88 |
|
---|
| 89 | static inline void ?{}( channel(T) &c ){ ((channel(T) &)c){ 0 }; }
|
---|
[a45e21c] | 90 | static inline void ^?{}( channel(T) &c ) with(c) {
|
---|
| 91 | #ifdef CHAN_STATS
|
---|
[88b49bb] | 92 | printf("Channel %p Blocks: %lu,\t\tOperations: %lu,\t%.2f%% of ops blocked\n", &c, p_blocks + c_blocks, p_ops + c_ops, ((double)p_blocks + c_blocks)/(p_ops + c_ops) * 100);
|
---|
| 93 | printf("Channel %p Consumer Blocks: %lu,\tConsumer Ops: %lu,\t%.2f%% of Consumer ops blocked\n", &c, p_blocks, p_ops, ((double)p_blocks)/p_ops * 100);
|
---|
| 94 | printf("Channel %p Producer Blocks: %lu,\tProducer Ops: %lu,\t%.2f%% of Producer ops blocked\n", &c, c_blocks, c_ops, ((double)c_blocks)/c_ops * 100);
|
---|
[a45e21c] | 95 | #endif
|
---|
[88b49bb] | 96 | verifyf( __handle_waituntil_OR( cons ) || __handle_waituntil_OR( prods ) || cons`isEmpty && prods`isEmpty,
|
---|
| 97 | "Attempted to delete channel with waiting threads (Deadlock).\n" );
|
---|
[beeff61e] | 98 | if ( size != 0 ) delete( buffer );
|
---|
[a45e21c] | 99 | }
|
---|
[5908fb4] | 100 | static inline size_t get_count( channel(T) & chan ) with(chan) { return __atomic_load_n( &count, __ATOMIC_RELAXED ); }
|
---|
| 101 | static inline size_t get_size( channel(T) & chan ) with(chan) { return __atomic_load_n( &size, __ATOMIC_RELAXED ); }
|
---|
[d30e3eb] | 102 | static inline bool has_waiters( channel(T) & chan ) with(chan) { return !cons`isEmpty || !prods`isEmpty; }
|
---|
| 103 | static inline bool has_waiting_consumers( channel(T) & chan ) with(chan) { return !cons`isEmpty; }
|
---|
| 104 | static inline bool has_waiting_producers( channel(T) & chan ) with(chan) { return !prods`isEmpty; }
|
---|
[4a962d8] | 105 |
|
---|
[a45e21c] | 106 | // closes the channel and notifies all blocked threads
|
---|
| 107 | static inline void close( channel(T) & chan ) with(chan) {
|
---|
| 108 | lock( mutex_lock );
|
---|
| 109 | closed = true;
|
---|
| 110 |
|
---|
| 111 | // flush waiting consumers and producers
|
---|
| 112 | while ( has_waiting_consumers( chan ) ) {
|
---|
[beeff61e] | 113 | if( !__handle_waituntil_OR( cons ) ) // ensure we only signal special OR case threads when they win the race
|
---|
| 114 | break; // if __handle_waituntil_OR returns false cons is empty so break
|
---|
| 115 | cons`first.extra = 0p;
|
---|
[a45e21c] | 116 | wake_one( cons );
|
---|
| 117 | }
|
---|
| 118 | while ( has_waiting_producers( chan ) ) {
|
---|
[beeff61e] | 119 | if( !__handle_waituntil_OR( prods ) ) // ensure we only signal special OR case threads when they win the race
|
---|
| 120 | break; // if __handle_waituntil_OR returns false prods is empty so break
|
---|
| 121 | prods`first.extra = 0p;
|
---|
[a45e21c] | 122 | wake_one( prods );
|
---|
| 123 | }
|
---|
| 124 | unlock(mutex_lock);
|
---|
| 125 | }
|
---|
| 126 |
|
---|
| 127 | static inline void is_closed( channel(T) & chan ) with(chan) { return closed; }
|
---|
| 128 |
|
---|
[beeff61e] | 129 | // used to hand an element to a blocked consumer and signal it
|
---|
| 130 | static inline void __cons_handoff( channel(T) & chan, T & elem ) with(chan) {
|
---|
| 131 | memcpy( cons`first.extra, (void *)&elem, sizeof(T) ); // do waiting consumer work
|
---|
[02c5880] | 132 | __atomic_thread_fence( __ATOMIC_SEQ_CST );
|
---|
[beeff61e] | 133 | wake_one( cons );
|
---|
| 134 | }
|
---|
| 135 |
|
---|
| 136 | // used to hand an element to a blocked producer and signal it
|
---|
| 137 | static inline void __prods_handoff( channel(T) & chan, T & retval ) with(chan) {
|
---|
| 138 | memcpy( (void *)&retval, prods`first.extra, sizeof(T) );
|
---|
[02c5880] | 139 | __atomic_thread_fence( __ATOMIC_SEQ_CST );
|
---|
[beeff61e] | 140 | wake_one( prods );
|
---|
| 141 | }
|
---|
| 142 |
|
---|
[a45e21c] | 143 | static inline void flush( channel(T) & chan, T elem ) with(chan) {
|
---|
| 144 | lock( mutex_lock );
|
---|
| 145 | while ( count == 0 && !cons`isEmpty ) {
|
---|
[beeff61e] | 146 | __cons_handoff( chan, elem );
|
---|
[a45e21c] | 147 | }
|
---|
| 148 | unlock( mutex_lock );
|
---|
| 149 | }
|
---|
| 150 |
|
---|
| 151 | // handles buffer insert
|
---|
| 152 | static inline void __buf_insert( channel(T) & chan, T & elem ) with(chan) {
|
---|
[beeff61e] | 153 | memcpy( (void *)&buffer[back], (void *)&elem, sizeof(T) );
|
---|
[4a962d8] | 154 | count += 1;
|
---|
| 155 | back++;
|
---|
| 156 | if ( back == size ) back = 0;
|
---|
| 157 | }
|
---|
| 158 |
|
---|
[a45e21c] | 159 | // needed to avoid an extra copy in closed case
|
---|
| 160 | static inline bool __internal_try_insert( channel(T) & chan, T & elem ) with(chan) {
|
---|
| 161 | lock( mutex_lock );
|
---|
| 162 | #ifdef CHAN_STATS
|
---|
[88b49bb] | 163 | p_ops++;
|
---|
[a45e21c] | 164 | #endif
|
---|
[beeff61e] | 165 |
|
---|
| 166 | ConsEmpty: if ( !cons`isEmpty ) {
|
---|
| 167 | if ( !__handle_waituntil_OR( cons ) ) break ConsEmpty;
|
---|
| 168 | __cons_handoff( chan, elem );
|
---|
| 169 | unlock( mutex_lock );
|
---|
| 170 | return true;
|
---|
| 171 | }
|
---|
| 172 |
|
---|
[a45e21c] | 173 | if ( count == size ) { unlock( mutex_lock ); return false; }
|
---|
[beeff61e] | 174 |
|
---|
| 175 | __buf_insert( chan, elem );
|
---|
[a45e21c] | 176 | unlock( mutex_lock );
|
---|
| 177 | return true;
|
---|
| 178 | }
|
---|
| 179 |
|
---|
| 180 | // attempts a nonblocking insert
|
---|
| 181 | // returns true if insert was successful, false otherwise
|
---|
| 182 | static inline bool try_insert( channel(T) & chan, T elem ) { return __internal_try_insert( chan, elem ); }
|
---|
| 183 |
|
---|
| 184 | // handles closed case of insert routine
|
---|
| 185 | static inline void __closed_insert( channel(T) & chan, T & elem ) with(chan) {
|
---|
[beeff61e] | 186 | channel_closed except{ &channel_closed_vt, &elem, &chan };
|
---|
[a45e21c] | 187 | throwResume except; // throw closed resumption
|
---|
| 188 | if ( !__internal_try_insert( chan, elem ) ) throw except; // if try to insert fails (would block), throw termination
|
---|
[d30e3eb] | 189 | }
|
---|
[4a962d8] | 190 |
|
---|
[42b739d7] | 191 | static inline void insert( channel(T) & chan, T elem ) with(chan) {
|
---|
[a45e21c] | 192 | // check for close before acquire mx
|
---|
| 193 | if ( unlikely(closed) ) {
|
---|
| 194 | __closed_insert( chan, elem );
|
---|
| 195 | return;
|
---|
| 196 | }
|
---|
| 197 |
|
---|
[4a962d8] | 198 | lock( mutex_lock );
|
---|
| 199 |
|
---|
[a45e21c] | 200 | #ifdef CHAN_STATS
|
---|
[88b49bb] | 201 | if ( !closed ) p_ops++;
|
---|
[a45e21c] | 202 | #endif
|
---|
| 203 |
|
---|
| 204 | // if closed handle
|
---|
| 205 | if ( unlikely(closed) ) {
|
---|
| 206 | unlock( mutex_lock );
|
---|
| 207 | __closed_insert( chan, elem );
|
---|
| 208 | return;
|
---|
| 209 | }
|
---|
| 210 |
|
---|
[beeff61e] | 211 | // buffer count must be zero if cons are blocked (also handles zero-size case)
|
---|
| 212 | ConsEmpty: if ( !cons`isEmpty ) {
|
---|
| 213 | if ( !__handle_waituntil_OR( cons ) ) break ConsEmpty;
|
---|
| 214 | __cons_handoff( chan, elem );
|
---|
[5c931e0] | 215 | unlock( mutex_lock );
|
---|
[beeff61e] | 216 | return;
|
---|
[5c931e0] | 217 | }
|
---|
| 218 |
|
---|
[4a962d8] | 219 | // wait if buffer is full, work will be completed by someone else
|
---|
[d30e3eb] | 220 | if ( count == size ) {
|
---|
[a45e21c] | 221 | #ifdef CHAN_STATS
|
---|
[88b49bb] | 222 | p_blocks++;
|
---|
[a45e21c] | 223 | #endif
|
---|
| 224 |
|
---|
| 225 | // check for if woken due to close
|
---|
| 226 | if ( unlikely( block( prods, &elem, mutex_lock ) ) )
|
---|
| 227 | __closed_insert( chan, elem );
|
---|
[4a962d8] | 228 | return;
|
---|
| 229 | } // if
|
---|
| 230 |
|
---|
[beeff61e] | 231 | __buf_insert( chan, elem );
|
---|
[4a962d8] | 232 | unlock( mutex_lock );
|
---|
[a45e21c] | 233 | }
|
---|
[4a962d8] | 234 |
|
---|
[a45e21c] | 235 | // does the buffer remove and potentially does waiting producer work
|
---|
| 236 | static inline void __do_remove( channel(T) & chan, T & retval ) with(chan) {
|
---|
[beeff61e] | 237 | memcpy( (void *)&retval, (void *)&buffer[front], sizeof(T) );
|
---|
| 238 | count -= 1;
|
---|
| 239 | front = (front + 1) % size;
|
---|
[d30e3eb] | 240 | if (count == size - 1 && !prods`isEmpty ) {
|
---|
[beeff61e] | 241 | if ( !__handle_waituntil_OR( prods ) ) return;
|
---|
| 242 | __buf_insert( chan, *(T *)prods`first.extra ); // do waiting producer work
|
---|
[d30e3eb] | 243 | wake_one( prods );
|
---|
| 244 | }
|
---|
[4a962d8] | 245 | }
|
---|
[0e16a2d] | 246 |
|
---|
[a45e21c] | 247 | // needed to avoid an extra copy in closed case and single return val case
|
---|
| 248 | static inline bool __internal_try_remove( channel(T) & chan, T & retval ) with(chan) {
|
---|
[0e16a2d] | 249 | lock( mutex_lock );
|
---|
[a45e21c] | 250 | #ifdef CHAN_STATS
|
---|
[88b49bb] | 251 | c_ops++;
|
---|
[a45e21c] | 252 | #endif
|
---|
[beeff61e] | 253 |
|
---|
| 254 | ZeroSize: if ( size == 0 && !prods`isEmpty ) {
|
---|
| 255 | if ( !__handle_waituntil_OR( prods ) ) break ZeroSize;
|
---|
| 256 | __prods_handoff( chan, retval );
|
---|
| 257 | unlock( mutex_lock );
|
---|
| 258 | return true;
|
---|
| 259 | }
|
---|
| 260 |
|
---|
[a45e21c] | 261 | if ( count == 0 ) { unlock( mutex_lock ); return false; }
|
---|
[beeff61e] | 262 |
|
---|
[a45e21c] | 263 | __do_remove( chan, retval );
|
---|
[0e16a2d] | 264 | unlock( mutex_lock );
|
---|
[a45e21c] | 265 | return true;
|
---|
[0e16a2d] | 266 | }
|
---|
| 267 |
|
---|
[a45e21c] | 268 | // attempts a nonblocking remove
|
---|
| 269 | // returns [T, true] if insert was successful
|
---|
| 270 | // returns [T, false] if insert was successful (T uninit)
|
---|
| 271 | static inline [T, bool] try_remove( channel(T) & chan ) {
|
---|
[0e16a2d] | 272 | T retval;
|
---|
[beeff61e] | 273 | bool success = __internal_try_remove( chan, retval );
|
---|
| 274 | return [ retval, success ];
|
---|
[a45e21c] | 275 | }
|
---|
[0e16a2d] | 276 |
|
---|
[beeff61e] | 277 | static inline T try_remove( channel(T) & chan ) {
|
---|
[a45e21c] | 278 | T retval;
|
---|
| 279 | __internal_try_remove( chan, retval );
|
---|
[0e16a2d] | 280 | return retval;
|
---|
| 281 | }
|
---|
| 282 |
|
---|
[a45e21c] | 283 | // handles closed case of insert routine
|
---|
| 284 | static inline void __closed_remove( channel(T) & chan, T & retval ) with(chan) {
|
---|
[beeff61e] | 285 | channel_closed except{ &channel_closed_vt, 0p, &chan };
|
---|
[a45e21c] | 286 | throwResume except; // throw resumption
|
---|
| 287 | if ( !__internal_try_remove( chan, retval ) ) throw except; // if try to remove fails (would block), throw termination
|
---|
[0e16a2d] | 288 | }
|
---|
| 289 |
|
---|
[a45e21c] | 290 | static inline T remove( channel(T) & chan ) with(chan) {
|
---|
| 291 | T retval;
|
---|
| 292 | if ( unlikely(closed) ) {
|
---|
| 293 | __closed_remove( chan, retval );
|
---|
| 294 | return retval;
|
---|
| 295 | }
|
---|
| 296 | lock( mutex_lock );
|
---|
[0e16a2d] | 297 |
|
---|
[a45e21c] | 298 | #ifdef CHAN_STATS
|
---|
[88b49bb] | 299 | if ( !closed ) c_ops++;
|
---|
[a45e21c] | 300 | #endif
|
---|
[0e16a2d] | 301 |
|
---|
[a45e21c] | 302 | if ( unlikely(closed) ) {
|
---|
| 303 | unlock( mutex_lock );
|
---|
| 304 | __closed_remove( chan, retval );
|
---|
| 305 | return retval;
|
---|
| 306 | }
|
---|
[0e16a2d] | 307 |
|
---|
[a45e21c] | 308 | // have to check for the zero size channel case
|
---|
[beeff61e] | 309 | ZeroSize: if ( size == 0 && !prods`isEmpty ) {
|
---|
| 310 | if ( !__handle_waituntil_OR( prods ) ) break ZeroSize;
|
---|
| 311 | __prods_handoff( chan, retval );
|
---|
[a45e21c] | 312 | unlock( mutex_lock );
|
---|
| 313 | return retval;
|
---|
| 314 | }
|
---|
[0e16a2d] | 315 |
|
---|
[a45e21c] | 316 | // wait if buffer is empty, work will be completed by someone else
|
---|
[beeff61e] | 317 | if ( count == 0 ) {
|
---|
[a45e21c] | 318 | #ifdef CHAN_STATS
|
---|
[88b49bb] | 319 | c_blocks++;
|
---|
[a45e21c] | 320 | #endif
|
---|
| 321 | // check for if woken due to close
|
---|
| 322 | if ( unlikely( block( cons, &retval, mutex_lock ) ) )
|
---|
| 323 | __closed_remove( chan, retval );
|
---|
| 324 | return retval;
|
---|
| 325 | }
|
---|
[0e16a2d] | 326 |
|
---|
| 327 | // Remove from buffer
|
---|
[a45e21c] | 328 | __do_remove( chan, retval );
|
---|
[0e16a2d] | 329 | unlock( mutex_lock );
|
---|
| 330 | return retval;
|
---|
| 331 | }
|
---|
[c44705c] | 332 | static inline void remove( channel(T) & chan ) { T elem = (T)remove( chan ); }
|
---|
| 333 |
|
---|
[ca22a7c] | 334 |
|
---|
[a0b59ed] | 335 | ///////////////////////////////////////////////////////////////////////////////////////////
|
---|
| 336 | // The following is Go-style operator support for channels
|
---|
| 337 | ///////////////////////////////////////////////////////////////////////////////////////////
|
---|
| 338 |
|
---|
[ca22a7c] | 339 | static inline void ?<<?( channel(T) & chan, T elem ) { insert( chan, elem ); }
|
---|
[bf55f32] | 340 | static inline void ?<<?( T & ret, channel(T) & chan ) { ret = remove( chan ); }
|
---|
[beeff61e] | 341 |
|
---|
| 342 | ///////////////////////////////////////////////////////////////////////////////////////////
|
---|
| 343 | // The following is support for waituntil (select) statements
|
---|
| 344 | ///////////////////////////////////////////////////////////////////////////////////////////
|
---|
| 345 | static inline bool unregister_chan( channel(T) & chan, select_node & node ) with(chan) {
|
---|
[88b49bb] | 346 | if ( !node`isListed && !node.park_counter ) return false; // handle special OR case
|
---|
[beeff61e] | 347 | lock( mutex_lock );
|
---|
| 348 | if ( node`isListed ) { // op wasn't performed
|
---|
| 349 | remove( node );
|
---|
| 350 | unlock( mutex_lock );
|
---|
| 351 | return false;
|
---|
| 352 | }
|
---|
| 353 | unlock( mutex_lock );
|
---|
| 354 |
|
---|
[00b046f] | 355 | // only return true when not special OR case and status is SAT
|
---|
| 356 | return !node.park_counter ? false : *node.clause_status == __SELECT_SAT;
|
---|
[beeff61e] | 357 | }
|
---|
| 358 |
|
---|
[6f774be] | 359 | // special case of __handle_waituntil_OR, that does some work to avoid starvation/deadlock case
|
---|
| 360 | static inline bool __handle_pending( dlist( select_node ) & queue, select_node & mine ) {
|
---|
| 361 | while ( !queue`isEmpty ) {
|
---|
| 362 | // if node not a special OR case or if we win the special OR case race break
|
---|
| 363 | if ( !queue`first.clause_status || queue`first.park_counter || __pending_set_other( queue`first, mine, ((unsigned long int)(&(queue`first))) ) )
|
---|
| 364 | return true;
|
---|
| 365 |
|
---|
| 366 | // our node lost the race when toggling in __pending_set_other
|
---|
| 367 | if ( *mine.clause_status != __SELECT_PENDING )
|
---|
| 368 | return false;
|
---|
| 369 |
|
---|
| 370 | // otherwise we lost the special OR race so discard node
|
---|
| 371 | try_pop_front( queue );
|
---|
| 372 | }
|
---|
| 373 | return false;
|
---|
| 374 | }
|
---|
| 375 |
|
---|
[beeff61e] | 376 | // type used by select statement to capture a chan read as the selected operation
|
---|
| 377 | struct chan_read {
|
---|
[7a2c6b18] | 378 | T * ret;
|
---|
| 379 | channel(T) * chan;
|
---|
[beeff61e] | 380 | };
|
---|
[bf55f32] | 381 | __CFA_SELECT_GET_TYPE( chan_read(T) );
|
---|
[beeff61e] | 382 |
|
---|
[7a2c6b18] | 383 | static inline void ?{}( chan_read(T) & cr, channel(T) * chan, T * ret ) {
|
---|
| 384 | cr.chan = chan;
|
---|
| 385 | cr.ret = ret;
|
---|
[beeff61e] | 386 | }
|
---|
[7a2c6b18] | 387 | static inline chan_read(T) ?<<?( T & ret, channel(T) & chan ) { chan_read(T) cr{ &chan, &ret }; return cr; }
|
---|
[beeff61e] | 388 |
|
---|
[7a2c6b18] | 389 | static inline void __handle_select_closed_read( chan_read(T) & this, select_node & node ) with(*this.chan, this) {
|
---|
| 390 | __closed_remove( *chan, *ret );
|
---|
[beeff61e] | 391 | // if we get here then the insert succeeded
|
---|
| 392 | __make_select_node_available( node );
|
---|
| 393 | }
|
---|
| 394 |
|
---|
[7a2c6b18] | 395 | static inline bool register_select( chan_read(T) & this, select_node & node ) with(*this.chan, this) {
|
---|
[beeff61e] | 396 | lock( mutex_lock );
|
---|
[7a2c6b18] | 397 | node.extra = ret; // set .extra so that if it == 0p later in on_selected it is due to channel close
|
---|
[beeff61e] | 398 |
|
---|
| 399 | #ifdef CHAN_STATS
|
---|
[88b49bb] | 400 | if ( !closed ) c_ops++;
|
---|
[beeff61e] | 401 | #endif
|
---|
| 402 |
|
---|
[c4f411e] | 403 | if ( !node.park_counter ) {
|
---|
| 404 | // are we special case OR and front of cons is also special case OR
|
---|
| 405 | if ( !unlikely(closed) && !prods`isEmpty && prods`first.clause_status && !prods`first.park_counter ) {
|
---|
| 406 | if ( !__make_select_node_pending( node ) ) {
|
---|
| 407 | unlock( mutex_lock );
|
---|
| 408 | return false;
|
---|
| 409 | }
|
---|
[6f774be] | 410 |
|
---|
| 411 | if ( __handle_pending( prods, node ) ) {
|
---|
[7a2c6b18] | 412 | __prods_handoff( *chan, *ret );
|
---|
[629c95a] | 413 | __make_select_node_sat( node ); // need to to mark SAT now that we know operation is done or else threads could get stuck in __mark_select_node
|
---|
[c4f411e] | 414 | unlock( mutex_lock );
|
---|
| 415 | return true;
|
---|
| 416 | }
|
---|
[6f774be] | 417 | if ( *node.clause_status == __SELECT_PENDING )
|
---|
| 418 | __make_select_node_unsat( node );
|
---|
[c4f411e] | 419 | }
|
---|
| 420 | // check if we can complete operation. If so race to establish winner in special OR case
|
---|
| 421 | if ( count != 0 || !prods`isEmpty || unlikely(closed) ) {
|
---|
| 422 | if ( !__make_select_node_available( node ) ) { // we didn't win the race so give up on registering
|
---|
| 423 | unlock( mutex_lock );
|
---|
| 424 | return false;
|
---|
| 425 | }
|
---|
[beeff61e] | 426 | }
|
---|
| 427 | }
|
---|
| 428 |
|
---|
| 429 | if ( unlikely(closed) ) {
|
---|
| 430 | unlock( mutex_lock );
|
---|
| 431 | __handle_select_closed_read( this, node );
|
---|
| 432 | return true;
|
---|
| 433 | }
|
---|
| 434 |
|
---|
| 435 | // have to check for the zero size channel case
|
---|
| 436 | ZeroSize: if ( size == 0 && !prods`isEmpty ) {
|
---|
| 437 | if ( !__handle_waituntil_OR( prods ) ) break ZeroSize;
|
---|
[7a2c6b18] | 438 | __prods_handoff( *chan, *ret );
|
---|
[beeff61e] | 439 | __set_avail_then_unlock( node, mutex_lock );
|
---|
| 440 | return true;
|
---|
| 441 | }
|
---|
| 442 |
|
---|
| 443 | // wait if buffer is empty, work will be completed by someone else
|
---|
| 444 | if ( count == 0 ) {
|
---|
| 445 | #ifdef CHAN_STATS
|
---|
[88b49bb] | 446 | c_blocks++;
|
---|
[beeff61e] | 447 | #endif
|
---|
| 448 |
|
---|
| 449 | insert_last( cons, node );
|
---|
| 450 | unlock( mutex_lock );
|
---|
| 451 | return false;
|
---|
| 452 | }
|
---|
| 453 |
|
---|
| 454 | // Remove from buffer
|
---|
[7a2c6b18] | 455 | __do_remove( *chan, *ret );
|
---|
[beeff61e] | 456 | __set_avail_then_unlock( node, mutex_lock );
|
---|
| 457 | return true;
|
---|
| 458 | }
|
---|
[7a2c6b18] | 459 | static inline bool unregister_select( chan_read(T) & this, select_node & node ) { return unregister_chan( *this.chan, node ); }
|
---|
[b93bf85] | 460 | static inline bool on_selected( chan_read(T) & this, select_node & node ) with(this) {
|
---|
| 461 | if ( unlikely(node.extra == 0p) ) {
|
---|
[7a2c6b18] | 462 | if ( !exception_in_flight() ) __closed_remove( *chan, *ret ); // check if woken up due to closed channel
|
---|
[b93bf85] | 463 | else return false;
|
---|
| 464 | }
|
---|
[beeff61e] | 465 | // This is only reachable if not closed or closed exception was handled
|
---|
[b93bf85] | 466 | return true;
|
---|
[beeff61e] | 467 | }
|
---|
| 468 |
|
---|
[c44705c] | 469 | // type used by select statement to capture a chan read as the selected operation that doesn't have a param to read to
|
---|
| 470 | struct chan_read_no_ret {
|
---|
[7a2c6b18] | 471 | T retval;
|
---|
| 472 | chan_read( T ) c_read;
|
---|
[c44705c] | 473 | };
|
---|
| 474 | __CFA_SELECT_GET_TYPE( chan_read_no_ret(T) );
|
---|
| 475 |
|
---|
| 476 | static inline void ?{}( chan_read_no_ret(T) & this, channel(T) & chan ) {
|
---|
[7a2c6b18] | 477 | this.c_read{ &chan, &this.retval };
|
---|
| 478 | }
|
---|
| 479 |
|
---|
| 480 | static inline chan_read_no_ret(T) remove( channel(T) & chan ) { chan_read_no_ret(T) c_read{ chan }; return c_read; }
|
---|
| 481 | static inline bool register_select( chan_read_no_ret(T) & this, select_node & node ) {
|
---|
| 482 | this.c_read.ret = &this.retval;
|
---|
| 483 | return register_select( this.c_read, node );
|
---|
[c44705c] | 484 | }
|
---|
[7a2c6b18] | 485 | static inline bool unregister_select( chan_read_no_ret(T) & this, select_node & node ) { return unregister_select( this.c_read, node ); }
|
---|
| 486 | static inline bool on_selected( chan_read_no_ret(T) & this, select_node & node ) { return on_selected( this.c_read, node ); }
|
---|
[c44705c] | 487 |
|
---|
[beeff61e] | 488 | // type used by select statement to capture a chan write as the selected operation
|
---|
| 489 | struct chan_write {
|
---|
| 490 | T elem;
|
---|
[7a2c6b18] | 491 | channel(T) * chan;
|
---|
[beeff61e] | 492 | };
|
---|
[bf55f32] | 493 | __CFA_SELECT_GET_TYPE( chan_write(T) );
|
---|
[beeff61e] | 494 |
|
---|
[7a2c6b18] | 495 | static inline void ?{}( chan_write(T) & cw, channel(T) * chan, T elem ) {
|
---|
| 496 | cw.chan = chan;
|
---|
[beeff61e] | 497 | memcpy( (void *)&cw.elem, (void *)&elem, sizeof(T) );
|
---|
| 498 | }
|
---|
[7a2c6b18] | 499 | static inline chan_write(T) ?<<?( channel(T) & chan, T elem ) { chan_write(T) cw{ &chan, elem }; return cw; }
|
---|
| 500 | static inline chan_write(T) insert( T elem, channel(T) & chan) { chan_write(T) cw{ &chan, elem }; return cw; }
|
---|
[beeff61e] | 501 |
|
---|
[7a2c6b18] | 502 | static inline void __handle_select_closed_write( chan_write(T) & this, select_node & node ) with(*this.chan, this) {
|
---|
| 503 | __closed_insert( *chan, elem );
|
---|
[beeff61e] | 504 | // if we get here then the insert succeeded
|
---|
| 505 | __make_select_node_available( node );
|
---|
| 506 | }
|
---|
| 507 |
|
---|
[7a2c6b18] | 508 | static inline bool register_select( chan_write(T) & this, select_node & node ) with(*this.chan, this) {
|
---|
[beeff61e] | 509 | lock( mutex_lock );
|
---|
| 510 | node.extra = &elem; // set .extra so that if it == 0p later in on_selected it is due to channel close
|
---|
| 511 |
|
---|
| 512 | #ifdef CHAN_STATS
|
---|
[88b49bb] | 513 | if ( !closed ) p_ops++;
|
---|
[beeff61e] | 514 | #endif
|
---|
| 515 |
|
---|
[c4f411e] | 516 | // special OR case handling
|
---|
| 517 | if ( !node.park_counter ) {
|
---|
| 518 | // are we special case OR and front of cons is also special case OR
|
---|
| 519 | if ( !unlikely(closed) && !cons`isEmpty && cons`first.clause_status && !cons`first.park_counter ) {
|
---|
| 520 | if ( !__make_select_node_pending( node ) ) {
|
---|
| 521 | unlock( mutex_lock );
|
---|
| 522 | return false;
|
---|
| 523 | }
|
---|
[6f774be] | 524 |
|
---|
| 525 | if ( __handle_pending( cons, node ) ) {
|
---|
[7a2c6b18] | 526 | __cons_handoff( *chan, elem );
|
---|
[629c95a] | 527 | __make_select_node_sat( node ); // need to to mark SAT now that we know operation is done or else threads could get stuck in __mark_select_node
|
---|
[c4f411e] | 528 | unlock( mutex_lock );
|
---|
| 529 | return true;
|
---|
| 530 | }
|
---|
[6f774be] | 531 | if ( *node.clause_status == __SELECT_PENDING )
|
---|
| 532 | __make_select_node_unsat( node );
|
---|
[c4f411e] | 533 | }
|
---|
| 534 | // check if we can complete operation. If so race to establish winner in special OR case
|
---|
| 535 | if ( count != size || !cons`isEmpty || unlikely(closed) ) {
|
---|
| 536 | if ( !__make_select_node_available( node ) ) { // we didn't win the race so give up on registering
|
---|
| 537 | unlock( mutex_lock );
|
---|
| 538 | return false;
|
---|
| 539 | }
|
---|
[beeff61e] | 540 | }
|
---|
| 541 | }
|
---|
| 542 |
|
---|
| 543 | // if closed handle
|
---|
| 544 | if ( unlikely(closed) ) {
|
---|
| 545 | unlock( mutex_lock );
|
---|
| 546 | __handle_select_closed_write( this, node );
|
---|
| 547 | return true;
|
---|
| 548 | }
|
---|
| 549 |
|
---|
| 550 | // handle blocked consumer case via handoff (buffer is implicitly empty)
|
---|
| 551 | ConsEmpty: if ( !cons`isEmpty ) {
|
---|
[cb69fba] | 552 | if ( !__handle_waituntil_OR( cons ) ) break ConsEmpty;
|
---|
[7a2c6b18] | 553 | __cons_handoff( *chan, elem );
|
---|
[beeff61e] | 554 | __set_avail_then_unlock( node, mutex_lock );
|
---|
| 555 | return true;
|
---|
| 556 | }
|
---|
| 557 |
|
---|
| 558 | // insert node in list if buffer is full, work will be completed by someone else
|
---|
| 559 | if ( count == size ) {
|
---|
| 560 | #ifdef CHAN_STATS
|
---|
[88b49bb] | 561 | p_blocks++;
|
---|
[beeff61e] | 562 | #endif
|
---|
| 563 |
|
---|
| 564 | insert_last( prods, node );
|
---|
| 565 | unlock( mutex_lock );
|
---|
| 566 | return false;
|
---|
| 567 | } // if
|
---|
| 568 |
|
---|
| 569 | // otherwise carry out write either via normal insert
|
---|
[7a2c6b18] | 570 | __buf_insert( *chan, elem );
|
---|
[beeff61e] | 571 | __set_avail_then_unlock( node, mutex_lock );
|
---|
| 572 | return true;
|
---|
| 573 | }
|
---|
[7a2c6b18] | 574 | static inline bool unregister_select( chan_write(T) & this, select_node & node ) { return unregister_chan( *this.chan, node ); }
|
---|
[beeff61e] | 575 |
|
---|
[b93bf85] | 576 | static inline bool on_selected( chan_write(T) & this, select_node & node ) with(this) {
|
---|
| 577 | if ( unlikely(node.extra == 0p) ) {
|
---|
[7a2c6b18] | 578 | if ( !exception_in_flight() ) __closed_insert( *chan, elem ); // check if woken up due to closed channel
|
---|
[b93bf85] | 579 | else return false;
|
---|
| 580 | }
|
---|
[beeff61e] | 581 | // This is only reachable if not closed or closed exception was handled
|
---|
[b93bf85] | 582 | return true;
|
---|
[beeff61e] | 583 | }
|
---|
| 584 |
|
---|
[0e16a2d] | 585 | } // forall( T )
|
---|
[beeff61e] | 586 |
|
---|
| 587 |
|
---|