7 #include <cdsannotate.h>
8 #include <specannotation.h>
9 #include <model_memory.h>
17 template <typename t_element, size_t t_size>
18 struct mpmc_boundq_1_alt
22 // elements should generally be cache-line-size padded :
23 t_element m_array[t_size];
25 // rdwr counts the reads & writes that have started
26 atomic<unsigned int> m_rdwr;
27 // "read" and "written" count the number completed
28 atomic<unsigned int> m_read;
29 atomic<unsigned int> m_written;
50 CLASS = mpmc_boundq_1_alt;
61 //-----------------------------------------------------
66 @Commit_point_set: Fetch_Succ_Point
67 @ID: (call_id_t) __RET__
70 t_element * read_fetch() {
71 unsigned int rdwr = m_rdwr.load(mo_acquire);
74 @Commit_point_define_check: (rdwr>>16) & 0xFFFF == rdwr & 0xFFFF
75 @Label: Fetch_Succ_Point1
80 rd = (rdwr>>16) & 0xFFFF;
83 if ( wr == rd ) { // empty
87 bool succ = m_rdwr.compare_exchange_weak(rdwr,rdwr+(1<<16),mo_acq_rel);
98 unsigned int tmp = m_written.load(mo_acquire);
101 @Commit_point_define_check: (tmp & 0xFFFF) == wr
102 @Label: Fetch_Succ_Point2
105 if ((tmp & 0xFFFF) == wr)
110 t_element * p = & ( m_array[ rd % t_size ] );
115 void read_consume() {
116 m_read.fetch_add(1,mo_release);
119 //-----------------------------------------------------
121 t_element * write_prepare() {
122 unsigned int rdwr = m_rdwr.load(mo_acquire);
125 rd = (rdwr>>16) & 0xFFFF;
128 if ( wr == ((rd + t_size)&0xFFFF) ) { // full
132 bool succ = m_rdwr.compare_exchange_weak(rdwr,(rd<<16) |
133 ((wr+1)&0xFFFF),mo_acq_rel);
142 while ( (m_read.load(mo_acquire) & 0xFFFF) != rd ) {
146 t_element * p = & ( m_array[ wr % t_size ] );
154 @Commit_point_set: Publish_Point
158 void write_publish(t_element *elem)
160 m_written.fetch_add(1,mo_release);
163 @Commit_point_define_check: true
164 @Label: Publish_Point
169 //-----------------------------------------------------