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_PLATFORM_MUTEX_H_ 18 #define CHRE_PLATFORM_MUTEX_H_ 19 20 #include "chre/target_platform/mutex_base.h" 21 #include "chre/util/non_copyable.h" 22 23 namespace chre { 24 25 /** 26 * Provides an implementation of a Mutex. The public API meets the BasicLockable 27 * requirements in order to be compatible with std::lock_guard. MutexBase is 28 * subclassed here to allow platforms to inject their own storage for their 29 * mutex implementation. 30 */ 31 class Mutex : public MutexBase, 32 public NonCopyable { 33 public: 34 /** 35 * Allows the platform to do any mutex initialization at construction time. 36 */ 37 Mutex(); 38 39 /** 40 * Allows the platform to do any mutex deinitialization at destruction time. 41 */ 42 ~Mutex(); 43 44 /** 45 * Locks the mutex, or blocks if it is held by another thread. Illegal to call 46 * if the current thread already holds the lock. 47 */ 48 void lock(); 49 50 /** 51 * Attempts to lock the mutex. If it is already held by some other thread, 52 * returns immediately. Illegal to call if the current thread already holds 53 * the lock. 54 * 55 * @return true if the mutex was acquired, false otherwise 56 */ 57 bool try_lock(); 58 59 /** 60 * Unlocks the mutex. Illegal to call if the current thread does not hold the 61 * lock. 62 */ 63 void unlock(); 64 }; 65 66 } // namespace chre 67 68 #include "chre/target_platform/mutex_base_impl.h" 69 70 #endif // CHRE_PLATFORM_MUTEX_H_ 71