1//===-- scudo_tsd_exclusive.inc ---------------------------------*- 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-exception
6//
7//===----------------------------------------------------------------------===//
8///
9/// Scudo exclusive TSD fastpath functions implementation.
10///
11//===----------------------------------------------------------------------===//
12
13#ifndef SCUDO_TSD_H_
14# error "This file must be included inside scudo_tsd.h."
15#endif  // SCUDO_TSD_H_
16
17#if SCUDO_TSD_EXCLUSIVE
18
19enum ThreadState : u8 {
20  ThreadNotInitialized = 0,
21  ThreadInitialized,
22  ThreadTornDown,
23};
24__attribute__((tls_model("initial-exec")))
25extern THREADLOCAL ThreadState ScudoThreadState;
26__attribute__((tls_model("initial-exec")))
27extern THREADLOCAL ScudoTSD TSD;
28
29extern ScudoTSD FallbackTSD;
30
31ALWAYS_INLINE void initThreadMaybe(bool MinimalInit = false) {
32  if (LIKELY(ScudoThreadState != ThreadNotInitialized))
33    return;
34  initThread(MinimalInit);
35}
36
37ALWAYS_INLINE ScudoTSD *getTSDAndLock(bool *UnlockRequired) {
38  if (UNLIKELY(ScudoThreadState != ThreadInitialized)) {
39    FallbackTSD.lock();
40    *UnlockRequired = true;
41    return &FallbackTSD;
42  }
43  *UnlockRequired = false;
44  return &TSD;
45}
46
47#endif  // SCUDO_TSD_EXCLUSIVE
48