blob: 1c0703667095c6c2ff7ee0d036c4c0470934587b [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() { }
28void ThreadLocalImpl::setInstance(void* d) { data = d; }
29void* ThreadLocalImpl::getInstance() { return data; }
30}
31#else
32
33#if defined(HAVE_PTHREAD_H) && defined(HAVE_PTHREAD_MUTEX_LOCK)
34
35#include <cassert>
36#include <pthread.h>
37#include <stdlib.h>
38
39namespace llvm {
40using namespace sys;
41
42ThreadLocalImpl::ThreadLocalImpl() : data(0) {
43 pthread_key_t* key = new pthread_key_t;
44 int errorcode = pthread_key_create(key, NULL);
45 assert(errorcode == 0);
46 data = key;
47}
48
49ThreadLocalImpl::~ThreadLocalImpl() {
50 pthread_key_t* key = static_cast<pthread_key_t*>(data);
51 int errorcode = pthread_key_delete(*key);
52 assert(errorcode = 0);
53 delete key;
54}
55
56void ThreadLocalImpl::setInstance(void* d) {
57 pthread_key_t* key = static_cast<pthread_key_t*>(data);
58 int errorcode = pthread_setspecific(*key, d);
59 assert(errorcode == 0);
60}
61
62void* ThreadLocalImpl::getInstance() {
63 pthread_key_t* key = static_cast<pthread_key_t*>(data);
64 return pthread_getspecific(*key);
65}
66
67}
68
69#elif defined(LLVM_ON_UNIX)
70#include "Unix/ThreadLocal.inc"
71#elif defined( LLVM_ON_WIN32)
72#include "Win32/ThreadLocal.inc"
73#else
74#warning Neither LLVM_ON_UNIX nor LLVM_ON_WIN32 was set in System/ThreadLocal.cpp
75#endif
76#endif
77