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, public NonCopyable { 32 public: 33 /** 34 * Allows the platform to do any mutex initialization at construction time. 35 */ 36 Mutex(); 37 38 /** 39 * Allows the platform to do any mutex deinitialization at destruction time. 40 */ 41 ~Mutex(); 42 43 /** 44 * Locks the mutex, or blocks if it is held by another thread. Illegal to call 45 * if the current thread already holds the lock. 46 */ 47 void lock(); 48 49 /** 50 * Attempts to lock the mutex. If it is already held by some other thread, 51 * returns immediately. Illegal to call if the current thread already holds 52 * the lock. 53 * 54 * @return true if the mutex was acquired, false otherwise 55 */ 56 bool try_lock(); 57 58 /** 59 * Unlocks the mutex. Illegal to call if the current thread does not hold the 60 * lock. 61 */ 62 void unlock(); 63 }; 64 65 } // namespace chre 66 67 #include "chre/target_platform/mutex_base_impl.h" 68 69 #endif // CHRE_PLATFORM_MUTEX_H_ 70