74 lines · c
1//===--- A platform independent abstraction layer for mutexes ---*- C++ -*-===//2//3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.4// See https://llvm.org/LICENSE.txt for license information.5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception6//7//===----------------------------------------------------------------------===//8 9#ifndef LLVM_LIBC_SRC___SUPPORT_THREADS_MUTEX_H10#define LLVM_LIBC_SRC___SUPPORT_THREADS_MUTEX_H11 12#include "src/__support/macros/attributes.h"13#include "src/__support/macros/config.h"14 15#if LIBC_THREAD_MODE == LIBC_THREAD_MODE_PLATFORM16 17// Platform independent code will include this header file which pulls18// the platform specific specializations using platform macros.19//20// The platform specific specializations should define a class by name21// Mutex with non-static methods having the following signature:22//23// MutexError lock();24// MutexError trylock();25// MutexError timedlock(...);26// MutexError unlock();27// MutexError reset(); // Used to reset inconsistent robust mutexes.28//29// Apart from the above non-static methods, the specializations should30// also provide few static methods with the following signature:31//32// static MutexError init(mtx_t *);33// static MutexError destroy(mtx_t *);34//35// All of the static and non-static methods should ideally be implemented36// as inline functions so that implementations of public functions can37// call them without a function call overhead.38//39// Another point to keep in mind that is that the libc internally needs a40// few global locks. So, to avoid static initialization order fiasco, we41// want the constructors of the Mutex classes to be constexprs.42 43#if defined(__linux__)44#include "src/__support/threads/linux/mutex.h"45#endif // __linux__46 47#elif LIBC_THREAD_MODE == LIBC_THREAD_MODE_SINGLE48 49#include "src/__support/threads/mutex_common.h"50 51namespace LIBC_NAMESPACE_DECL {52 53/// Implementation of a simple passthrough mutex which guards nothing. A54/// complete Mutex locks in general cannot be implemented on the GPU, or on some55/// baremetal platforms. We simply define the Mutex interface and require that56/// only a single thread executes code requiring a mutex lock.57struct Mutex {58 LIBC_INLINE constexpr Mutex(bool, bool, bool, bool) {}59 60 LIBC_INLINE MutexError lock() { return MutexError::NONE; }61 LIBC_INLINE MutexError unlock() { return MutexError::NONE; }62 LIBC_INLINE MutexError reset() { return MutexError::NONE; }63};64 65} // namespace LIBC_NAMESPACE_DECL66 67#elif LIBC_THREAD_MODE == LIBC_THREAD_MODE_EXTERNAL68 69// TODO: Implement the interfacing, if necessary, e.g. "extern struct Mutex;"70 71#endif // LIBC_THREAD_MODE == LIBC_THREAD_MODE_PLATFORM72 73#endif // LLVM_LIBC_SRC___SUPPORT_THREADS_MUTEX_H74