aboutsummaryrefslogtreecommitdiffstats
path: root/src/framework/ring/dmm_ring_base.h
blob: 9a48eead1b7e1c58e9b45c2768eb0abe487c398a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
/*
*
* Copyright (c) 2018 Huawei Technologies Co.,Ltd.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at:
*
*     http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#ifndef _DMM_RING_BASE_H_
#define _DMM_RING_BASE_H_

#ifndef _DMM_RING_H_
#error include dmm_ring.h please
#endif

inline static int
__move_head (volatile int *head, int ring_size,
             int old_head, int real_num, int is_single)
{
  int new_head = old_head + real_num;

  if (new_head >= ring_size)
    {
      new_head -= ring_size;
    }

  if (is_single)
    {
      *head = new_head;
      return 1;
    }

  return dmm_atomic_swap ((dmm_atomic_t *) head, old_head, new_head);
}

inline static int
dmm_enqueue_prep (struct dmm_ring *ring, const int num,
                  int *pos, const int fixed, int is_sp)
{
  int succ, real, head;
  const int ring_size = ring->size;

  do
    {
      head = ring->prod_head;

      dmm_barrier ();

      if ((real = ring->cons_tail - head - 1) < 0)
        real += ring_size;

      if (real >= num)
        real = num;
      else if (fixed)
        return 0;

      if (real <= 0)
        return 0;

      succ = __move_head (&ring->prod_head, ring_size, head, real, is_sp);
    }
  while (!succ);

  *pos = head;
  return real;
}

inline static int
dmm_enqueue_copy (struct dmm_ring *ring, int pos, void **from, int num)
{
  const int ring_size = ring->size;
  void **box = (void **) (ring + 1);

  while (num > 0)
    {
      box[pos++] = *from++;
      if (pos >= ring_size)
        pos = 0;
      --num;
    }

  return pos;
}

inline static void
dmm_enqueue_done (struct dmm_ring *ring, int pos, int new_pos, int is_sp)
{
  dmm_barrier ();

  if (!is_sp)
    {
      while (ring->prod_tail != pos)
        dmm_pause ();
    }

  ring->prod_tail = new_pos;
}

inline static int
dmm_enqueue_copy_array (struct dmm_ring *ring, int pos,
                        void *array, int num, size_t elt_size)
{
  const int ring_size = ring->size;
  void **box = (void **) (ring + 1);
  char *from = (char *) array;

  while (num > 0)
    {
      box[pos++] = from;
      if (pos >= ring_size)
        pos = 0;
      from += elt_size;
      --num;
    }

  return pos;
}

inline static int
dmm_dequeue_prep (struct dmm_ring *ring, const int num, int *pos,
                  const int fixed, int is_sc)
{
  int succ, real, head;
  const int ring_size = ring->size;

  do
    {
      head = ring->cons_head;

      dmm_barrier ();

      if ((real = ring->prod_tail - head) < 0)
        real += ring_size;

      if (real >= num)
        real = num;
      else if (fixed)
        return 0;

      if (real <= 0)
        return 0;

      succ = __move_head (&ring->cons_head, ring_size, head, real, is_sc);
    }
  while (!succ);

  *pos = head;
  return real;
}

inline static int
dmm_dequeue_copy (struct dmm_ring *ring, int pos, void **to, int num)
{
  const int ring_size = ring->size;
  void **box = (void **) (ring + 1);

  while (num > 0)
    {
      *to++ = box[pos++];
      if (pos >= ring_size)
        pos = 0;
      --num;
    }

  return pos;
}

inline static void
dmm_dequeue_done (struct dmm_ring *ring, int pos, int new_pos, int is_sc)
{
  dmm_barrier ();

  if (!is_sc)
    {
      while (ring->cons_tail != pos)
        dmm_pause ();
    }

  ring->cons_tail = new_pos;
}

#endif