blob: aebbcad25cdac17999793afb400cf7571f634d32 [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"
Stephen Hines36b56882014-04-23 16:57:46 -070015#include "llvm/Support/Compiler.h"
Michael J. Spencer1f6efa32010-11-29 18:16:10 +000016#include "llvm/Support/ThreadLocal.h"
Owen Anderson4a285222009-06-25 21:58:01 +000017
18//===----------------------------------------------------------------------===//
19//=== WARNING: Implementation here must contain only TRULY operating system
20//=== independent code.
21//===----------------------------------------------------------------------===//
22
Dylan Noblesmith08b73a32011-11-28 00:48:58 +000023#if !defined(LLVM_ENABLE_THREADS) || LLVM_ENABLE_THREADS == 0
Owen Anderson4a285222009-06-25 21:58:01 +000024// Define all methods as no-ops if threading is explicitly disabled
25namespace llvm {
26using namespace sys;
Bill Wendlingb95f3612013-12-20 04:26:57 +000027ThreadLocalImpl::ThreadLocalImpl() : data() { }
Owen Anderson4a285222009-06-25 21:58:01 +000028ThreadLocalImpl::~ThreadLocalImpl() { }
Argyrios Kyrtzidis1a18e9a2012-06-13 16:30:06 +000029void ThreadLocalImpl::setInstance(const void* d) {
Stephen Hines36b56882014-04-23 16:57:46 -070030 static_assert(sizeof(d) <= sizeof(data), "size too big");
Argyrios Kyrtzidis1a18e9a2012-06-13 16:30:06 +000031 void **pd = reinterpret_cast<void**>(&data);
32 *pd = const_cast<void*>(d);
33}
Argyrios Kyrtzidisda72dd22012-06-26 17:13:58 +000034const void* ThreadLocalImpl::getInstance() {
Argyrios Kyrtzidis1a18e9a2012-06-13 16:30:06 +000035 void **pd = reinterpret_cast<void**>(&data);
Argyrios Kyrtzidisda72dd22012-06-26 17:13:58 +000036 return *pd;
37}
38void ThreadLocalImpl::removeInstance() {
39 setInstance(0);
Argyrios Kyrtzidis1a18e9a2012-06-13 16:30:06 +000040}
Owen Anderson4a285222009-06-25 21:58:01 +000041}
42#else
43
Owen Anderson27fcfe12009-06-25 23:10:26 +000044#if defined(HAVE_PTHREAD_H) && defined(HAVE_PTHREAD_GETSPECIFIC)
Owen Anderson4a285222009-06-25 21:58:01 +000045
46#include <cassert>
47#include <pthread.h>
48#include <stdlib.h>
49
50namespace llvm {
51using namespace sys;
52
Argyrios Kyrtzidis3e613742012-06-12 01:06:16 +000053ThreadLocalImpl::ThreadLocalImpl() : data() {
Stephen Hines36b56882014-04-23 16:57:46 -070054 static_assert(sizeof(pthread_key_t) <= sizeof(data), "size too big");
Argyrios Kyrtzidis793537d2012-06-12 00:21:31 +000055 pthread_key_t* key = reinterpret_cast<pthread_key_t*>(&data);
Owen Anderson4a285222009-06-25 21:58:01 +000056 int errorcode = pthread_key_create(key, NULL);
57 assert(errorcode == 0);
Daniel Dunbar6f8f6062009-06-26 01:34:35 +000058 (void) errorcode;
Owen Anderson4a285222009-06-25 21:58:01 +000059}
60
61ThreadLocalImpl::~ThreadLocalImpl() {
Argyrios Kyrtzidis793537d2012-06-12 00:21:31 +000062 pthread_key_t* key = reinterpret_cast<pthread_key_t*>(&data);
Owen Anderson4a285222009-06-25 21:58:01 +000063 int errorcode = pthread_key_delete(*key);
Owen Anderson7e026b72009-06-25 23:28:28 +000064 assert(errorcode == 0);
Daniel Dunbar6f8f6062009-06-26 01:34:35 +000065 (void) errorcode;
Owen Anderson4a285222009-06-25 21:58:01 +000066}
67
Owen Anderson438d3942009-06-25 23:31:18 +000068void ThreadLocalImpl::setInstance(const void* d) {
Argyrios Kyrtzidis793537d2012-06-12 00:21:31 +000069 pthread_key_t* key = reinterpret_cast<pthread_key_t*>(&data);
Owen Anderson4a285222009-06-25 21:58:01 +000070 int errorcode = pthread_setspecific(*key, d);
71 assert(errorcode == 0);
Daniel Dunbar6f8f6062009-06-26 01:34:35 +000072 (void) errorcode;
Owen Anderson4a285222009-06-25 21:58:01 +000073}
74
Owen Anderson438d3942009-06-25 23:31:18 +000075const void* ThreadLocalImpl::getInstance() {
Argyrios Kyrtzidis793537d2012-06-12 00:21:31 +000076 pthread_key_t* key = reinterpret_cast<pthread_key_t*>(&data);
Owen Anderson4a285222009-06-25 21:58:01 +000077 return pthread_getspecific(*key);
78}
79
Owen Anderson826c1482010-07-28 22:49:43 +000080void ThreadLocalImpl::removeInstance() {
81 setInstance(0);
82}
83
Owen Anderson4a285222009-06-25 21:58:01 +000084}
85
86#elif defined(LLVM_ON_UNIX)
87#include "Unix/ThreadLocal.inc"
88#elif defined( LLVM_ON_WIN32)
Michael J. Spencer1f6efa32010-11-29 18:16:10 +000089#include "Windows/ThreadLocal.inc"
Owen Anderson4a285222009-06-25 21:58:01 +000090#else
Daniel Dunbar2c607b62011-10-11 20:02:52 +000091#warning Neither LLVM_ON_UNIX nor LLVM_ON_WIN32 set in Support/ThreadLocal.cpp
Owen Anderson4a285222009-06-25 21:58:01 +000092#endif
93#endif