1 // Copyright 2010 the V8 project authors. All rights reserved. 2 // Redistribution and use in source and binary forms, with or without 3 // modification, are permitted provided that the following conditions are 4 // met: 5 // 6 // * Redistributions of source code must retain the above copyright 7 // notice, this list of conditions and the following disclaimer. 8 // * Redistributions in binary form must reproduce the above 9 // copyright notice, this list of conditions and the following 10 // disclaimer in the documentation and/or other materials provided 11 // with the distribution. 12 // * Neither the name of Google Inc. nor the names of its 13 // contributors may be used to endorse or promote products derived 14 // from this software without specific prior written permission. 15 // 16 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 17 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 18 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 19 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 20 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 21 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 22 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 23 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 24 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 25 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 26 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 27 28 #ifndef V8_CIRCULAR_QUEUE_H_ 29 #define V8_CIRCULAR_QUEUE_H_ 30 31 #include "v8globals.h" 32 33 namespace v8 { 34 namespace internal { 35 36 37 // Lock-free cache-friendly sampling circular queue for large 38 // records. Intended for fast transfer of large records between a 39 // single producer and a single consumer. If the queue is full, 40 // StartEnqueue will return NULL. The queue is designed with 41 // a goal in mind to evade cache lines thrashing by preventing 42 // simultaneous reads and writes to adjanced memory locations. 43 template<typename T, unsigned Length> 44 class SamplingCircularQueue { 45 public: 46 // Executed on the application thread. 47 SamplingCircularQueue(); 48 ~SamplingCircularQueue(); 49 50 // StartEnqueue returns a pointer to a memory location for storing the next 51 // record or NULL if all entries are full at the moment. 52 T* StartEnqueue(); 53 // Notifies the queue that the producer has complete writing data into the 54 // memory returned by StartEnqueue and it can be passed to the consumer. 55 void FinishEnqueue(); 56 57 // Executed on the consumer (analyzer) thread. 58 // Retrieves, but does not remove, the head of this queue, returning NULL 59 // if this queue is empty. After the record had been read by a consumer, 60 // Remove must be called. 61 T* Peek(); 62 void Remove(); 63 64 private: 65 // Reserved values for the entry marker. 66 enum { 67 kEmpty, // Marks clean (processed) entries. 68 kFull // Marks entries already filled by the producer but not yet 69 // completely processed by the consumer. 70 }; 71 72 struct V8_ALIGNED(PROCESSOR_CACHE_LINE_SIZE) Entry { 73 Entry() : marker(kEmpty) {} 74 T record; 75 Atomic32 marker; 76 }; 77 78 Entry* Next(Entry* entry); 79 80 Entry buffer_[Length]; 81 V8_ALIGNED(PROCESSOR_CACHE_LINE_SIZE) Entry* enqueue_pos_; 82 V8_ALIGNED(PROCESSOR_CACHE_LINE_SIZE) Entry* dequeue_pos_; 83 84 DISALLOW_COPY_AND_ASSIGN(SamplingCircularQueue); 85 }; 86 87 88 } } // namespace v8::internal 89 90 #endif // V8_CIRCULAR_QUEUE_H_ 91