blob: 109580478deeb6003cc615cb246503813a428ed2 [file] [log] [blame]
Owen Anderson4a285222009-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. Spencer1f6efa32010-11-29 18:16:10 +000015#include "llvm/Support/ThreadLocal.h"
Owen Anderson4a285222009-06-25 21:58:01 +000016
17//===----------------------------------------------------------------------===//
18//=== WARNING: Implementation here must contain only TRULY operating system
19//=== independent code.
20//===----------------------------------------------------------------------===//
21
Dylan Noblesmith08b73a32011-11-28 00:48:58 +000022#if !defined(LLVM_ENABLE_THREADS) || LLVM_ENABLE_THREADS == 0
Owen Anderson4a285222009-06-25 21:58:01 +000023// Define all methods as no-ops if threading is explicitly disabled
24namespace llvm {
25using namespace sys;
26ThreadLocalImpl::ThreadLocalImpl() { }
27ThreadLocalImpl::~ThreadLocalImpl() { }
Argyrios Kyrtzidis1a18e9a2012-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}
Owen Anderson438d3942009-06-25 23:31:18 +000033const void* ThreadLocalImpl::getInstance() { return data; }
Argyrios Kyrtzidis1a18e9a2012-06-13 16:30:06 +000034void ThreadLocalImpl::removeInstance() {
35 void **pd = reinterpret_cast<void**>(&data);
36 *pd = 0;
37}
Owen Anderson4a285222009-06-25 21:58:01 +000038}
39#else
40
Owen Anderson27fcfe12009-06-25 23:10:26 +000041#if defined(HAVE_PTHREAD_H) && defined(HAVE_PTHREAD_GETSPECIFIC)
Owen Anderson4a285222009-06-25 21:58:01 +000042
43#include <cassert>
44#include <pthread.h>
45#include <stdlib.h>
46
47namespace llvm {
48using namespace sys;
49
Argyrios Kyrtzidis3e613742012-06-12 01:06:16 +000050ThreadLocalImpl::ThreadLocalImpl() : data() {
Argyrios Kyrtzidis793537d2012-06-12 00:21:31 +000051 typedef int SIZE_TOO_BIG[sizeof(pthread_key_t) <= sizeof(data) ? 1 : -1];
52 pthread_key_t* key = reinterpret_cast<pthread_key_t*>(&data);
Owen Anderson4a285222009-06-25 21:58:01 +000053 int errorcode = pthread_key_create(key, NULL);
54 assert(errorcode == 0);
Daniel Dunbar6f8f6062009-06-26 01:34:35 +000055 (void) errorcode;
Owen Anderson4a285222009-06-25 21:58:01 +000056}
57
58ThreadLocalImpl::~ThreadLocalImpl() {
Argyrios Kyrtzidis793537d2012-06-12 00:21:31 +000059 pthread_key_t* key = reinterpret_cast<pthread_key_t*>(&data);
Owen Anderson4a285222009-06-25 21:58:01 +000060 int errorcode = pthread_key_delete(*key);
Owen Anderson7e026b72009-06-25 23:28:28 +000061 assert(errorcode == 0);
Daniel Dunbar6f8f6062009-06-26 01:34:35 +000062 (void) errorcode;
Owen Anderson4a285222009-06-25 21:58:01 +000063}
64
Owen Anderson438d3942009-06-25 23:31:18 +000065void ThreadLocalImpl::setInstance(const void* d) {
Argyrios Kyrtzidis793537d2012-06-12 00:21:31 +000066 pthread_key_t* key = reinterpret_cast<pthread_key_t*>(&data);
Owen Anderson4a285222009-06-25 21:58:01 +000067 int errorcode = pthread_setspecific(*key, d);
68 assert(errorcode == 0);
Daniel Dunbar6f8f6062009-06-26 01:34:35 +000069 (void) errorcode;
Owen Anderson4a285222009-06-25 21:58:01 +000070}
71
Owen Anderson438d3942009-06-25 23:31:18 +000072const void* ThreadLocalImpl::getInstance() {
Argyrios Kyrtzidis793537d2012-06-12 00:21:31 +000073 pthread_key_t* key = reinterpret_cast<pthread_key_t*>(&data);
Owen Anderson4a285222009-06-25 21:58:01 +000074 return pthread_getspecific(*key);
75}
76
Owen Anderson826c1482010-07-28 22:49:43 +000077void ThreadLocalImpl::removeInstance() {
78 setInstance(0);
79}
80
Owen Anderson4a285222009-06-25 21:58:01 +000081}
82
83#elif defined(LLVM_ON_UNIX)
84#include "Unix/ThreadLocal.inc"
85#elif defined( LLVM_ON_WIN32)
Michael J. Spencer1f6efa32010-11-29 18:16:10 +000086#include "Windows/ThreadLocal.inc"
Owen Anderson4a285222009-06-25 21:58:01 +000087#else
Daniel Dunbar2c607b62011-10-11 20:02:52 +000088#warning Neither LLVM_ON_UNIX nor LLVM_ON_WIN32 set in Support/ThreadLocal.cpp
Owen Anderson4a285222009-06-25 21:58:01 +000089#endif
90#endif