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"
15 class SharedMemorySeqLockReaderBase
{
17 SharedMemorySeqLockReaderBase();
18 virtual ~SharedMemorySeqLockReaderBase();
20 void* InitializeSharedMemory(
21 base::SharedMemoryHandle shared_memory_handle
,
24 bool FetchFromBuffer(content::OneWriterSeqLock
* seqlock
, void* final
,
25 void* temp
, void* from
, size_t size
);
27 static const int kMaximumContentionCount
= 10;
28 base::SharedMemoryHandle renderer_shared_memory_handle_
;
29 scoped_ptr
<base::SharedMemory
> renderer_shared_memory_
;
32 } // namespace internal
36 // Template argument Data should be a pod-like structure only containing
37 // data fields, such that it is copyable by memcpy method.
38 template<typename Data
>
39 class SharedMemorySeqLockReader
40 : private internal::SharedMemorySeqLockReaderBase
{
42 SharedMemorySeqLockReader() : buffer_(0) { }
43 virtual ~SharedMemorySeqLockReader() { }
45 bool GetLatestData(Data
* data
) {
47 DCHECK(sizeof(*data
) == sizeof(*temp_buffer_
));
48 return FetchFromBuffer(&buffer_
->seqlock
, data
, temp_buffer_
.get(),
49 &buffer_
->data
, sizeof(*temp_buffer_
));
52 bool Initialize(base::SharedMemoryHandle shared_memory_handle
) {
53 if (void* memory
= InitializeSharedMemory(
54 shared_memory_handle
, sizeof(SharedMemorySeqLockBuffer
<Data
>))) {
55 buffer_
= static_cast<SharedMemorySeqLockBuffer
<Data
>*>(memory
);
56 temp_buffer_
.reset(new Data
);
63 SharedMemorySeqLockBuffer
<Data
>* buffer_
;
64 scoped_ptr
<Data
> temp_buffer_
;
66 DISALLOW_COPY_AND_ASSIGN(SharedMemorySeqLockReader
);
69 } // namespace content
71 #endif // CONTENT_RENDERER_SHARED_MEMORY_SEQLOCK_READER_H_