Anforderungen  |   Konzepte  |   Entwurf  |   Entwicklung  |   Qualitätssicherung  |   Lebenszyklus  |   Steuerung
 
 
 
 


Quelle  SafeThreadLocal.h   Sprache: C

 
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at https://mozilla.org/MPL/2.0/. */


#ifndef mozilla_freestanding_SafeThreadLocal_h
#define mozilla_freestanding_SafeThreadLocal_h

#include <type_traits>

#include "mozilla/NativeNt.h"
#include "mozilla/ThreadLocal.h"

namespace mozilla {
namespace freestanding {

// We cannot fall back to the Tls* APIs because kernel32 might not have been
// loaded yet.
#if defined(__MINGW32__) && !defined(HAVE_THREAD_TLS_KEYWORD)
#  error "This code requires the compiler to have native TLS support"
#endif  // defined(__MINGW32__) && !defined(HAVE_THREAD_TLS_KEYWORD)

/**
 * This class holds data as a thread-local variable, or as a global variable
 * if the thread local storage is not initialized yet.  It should be safe
 * because in that early stage we assume there is no more than a single thread.
 */

template <typename T>
class SafeThreadLocal final {
  static MOZ_THREAD_LOCAL(T) sThreadLocal;
  static T sGlobal;
  static bool sIsTlsUsed;

  // In normal cases, TLS is always available and the class uses sThreadLocal
  // without changing sMainThreadId.  So sMainThreadId is likely to be 0.
  //
  // If TLS is not available, we use sGlobal instead and update sMainThreadId
  // so that that thread keeps using sGlobal even after TLS is initialized
  // later.
  static DWORD sMainThreadId;

  // Need non-inline accessors to prevent the compiler from generating code
  // accessing sThreadLocal before checking a condition.
  MOZ_NEVER_INLINE static void SetGlobalValue(T aValue) { sGlobal = aValue; }
  MOZ_NEVER_INLINE static T GetGlobalValue() { return sGlobal; }

 public:
  static void set(T aValue) {
    static_assert(std::is_pointer_v<T>,
                  "SafeThreadLocal must be used with a pointer");

    if (sMainThreadId == mozilla::nt::RtlGetCurrentThreadId()) {
      SetGlobalValue(aValue);
    } else if (sIsTlsUsed) {
      MOZ_ASSERT(mozilla::nt::RtlGetThreadLocalStoragePointer(),
                 "Once TLS is used, TLS should be available till the end.");
      sThreadLocal.set(aValue);
    } else if (mozilla::nt::RtlGetThreadLocalStoragePointer()) {
      sIsTlsUsed = true;
      sThreadLocal.set(aValue);
    } else {
      MOZ_ASSERT(sMainThreadId == 0,
                 "A second thread cannot be created before TLS is available.");
      sMainThreadId = mozilla::nt::RtlGetCurrentThreadId();
      SetGlobalValue(aValue);
    }
  }

  static T get() {
    if (sMainThreadId == mozilla::nt::RtlGetCurrentThreadId()) {
      return GetGlobalValue();
    } else if (sIsTlsUsed) {
      return sThreadLocal.get();
    }
    return GetGlobalValue();
  }
};

template <typename T>
MOZ_GLOBINIT MOZ_THREAD_LOCAL(T) SafeThreadLocal<T>::sThreadLocal;

template <typename T>
T SafeThreadLocal<T>::sGlobal = nullptr;

template <typename T>
bool SafeThreadLocal<T>::sIsTlsUsed = false;

template <typename T>
DWORD SafeThreadLocal<T>::sMainThreadId = 0;

}  // namespace freestanding
}  // namespace mozilla

#endif  // mozilla_freestanding_SafeThreadLocal_h

Messung V0.5
C=94 H=91 G=92

¤ Dauer der Verarbeitung: 0.3 Sekunden  ¤

*© Formatika GbR, Deutschland






Wurzel

Suchen

Beweissystem der NASA

Beweissystem Isabelle

NIST Cobol Testsuite

Cephes Mathematical Library

Wiener Entwicklungsmethode

Haftungshinweis

Die Informationen auf dieser Webseite wurden nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit, noch Qualität der bereit gestellten Informationen zugesichert.

Bemerkung:

Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.






                                                                                                                                                                                                                                                                                                                                                                                                     


Neuigkeiten

     Aktuelles
     Motto des Tages

Software

     Produkte
     Quellcodebibliothek

Aktivitäten

     Artikel über Sicherheit
     Anleitung zur Aktivierung von SSL

Muße

     Gedichte
     Musik
     Bilder

Jenseits des Üblichen ....

Besucherstatistik

Besucherstatistik

Monitoring

Montastic status badge