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 ) { |
---|
18 | struct channel { |
---|
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 | static inline size_t get_count( channel(T) & chan ) with(chan) { return count; } |
---|
38 | static inline size_t get_size( channel(T) & chan ) with(chan) { return size; } |
---|
39 | static inline bool has_waiters( channel(T) & chan ) with(chan) { return !empty( cons ) || !empty( prods ); } |
---|
40 | static inline bool has_waiting_consumers( channel(T) & chan ) with(chan) { return !empty( cons ); } |
---|
41 | static inline bool has_waiting_producers( channel(T) & chan ) with(chan) { return !empty( prods ); } |
---|
42 | |
---|
43 | static inline void insert_( channel(T) & chan, T elem ) with(chan) { |
---|
44 | memcpy((void *)&buffer[back], (void *)&elem, sizeof(T)); |
---|
45 | count += 1; |
---|
46 | back++; |
---|
47 | if ( back == size ) back = 0; |
---|
48 | } |
---|
49 | |
---|
50 | |
---|
51 | static inline void insert( channel(T) & chan, T elem ) with(chan) { |
---|
52 | lock( mutex_lock ); |
---|
53 | |
---|
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 | |
---|
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 | |
---|
68 | if ( count == 0 && !empty( cons ) ) |
---|
69 | // do waiting consumer work |
---|
70 | memcpy((void *)front( cons ), (void *)&elem, sizeof(T)); |
---|
71 | else insert_( chan, elem ); |
---|
72 | |
---|
73 | notify_one( cons ); |
---|
74 | unlock( mutex_lock ); |
---|
75 | } |
---|
76 | |
---|
77 | static inline T remove( channel(T) & chan ) with(chan) { |
---|
78 | lock( mutex_lock ); |
---|
79 | T retval; |
---|
80 | |
---|
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 | |
---|
89 | // wait if buffer is empty, work will be completed by someone else |
---|
90 | if (count == 0) { |
---|
91 | wait( cons, mutex_lock, (uintptr_t)&retval ); |
---|
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 ) |
---|