Chromium Code Reviews| Index: mojo/public/cpp/bindings/lib/may_auto_lock.h |
| diff --git a/mojo/public/cpp/bindings/lib/may_auto_lock.h b/mojo/public/cpp/bindings/lib/may_auto_lock.h |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..0eba7356002bc30b4ade6aeb1c06371fb0f6dd4f |
| --- /dev/null |
| +++ b/mojo/public/cpp/bindings/lib/may_auto_lock.h |
| @@ -0,0 +1,55 @@ |
| +// 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. |
| + |
| +#include "base/macros.h" |
| +#include "base/synchronization/lock.h" |
| + |
| +namespace mojo { |
| +namespace internal { |
| + |
| +// Similar to base::AutoLock, except that it does nothing if |lock| passed into |
| +// the constructor is null. |
| +class MayAutoLock { |
| + public: |
| + explicit MayAutoLock(base::Lock* lock) : lock_(lock) { |
| + if (lock_) |
| + lock_->Acquire(); |
| + } |
| + |
| + ~MayAutoLock() { |
| + if (lock_) { |
| + lock_->AssertAcquired(); |
| + lock_->Release(); |
| + } |
| + } |
| + |
| + private: |
| + base::Lock* lock_; |
| + DISALLOW_COPY_AND_ASSIGN(MayAutoLock); |
| +}; |
| + |
| +// Similar to base::AutoUnlock, except that it does nothing if |lock| passed |
| +// into |
|
Ken Rockot(use gerrit already)
2016/09/15 20:21:41
nit: weird formatting
yzshen1
2016/09/15 21:24:59
Done.
I guess git cl format is not smart enough!
|
| +// the constructor is null. |
| +class MayAutoUnlock { |
| + public: |
| + explicit MayAutoUnlock(base::Lock* lock) : lock_(lock) { |
| + if (lock_) { |
| + lock_->AssertAcquired(); |
| + lock_->Release(); |
| + } |
| + } |
| + |
| + ~MayAutoUnlock() { |
| + if (lock_) |
| + lock_->Acquire(); |
| + } |
| + |
| + private: |
| + base::Lock* lock_; |
| + DISALLOW_COPY_AND_ASSIGN(MayAutoUnlock); |
| +}; |
| + |
| +} // namespace internal |
| +} // namespace mojo |