Home | History | Annotate | Download | only in renderer
      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.
      4 
      5 #ifndef CONTENT_RENDERER_SHARED_MEMORY_SEQLOCK_READER_H_
      6 #define CONTENT_RENDERER_SHARED_MEMORY_SEQLOCK_READER_H_
      7 
      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"
     12 
     13 namespace internal {
     14 
     15 class SharedMemorySeqLockReaderBase  {
     16  protected:
     17   SharedMemorySeqLockReaderBase();
     18   virtual ~SharedMemorySeqLockReaderBase();
     19 
     20   void* InitializeSharedMemory(
     21       base::SharedMemoryHandle shared_memory_handle,
     22       size_t buffer_size);
     23 
     24   bool FetchFromBuffer(content::OneWriterSeqLock* seqlock, void* final,
     25       void* temp, void* from, size_t size);
     26 
     27   static const int kMaximumContentionCount = 10;
     28   base::SharedMemoryHandle renderer_shared_memory_handle_;
     29   scoped_ptr<base::SharedMemory> renderer_shared_memory_;
     30 };
     31 
     32 }  // namespace internal
     33 
     34 namespace content {
     35 
     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 {
     41  public:
     42   SharedMemorySeqLockReader() : buffer_(0) { }
     43   virtual ~SharedMemorySeqLockReader() { }
     44 
     45   bool GetLatestData(Data* data) {
     46     DCHECK(buffer_);
     47     DCHECK(sizeof(*data) == sizeof(*temp_buffer_));
     48     return FetchFromBuffer(&buffer_->seqlock, data, temp_buffer_.get(),
     49         &buffer_->data, sizeof(*temp_buffer_));
     50   }
     51 
     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);
     57       return true;
     58     }
     59     return false;
     60   }
     61 
     62  private:
     63   SharedMemorySeqLockBuffer<Data>* buffer_;
     64   scoped_ptr<Data> temp_buffer_;
     65 
     66   DISALLOW_COPY_AND_ASSIGN(SharedMemorySeqLockReader);
     67 };
     68 
     69 }  // namespace content
     70 
     71 #endif  // CONTENT_RENDERER_SHARED_MEMORY_SEQLOCK_READER_H_
     72