direct-io.hg

view xen/include/public/io/ring.h @ 11697:a5a3f48e92c9

Remove redundant '&' before array name in __RING_SIZE macro.
Signed-off-by: Richard Miller <9xen@hamnavoe.com>
author kfraser@localhost.localdomain
date Wed Oct 04 19:00:11 2006 +0100 (2006-10-04)
parents aecdb4c52fa7
children 317e71a06828
line source
1 /******************************************************************************
2 * ring.h
3 *
4 * Shared producer-consumer ring macros.
5 *
6 * Tim Deegan and Andrew Warfield November 2004.
7 */
9 #ifndef __XEN_PUBLIC_IO_RING_H__
10 #define __XEN_PUBLIC_IO_RING_H__
12 typedef unsigned int RING_IDX;
14 /* Round a 32-bit unsigned constant down to the nearest power of two. */
15 #define __RD2(_x) (((_x) & 0x00000002) ? 0x2 : ((_x) & 0x1))
16 #define __RD4(_x) (((_x) & 0x0000000c) ? __RD2((_x)>>2)<<2 : __RD2(_x))
17 #define __RD8(_x) (((_x) & 0x000000f0) ? __RD4((_x)>>4)<<4 : __RD4(_x))
18 #define __RD16(_x) (((_x) & 0x0000ff00) ? __RD8((_x)>>8)<<8 : __RD8(_x))
19 #define __RD32(_x) (((_x) & 0xffff0000) ? __RD16((_x)>>16)<<16 : __RD16(_x))
21 /*
22 * Calculate size of a shared ring, given the total available space for the
23 * ring and indexes (_sz), and the name tag of the request/response structure.
24 * A ring contains as many entries as will fit, rounded down to the nearest
25 * power of two (so we can mask with (size-1) to loop around).
26 */
27 #define __RING_SIZE(_s, _sz) \
28 (__RD32(((_sz) - (long)(_s)->ring + (long)(_s)) / sizeof((_s)->ring[0])))
30 /*
31 * Macros to make the correct C datatypes for a new kind of ring.
32 *
33 * To make a new ring datatype, you need to have two message structures,
34 * let's say request_t, and response_t already defined.
35 *
36 * In a header where you want the ring datatype declared, you then do:
37 *
38 * DEFINE_RING_TYPES(mytag, request_t, response_t);
39 *
40 * These expand out to give you a set of types, as you can see below.
41 * The most important of these are:
42 *
43 * mytag_sring_t - The shared ring.
44 * mytag_front_ring_t - The 'front' half of the ring.
45 * mytag_back_ring_t - The 'back' half of the ring.
46 *
47 * To initialize a ring in your code you need to know the location and size
48 * of the shared memory area (PAGE_SIZE, for instance). To initialise
49 * the front half:
50 *
51 * mytag_front_ring_t front_ring;
52 * SHARED_RING_INIT((mytag_sring_t *)shared_page);
53 * FRONT_RING_INIT(&front_ring, (mytag_sring_t *)shared_page, PAGE_SIZE);
54 *
55 * Initializing the back follows similarly (note that only the front
56 * initializes the shared ring):
57 *
58 * mytag_back_ring_t back_ring;
59 * BACK_RING_INIT(&back_ring, (mytag_sring_t *)shared_page, PAGE_SIZE);
60 */
62 #define DEFINE_RING_TYPES(__name, __req_t, __rsp_t) \
63 \
64 /* Shared ring entry */ \
65 union __name##_sring_entry { \
66 __req_t req; \
67 __rsp_t rsp; \
68 }; \
69 \
70 /* Shared ring page */ \
71 struct __name##_sring { \
72 RING_IDX req_prod, req_event; \
73 RING_IDX rsp_prod, rsp_event; \
74 uint8_t pad[48]; \
75 union __name##_sring_entry ring[1]; /* variable-length */ \
76 }; \
77 \
78 /* "Front" end's private variables */ \
79 struct __name##_front_ring { \
80 RING_IDX req_prod_pvt; \
81 RING_IDX rsp_cons; \
82 unsigned int nr_ents; \
83 struct __name##_sring *sring; \
84 }; \
85 \
86 /* "Back" end's private variables */ \
87 struct __name##_back_ring { \
88 RING_IDX rsp_prod_pvt; \
89 RING_IDX req_cons; \
90 unsigned int nr_ents; \
91 struct __name##_sring *sring; \
92 }; \
93 \
94 /* Syntactic sugar */ \
95 typedef struct __name##_sring __name##_sring_t; \
96 typedef struct __name##_front_ring __name##_front_ring_t; \
97 typedef struct __name##_back_ring __name##_back_ring_t
99 /*
100 * Macros for manipulating rings.
101 *
102 * FRONT_RING_whatever works on the "front end" of a ring: here
103 * requests are pushed on to the ring and responses taken off it.
104 *
105 * BACK_RING_whatever works on the "back end" of a ring: here
106 * requests are taken off the ring and responses put on.
107 *
108 * N.B. these macros do NO INTERLOCKS OR FLOW CONTROL.
109 * This is OK in 1-for-1 request-response situations where the
110 * requestor (front end) never has more than RING_SIZE()-1
111 * outstanding requests.
112 */
114 /* Initialising empty rings */
115 #define SHARED_RING_INIT(_s) do { \
116 (_s)->req_prod = (_s)->rsp_prod = 0; \
117 (_s)->req_event = (_s)->rsp_event = 1; \
118 memset((_s)->pad, 0, sizeof((_s)->pad)); \
119 } while(0)
121 #define FRONT_RING_INIT(_r, _s, __size) do { \
122 (_r)->req_prod_pvt = 0; \
123 (_r)->rsp_cons = 0; \
124 (_r)->nr_ents = __RING_SIZE(_s, __size); \
125 (_r)->sring = (_s); \
126 } while (0)
128 #define BACK_RING_INIT(_r, _s, __size) do { \
129 (_r)->rsp_prod_pvt = 0; \
130 (_r)->req_cons = 0; \
131 (_r)->nr_ents = __RING_SIZE(_s, __size); \
132 (_r)->sring = (_s); \
133 } while (0)
135 /* Initialize to existing shared indexes -- for recovery */
136 #define FRONT_RING_ATTACH(_r, _s, __size) do { \
137 (_r)->sring = (_s); \
138 (_r)->req_prod_pvt = (_s)->req_prod; \
139 (_r)->rsp_cons = (_s)->rsp_prod; \
140 (_r)->nr_ents = __RING_SIZE(_s, __size); \
141 } while (0)
143 #define BACK_RING_ATTACH(_r, _s, __size) do { \
144 (_r)->sring = (_s); \
145 (_r)->rsp_prod_pvt = (_s)->rsp_prod; \
146 (_r)->req_cons = (_s)->req_prod; \
147 (_r)->nr_ents = __RING_SIZE(_s, __size); \
148 } while (0)
150 /* How big is this ring? */
151 #define RING_SIZE(_r) \
152 ((_r)->nr_ents)
154 /* Number of free requests (for use on front side only). */
155 #define RING_FREE_REQUESTS(_r) \
156 (RING_SIZE(_r) - ((_r)->req_prod_pvt - (_r)->rsp_cons))
158 /* Test if there is an empty slot available on the front ring.
159 * (This is only meaningful from the front. )
160 */
161 #define RING_FULL(_r) \
162 (RING_FREE_REQUESTS(_r) == 0)
164 /* Test if there are outstanding messages to be processed on a ring. */
165 #define RING_HAS_UNCONSUMED_RESPONSES(_r) \
166 ((_r)->sring->rsp_prod - (_r)->rsp_cons)
168 #define RING_HAS_UNCONSUMED_REQUESTS(_r) \
169 ({ \
170 unsigned int req = (_r)->sring->req_prod - (_r)->req_cons; \
171 unsigned int rsp = RING_SIZE(_r) - \
172 ((_r)->req_cons - (_r)->rsp_prod_pvt); \
173 req < rsp ? req : rsp; \
174 })
176 /* Direct access to individual ring elements, by index. */
177 #define RING_GET_REQUEST(_r, _idx) \
178 (&((_r)->sring->ring[((_idx) & (RING_SIZE(_r) - 1))].req))
180 #define RING_GET_RESPONSE(_r, _idx) \
181 (&((_r)->sring->ring[((_idx) & (RING_SIZE(_r) - 1))].rsp))
183 /* Loop termination condition: Would the specified index overflow the ring? */
184 #define RING_REQUEST_CONS_OVERFLOW(_r, _cons) \
185 (((_cons) - (_r)->rsp_prod_pvt) >= RING_SIZE(_r))
187 #define RING_PUSH_REQUESTS(_r) do { \
188 wmb(); /* back sees requests /before/ updated producer index */ \
189 (_r)->sring->req_prod = (_r)->req_prod_pvt; \
190 } while (0)
192 #define RING_PUSH_RESPONSES(_r) do { \
193 wmb(); /* front sees responses /before/ updated producer index */ \
194 (_r)->sring->rsp_prod = (_r)->rsp_prod_pvt; \
195 } while (0)
197 /*
198 * Notification hold-off (req_event and rsp_event):
199 *
200 * When queueing requests or responses on a shared ring, it may not always be
201 * necessary to notify the remote end. For example, if requests are in flight
202 * in a backend, the front may be able to queue further requests without
203 * notifying the back (if the back checks for new requests when it queues
204 * responses).
205 *
206 * When enqueuing requests or responses:
207 *
208 * Use RING_PUSH_{REQUESTS,RESPONSES}_AND_CHECK_NOTIFY(). The second argument
209 * is a boolean return value. True indicates that the receiver requires an
210 * asynchronous notification.
211 *
212 * After dequeuing requests or responses (before sleeping the connection):
213 *
214 * Use RING_FINAL_CHECK_FOR_REQUESTS() or RING_FINAL_CHECK_FOR_RESPONSES().
215 * The second argument is a boolean return value. True indicates that there
216 * are pending messages on the ring (i.e., the connection should not be put
217 * to sleep).
218 *
219 * These macros will set the req_event/rsp_event field to trigger a
220 * notification on the very next message that is enqueued. If you want to
221 * create batches of work (i.e., only receive a notification after several
222 * messages have been enqueued) then you will need to create a customised
223 * version of the FINAL_CHECK macro in your own code, which sets the event
224 * field appropriately.
225 */
227 #define RING_PUSH_REQUESTS_AND_CHECK_NOTIFY(_r, _notify) do { \
228 RING_IDX __old = (_r)->sring->req_prod; \
229 RING_IDX __new = (_r)->req_prod_pvt; \
230 wmb(); /* back sees requests /before/ updated producer index */ \
231 (_r)->sring->req_prod = __new; \
232 mb(); /* back sees new requests /before/ we check req_event */ \
233 (_notify) = ((RING_IDX)(__new - (_r)->sring->req_event) < \
234 (RING_IDX)(__new - __old)); \
235 } while (0)
237 #define RING_PUSH_RESPONSES_AND_CHECK_NOTIFY(_r, _notify) do { \
238 RING_IDX __old = (_r)->sring->rsp_prod; \
239 RING_IDX __new = (_r)->rsp_prod_pvt; \
240 wmb(); /* front sees responses /before/ updated producer index */ \
241 (_r)->sring->rsp_prod = __new; \
242 mb(); /* front sees new responses /before/ we check rsp_event */ \
243 (_notify) = ((RING_IDX)(__new - (_r)->sring->rsp_event) < \
244 (RING_IDX)(__new - __old)); \
245 } while (0)
247 #define RING_FINAL_CHECK_FOR_REQUESTS(_r, _work_to_do) do { \
248 (_work_to_do) = RING_HAS_UNCONSUMED_REQUESTS(_r); \
249 if (_work_to_do) break; \
250 (_r)->sring->req_event = (_r)->req_cons + 1; \
251 mb(); \
252 (_work_to_do) = RING_HAS_UNCONSUMED_REQUESTS(_r); \
253 } while (0)
255 #define RING_FINAL_CHECK_FOR_RESPONSES(_r, _work_to_do) do { \
256 (_work_to_do) = RING_HAS_UNCONSUMED_RESPONSES(_r); \
257 if (_work_to_do) break; \
258 (_r)->sring->rsp_event = (_r)->rsp_cons + 1; \
259 mb(); \
260 (_work_to_do) = RING_HAS_UNCONSUMED_RESPONSES(_r); \
261 } while (0)
263 #endif /* __XEN_PUBLIC_IO_RING_H__ */
265 /*
266 * Local variables:
267 * mode: C
268 * c-set-style: "BSD"
269 * c-basic-offset: 4
270 * tab-width: 4
271 * indent-tabs-mode: nil
272 * End:
273 */