summaryrefslogtreecommitdiff
path: root/chromium/base/synchronization/read_write_lock.h
blob: 4c59b7b1168f18b3ee1281c8184c76d5855656da (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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
// Copyright 2016 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 BASE_SYNCHRONIZATION_READ_WRITE_LOCK_H_
#define BASE_SYNCHRONIZATION_READ_WRITE_LOCK_H_

#include "base/base_export.h"
#include "base/macros.h"
#include "build/build_config.h"

#if defined(OS_NACL)
#include "base/synchronization/lock.h"
#endif

#if defined(OS_WIN)
#include <windows.h>
#elif defined(OS_POSIX)
#include <pthread.h>
#else
#  error No reader-writer lock defined for this platform.
#endif

namespace base {
namespace subtle {

// An OS-independent wrapper around reader-writer locks. There's no magic here.
//
// You are strongly encouraged to use base::Lock instead of this, unless you
// can demonstrate contention and show that this would lead to an improvement.
// This lock does not make any guarantees of fairness, which can lead to writer
// starvation under certain access patterns. You should carefully consider your
// writer access patterns before using this lock.
class BASE_EXPORT ReadWriteLock {
 public:
  ReadWriteLock();
  ~ReadWriteLock();

  // Reader lock functions.
  void ReadAcquire();
  void ReadRelease();

  // Writer lock functions.
  void WriteAcquire();
  void WriteRelease();

 private:
#if defined(OS_WIN)
  using NativeHandle = SRWLOCK;
#elif defined(OS_NACL)
  using NativeHandle = Lock;
#elif defined(OS_POSIX)
  using NativeHandle = pthread_rwlock_t;
#endif

  NativeHandle native_handle_;

#if defined(OS_NACL)
  // Even though NaCl has a pthread_rwlock implementation, the build rules don't
  // make it universally available. So instead, implement a slower and trivial
  // reader-writer lock using a regular mutex.
  // TODO(amistry): Remove this and use the posix implementation when it's
  // available in all build configurations.
  uint32_t readers_ = 0;
  // base::Lock does checking to ensure the lock is acquired and released on the
  // same thread. This is not the case for this lock, so use pthread mutexes
  // directly here.
  pthread_mutex_t writer_lock_ = PTHREAD_MUTEX_INITIALIZER;
#endif

  DISALLOW_COPY_AND_ASSIGN(ReadWriteLock);
};

class AutoReadLock {
 public:
  explicit AutoReadLock(ReadWriteLock& lock) : lock_(lock) {
    lock_.ReadAcquire();
  }
  ~AutoReadLock() {
    lock_.ReadRelease();
  }

 private:
  ReadWriteLock& lock_;
  DISALLOW_COPY_AND_ASSIGN(AutoReadLock);
};

class AutoWriteLock {
 public:
  explicit AutoWriteLock(ReadWriteLock& lock) : lock_(lock) {
    lock_.WriteAcquire();
  }
  ~AutoWriteLock() {
    lock_.WriteRelease();
  }

 private:
  ReadWriteLock& lock_;
  DISALLOW_COPY_AND_ASSIGN(AutoWriteLock);
};

}  // namespace subtle
}  // namespace base

#endif  // BASE_SYNCHRONIZATION_READ_WRITE_LOCK_H_