Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 // Copyright 2016 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 BASE_SYNCHRONIZATION_RW_LOCK_H_ | |
| 6 #define BASE_SYNCHRONIZATION_RW_LOCK_H_ | |
| 7 | |
| 8 #include "base/base_export.h" | |
| 9 #include "base/macros.h" | |
| 10 #include "build/build_config.h" | |
| 11 | |
| 12 #if defined(OS_WIN) | |
| 13 #include <windows.h> | |
| 14 #elif defined(OS_NACL) | |
| 15 #include "base/synchronization/lock.h" | |
| 16 #elif defined(OS_POSIX) | |
| 17 #include <pthread.h> | |
| 18 #else | |
| 19 # error No reader-writer lock defined for this platform. | |
| 20 #endif | |
| 21 | |
| 22 namespace base { | |
| 23 | |
| 24 // An OS-independent wrapper around reader-writer locks. There's no magic here. | |
| 25 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.
| |
| 26 public: | |
| 27 RWLock(); | |
| 28 ~RWLock(); | |
| 29 | |
| 30 // Reader lock functions. | |
| 31 void ReadAcquire(); | |
| 32 void ReadRelease(); | |
| 33 | |
| 34 // Writer lock functions. | |
| 35 void WriteAcquire(); | |
| 36 void WriteRelease(); | |
| 37 | |
| 38 private: | |
| 39 #if defined(OS_WIN) | |
| 40 using NativeHandle = SRWLOCK; | |
| 41 #elif defined(OS_NACL) | |
| 42 using NativeHandle = Lock; | |
| 43 #elif defined(OS_POSIX) | |
| 44 using NativeHandle = pthread_rwlock_t; | |
| 45 #endif | |
| 46 | |
| 47 NativeHandle native_handle_; | |
| 48 | |
| 49 #if defined(OS_NACL) | |
| 50 // Even though NaCl has a pthread_rwlock implementation, the build rules don't | |
| 51 // make it universally available. So instead, implement a slower and trivial | |
| 52 // reader-writer lock using a regular mutex. | |
| 53 // TODO(amistry): Remove this and use the posix implementation when it's | |
| 54 // available in all build configurations. | |
| 55 uint32_t readers_ = 0; | |
| 56 Lock writer_lock_; | |
| 57 #endif | |
| 58 | |
| 59 DISALLOW_COPY_AND_ASSIGN(RWLock); | |
| 60 }; | |
| 61 | |
| 62 class AutoReadLock { | |
| 63 public: | |
| 64 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.
| |
| 65 lock_.ReadAcquire(); | |
| 66 } | |
| 67 ~AutoReadLock() { | |
| 68 lock_.ReadRelease(); | |
| 69 } | |
| 70 | |
| 71 private: | |
| 72 RWLock& lock_; | |
| 73 DISALLOW_COPY_AND_ASSIGN(AutoReadLock); | |
| 74 }; | |
| 75 | |
| 76 class AutoWriteLock { | |
| 77 public: | |
| 78 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.
| |
| 79 lock_.WriteAcquire(); | |
| 80 } | |
| 81 ~AutoWriteLock() { | |
| 82 lock_.WriteRelease(); | |
| 83 } | |
| 84 | |
| 85 private: | |
| 86 RWLock& lock_; | |
| 87 DISALLOW_COPY_AND_ASSIGN(AutoWriteLock); | |
| 88 }; | |
| 89 | |
| 90 } // namespace base | |
| 91 | |
| 92 #endif // BASE_SYNCHRONIZATION_RW_LOCK_H_ | |
| OLD | NEW |