| OLD | NEW |
| (Empty) |
| 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_BUFFER_INL_H_ | |
| 29 #define V8_CIRCULAR_BUFFER_INL_H_ | |
| 30 | |
| 31 #include "circular-queue.h" | |
| 32 | |
| 33 namespace v8 { | |
| 34 namespace internal { | |
| 35 | |
| 36 | |
| 37 template<typename Record> | |
| 38 CircularQueue<Record>::CircularQueue(int desired_buffer_size_in_bytes) | |
| 39 : buffer_(NewArray<Record>(desired_buffer_size_in_bytes / sizeof(Record))), | |
| 40 buffer_end_(buffer_ + desired_buffer_size_in_bytes / sizeof(Record)), | |
| 41 enqueue_semaphore_(OS::CreateSemaphore((buffer_end_ - buffer_) - 1)), | |
| 42 enqueue_pos_(buffer_), | |
| 43 dequeue_pos_(buffer_) { | |
| 44 // To be able to distinguish between a full and an empty queue | |
| 45 // state, the queue must be capable of containing at least 2 | |
| 46 // records. | |
| 47 ASSERT((buffer_end_ - buffer_) >= 2); | |
| 48 } | |
| 49 | |
| 50 | |
| 51 template<typename Record> | |
| 52 CircularQueue<Record>::~CircularQueue() { | |
| 53 DeleteArray(buffer_); | |
| 54 delete enqueue_semaphore_; | |
| 55 } | |
| 56 | |
| 57 | |
| 58 template<typename Record> | |
| 59 void CircularQueue<Record>::Dequeue(Record* rec) { | |
| 60 ASSERT(!IsEmpty()); | |
| 61 *rec = *dequeue_pos_; | |
| 62 dequeue_pos_ = Next(dequeue_pos_); | |
| 63 // Tell we have a spare record. | |
| 64 enqueue_semaphore_->Signal(); | |
| 65 } | |
| 66 | |
| 67 | |
| 68 template<typename Record> | |
| 69 void CircularQueue<Record>::Enqueue(const Record& rec) { | |
| 70 // Wait until we have at least one spare record. | |
| 71 enqueue_semaphore_->Wait(); | |
| 72 ASSERT(Next(enqueue_pos_) != dequeue_pos_); | |
| 73 *enqueue_pos_ = rec; | |
| 74 enqueue_pos_ = Next(enqueue_pos_); | |
| 75 } | |
| 76 | |
| 77 | |
| 78 template<typename Record> | |
| 79 Record* CircularQueue<Record>::Next(Record* curr) { | |
| 80 return ++curr != buffer_end_ ? curr : buffer_; | |
| 81 } | |
| 82 | |
| 83 | |
| 84 void* SamplingCircularQueue::Enqueue() { | |
| 85 Cell* enqueue_pos = reinterpret_cast<Cell*>( | |
| 86 Thread::GetThreadLocal(producer_key_)); | |
| 87 WrapPositionIfNeeded(&enqueue_pos); | |
| 88 Thread::SetThreadLocal(producer_key_, enqueue_pos + record_size_); | |
| 89 return enqueue_pos; | |
| 90 } | |
| 91 | |
| 92 | |
| 93 void SamplingCircularQueue::WrapPositionIfNeeded( | |
| 94 SamplingCircularQueue::Cell** pos) { | |
| 95 if (**pos == kEnd) *pos = buffer_; | |
| 96 } | |
| 97 | |
| 98 | |
| 99 } } // namespace v8::internal | |
| 100 | |
| 101 #endif // V8_CIRCULAR_BUFFER_INL_H_ | |
| OLD | NEW |