1 // Copyright 2018 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_TASK_SEQUENCE_MANAGER_MOVEABLE_AUTO_LOCK_H_ 6 #define BASE_TASK_SEQUENCE_MANAGER_MOVEABLE_AUTO_LOCK_H_ 7 8 #include "base/synchronization/lock.h" 9 10 namespace base { 11 namespace sequence_manager { 12 13 class MoveableAutoLock { 14 public: MoveableAutoLock(Lock & lock)15 explicit MoveableAutoLock(Lock& lock) : lock_(lock), moved_(false) { 16 lock_.Acquire(); 17 } 18 MoveableAutoLock(MoveableAutoLock && other)19 MoveableAutoLock(MoveableAutoLock&& other) noexcept 20 : lock_(other.lock_), moved_(other.moved_) { 21 lock_.AssertAcquired(); 22 other.moved_ = true; 23 } 24 ~MoveableAutoLock()25 ~MoveableAutoLock() { 26 if (moved_) 27 return; 28 lock_.AssertAcquired(); 29 lock_.Release(); 30 } 31 32 private: 33 Lock& lock_; 34 bool moved_; 35 DISALLOW_COPY_AND_ASSIGN(MoveableAutoLock); 36 }; 37 38 } // namespace sequence_manager 39 } // namespace base 40 41 #endif // BASE_TASK_SEQUENCE_MANAGER_MOVEABLE_AUTO_LOCK_H_ 42