Chromium Code Reviews| Index: base/synchronization/rw_lock.h |
| diff --git a/base/synchronization/rw_lock.h b/base/synchronization/rw_lock.h |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..04d3f18f4f5d0de67e8b1e4a46e89190df1d9cce |
| --- /dev/null |
| +++ b/base/synchronization/rw_lock.h |
| @@ -0,0 +1,92 @@ |
| +// 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_RW_LOCK_H_ |
| +#define BASE_SYNCHRONIZATION_RW_LOCK_H_ |
| + |
| +#include "base/base_export.h" |
| +#include "base/macros.h" |
| +#include "build/build_config.h" |
| + |
| +#if defined(OS_WIN) |
| +#include <windows.h> |
| +#elif defined(OS_NACL) |
| +#include "base/synchronization/lock.h" |
| +#elif defined(OS_POSIX) |
| +#include <pthread.h> |
| +#else |
| +# error No reader-writer lock defined for this platform. |
| +#endif |
| + |
| +namespace base { |
| + |
| +// An OS-independent wrapper around reader-writer locks. There's no magic here. |
| +class BASE_EXPORT RWLock { |
|
danakj
2016/05/19 23:01:36
ReadWriteLock or ReaderWriterLock. Then make the f
Anand Mistry (off Chromium)
2016/05/20 04:06:38
Done.
|
| + public: |
| + RWLock(); |
| + ~RWLock(); |
| + |
| + // 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; |
| + Lock writer_lock_; |
| +#endif |
| + |
| + DISALLOW_COPY_AND_ASSIGN(RWLock); |
| +}; |
| + |
| +class AutoReadLock { |
| + public: |
| + explicit AutoReadLock(RWLock &lock) : lock_(lock) { |
|
danakj
2016/05/19 23:01:36
the & should go on the type not the variable name
Anand Mistry (off Chromium)
2016/05/20 04:06:38
Done.
|
| + lock_.ReadAcquire(); |
| + } |
| + ~AutoReadLock() { |
| + lock_.ReadRelease(); |
| + } |
| + |
| + private: |
| + RWLock& lock_; |
| + DISALLOW_COPY_AND_ASSIGN(AutoReadLock); |
| +}; |
| + |
| +class AutoWriteLock { |
| + public: |
| + explicit AutoWriteLock(RWLock &lock) : lock_(lock) { |
|
danakj
2016/05/19 23:01:36
ditto
Anand Mistry (off Chromium)
2016/05/20 04:06:38
Done.
|
| + lock_.WriteAcquire(); |
| + } |
| + ~AutoWriteLock() { |
| + lock_.WriteRelease(); |
| + } |
| + |
| + private: |
| + RWLock& lock_; |
| + DISALLOW_COPY_AND_ASSIGN(AutoWriteLock); |
| +}; |
| + |
| +} // namespace base |
| + |
| +#endif // BASE_SYNCHRONIZATION_RW_LOCK_H_ |