OLD | NEW |
1 // Copyright 2010 the V8 project authors. All rights reserved. | 1 // Copyright 2010 the V8 project authors. All rights reserved. |
2 // Redistribution and use in source and binary forms, with or without | 2 // Redistribution and use in source and binary forms, with or without |
3 // modification, are permitted provided that the following conditions are | 3 // modification, are permitted provided that the following conditions are |
4 // met: | 4 // met: |
5 // | 5 // |
6 // * Redistributions of source code must retain the above copyright | 6 // * Redistributions of source code must retain the above copyright |
7 // notice, this list of conditions and the following disclaimer. | 7 // notice, this list of conditions and the following disclaimer. |
8 // * Redistributions in binary form must reproduce the above | 8 // * Redistributions in binary form must reproduce the above |
9 // copyright notice, this list of conditions and the following | 9 // copyright notice, this list of conditions and the following |
10 // disclaimer in the documentation and/or other materials provided | 10 // disclaimer in the documentation and/or other materials provided |
(...skipping 16 matching lines...) Expand all Loading... |
27 | 27 |
28 #ifndef V8_CIRCULAR_BUFFER_INL_H_ | 28 #ifndef V8_CIRCULAR_BUFFER_INL_H_ |
29 #define V8_CIRCULAR_BUFFER_INL_H_ | 29 #define V8_CIRCULAR_BUFFER_INL_H_ |
30 | 30 |
31 #include "circular-queue.h" | 31 #include "circular-queue.h" |
32 | 32 |
33 namespace v8 { | 33 namespace v8 { |
34 namespace internal { | 34 namespace internal { |
35 | 35 |
36 | 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_( | |
42 OS::CreateSemaphore(static_cast<int>(buffer_end_ - buffer_) - 1)), | |
43 enqueue_pos_(buffer_), | |
44 dequeue_pos_(buffer_) { | |
45 // To be able to distinguish between a full and an empty queue | |
46 // state, the queue must be capable of containing at least 2 | |
47 // records. | |
48 ASSERT((buffer_end_ - buffer_) >= 2); | |
49 } | |
50 | |
51 | |
52 template<typename Record> | |
53 CircularQueue<Record>::~CircularQueue() { | |
54 DeleteArray(buffer_); | |
55 delete enqueue_semaphore_; | |
56 } | |
57 | |
58 | |
59 template<typename Record> | |
60 void CircularQueue<Record>::Dequeue(Record* rec) { | |
61 ASSERT(!IsEmpty()); | |
62 *rec = *dequeue_pos_; | |
63 dequeue_pos_ = Next(dequeue_pos_); | |
64 // Tell we have a spare record. | |
65 enqueue_semaphore_->Signal(); | |
66 } | |
67 | |
68 | |
69 template<typename Record> | |
70 void CircularQueue<Record>::Enqueue(const Record& rec) { | |
71 // Wait until we have at least one spare record. | |
72 enqueue_semaphore_->Wait(); | |
73 ASSERT(Next(enqueue_pos_) != dequeue_pos_); | |
74 *enqueue_pos_ = rec; | |
75 enqueue_pos_ = Next(enqueue_pos_); | |
76 } | |
77 | |
78 | |
79 template<typename Record> | |
80 Record* CircularQueue<Record>::Next(Record* curr) { | |
81 return ++curr != buffer_end_ ? curr : buffer_; | |
82 } | |
83 | |
84 | |
85 void* SamplingCircularQueue::Enqueue() { | 37 void* SamplingCircularQueue::Enqueue() { |
86 WrapPositionIfNeeded(&producer_pos_->enqueue_pos); | 38 WrapPositionIfNeeded(&producer_pos_->enqueue_pos); |
87 void* result = producer_pos_->enqueue_pos; | 39 void* result = producer_pos_->enqueue_pos; |
88 producer_pos_->enqueue_pos += record_size_; | 40 producer_pos_->enqueue_pos += record_size_; |
89 return result; | 41 return result; |
90 } | 42 } |
91 | 43 |
92 | 44 |
93 void SamplingCircularQueue::WrapPositionIfNeeded( | 45 void SamplingCircularQueue::WrapPositionIfNeeded( |
94 SamplingCircularQueue::Cell** pos) { | 46 SamplingCircularQueue::Cell** pos) { |
95 if (**pos == kEnd) *pos = buffer_; | 47 if (**pos == kEnd) *pos = buffer_; |
96 } | 48 } |
97 | 49 |
98 | 50 |
99 } } // namespace v8::internal | 51 } } // namespace v8::internal |
100 | 52 |
101 #endif // V8_CIRCULAR_BUFFER_INL_H_ | 53 #endif // V8_CIRCULAR_BUFFER_INL_H_ |
OLD | NEW |