blob: c312c597c673e71f5903f6a718e017a71121527f [file] [log] [blame]
Steve Block6ded16b2010-05-10 14:33:55 +01001// Copyright 2010 the V8 project authors. All rights reserved.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
Steve Block6ded16b2010-05-10 14:33:55 +01004
5#ifndef V8_CIRCULAR_QUEUE_H_
6#define V8_CIRCULAR_QUEUE_H_
7
Ben Murdochb8a8cc12014-11-26 15:28:44 +00008#include "src/base/atomicops.h"
9#include "src/globals.h"
10
Steve Block6ded16b2010-05-10 14:33:55 +010011namespace v8 {
12namespace internal {
13
14
Steve Block6ded16b2010-05-10 14:33:55 +010015// Lock-free cache-friendly sampling circular queue for large
16// records. Intended for fast transfer of large records between a
17// single producer and a single consumer. If the queue is full,
Ben Murdochb8a8cc12014-11-26 15:28:44 +000018// StartEnqueue will return NULL. The queue is designed with
Steve Block6ded16b2010-05-10 14:33:55 +010019// a goal in mind to evade cache lines thrashing by preventing
20// simultaneous reads and writes to adjanced memory locations.
Ben Murdochb8a8cc12014-11-26 15:28:44 +000021template<typename T, unsigned Length>
Steve Block6ded16b2010-05-10 14:33:55 +010022class SamplingCircularQueue {
23 public:
24 // Executed on the application thread.
Ben Murdochb8a8cc12014-11-26 15:28:44 +000025 SamplingCircularQueue();
Steve Block6ded16b2010-05-10 14:33:55 +010026 ~SamplingCircularQueue();
27
Ben Murdochb8a8cc12014-11-26 15:28:44 +000028 // StartEnqueue returns a pointer to a memory location for storing the next
29 // record or NULL if all entries are full at the moment.
30 T* StartEnqueue();
31 // Notifies the queue that the producer has complete writing data into the
32 // memory returned by StartEnqueue and it can be passed to the consumer.
33 void FinishEnqueue();
Steve Block6ded16b2010-05-10 14:33:55 +010034
35 // Executed on the consumer (analyzer) thread.
Ben Murdochb8a8cc12014-11-26 15:28:44 +000036 // Retrieves, but does not remove, the head of this queue, returning NULL
37 // if this queue is empty. After the record had been read by a consumer,
38 // Remove must be called.
39 T* Peek();
40 void Remove();
Steve Block6ded16b2010-05-10 14:33:55 +010041
42 private:
Ben Murdochb8a8cc12014-11-26 15:28:44 +000043 // Reserved values for the entry marker.
44 enum {
45 kEmpty, // Marks clean (processed) entries.
46 kFull // Marks entries already filled by the producer but not yet
47 // completely processed by the consumer.
Steve Block6ded16b2010-05-10 14:33:55 +010048 };
49
Ben Murdochb8a8cc12014-11-26 15:28:44 +000050 struct V8_ALIGNED(PROCESSOR_CACHE_LINE_SIZE) Entry {
51 Entry() : marker(kEmpty) {}
52 T record;
53 base::Atomic32 marker;
54 };
Steve Block6ded16b2010-05-10 14:33:55 +010055
Ben Murdochb8a8cc12014-11-26 15:28:44 +000056 Entry* Next(Entry* entry);
57
58 Entry buffer_[Length];
59 V8_ALIGNED(PROCESSOR_CACHE_LINE_SIZE) Entry* enqueue_pos_;
60 V8_ALIGNED(PROCESSOR_CACHE_LINE_SIZE) Entry* dequeue_pos_;
Steve Block6ded16b2010-05-10 14:33:55 +010061
62 DISALLOW_COPY_AND_ASSIGN(SamplingCircularQueue);
63};
64
65
66} } // namespace v8::internal
67
68#endif // V8_CIRCULAR_QUEUE_H_