source: libcfa/src/concurrency/channel.hfa @ 640b3df

ADTast-experimental
Last change on this file since 640b3df was 42b739d7, checked in by caparsons <caparson@…>, 19 months ago

fixed termination synchronization in the channel benchmark to get rid of deadlock case

  • Property mode set to 100644
File size: 3.7 KB
RevLine 
[4a962d8]1#include <locks.hfa>
2
3struct 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
8static inline void   ?{}( no_reacq_lock & this ) { ((exp_backoff_then_block_lock &)this){}; }
9static inline bool   try_lock(no_reacq_lock & this) { return try_lock(((exp_backoff_then_block_lock &)this)); }
10static inline void   lock(no_reacq_lock & this) { lock(((exp_backoff_then_block_lock &)this)); }
11static inline void   unlock(no_reacq_lock & this) { unlock(((exp_backoff_then_block_lock &)this)); }
12static inline void   on_notify(no_reacq_lock & this, struct thread$ * t ) { on_notify(((exp_backoff_then_block_lock &)this), t); }
13static 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
15static inline void   on_wakeup( no_reacq_lock & this, size_t recursion ) {}
16
17forall( T ) {
[5c931e0]18struct 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
26static 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
35static inline void ?{}( channel(T) &c ){ ((channel(T) &)c){ 0 }; }
36static inline void ^?{}( channel(T) &c ) with(c) { delete( buffer ); }
[42b739d7]37static inline size_t get_count( channel(T) & chan ) with(chan) { return count; }
38static inline size_t get_size( channel(T) & chan ) with(chan) { return size; }
39static inline bool has_waiters( channel(T) & chan ) with(chan) { return !empty( cons ) || !empty( prods ); }
40static inline bool has_waiting_consumers( channel(T) & chan ) with(chan) { return !empty( cons ); }
41static inline bool has_waiting_producers( channel(T) & chan ) with(chan) { return !empty( prods ); }
[4a962d8]42
[42b739d7]43static inline void insert_( channel(T) & chan, T elem ) with(chan) {
[4a962d8]44    memcpy((void *)&buffer[back], (void *)&elem, sizeof(T));
45    count += 1;
46    back++;
47    if ( back == size ) back = 0;
48}
49
50
[42b739d7]51static inline void insert( channel(T) & chan, T elem ) with(chan) {
[4a962d8]52    lock( mutex_lock );
53
[5c931e0]54    // have to check for the zero size channel case
55    if ( size == 0 && !empty( cons ) ) {
56        memcpy((void *)front( cons ), (void *)&elem, sizeof(T));
57        notify_one( cons );
58        unlock( mutex_lock );
59        return;
60    }
61
[4a962d8]62    // wait if buffer is full, work will be completed by someone else
63    if ( count == size ) {
64        wait( prods, mutex_lock, (uintptr_t)&elem );
65        return;
66    } // if
67
[5c931e0]68    if ( count == 0 && !empty( cons ) )
[4a962d8]69        // do waiting consumer work
[5c931e0]70        memcpy((void *)front( cons ), (void *)&elem, sizeof(T));
[4a962d8]71    else insert_( chan, elem );
72   
[5c931e0]73    notify_one( cons );
[4a962d8]74    unlock( mutex_lock );
75}
76
[42b739d7]77static inline T remove( channel(T) & chan ) with(chan) {
[4a962d8]78    lock( mutex_lock );
79    T retval;
80
[5c931e0]81    // have to check for the zero size channel case
82    if ( size == 0 && !empty( prods ) ) {
83        memcpy((void *)&retval, (void *)front( prods ), sizeof(T));
84        notify_one( prods );
85        unlock( mutex_lock );
86        return retval;
87    }
88
[4a962d8]89    // wait if buffer is empty, work will be completed by someone else
90    if (count == 0) {
[5c931e0]91        wait( cons, mutex_lock, (uintptr_t)&retval );
[4a962d8]92        return retval;
93    }
94
95    // Remove from buffer
96    memcpy((void *)&retval, (void *)&buffer[front], sizeof(T));
97    count -= 1;
98    front = (front + 1) % size;
99
100    if (count == size - 1 && !empty( prods ) )
101        insert_( chan, *((T *)front( prods )) );  // do waiting producer work
102
103    notify_one( prods );
104    unlock( mutex_lock );
105    return retval;
106}
107
108} // forall( T )
Note: See TracBrowser for help on using the repository browser.