.net vs 2010 barcode Understanding Shared Memory Ring Bu ers in Java Creator PDF417 in Java Understanding Shared Memory Ring Bu ers

6.3. Understanding Shared Memory Ring Bu ers using none tointegrate none with web,windows applicationprinting pdf-417 .net 6.3 Understanding Shared Memory Ring Buffers ASP.NET Web Forms The ring bu er is a fairly st andard lockless data structure for producer-consumer communications. The variant used by Xen is somewhat unusual in that it uses freerunning counters. A typical ring bu er has a producer and a consumer pointer.

Each of these is tested by one and incremented by the other. When one pointer goes past the end of the bu er, it must be wrapped. This is relatively expensive, and because it involves programming for a number of corner cases, it is relatively easy to get wrong.

The Xen version avoids this by ensuring that the bu er size is a power of two. This means that the lowest n bits of the counter can be used to give an index within the bu er, and these bits can be obtained with a simple mask. Because the counters can run to more than the bu er size, you do not need to manually account for over ow; subtracting one from the other always gives you the amount of data in the bu er.

The one special case comes from the fact that the counters themselves can over ow. Let s take a simple example an 8-bit counter on a 32element bu er and see what happens when it over ows. The producer value is incremented to 258, which causes an over ow, wrapping the value around to two.

The consumer value has not yet over owed, so it is now bigger than the producer. What happens when we try a subtraction 0 1 0 0 0 1 0 0 0 1 0 0 0 1 0 0 0 1 0 0 0 1 1 1 1 0 1 1 0 0 0 0 (Producer value of 2) (Consumer value of 252) (Subtraction gives 250) (Truncation gives 6). The leading 1 in the subtract none for none ion result comes from the use of 1 s complement arithmetic. If the result of a calculation is negative, the leading bit will be 1, and the remaining bits will be the inverse of their positive values. This representation is used in all modern CPUs, because it simpli es a number of cases (adding signed values can be implemented using the same logic, irrespective of their signs).

One nice side e ect of this is that subtraction still works if one of the values has over owed. Note that the leading bit is truncated, because it doesn t t in the 8-bit value. Most CPUs will store this in a condition register somewhere; however, it can simply be discarded for our purposes.

One thing to note is that this value will be wrong if the producer value over ows twice before the consumer value over ows. Fortunately, this can never happen, because that would require the bu er to be larger than the counter, meaning that no mask exists that would convert the counter value to a bu er index. The other interesting thing about the Xen ring mechanism is that each ring contains two kinds of data, a request and a response, updated by the two halves of the driver.

In principle, this could cause some problems. If the responses are. 6. Understanding Device Drivers DomU Writes Request 1 DomU Writes Request 2 Dom0 Writes Response 1 Dom0 Reads Response 1 Dom0 Writes Response 2 Dom0 Reads Response 2 Figure 6.2: A sequence of actions on a ring bu er larger than the requests, the response pointer catches up with the request pointer, preventing the back end from writing any more responses. Xen solves this by only permitting responses to be written in a way that overwrites requests. Figure 6.

2 shows a typical sequence of actions in an I/O ring. In this sequence, the front half of the driver issues two requests, and the back half lls them in. The rst two operations in this sequence show the front half of the driver writing a pair of requests into the ring.

After writing each request, it increments a counter indicating the used part of the ring. The back half then writes a response over the rst request. After doing this, it increments the response counter, indicating that the response has been lled in.

The front half can then read this response and increment a counter indicating where the back end is. This is then repeated for the second response. There are three counters of relevance here.

The rst indicates the start of the requests. This is only ever incremented by the front end, and never decremented. If the back end reads this value, it can guarantee that the value will never be lower than this value.

1 It can then use any space from the back of the request segment to the front in order to store responses. After storing a response, the back end increments a counter indicating that the response has been stored. The front end can read this counter, and know that anything between it and the back of the tail counter contains responses.

It can then read them, and update the tail counter to any value that doesn t cause it to. 1 The only requirement for th none for none is to work is that memory writes are atomic. This is true on most modern CPUs. Note that a memory barrier is not required; the old value for the counter is always safe to use, because it is monotonic.

Copyright © . All rights reserved.