1 /** 2 * @file unique_rwlock.h 3 * 4 * @section LICENSE 5 * 6 * The MIT License 7 * 8 * @copyright Copyright (c) 2021 TileDB, Inc. 9 * 10 * Permission is hereby granted, free of charge, to any person obtaining a copy 11 * of this software and associated documentation files (the "Software"), to deal 12 * in the Software without restriction, including without limitation the rights 13 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 14 * copies of the Software, and to permit persons to whom the Software is 15 * furnished to do so, subject to the following conditions: 16 * 17 * The above copyright notice and this permission notice shall be included in 18 * all copies or substantial portions of the Software. 19 * 20 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 21 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 22 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 23 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 24 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 25 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 26 * THE SOFTWARE. 27 * 28 * @section DESCRIPTION 29 * 30 * This file defines and implements the UniqueRWLock<R> class and 31 * type-defintions for UniqueReadLock and UniqueWriteLock. 32 */ 33 34 #ifndef TILEDB_UNIQUE_RWLOCK_H 35 #define TILEDB_UNIQUE_RWLOCK_H 36 37 #include "tiledb/common/macros.h" 38 #include "tiledb/common/rwlock.h" 39 40 namespace tiledb { 41 namespace common { 42 43 template <bool R> 44 class UniqueRWLock final { 45 public: 46 /* ********************************* */ 47 /* CONSTRUCTORS & DESTRUCTORS */ 48 /* ********************************* */ 49 50 /* 51 * Value constructor. Locks `rwlock` and releases 52 * it in the destructor. If template-type `R` is 53 * true, this class performs read-locking. Otherwise, 54 * this performs write-locking. 55 * 56 * @param rwlock the read-write lock. 57 */ UniqueRWLock(RWLock * const rwlock)58 explicit UniqueRWLock(RWLock* const rwlock) 59 : rwlock_(rwlock) 60 , locked_(false) { 61 assert(rwlock_); 62 63 lock(); 64 } 65 66 /** Destructor. Releases the read-write lock. */ ~UniqueRWLock()67 ~UniqueRWLock() { 68 if (locked_) 69 unlock(); 70 } 71 72 DISABLE_COPY_AND_COPY_ASSIGN(UniqueRWLock); 73 DISABLE_MOVE_AND_MOVE_ASSIGN(UniqueRWLock); 74 75 /* ********************************* */ 76 /* API */ 77 /* ********************************* */ 78 79 /** Acquires the read-write lock. */ lock()80 void lock() { 81 assert(!locked_); 82 83 if (R) 84 rwlock_->read_lock(); 85 else 86 rwlock_->write_lock(); 87 88 locked_ = true; 89 } 90 91 /** Releases the read-write lock. */ unlock()92 void unlock() { 93 assert(locked_); 94 95 if (R) 96 rwlock_->read_unlock(); 97 else 98 rwlock_->write_unlock(); 99 100 locked_ = false; 101 } 102 103 private: 104 /* ********************************* */ 105 /* PRIVATE ATTRIBUTES */ 106 /* ********************************* */ 107 108 /** The read-write lock. */ 109 RWLock* const rwlock_; 110 111 /** True if holding a lock on `rwlock_`. */ 112 bool locked_; 113 }; 114 115 // Type-define UniqueReadLock and UniqueWriteLock. 116 using UniqueReadLock = UniqueRWLock<true>; 117 using UniqueWriteLock = UniqueRWLock<false>; 118 119 } // namespace common 120 } // namespace tiledb 121 122 #endif // TILEDB_UNIQUE_RWLOCK_H 123