[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
|
---|
| 132 | wake_one( cons );
|
---|
| 133 | }
|
---|
| 134 |
|
---|
| 135 | // used to hand an element to a blocked producer and signal it
|
---|
| 136 | static inline void __prods_handoff( channel(T) & chan, T & retval ) with(chan) {
|
---|
| 137 | memcpy( (void *)&retval, prods`first.extra, sizeof(T) );
|
---|
| 138 | wake_one( prods );
|
---|
| 139 | }
|
---|
| 140 |
|
---|
[a45e21c] | 141 | static inline void flush( channel(T) & chan, T elem ) with(chan) {
|
---|
| 142 | lock( mutex_lock );
|
---|
| 143 | while ( count == 0 && !cons`isEmpty ) {
|
---|
[beeff61e] | 144 | __cons_handoff( chan, elem );
|
---|
[a45e21c] | 145 | }
|
---|
| 146 | unlock( mutex_lock );
|
---|
| 147 | }
|
---|
| 148 |
|
---|
| 149 | // handles buffer insert
|
---|
| 150 | static inline void __buf_insert( channel(T) & chan, T & elem ) with(chan) {
|
---|
[beeff61e] | 151 | memcpy( (void *)&buffer[back], (void *)&elem, sizeof(T) );
|
---|
[4a962d8] | 152 | count += 1;
|
---|
| 153 | back++;
|
---|
| 154 | if ( back == size ) back = 0;
|
---|
| 155 | }
|
---|
| 156 |
|
---|
[a45e21c] | 157 | // needed to avoid an extra copy in closed case
|
---|
| 158 | static inline bool __internal_try_insert( channel(T) & chan, T & elem ) with(chan) {
|
---|
| 159 | lock( mutex_lock );
|
---|
| 160 | #ifdef CHAN_STATS
|
---|
[88b49bb] | 161 | p_ops++;
|
---|
[a45e21c] | 162 | #endif
|
---|
[beeff61e] | 163 |
|
---|
| 164 | ConsEmpty: if ( !cons`isEmpty ) {
|
---|
| 165 | if ( !__handle_waituntil_OR( cons ) ) break ConsEmpty;
|
---|
| 166 | __cons_handoff( chan, elem );
|
---|
| 167 | unlock( mutex_lock );
|
---|
| 168 | return true;
|
---|
| 169 | }
|
---|
| 170 |
|
---|
[a45e21c] | 171 | if ( count == size ) { unlock( mutex_lock ); return false; }
|
---|
[beeff61e] | 172 |
|
---|
| 173 | __buf_insert( chan, elem );
|
---|
[a45e21c] | 174 | unlock( mutex_lock );
|
---|
| 175 | return true;
|
---|
| 176 | }
|
---|
| 177 |
|
---|
| 178 | // attempts a nonblocking insert
|
---|
| 179 | // returns true if insert was successful, false otherwise
|
---|
| 180 | static inline bool try_insert( channel(T) & chan, T elem ) { return __internal_try_insert( chan, elem ); }
|
---|
| 181 |
|
---|
| 182 | // handles closed case of insert routine
|
---|
| 183 | static inline void __closed_insert( channel(T) & chan, T & elem ) with(chan) {
|
---|
[beeff61e] | 184 | channel_closed except{ &channel_closed_vt, &elem, &chan };
|
---|
[a45e21c] | 185 | throwResume except; // throw closed resumption
|
---|
| 186 | if ( !__internal_try_insert( chan, elem ) ) throw except; // if try to insert fails (would block), throw termination
|
---|
[d30e3eb] | 187 | }
|
---|
[4a962d8] | 188 |
|
---|
[42b739d7] | 189 | static inline void insert( channel(T) & chan, T elem ) with(chan) {
|
---|
[a45e21c] | 190 | // check for close before acquire mx
|
---|
| 191 | if ( unlikely(closed) ) {
|
---|
| 192 | __closed_insert( chan, elem );
|
---|
| 193 | return;
|
---|
| 194 | }
|
---|
| 195 |
|
---|
[4a962d8] | 196 | lock( mutex_lock );
|
---|
| 197 |
|
---|
[a45e21c] | 198 | #ifdef CHAN_STATS
|
---|
[88b49bb] | 199 | if ( !closed ) p_ops++;
|
---|
[a45e21c] | 200 | #endif
|
---|
| 201 |
|
---|
| 202 | // if closed handle
|
---|
| 203 | if ( unlikely(closed) ) {
|
---|
| 204 | unlock( mutex_lock );
|
---|
| 205 | __closed_insert( chan, elem );
|
---|
| 206 | return;
|
---|
| 207 | }
|
---|
| 208 |
|
---|
[beeff61e] | 209 | // buffer count must be zero if cons are blocked (also handles zero-size case)
|
---|
| 210 | ConsEmpty: if ( !cons`isEmpty ) {
|
---|
| 211 | if ( !__handle_waituntil_OR( cons ) ) break ConsEmpty;
|
---|
| 212 | __cons_handoff( chan, elem );
|
---|
[5c931e0] | 213 | unlock( mutex_lock );
|
---|
[beeff61e] | 214 | return;
|
---|
[5c931e0] | 215 | }
|
---|
| 216 |
|
---|
[4a962d8] | 217 | // wait if buffer is full, work will be completed by someone else
|
---|
[d30e3eb] | 218 | if ( count == size ) {
|
---|
[a45e21c] | 219 | #ifdef CHAN_STATS
|
---|
[88b49bb] | 220 | p_blocks++;
|
---|
[a45e21c] | 221 | #endif
|
---|
| 222 |
|
---|
| 223 | // check for if woken due to close
|
---|
| 224 | if ( unlikely( block( prods, &elem, mutex_lock ) ) )
|
---|
| 225 | __closed_insert( chan, elem );
|
---|
[4a962d8] | 226 | return;
|
---|
| 227 | } // if
|
---|
| 228 |
|
---|
[beeff61e] | 229 | __buf_insert( chan, elem );
|
---|
[4a962d8] | 230 | unlock( mutex_lock );
|
---|
[a45e21c] | 231 | }
|
---|
[4a962d8] | 232 |
|
---|
[a45e21c] | 233 | // does the buffer remove and potentially does waiting producer work
|
---|
| 234 | static inline void __do_remove( channel(T) & chan, T & retval ) with(chan) {
|
---|
[beeff61e] | 235 | memcpy( (void *)&retval, (void *)&buffer[front], sizeof(T) );
|
---|
| 236 | count -= 1;
|
---|
| 237 | front = (front + 1) % size;
|
---|
[d30e3eb] | 238 | if (count == size - 1 && !prods`isEmpty ) {
|
---|
[beeff61e] | 239 | if ( !__handle_waituntil_OR( prods ) ) return;
|
---|
| 240 | __buf_insert( chan, *(T *)prods`first.extra ); // do waiting producer work
|
---|
[d30e3eb] | 241 | wake_one( prods );
|
---|
| 242 | }
|
---|
[4a962d8] | 243 | }
|
---|
[0e16a2d] | 244 |
|
---|
[a45e21c] | 245 | // needed to avoid an extra copy in closed case and single return val case
|
---|
| 246 | static inline bool __internal_try_remove( channel(T) & chan, T & retval ) with(chan) {
|
---|
[0e16a2d] | 247 | lock( mutex_lock );
|
---|
[a45e21c] | 248 | #ifdef CHAN_STATS
|
---|
[88b49bb] | 249 | c_ops++;
|
---|
[a45e21c] | 250 | #endif
|
---|
[beeff61e] | 251 |
|
---|
| 252 | ZeroSize: if ( size == 0 && !prods`isEmpty ) {
|
---|
| 253 | if ( !__handle_waituntil_OR( prods ) ) break ZeroSize;
|
---|
| 254 | __prods_handoff( chan, retval );
|
---|
| 255 | unlock( mutex_lock );
|
---|
| 256 | return true;
|
---|
| 257 | }
|
---|
| 258 |
|
---|
[a45e21c] | 259 | if ( count == 0 ) { unlock( mutex_lock ); return false; }
|
---|
[beeff61e] | 260 |
|
---|
[a45e21c] | 261 | __do_remove( chan, retval );
|
---|
[0e16a2d] | 262 | unlock( mutex_lock );
|
---|
[a45e21c] | 263 | return true;
|
---|
[0e16a2d] | 264 | }
|
---|
| 265 |
|
---|
[a45e21c] | 266 | // attempts a nonblocking remove
|
---|
| 267 | // returns [T, true] if insert was successful
|
---|
| 268 | // returns [T, false] if insert was successful (T uninit)
|
---|
| 269 | static inline [T, bool] try_remove( channel(T) & chan ) {
|
---|
[0e16a2d] | 270 | T retval;
|
---|
[beeff61e] | 271 | bool success = __internal_try_remove( chan, retval );
|
---|
| 272 | return [ retval, success ];
|
---|
[a45e21c] | 273 | }
|
---|
[0e16a2d] | 274 |
|
---|
[beeff61e] | 275 | static inline T try_remove( channel(T) & chan ) {
|
---|
[a45e21c] | 276 | T retval;
|
---|
| 277 | __internal_try_remove( chan, retval );
|
---|
[0e16a2d] | 278 | return retval;
|
---|
| 279 | }
|
---|
| 280 |
|
---|
[a45e21c] | 281 | // handles closed case of insert routine
|
---|
| 282 | static inline void __closed_remove( channel(T) & chan, T & retval ) with(chan) {
|
---|
[beeff61e] | 283 | channel_closed except{ &channel_closed_vt, 0p, &chan };
|
---|
[a45e21c] | 284 | throwResume except; // throw resumption
|
---|
| 285 | if ( !__internal_try_remove( chan, retval ) ) throw except; // if try to remove fails (would block), throw termination
|
---|
[0e16a2d] | 286 | }
|
---|
| 287 |
|
---|
[a45e21c] | 288 | static inline T remove( channel(T) & chan ) with(chan) {
|
---|
| 289 | T retval;
|
---|
| 290 | if ( unlikely(closed) ) {
|
---|
| 291 | __closed_remove( chan, retval );
|
---|
| 292 | return retval;
|
---|
| 293 | }
|
---|
| 294 | lock( mutex_lock );
|
---|
[0e16a2d] | 295 |
|
---|
[a45e21c] | 296 | #ifdef CHAN_STATS
|
---|
[88b49bb] | 297 | if ( !closed ) c_ops++;
|
---|
[a45e21c] | 298 | #endif
|
---|
[0e16a2d] | 299 |
|
---|
[a45e21c] | 300 | if ( unlikely(closed) ) {
|
---|
| 301 | unlock( mutex_lock );
|
---|
| 302 | __closed_remove( chan, retval );
|
---|
| 303 | return retval;
|
---|
| 304 | }
|
---|
[0e16a2d] | 305 |
|
---|
[a45e21c] | 306 | // have to check for the zero size channel case
|
---|
[beeff61e] | 307 | ZeroSize: if ( size == 0 && !prods`isEmpty ) {
|
---|
| 308 | if ( !__handle_waituntil_OR( prods ) ) break ZeroSize;
|
---|
| 309 | __prods_handoff( chan, retval );
|
---|
[a45e21c] | 310 | unlock( mutex_lock );
|
---|
| 311 | return retval;
|
---|
| 312 | }
|
---|
[0e16a2d] | 313 |
|
---|
[a45e21c] | 314 | // wait if buffer is empty, work will be completed by someone else
|
---|
[beeff61e] | 315 | if ( count == 0 ) {
|
---|
[a45e21c] | 316 | #ifdef CHAN_STATS
|
---|
[88b49bb] | 317 | c_blocks++;
|
---|
[a45e21c] | 318 | #endif
|
---|
| 319 | // check for if woken due to close
|
---|
| 320 | if ( unlikely( block( cons, &retval, mutex_lock ) ) )
|
---|
| 321 | __closed_remove( chan, retval );
|
---|
| 322 | return retval;
|
---|
| 323 | }
|
---|
[0e16a2d] | 324 |
|
---|
| 325 | // Remove from buffer
|
---|
[a45e21c] | 326 | __do_remove( chan, retval );
|
---|
[0e16a2d] | 327 | unlock( mutex_lock );
|
---|
| 328 | return retval;
|
---|
| 329 | }
|
---|
[c44705c] | 330 | static inline void remove( channel(T) & chan ) { T elem = (T)remove( chan ); }
|
---|
| 331 |
|
---|
[ca22a7c] | 332 |
|
---|
[a0b59ed] | 333 | ///////////////////////////////////////////////////////////////////////////////////////////
|
---|
| 334 | // The following is Go-style operator support for channels
|
---|
| 335 | ///////////////////////////////////////////////////////////////////////////////////////////
|
---|
| 336 |
|
---|
[ca22a7c] | 337 | static inline void ?<<?( channel(T) & chan, T elem ) { insert( chan, elem ); }
|
---|
[bf55f32] | 338 | static inline void ?<<?( T & ret, channel(T) & chan ) { ret = remove( chan ); }
|
---|
[beeff61e] | 339 |
|
---|
| 340 | ///////////////////////////////////////////////////////////////////////////////////////////
|
---|
| 341 | // The following is support for waituntil (select) statements
|
---|
| 342 | ///////////////////////////////////////////////////////////////////////////////////////////
|
---|
| 343 | static inline bool unregister_chan( channel(T) & chan, select_node & node ) with(chan) {
|
---|
[88b49bb] | 344 | if ( !node`isListed && !node.park_counter ) return false; // handle special OR case
|
---|
[beeff61e] | 345 | lock( mutex_lock );
|
---|
| 346 | if ( node`isListed ) { // op wasn't performed
|
---|
| 347 | remove( node );
|
---|
| 348 | unlock( mutex_lock );
|
---|
| 349 | return false;
|
---|
| 350 | }
|
---|
| 351 | unlock( mutex_lock );
|
---|
| 352 |
|
---|
[00b046f] | 353 | // only return true when not special OR case and status is SAT
|
---|
| 354 | return !node.park_counter ? false : *node.clause_status == __SELECT_SAT;
|
---|
[beeff61e] | 355 | }
|
---|
| 356 |
|
---|
[6f774be] | 357 | // special case of __handle_waituntil_OR, that does some work to avoid starvation/deadlock case
|
---|
| 358 | static inline bool __handle_pending( dlist( select_node ) & queue, select_node & mine ) {
|
---|
| 359 | while ( !queue`isEmpty ) {
|
---|
| 360 | // if node not a special OR case or if we win the special OR case race break
|
---|
| 361 | if ( !queue`first.clause_status || queue`first.park_counter || __pending_set_other( queue`first, mine, ((unsigned long int)(&(queue`first))) ) )
|
---|
| 362 | return true;
|
---|
| 363 |
|
---|
| 364 | // our node lost the race when toggling in __pending_set_other
|
---|
| 365 | if ( *mine.clause_status != __SELECT_PENDING )
|
---|
| 366 | return false;
|
---|
| 367 |
|
---|
| 368 | // otherwise we lost the special OR race so discard node
|
---|
| 369 | try_pop_front( queue );
|
---|
| 370 | }
|
---|
| 371 | return false;
|
---|
| 372 | }
|
---|
| 373 |
|
---|
[beeff61e] | 374 | // type used by select statement to capture a chan read as the selected operation
|
---|
| 375 | struct chan_read {
|
---|
[7a2c6b18] | 376 | T * ret;
|
---|
| 377 | channel(T) * chan;
|
---|
[beeff61e] | 378 | };
|
---|
[bf55f32] | 379 | __CFA_SELECT_GET_TYPE( chan_read(T) );
|
---|
[beeff61e] | 380 |
|
---|
[7a2c6b18] | 381 | static inline void ?{}( chan_read(T) & cr, channel(T) * chan, T * ret ) {
|
---|
| 382 | cr.chan = chan;
|
---|
| 383 | cr.ret = ret;
|
---|
[beeff61e] | 384 | }
|
---|
[7a2c6b18] | 385 | static inline chan_read(T) ?<<?( T & ret, channel(T) & chan ) { chan_read(T) cr{ &chan, &ret }; return cr; }
|
---|
[beeff61e] | 386 |
|
---|
[7a2c6b18] | 387 | static inline void __handle_select_closed_read( chan_read(T) & this, select_node & node ) with(*this.chan, this) {
|
---|
| 388 | __closed_remove( *chan, *ret );
|
---|
[beeff61e] | 389 | // if we get here then the insert succeeded
|
---|
| 390 | __make_select_node_available( node );
|
---|
| 391 | }
|
---|
| 392 |
|
---|
[7a2c6b18] | 393 | static inline bool register_select( chan_read(T) & this, select_node & node ) with(*this.chan, this) {
|
---|
[beeff61e] | 394 | lock( mutex_lock );
|
---|
[7a2c6b18] | 395 | node.extra = ret; // set .extra so that if it == 0p later in on_selected it is due to channel close
|
---|
[beeff61e] | 396 |
|
---|
| 397 | #ifdef CHAN_STATS
|
---|
[88b49bb] | 398 | if ( !closed ) c_ops++;
|
---|
[beeff61e] | 399 | #endif
|
---|
| 400 |
|
---|
[c4f411e] | 401 | if ( !node.park_counter ) {
|
---|
| 402 | // are we special case OR and front of cons is also special case OR
|
---|
| 403 | if ( !unlikely(closed) && !prods`isEmpty && prods`first.clause_status && !prods`first.park_counter ) {
|
---|
| 404 | if ( !__make_select_node_pending( node ) ) {
|
---|
| 405 | unlock( mutex_lock );
|
---|
| 406 | return false;
|
---|
| 407 | }
|
---|
[6f774be] | 408 |
|
---|
| 409 | if ( __handle_pending( prods, node ) ) {
|
---|
[7a2c6b18] | 410 | __prods_handoff( *chan, *ret );
|
---|
[629c95a] | 411 | __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] | 412 | unlock( mutex_lock );
|
---|
| 413 | return true;
|
---|
| 414 | }
|
---|
[6f774be] | 415 | if ( *node.clause_status == __SELECT_PENDING )
|
---|
| 416 | __make_select_node_unsat( node );
|
---|
[c4f411e] | 417 | }
|
---|
| 418 | // check if we can complete operation. If so race to establish winner in special OR case
|
---|
| 419 | if ( count != 0 || !prods`isEmpty || unlikely(closed) ) {
|
---|
| 420 | if ( !__make_select_node_available( node ) ) { // we didn't win the race so give up on registering
|
---|
| 421 | unlock( mutex_lock );
|
---|
| 422 | return false;
|
---|
| 423 | }
|
---|
[beeff61e] | 424 | }
|
---|
| 425 | }
|
---|
| 426 |
|
---|
| 427 | if ( unlikely(closed) ) {
|
---|
| 428 | unlock( mutex_lock );
|
---|
| 429 | __handle_select_closed_read( this, node );
|
---|
| 430 | return true;
|
---|
| 431 | }
|
---|
| 432 |
|
---|
| 433 | // have to check for the zero size channel case
|
---|
| 434 | ZeroSize: if ( size == 0 && !prods`isEmpty ) {
|
---|
| 435 | if ( !__handle_waituntil_OR( prods ) ) break ZeroSize;
|
---|
[7a2c6b18] | 436 | __prods_handoff( *chan, *ret );
|
---|
[beeff61e] | 437 | __set_avail_then_unlock( node, mutex_lock );
|
---|
| 438 | return true;
|
---|
| 439 | }
|
---|
| 440 |
|
---|
| 441 | // wait if buffer is empty, work will be completed by someone else
|
---|
| 442 | if ( count == 0 ) {
|
---|
| 443 | #ifdef CHAN_STATS
|
---|
[88b49bb] | 444 | c_blocks++;
|
---|
[beeff61e] | 445 | #endif
|
---|
| 446 |
|
---|
| 447 | insert_last( cons, node );
|
---|
| 448 | unlock( mutex_lock );
|
---|
| 449 | return false;
|
---|
| 450 | }
|
---|
| 451 |
|
---|
| 452 | // Remove from buffer
|
---|
[7a2c6b18] | 453 | __do_remove( *chan, *ret );
|
---|
[beeff61e] | 454 | __set_avail_then_unlock( node, mutex_lock );
|
---|
| 455 | return true;
|
---|
| 456 | }
|
---|
[7a2c6b18] | 457 | static inline bool unregister_select( chan_read(T) & this, select_node & node ) { return unregister_chan( *this.chan, node ); }
|
---|
[b93bf85] | 458 | static inline bool on_selected( chan_read(T) & this, select_node & node ) with(this) {
|
---|
| 459 | if ( unlikely(node.extra == 0p) ) {
|
---|
[7a2c6b18] | 460 | if ( !exception_in_flight() ) __closed_remove( *chan, *ret ); // check if woken up due to closed channel
|
---|
[b93bf85] | 461 | else return false;
|
---|
| 462 | }
|
---|
[beeff61e] | 463 | // This is only reachable if not closed or closed exception was handled
|
---|
[b93bf85] | 464 | return true;
|
---|
[beeff61e] | 465 | }
|
---|
| 466 |
|
---|
[c44705c] | 467 | // type used by select statement to capture a chan read as the selected operation that doesn't have a param to read to
|
---|
| 468 | struct chan_read_no_ret {
|
---|
[7a2c6b18] | 469 | T retval;
|
---|
| 470 | chan_read( T ) c_read;
|
---|
[c44705c] | 471 | };
|
---|
| 472 | __CFA_SELECT_GET_TYPE( chan_read_no_ret(T) );
|
---|
| 473 |
|
---|
| 474 | static inline void ?{}( chan_read_no_ret(T) & this, channel(T) & chan ) {
|
---|
[7a2c6b18] | 475 | this.c_read{ &chan, &this.retval };
|
---|
| 476 | }
|
---|
| 477 |
|
---|
| 478 | static inline chan_read_no_ret(T) remove( channel(T) & chan ) { chan_read_no_ret(T) c_read{ chan }; return c_read; }
|
---|
| 479 | static inline bool register_select( chan_read_no_ret(T) & this, select_node & node ) {
|
---|
| 480 | this.c_read.ret = &this.retval;
|
---|
| 481 | return register_select( this.c_read, node );
|
---|
[c44705c] | 482 | }
|
---|
[7a2c6b18] | 483 | static inline bool unregister_select( chan_read_no_ret(T) & this, select_node & node ) { return unregister_select( this.c_read, node ); }
|
---|
| 484 | static inline bool on_selected( chan_read_no_ret(T) & this, select_node & node ) { return on_selected( this.c_read, node ); }
|
---|
[c44705c] | 485 |
|
---|
[beeff61e] | 486 | // type used by select statement to capture a chan write as the selected operation
|
---|
| 487 | struct chan_write {
|
---|
| 488 | T elem;
|
---|
[7a2c6b18] | 489 | channel(T) * chan;
|
---|
[beeff61e] | 490 | };
|
---|
[bf55f32] | 491 | __CFA_SELECT_GET_TYPE( chan_write(T) );
|
---|
[beeff61e] | 492 |
|
---|
[7a2c6b18] | 493 | static inline void ?{}( chan_write(T) & cw, channel(T) * chan, T elem ) {
|
---|
| 494 | cw.chan = chan;
|
---|
[beeff61e] | 495 | memcpy( (void *)&cw.elem, (void *)&elem, sizeof(T) );
|
---|
| 496 | }
|
---|
[7a2c6b18] | 497 | static inline chan_write(T) ?<<?( channel(T) & chan, T elem ) { chan_write(T) cw{ &chan, elem }; return cw; }
|
---|
| 498 | static inline chan_write(T) insert( T elem, channel(T) & chan) { chan_write(T) cw{ &chan, elem }; return cw; }
|
---|
[beeff61e] | 499 |
|
---|
[7a2c6b18] | 500 | static inline void __handle_select_closed_write( chan_write(T) & this, select_node & node ) with(*this.chan, this) {
|
---|
| 501 | __closed_insert( *chan, elem );
|
---|
[beeff61e] | 502 | // if we get here then the insert succeeded
|
---|
| 503 | __make_select_node_available( node );
|
---|
| 504 | }
|
---|
| 505 |
|
---|
[7a2c6b18] | 506 | static inline bool register_select( chan_write(T) & this, select_node & node ) with(*this.chan, this) {
|
---|
[beeff61e] | 507 | lock( mutex_lock );
|
---|
| 508 | node.extra = &elem; // set .extra so that if it == 0p later in on_selected it is due to channel close
|
---|
| 509 |
|
---|
| 510 | #ifdef CHAN_STATS
|
---|
[88b49bb] | 511 | if ( !closed ) p_ops++;
|
---|
[beeff61e] | 512 | #endif
|
---|
| 513 |
|
---|
[c4f411e] | 514 | // special OR case handling
|
---|
| 515 | if ( !node.park_counter ) {
|
---|
| 516 | // are we special case OR and front of cons is also special case OR
|
---|
| 517 | if ( !unlikely(closed) && !cons`isEmpty && cons`first.clause_status && !cons`first.park_counter ) {
|
---|
| 518 | if ( !__make_select_node_pending( node ) ) {
|
---|
| 519 | unlock( mutex_lock );
|
---|
| 520 | return false;
|
---|
| 521 | }
|
---|
[6f774be] | 522 |
|
---|
| 523 | if ( __handle_pending( cons, node ) ) {
|
---|
[7a2c6b18] | 524 | __cons_handoff( *chan, elem );
|
---|
[629c95a] | 525 | __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] | 526 | unlock( mutex_lock );
|
---|
| 527 | return true;
|
---|
| 528 | }
|
---|
[6f774be] | 529 | if ( *node.clause_status == __SELECT_PENDING )
|
---|
| 530 | __make_select_node_unsat( node );
|
---|
[c4f411e] | 531 | }
|
---|
| 532 | // check if we can complete operation. If so race to establish winner in special OR case
|
---|
| 533 | if ( count != size || !cons`isEmpty || unlikely(closed) ) {
|
---|
| 534 | if ( !__make_select_node_available( node ) ) { // we didn't win the race so give up on registering
|
---|
| 535 | unlock( mutex_lock );
|
---|
| 536 | return false;
|
---|
| 537 | }
|
---|
[beeff61e] | 538 | }
|
---|
| 539 | }
|
---|
| 540 |
|
---|
| 541 | // if closed handle
|
---|
| 542 | if ( unlikely(closed) ) {
|
---|
| 543 | unlock( mutex_lock );
|
---|
| 544 | __handle_select_closed_write( this, node );
|
---|
| 545 | return true;
|
---|
| 546 | }
|
---|
| 547 |
|
---|
| 548 | // handle blocked consumer case via handoff (buffer is implicitly empty)
|
---|
| 549 | ConsEmpty: if ( !cons`isEmpty ) {
|
---|
[cb69fba] | 550 | if ( !__handle_waituntil_OR( cons ) ) break ConsEmpty;
|
---|
[7a2c6b18] | 551 | __cons_handoff( *chan, elem );
|
---|
[beeff61e] | 552 | __set_avail_then_unlock( node, mutex_lock );
|
---|
| 553 | return true;
|
---|
| 554 | }
|
---|
| 555 |
|
---|
| 556 | // insert node in list if buffer is full, work will be completed by someone else
|
---|
| 557 | if ( count == size ) {
|
---|
| 558 | #ifdef CHAN_STATS
|
---|
[88b49bb] | 559 | p_blocks++;
|
---|
[beeff61e] | 560 | #endif
|
---|
| 561 |
|
---|
| 562 | insert_last( prods, node );
|
---|
| 563 | unlock( mutex_lock );
|
---|
| 564 | return false;
|
---|
| 565 | } // if
|
---|
| 566 |
|
---|
| 567 | // otherwise carry out write either via normal insert
|
---|
[7a2c6b18] | 568 | __buf_insert( *chan, elem );
|
---|
[beeff61e] | 569 | __set_avail_then_unlock( node, mutex_lock );
|
---|
| 570 | return true;
|
---|
| 571 | }
|
---|
[7a2c6b18] | 572 | static inline bool unregister_select( chan_write(T) & this, select_node & node ) { return unregister_chan( *this.chan, node ); }
|
---|
[beeff61e] | 573 |
|
---|
[b93bf85] | 574 | static inline bool on_selected( chan_write(T) & this, select_node & node ) with(this) {
|
---|
| 575 | if ( unlikely(node.extra == 0p) ) {
|
---|
[7a2c6b18] | 576 | if ( !exception_in_flight() ) __closed_insert( *chan, elem ); // check if woken up due to closed channel
|
---|
[b93bf85] | 577 | else return false;
|
---|
| 578 | }
|
---|
[beeff61e] | 579 | // This is only reachable if not closed or closed exception was handled
|
---|
[b93bf85] | 580 | return true;
|
---|
[beeff61e] | 581 | }
|
---|
| 582 |
|
---|
[0e16a2d] | 583 | } // forall( T )
|
---|
[beeff61e] | 584 |
|
---|
| 585 |
|
---|