1//=== llvm/Support/Unix/ThreadLocal.inc - Unix Thread Local Data -*- 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// This file implements the Unix specific (non-pthread) ThreadLocal class. 10// 11//===----------------------------------------------------------------------===// 12 13//===----------------------------------------------------------------------===// 14//=== WARNING: Implementation here must contain only generic UNIX code that 15//=== is guaranteed to work on *all* UNIX variants. 16//===----------------------------------------------------------------------===// 17 18#include "llvm/Config/config.h" 19 20#include <cassert> 21#include <pthread.h> 22#include <stdlib.h> 23 24namespace llvm { 25using namespace sys; 26 27ThreadLocalImpl::ThreadLocalImpl() : data() { 28 static_assert(sizeof(pthread_key_t) <= sizeof(data), "size too big"); 29 pthread_key_t* key = reinterpret_cast<pthread_key_t*>(&data); 30 int errorcode = pthread_key_create(key, nullptr); 31 assert(errorcode == 0); 32 (void) errorcode; 33} 34 35ThreadLocalImpl::~ThreadLocalImpl() { 36 pthread_key_t* key = reinterpret_cast<pthread_key_t*>(&data); 37 int errorcode = pthread_key_delete(*key); 38 assert(errorcode == 0); 39 (void) errorcode; 40} 41 42void ThreadLocalImpl::setInstance(const void* d) { 43 pthread_key_t* key = reinterpret_cast<pthread_key_t*>(&data); 44 int errorcode = pthread_setspecific(*key, d); 45 assert(errorcode == 0); 46 (void) errorcode; 47} 48 49void *ThreadLocalImpl::getInstance() { 50 pthread_key_t* key = reinterpret_cast<pthread_key_t*>(&data); 51 return pthread_getspecific(*key); 52} 53 54void ThreadLocalImpl::removeInstance() { 55 setInstance(nullptr); 56} 57 58} 59