blob: f6a55a1c0b9b183b1fbccc7976e52bf95406abbc [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"
15#include "llvm/System/ThreadLocal.h"
16
17//===----------------------------------------------------------------------===//
18//=== WARNING: Implementation here must contain only TRULY operating system
19//=== independent code.
20//===----------------------------------------------------------------------===//
21
22#if !defined(ENABLE_THREADS) || ENABLE_THREADS == 0
23// Define all methods as no-ops if threading is explicitly disabled
24namespace llvm {
25using namespace sys;
26ThreadLocalImpl::ThreadLocalImpl() { }
27ThreadLocalImpl::~ThreadLocalImpl() { }
Owen Anderson438d3942009-06-25 23:31:18 +000028void ThreadLocalImpl::setInstance(const void* d) { data = const_cast<void*>(d);}
29const void* ThreadLocalImpl::getInstance() { return data; }
Daniel Dunbarb0270142010-08-20 20:54:37 +000030void ThreadLocalImpl::removeInstance() { data = 0; }
Owen Anderson4a285222009-06-25 21:58:01 +000031}
32#else
33
Owen Anderson27fcfe12009-06-25 23:10:26 +000034#if defined(HAVE_PTHREAD_H) && defined(HAVE_PTHREAD_GETSPECIFIC)
Owen Anderson4a285222009-06-25 21:58:01 +000035
36#include <cassert>
37#include <pthread.h>
38#include <stdlib.h>
39
40namespace llvm {
41using namespace sys;
42
43ThreadLocalImpl::ThreadLocalImpl() : data(0) {
44 pthread_key_t* key = new pthread_key_t;
45 int errorcode = pthread_key_create(key, NULL);
46 assert(errorcode == 0);
Daniel Dunbar6f8f6062009-06-26 01:34:35 +000047 (void) errorcode;
Owen Anderson4127ccd2009-07-01 16:19:23 +000048 data = (void*)key;
Owen Anderson4a285222009-06-25 21:58:01 +000049}
50
51ThreadLocalImpl::~ThreadLocalImpl() {
52 pthread_key_t* key = static_cast<pthread_key_t*>(data);
53 int errorcode = pthread_key_delete(*key);
Owen Anderson7e026b72009-06-25 23:28:28 +000054 assert(errorcode == 0);
Daniel Dunbar6f8f6062009-06-26 01:34:35 +000055 (void) errorcode;
Owen Anderson4a285222009-06-25 21:58:01 +000056 delete key;
57}
58
Owen Anderson438d3942009-06-25 23:31:18 +000059void ThreadLocalImpl::setInstance(const void* d) {
Owen Anderson4a285222009-06-25 21:58:01 +000060 pthread_key_t* key = static_cast<pthread_key_t*>(data);
61 int errorcode = pthread_setspecific(*key, d);
62 assert(errorcode == 0);
Daniel Dunbar6f8f6062009-06-26 01:34:35 +000063 (void) errorcode;
Owen Anderson4a285222009-06-25 21:58:01 +000064}
65
Owen Anderson438d3942009-06-25 23:31:18 +000066const void* ThreadLocalImpl::getInstance() {
Owen Anderson4a285222009-06-25 21:58:01 +000067 pthread_key_t* key = static_cast<pthread_key_t*>(data);
68 return pthread_getspecific(*key);
69}
70
Owen Anderson826c1482010-07-28 22:49:43 +000071void ThreadLocalImpl::removeInstance() {
72 setInstance(0);
73}
74
Owen Anderson4a285222009-06-25 21:58:01 +000075}
76
77#elif defined(LLVM_ON_UNIX)
78#include "Unix/ThreadLocal.inc"
79#elif defined( LLVM_ON_WIN32)
80#include "Win32/ThreadLocal.inc"
81#else
82#warning Neither LLVM_ON_UNIX nor LLVM_ON_WIN32 was set in System/ThreadLocal.cpp
83#endif
84#endif
85