dave-hillier / disruptor-unity3d

Basic implementation of Disruptor for Unity3d
Apache License 2.0
200 stars 35 forks source link

Correct Memory Barriers and data validation in benchmark #4

Closed Petrakeas closed 8 years ago

Petrakeas commented 8 years ago

The Memory Barriers used in RingBuffer do not prevent reordering of read and write instructions when accessing _entries and the producer cursor. This could have an effect that the consumer reads a value from _entries before it gets actually updated, thus returning an older value.

This pull request makes sure that the producer writes the new value in _entriesand then updates the producer cursor (to signal that the data are ready to be used). The consumer first reads the producer cursor to make sure that the data are ready and then reads the actual data stored in _entries to make sure it reads the latest value.

The updated benchmark script adds increasing numbers. Thus, when dequeuing items, we can check their value with the expected one.

Please read the corresponding issue: https://github.com/dave-hillier/disruptor-unity3d/issues/5

dave-hillier commented 8 years ago

I like the PR, I'll review in detail next week.

dave-hillier commented 8 years ago

Ok the minimal fix for this bug is actually line 96.

The rest shouldn't change the semantics; I think I was focused on preserving the ordering of the cursors but neglected the entries.

I like the acquire/release pattern, probably a little easier to reason about, so I'll accept.

Petrakeas commented 8 years ago

It's line 96 and line 52 (for the read-acquire part).