bench.sh: rearrange order again
[model-checker-benchmarks.git] / mpmc-queue / mpmc-queue.h
1 #include <stdatomic.h>
2 #include <unrelacy.h>
3
4 template <typename t_element, size_t t_size>
5 struct mpmc_boundq_1_alt
6 {
7 private:
8
9         // elements should generally be cache-line-size padded :
10         t_element               m_array[t_size];
11
12         // rdwr counts the reads & writes that have started
13         atomic<unsigned int>    m_rdwr;
14         // "read" and "written" count the number completed
15         atomic<unsigned int>    m_read;
16         atomic<unsigned int>    m_written;
17
18 public:
19
20         mpmc_boundq_1_alt()
21         {
22                 m_rdwr = 0;
23                 m_read = 0;
24                 m_written = 0;
25         }
26
27         //-----------------------------------------------------
28
29         t_element * read_fetch() {
30                 unsigned int rdwr = m_rdwr.load(mo_acquire);
31                 unsigned int rd,wr;
32                 for(;;) {
33                         rd = (rdwr>>16) & 0xFFFF;
34                         wr = rdwr & 0xFFFF;
35
36                         if ( wr == rd ) // empty
37                                 return false;
38
39                         if ( m_rdwr.compare_exchange_weak(rdwr,rdwr+(1<<16),mo_acq_rel) )
40                                 break;
41                         else
42                                 thrd_yield();
43                 }
44
45                 // (*1)
46                 rl::backoff bo;
47                 while ( (m_written.load(mo_acquire) & 0xFFFF) != wr ) {
48                         thrd_yield();
49                 }
50
51                 t_element * p = & ( m_array[ rd % t_size ] );
52
53                 return p;
54         }
55
56         void read_consume() {
57                 m_read.fetch_add(1,mo_release);
58         }
59
60         //-----------------------------------------------------
61
62         t_element * write_prepare() {
63                 unsigned int rdwr = m_rdwr.load(mo_acquire);
64                 unsigned int rd,wr;
65                 for(;;) {
66                         rd = (rdwr>>16) & 0xFFFF;
67                         wr = rdwr & 0xFFFF;
68
69                         if ( wr == ((rd + t_size)&0xFFFF) ) // full
70                                 return NULL;
71
72                         if ( m_rdwr.compare_exchange_weak(rdwr,(rd<<16) | ((wr+1)&0xFFFF),mo_acq_rel) )
73                                 break;
74                         else
75                                 thrd_yield();
76                 }
77
78                 // (*1)
79                 rl::backoff bo;
80                 while ( (m_read.load(mo_acquire) & 0xFFFF) != rd ) {
81                         thrd_yield();
82                 }
83
84                 t_element * p = & ( m_array[ wr % t_size ] );
85
86                 return p;
87         }
88
89         void write_publish()
90         {
91                 m_written.fetch_add(1,mo_release);
92         }
93
94         //-----------------------------------------------------
95
96
97 };