lock_guard.h revision ac023ca930f64b385a77f89cb332e312f9b07362
1/*
2 * Copyright (C) 2016 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#ifndef CHRE_UTIL_LOCK_GUARD_H_
18#define CHRE_UTIL_LOCK_GUARD_H_
19
20#include "chre/util/non_copyable.h"
21
22namespace chre {
23
24/**
25 * This is an RAII-style wrapper for a mutex that allows acquiring and releasing
26 * locks that follow the scope of the guard. The concept is the same as
27 * std::lock_guard.
28 */
29template<typename MutexType>
30class LockGuard : public NonCopyable {
31 public:
32  /**
33   * Constructs a LockGuard and acquires the lock.
34   */
35  LockGuard(MutexType& mutex);
36
37  /**
38   * Deconstructs a LockGuard and releases the lock.
39   */
40  ~LockGuard();
41
42 private:
43  //! The mutex to lock and unlock on destruction.
44  MutexType& mMutex;
45};
46
47}  // namespace chre
48
49#include "chre/util/lock_guard_impl.h"
50
51#endif  // CHRE_UTIL_LOCK_GUARD_H_
52