[4a962d8] | 1 | #include <locks.hfa>
|
---|
| 2 |
|
---|
| 3 | struct no_reacq_lock {
|
---|
| 4 | inline exp_backoff_then_block_lock;
|
---|
| 5 | };
|
---|
| 6 |
|
---|
| 7 | // have to override these by hand to get around plan 9 inheritance bug where resolver can't find the appropriate routine to call
|
---|
| 8 | static inline void ?{}( no_reacq_lock & this ) { ((exp_backoff_then_block_lock &)this){}; }
|
---|
| 9 | static inline bool try_lock(no_reacq_lock & this) { return try_lock(((exp_backoff_then_block_lock &)this)); }
|
---|
| 10 | static inline void lock(no_reacq_lock & this) { lock(((exp_backoff_then_block_lock &)this)); }
|
---|
| 11 | static inline void unlock(no_reacq_lock & this) { unlock(((exp_backoff_then_block_lock &)this)); }
|
---|
| 12 | static inline void on_notify(no_reacq_lock & this, struct thread$ * t ) { on_notify(((exp_backoff_then_block_lock &)this), t); }
|
---|
| 13 | static inline size_t on_wait(no_reacq_lock & this) { return on_wait(((exp_backoff_then_block_lock &)this)); }
|
---|
| 14 | // override wakeup so that we don't reacquire the lock if using a condvar
|
---|
| 15 | static inline void on_wakeup( no_reacq_lock & this, size_t recursion ) {}
|
---|
| 16 |
|
---|
| 17 | forall( T ) {
|
---|
[5c931e0] | 18 | struct channel {
|
---|
[4a962d8] | 19 | size_t size;
|
---|
| 20 | size_t front, back, count;
|
---|
| 21 | T * buffer;
|
---|
| 22 | fast_cond_var( no_reacq_lock ) prods, cons;
|
---|
| 23 | no_reacq_lock mutex_lock;
|
---|
| 24 | };
|
---|
| 25 |
|
---|
| 26 | static inline void ?{}( channel(T) &c, size_t _size ) with(c) {
|
---|
| 27 | size = _size;
|
---|
| 28 | front = back = count = 0;
|
---|
| 29 | buffer = anew( size );
|
---|
| 30 | prods{};
|
---|
| 31 | cons{};
|
---|
| 32 | mutex_lock{};
|
---|
| 33 | }
|
---|
| 34 |
|
---|
| 35 | static inline void ?{}( channel(T) &c ){ ((channel(T) &)c){ 0 }; }
|
---|
| 36 | static inline void ^?{}( channel(T) &c ) with(c) { delete( buffer ); }
|
---|
| 37 | inline size_t get_count( channel(T) & chan ) with(chan) { return count; }
|
---|
| 38 | inline size_t get_size( channel(T) & chan ) with(chan) { return size; }
|
---|
| 39 |
|
---|
| 40 | inline void insert_( channel(T) & chan, T elem ) with(chan) {
|
---|
| 41 | memcpy((void *)&buffer[back], (void *)&elem, sizeof(T));
|
---|
| 42 | count += 1;
|
---|
| 43 | back++;
|
---|
| 44 | if ( back == size ) back = 0;
|
---|
| 45 | }
|
---|
| 46 |
|
---|
| 47 |
|
---|
| 48 | inline void insert( channel(T) & chan, T elem ) with(chan) {
|
---|
| 49 | lock( mutex_lock );
|
---|
| 50 |
|
---|
[5c931e0] | 51 | // have to check for the zero size channel case
|
---|
| 52 | if ( size == 0 && !empty( cons ) ) {
|
---|
| 53 | memcpy((void *)front( cons ), (void *)&elem, sizeof(T));
|
---|
| 54 | notify_one( cons );
|
---|
| 55 | unlock( mutex_lock );
|
---|
| 56 | return;
|
---|
| 57 | }
|
---|
| 58 |
|
---|
[4a962d8] | 59 | // wait if buffer is full, work will be completed by someone else
|
---|
| 60 | if ( count == size ) {
|
---|
| 61 | wait( prods, mutex_lock, (uintptr_t)&elem );
|
---|
| 62 | return;
|
---|
| 63 | } // if
|
---|
| 64 |
|
---|
[5c931e0] | 65 | if ( count == 0 && !empty( cons ) )
|
---|
[4a962d8] | 66 | // do waiting consumer work
|
---|
[5c931e0] | 67 | memcpy((void *)front( cons ), (void *)&elem, sizeof(T));
|
---|
[4a962d8] | 68 | else insert_( chan, elem );
|
---|
| 69 |
|
---|
[5c931e0] | 70 | notify_one( cons );
|
---|
[4a962d8] | 71 | unlock( mutex_lock );
|
---|
| 72 | }
|
---|
| 73 |
|
---|
| 74 | inline T remove( channel(T) & chan ) with(chan) {
|
---|
| 75 | lock( mutex_lock );
|
---|
| 76 | T retval;
|
---|
| 77 |
|
---|
[5c931e0] | 78 | // have to check for the zero size channel case
|
---|
| 79 | if ( size == 0 && !empty( prods ) ) {
|
---|
| 80 | memcpy((void *)&retval, (void *)front( prods ), sizeof(T));
|
---|
| 81 | notify_one( prods );
|
---|
| 82 | unlock( mutex_lock );
|
---|
| 83 | return retval;
|
---|
| 84 | }
|
---|
| 85 |
|
---|
[4a962d8] | 86 | // wait if buffer is empty, work will be completed by someone else
|
---|
| 87 | if (count == 0) {
|
---|
[5c931e0] | 88 | wait( cons, mutex_lock, (uintptr_t)&retval );
|
---|
[4a962d8] | 89 | return retval;
|
---|
| 90 | }
|
---|
| 91 |
|
---|
| 92 | // Remove from buffer
|
---|
| 93 | memcpy((void *)&retval, (void *)&buffer[front], sizeof(T));
|
---|
| 94 | count -= 1;
|
---|
| 95 | front = (front + 1) % size;
|
---|
| 96 |
|
---|
| 97 | if (count == size - 1 && !empty( prods ) )
|
---|
| 98 | insert_( chan, *((T *)front( prods )) ); // do waiting producer work
|
---|
| 99 |
|
---|
| 100 | notify_one( prods );
|
---|
| 101 | unlock( mutex_lock );
|
---|
| 102 | return retval;
|
---|
| 103 | }
|
---|
| 104 |
|
---|
| 105 | } // forall( T )
|
---|