v8  3.25.30(node0.11.13)
V8 is Google's open source JavaScript engine
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
circular-queue.h
Go to the documentation of this file.
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 "atomicops.h"
32 #include "v8globals.h"
33 
34 namespace v8 {
35 namespace internal {
36 
37 
38 // Lock-free cache-friendly sampling circular queue for large
39 // records. Intended for fast transfer of large records between a
40 // single producer and a single consumer. If the queue is full,
41 // StartEnqueue will return NULL. The queue is designed with
42 // a goal in mind to evade cache lines thrashing by preventing
43 // simultaneous reads and writes to adjanced memory locations.
44 template<typename T, unsigned Length>
46  public:
47  // Executed on the application thread.
50 
51  // StartEnqueue returns a pointer to a memory location for storing the next
52  // record or NULL if all entries are full at the moment.
53  T* StartEnqueue();
54  // Notifies the queue that the producer has complete writing data into the
55  // memory returned by StartEnqueue and it can be passed to the consumer.
56  void FinishEnqueue();
57 
58  // Executed on the consumer (analyzer) thread.
59  // Retrieves, but does not remove, the head of this queue, returning NULL
60  // if this queue is empty. After the record had been read by a consumer,
61  // Remove must be called.
62  T* Peek();
63  void Remove();
64 
65  private:
66  // Reserved values for the entry marker.
67  enum {
68  kEmpty, // Marks clean (processed) entries.
69  kFull // Marks entries already filled by the producer but not yet
70  // completely processed by the consumer.
71  };
72 
73  struct V8_ALIGNED(PROCESSOR_CACHE_LINE_SIZE) Entry {
74  Entry() : marker(kEmpty) {}
75  T record;
76  Atomic32 marker;
77  };
78 
79  Entry* Next(Entry* entry);
80 
81  Entry buffer_[Length];
82  V8_ALIGNED(PROCESSOR_CACHE_LINE_SIZE) Entry* enqueue_pos_;
83  V8_ALIGNED(PROCESSOR_CACHE_LINE_SIZE) Entry* dequeue_pos_;
84 
86 };
87 
88 
89 } } // namespace v8::internal
90 
91 #endif // V8_CIRCULAR_QUEUE_H_
#define PROCESSOR_CACHE_LINE_SIZE
Definition: v8globals.h:100
#define DISALLOW_COPY_AND_ASSIGN(TypeName)
Definition: globals.h:359
#define T(name, string, precedence)
Definition: token.cc:48
int32_t Atomic32
Definition: atomicops.h:66
#define V8_ALIGNED(n)
Definition: v8config.h:433