blob: 868b6ea566a901ff14503095b00396f9efbb722a [file] [log] [blame]
Owen Andersonf17f6f02009-06-25 21:58:01 +00001//===- ThreadLocal.cpp - Thread Local Data ----------------------*- C++ -*-===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file implements the llvm::sys::ThreadLocal class.
11//
12//===----------------------------------------------------------------------===//
13
14#include "llvm/Config/config.h"
Michael J. Spencer447762d2010-11-29 18:16:10 +000015#include "llvm/Support/ThreadLocal.h"
Owen Andersonf17f6f02009-06-25 21:58:01 +000016
17//===----------------------------------------------------------------------===//
18//=== WARNING: Implementation here must contain only TRULY operating system
19//=== independent code.
20//===----------------------------------------------------------------------===//
21
Dylan Noblesmithefddf202011-11-28 00:48:58 +000022#if !defined(LLVM_ENABLE_THREADS) || LLVM_ENABLE_THREADS == 0
Owen Andersonf17f6f02009-06-25 21:58:01 +000023// Define all methods as no-ops if threading is explicitly disabled
24namespace llvm {
25using namespace sys;
Hans Wennborgfabf8bf2013-12-19 20:32:44 +000026ThreadLocalImpl::ThreadLocalImpl() : data() { }
Owen Andersonf17f6f02009-06-25 21:58:01 +000027ThreadLocalImpl::~ThreadLocalImpl() { }
Argyrios Kyrtzidis444fd422012-06-13 16:30:06 +000028void ThreadLocalImpl::setInstance(const void* d) {
29 typedef int SIZE_TOO_BIG[sizeof(d) <= sizeof(data) ? 1 : -1];
30 void **pd = reinterpret_cast<void**>(&data);
31 *pd = const_cast<void*>(d);
32}
Argyrios Kyrtzidis46785f92012-06-26 17:13:58 +000033const void* ThreadLocalImpl::getInstance() {
Argyrios Kyrtzidis444fd422012-06-13 16:30:06 +000034 void **pd = reinterpret_cast<void**>(&data);
Argyrios Kyrtzidis46785f92012-06-26 17:13:58 +000035 return *pd;
36}
37void ThreadLocalImpl::removeInstance() {
38 setInstance(0);
Argyrios Kyrtzidis444fd422012-06-13 16:30:06 +000039}
Owen Andersonf17f6f02009-06-25 21:58:01 +000040}
41#else
42
Owen Anderson11549832009-06-25 23:10:26 +000043#if defined(HAVE_PTHREAD_H) && defined(HAVE_PTHREAD_GETSPECIFIC)
Owen Andersonf17f6f02009-06-25 21:58:01 +000044
45#include <cassert>
46#include <pthread.h>
47#include <stdlib.h>
48
49namespace llvm {
50using namespace sys;
51
Argyrios Kyrtzidisc6dc4d72012-06-12 01:06:16 +000052ThreadLocalImpl::ThreadLocalImpl() : data() {
Argyrios Kyrtzidis8d19c862012-06-12 00:21:31 +000053 typedef int SIZE_TOO_BIG[sizeof(pthread_key_t) <= sizeof(data) ? 1 : -1];
54 pthread_key_t* key = reinterpret_cast<pthread_key_t*>(&data);
Owen Andersonf17f6f02009-06-25 21:58:01 +000055 int errorcode = pthread_key_create(key, NULL);
56 assert(errorcode == 0);
Daniel Dunbaraa311ca2009-06-26 01:34:35 +000057 (void) errorcode;
Owen Andersonf17f6f02009-06-25 21:58:01 +000058}
59
60ThreadLocalImpl::~ThreadLocalImpl() {
Argyrios Kyrtzidis8d19c862012-06-12 00:21:31 +000061 pthread_key_t* key = reinterpret_cast<pthread_key_t*>(&data);
Owen Andersonf17f6f02009-06-25 21:58:01 +000062 int errorcode = pthread_key_delete(*key);
Owen Andersoneb511112009-06-25 23:28:28 +000063 assert(errorcode == 0);
Daniel Dunbaraa311ca2009-06-26 01:34:35 +000064 (void) errorcode;
Owen Andersonf17f6f02009-06-25 21:58:01 +000065}
66
Owen Andersoneba6e652009-06-25 23:31:18 +000067void ThreadLocalImpl::setInstance(const void* d) {
Argyrios Kyrtzidis8d19c862012-06-12 00:21:31 +000068 pthread_key_t* key = reinterpret_cast<pthread_key_t*>(&data);
Owen Andersonf17f6f02009-06-25 21:58:01 +000069 int errorcode = pthread_setspecific(*key, d);
70 assert(errorcode == 0);
Daniel Dunbaraa311ca2009-06-26 01:34:35 +000071 (void) errorcode;
Owen Andersonf17f6f02009-06-25 21:58:01 +000072}
73
Owen Andersoneba6e652009-06-25 23:31:18 +000074const void* ThreadLocalImpl::getInstance() {
Argyrios Kyrtzidis8d19c862012-06-12 00:21:31 +000075 pthread_key_t* key = reinterpret_cast<pthread_key_t*>(&data);
Owen Andersonf17f6f02009-06-25 21:58:01 +000076 return pthread_getspecific(*key);
77}
78
Owen Andersoncfc2a572010-07-28 22:49:43 +000079void ThreadLocalImpl::removeInstance() {
80 setInstance(0);
81}
82
Owen Andersonf17f6f02009-06-25 21:58:01 +000083}
84
85#elif defined(LLVM_ON_UNIX)
86#include "Unix/ThreadLocal.inc"
87#elif defined( LLVM_ON_WIN32)
Michael J. Spencer447762d2010-11-29 18:16:10 +000088#include "Windows/ThreadLocal.inc"
Owen Andersonf17f6f02009-06-25 21:58:01 +000089#else
Daniel Dunbar037fc932011-10-11 20:02:52 +000090#warning Neither LLVM_ON_UNIX nor LLVM_ON_WIN32 set in Support/ThreadLocal.cpp
Owen Andersonf17f6f02009-06-25 21:58:01 +000091#endif
92#endif