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 | Fetch_Fail_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: (unsigned int) ((rdwr>>16) & 0xFFFF) == (unsigned int) (rdwr & 0xFFFF)
75 @Label: Fetch_Fail_Point
80 rd = (rdwr>>16) & 0xFFFF;
83 //model_print("cond: %d\n", (unsigned int) ((rdwr>>16) & 0xFFFF) ==
84 //(unsigned int) (rdwr & 0xFFFF));
85 //model_print("cond: %d\n", wr == rd);
86 if ( wr == rd ) { // empty
89 @Commit_point_define: true
90 @Potential_commit_point_label: Fetch_Potential_Point
91 @Label: Fetch_Fail_Point
94 model_print("in cond\n");
98 bool succ = m_rdwr.compare_exchange_weak(rdwr,rdwr+(1<<16),mo_acq_rel);
110 if ((m_written.load(mo_acquire) & 0xFFFF) == wr) {
113 @Commit_point_define_check: true
114 @Label: Fetch_Succ_Point
122 t_element * p = & ( m_array[ rd % t_size ] );
127 void read_consume() {
128 m_read.fetch_add(1,mo_release);
131 //-----------------------------------------------------
133 t_element * write_prepare() {
134 unsigned int rdwr = m_rdwr.load(mo_acquire);
137 rd = (rdwr>>16) & 0xFFFF;
140 if ( wr == ((rd + t_size)&0xFFFF) ) { // full
144 bool succ = m_rdwr.compare_exchange_weak(rdwr,(rd<<16) |
145 ((wr+1)&0xFFFF),mo_acq_rel);
154 while ( (m_read.load(mo_acquire) & 0xFFFF) != rd ) {
158 t_element * p = & ( m_array[ wr % t_size ] );
166 @Commit_point_set: Publish_Point
170 void write_publish(t_element *elem)
172 m_written.fetch_add(1,mo_release);
175 @Commit_point_define_check: true
176 @Label: Publish_Point
181 //-----------------------------------------------------