summaryrefslogtreecommitdiffstats
path: root/content/renderer/shared_memory_seqlock_reader.h
blob: 7fd7beb95bbcb951d82ac333d7fd75237eb060de (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
// Copyright 2013 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef CONTENT_RENDERER_SHARED_MEMORY_SEQLOCK_READER_H_
#define CONTENT_RENDERER_SHARED_MEMORY_SEQLOCK_READER_H_

#include "base/logging.h"
#include "base/memory/scoped_ptr.h"
#include "base/memory/shared_memory.h"
#include "content/common/shared_memory_seqlock_buffer.h"

namespace content {
namespace internal {

class SharedMemorySeqLockReaderBase  {
 protected:
  SharedMemorySeqLockReaderBase();
  virtual ~SharedMemorySeqLockReaderBase();

  void* InitializeSharedMemory(
      base::SharedMemoryHandle shared_memory_handle,
      size_t buffer_size);

  bool FetchFromBuffer(content::OneWriterSeqLock* seqlock, void* final,
      void* temp, void* from, size_t size);

  static const int kMaximumContentionCount = 10;
  base::SharedMemoryHandle renderer_shared_memory_handle_;
  scoped_ptr<base::SharedMemory> renderer_shared_memory_;
};

}  // namespace internal

// Template argument Data should be a pod-like structure only containing
// data fields, such that it is copyable by memcpy method.
template<typename Data>
class SharedMemorySeqLockReader
    : private internal::SharedMemorySeqLockReaderBase {
 public:
  SharedMemorySeqLockReader() : buffer_(0) { }
  virtual ~SharedMemorySeqLockReader() { }

  bool GetLatestData(Data* data) {
    DCHECK(buffer_);
    DCHECK(sizeof(*data) == sizeof(*temp_buffer_));
    return FetchFromBuffer(&buffer_->seqlock, data, temp_buffer_.get(),
        &buffer_->data, sizeof(*temp_buffer_));
  }

  bool Initialize(base::SharedMemoryHandle shared_memory_handle) {
    if (void* memory = InitializeSharedMemory(
        shared_memory_handle, sizeof(SharedMemorySeqLockBuffer<Data>))) {
      buffer_ = static_cast<SharedMemorySeqLockBuffer<Data>*>(memory);
      temp_buffer_.reset(new Data);
      return true;
    }
    return false;
  }

 private:
  SharedMemorySeqLockBuffer<Data>* buffer_;
  scoped_ptr<Data> temp_buffer_;

  DISALLOW_COPY_AND_ASSIGN(SharedMemorySeqLockReader);
};

}  // namespace content

#endif  // CONTENT_RENDERER_SHARED_MEMORY_SEQLOCK_READER_H_