Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(81)

Side by Side Diff: mojo/public/cpp/bindings/lib/may_auto_lock.h

Issue 2707483002: Mojo C++ bindings: change some std::unique_ptr<base::Lock> to base::Optional<base::Lock>. (Closed)
Patch Set: Created 3 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 // Copyright 2016 The Chromium Authors. All rights reserved. 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 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #ifndef MOJO_PUBLIC_CPP_BINDINGS_LIB_MAY_AUTO_LOCK_H_ 5 #ifndef MOJO_PUBLIC_CPP_BINDINGS_LIB_MAY_AUTO_LOCK_H_
6 #define MOJO_PUBLIC_CPP_BINDINGS_LIB_MAY_AUTO_LOCK_H_ 6 #define MOJO_PUBLIC_CPP_BINDINGS_LIB_MAY_AUTO_LOCK_H_
7 7
8 #include "base/macros.h" 8 #include "base/macros.h"
9 #include "base/optional.h"
9 #include "base/synchronization/lock.h" 10 #include "base/synchronization/lock.h"
10 11
11 namespace mojo { 12 namespace mojo {
12 namespace internal { 13 namespace internal {
13 14
14 // Similar to base::AutoLock, except that it does nothing if |lock| passed into 15 // Similar to base::AutoLock, except that it does nothing if |lock| passed into
15 // the constructor is null. 16 // the constructor is null.
16 class MayAutoLock { 17 class MayAutoLock {
17 public: 18 public:
18 explicit MayAutoLock(base::Lock* lock) : lock_(lock) { 19 explicit MayAutoLock(base::Optional<base::Lock>* lock)
20 : lock_(lock->has_value() ? &lock->value() : nullptr) {
19 if (lock_) 21 if (lock_)
20 lock_->Acquire(); 22 lock_->Acquire();
21 } 23 }
22 24
23 ~MayAutoLock() { 25 ~MayAutoLock() {
24 if (lock_) { 26 if (lock_) {
25 lock_->AssertAcquired(); 27 lock_->AssertAcquired();
26 lock_->Release(); 28 lock_->Release();
27 } 29 }
28 } 30 }
29 31
30 private: 32 private:
31 base::Lock* lock_; 33 base::Lock* lock_;
32 DISALLOW_COPY_AND_ASSIGN(MayAutoLock); 34 DISALLOW_COPY_AND_ASSIGN(MayAutoLock);
33 }; 35 };
34 36
35 // Similar to base::AutoUnlock, except that it does nothing if |lock| passed 37 // Similar to base::AutoUnlock, except that it does nothing if |lock| passed
36 // into the constructor is null. 38 // into the constructor is null.
37 class MayAutoUnlock { 39 class MayAutoUnlock {
38 public: 40 public:
39 explicit MayAutoUnlock(base::Lock* lock) : lock_(lock) { 41 explicit MayAutoUnlock(base::Optional<base::Lock>* lock)
42 : lock_(lock->has_value() ? &lock->value() : nullptr) {
40 if (lock_) { 43 if (lock_) {
41 lock_->AssertAcquired(); 44 lock_->AssertAcquired();
42 lock_->Release(); 45 lock_->Release();
43 } 46 }
44 } 47 }
45 48
46 ~MayAutoUnlock() { 49 ~MayAutoUnlock() {
47 if (lock_) 50 if (lock_)
48 lock_->Acquire(); 51 lock_->Acquire();
49 } 52 }
50 53
51 private: 54 private:
52 base::Lock* lock_; 55 base::Lock* lock_;
53 DISALLOW_COPY_AND_ASSIGN(MayAutoUnlock); 56 DISALLOW_COPY_AND_ASSIGN(MayAutoUnlock);
54 }; 57 };
55 58
56 } // namespace internal 59 } // namespace internal
57 } // namespace mojo 60 } // namespace mojo
58 61
59 #endif // MOJO_PUBLIC_CPP_BINDINGS_LIB_MAY_AUTO_LOCK_H_ 62 #endif // MOJO_PUBLIC_CPP_BINDINGS_LIB_MAY_AUTO_LOCK_H_
OLDNEW
« no previous file with comments | « mojo/public/cpp/bindings/lib/connector.cc ('k') | mojo/public/cpp/bindings/lib/multiplex_router.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698