Further cleanup of some assets
[chromium-blink-merge.git] / content / renderer / shared_memory_seqlock_reader.h
blob7fd7beb95bbcb951d82ac333d7fd75237eb060de
1 // Copyright 2013 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #ifndef CONTENT_RENDERER_SHARED_MEMORY_SEQLOCK_READER_H_
6 #define CONTENT_RENDERER_SHARED_MEMORY_SEQLOCK_READER_H_
8 #include "base/logging.h"
9 #include "base/memory/scoped_ptr.h"
10 #include "base/memory/shared_memory.h"
11 #include "content/common/shared_memory_seqlock_buffer.h"
13 namespace content {
14 namespace internal {
16 class SharedMemorySeqLockReaderBase {
17 protected:
18 SharedMemorySeqLockReaderBase();
19 virtual ~SharedMemorySeqLockReaderBase();
21 void* InitializeSharedMemory(
22 base::SharedMemoryHandle shared_memory_handle,
23 size_t buffer_size);
25 bool FetchFromBuffer(content::OneWriterSeqLock* seqlock, void* final,
26 void* temp, void* from, size_t size);
28 static const int kMaximumContentionCount = 10;
29 base::SharedMemoryHandle renderer_shared_memory_handle_;
30 scoped_ptr<base::SharedMemory> renderer_shared_memory_;
33 } // namespace internal
35 // Template argument Data should be a pod-like structure only containing
36 // data fields, such that it is copyable by memcpy method.
37 template<typename Data>
38 class SharedMemorySeqLockReader
39 : private internal::SharedMemorySeqLockReaderBase {
40 public:
41 SharedMemorySeqLockReader() : buffer_(0) { }
42 virtual ~SharedMemorySeqLockReader() { }
44 bool GetLatestData(Data* data) {
45 DCHECK(buffer_);
46 DCHECK(sizeof(*data) == sizeof(*temp_buffer_));
47 return FetchFromBuffer(&buffer_->seqlock, data, temp_buffer_.get(),
48 &buffer_->data, sizeof(*temp_buffer_));
51 bool Initialize(base::SharedMemoryHandle shared_memory_handle) {
52 if (void* memory = InitializeSharedMemory(
53 shared_memory_handle, sizeof(SharedMemorySeqLockBuffer<Data>))) {
54 buffer_ = static_cast<SharedMemorySeqLockBuffer<Data>*>(memory);
55 temp_buffer_.reset(new Data);
56 return true;
58 return false;
61 private:
62 SharedMemorySeqLockBuffer<Data>* buffer_;
63 scoped_ptr<Data> temp_buffer_;
65 DISALLOW_COPY_AND_ASSIGN(SharedMemorySeqLockReader);
68 } // namespace content
70 #endif // CONTENT_RENDERER_SHARED_MEMORY_SEQLOCK_READER_H_