1 /*
2  * Copyright (C) 2017 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #pragma once
18 
19 #include <mutex>
20 #include <optional>
21 #include <type_traits>
22 #include <utility>
23 
24 #include "android-base/macros.h"
25 
26 namespace android {
27 
28 template <typename T>
29 class ScopedLock;
30 
31 // Owns the guarded object and protects access to it via a mutex.
32 // The guarded object is inaccessible via this class.
33 // The mutex is locked and the object accessed via the ScopedLock<T> class.
34 //
35 // NOTE: The template parameter T should not be a raw pointer, since ownership
36 // is ambiguous and error-prone. Instead use an std::unique_ptr<>.
37 //
38 // Example use:
39 //
40 //   Guarded<std::string> shared_string("hello");
41 //   {
42 //     ScopedLock<std::string> locked_string(shared_string);
43 //     *locked_string += " world";
44 //   }
45 //
46 template <typename T>
47 class Guarded {
48   static_assert(!std::is_pointer_v<T>, "T must not be a raw pointer");
49 
50  public:
Guarded()51   Guarded() : guarded_(std::in_place) {
52   }
53 
Guarded(const T & guarded)54   explicit Guarded(const T& guarded) : guarded_(std::in_place, guarded) {
55   }
56 
Guarded(T && guarded)57   explicit Guarded(T&& guarded) : guarded_(std::in_place, std::move(guarded)) {
58   }
59 
60   // Unfortunately, some legacy designs make even class deletion race-prone, where some other
61   // thread may have not finished working with the same object. For those cases one may destroy the
62   // object under a lock (but please fix your code, at least eventually!).
63   template <class Func>
safeDelete(Func f)64   void safeDelete(Func f) {
65     std::lock_guard scoped_lock(lock_);
66     f(guarded_ ? &guarded_.value() : nullptr);
67     guarded_.reset();
68   }
69 
70  private:
71   friend class ScopedLock<T>;
72   DISALLOW_COPY_AND_ASSIGN(Guarded);
73 
74   std::mutex lock_;
75   std::optional<T> guarded_;
76 };
77 
78 template <typename T>
79 class ScopedLock {
80  public:
ScopedLock(Guarded<T> & guarded)81   explicit ScopedLock(Guarded<T>& guarded) : lock_(guarded.lock_), guarded_(*guarded.guarded_) {
82   }
83 
84   T& operator*() {
85     return guarded_;
86   }
87 
88   T* operator->() {
89     return &guarded_;
90   }
91 
get()92   T* get() {
93     return &guarded_;
94   }
95 
96  private:
97   DISALLOW_COPY_AND_ASSIGN(ScopedLock);
98 
99   std::lock_guard<std::mutex> lock_;
100   T& guarded_;
101 };
102 
103 }  // namespace android
104