blob: a6564f0fa281dd26a42dcd63bcfb97237bae7183 [file] [log] [blame]
Charles Davis54c9eb62010-11-29 19:44:50 +00001//=== llvm/Support/Unix/ThreadLocal.inc - Unix Thread Local Data -*- C++ -*-===//
Michael J. Spencer447762d2010-11-29 18:16:10 +00002//
Owen Andersonf17f6f02009-06-25 21:58:01 +00003// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Michael J. Spencer447762d2010-11-29 18:16:10 +00007//
Owen Andersonf17f6f02009-06-25 21:58:01 +00008//===----------------------------------------------------------------------===//
9//
10// This file implements the Unix specific (non-pthread) ThreadLocal class.
11//
12//===----------------------------------------------------------------------===//
13
14//===----------------------------------------------------------------------===//
15//=== WARNING: Implementation here must contain only generic UNIX code that
16//=== is guaranteed to work on *all* UNIX variants.
17//===----------------------------------------------------------------------===//
18
Nico Weber432a3882018-04-30 14:59:11 +000019#include "llvm/Config/config.h"
20
David Majnemerc175dd2e2014-12-15 01:19:53 +000021#if defined(HAVE_PTHREAD_H) && defined(HAVE_PTHREAD_GETSPECIFIC)
22
23#include <cassert>
24#include <pthread.h>
Duncan P. N. Exon Smith91d3cfe2016-04-05 20:45:04 +000025#include <stdlib.h>
David Majnemerc175dd2e2014-12-15 01:19:53 +000026
27namespace llvm {
28using namespace sys;
29
30ThreadLocalImpl::ThreadLocalImpl() : data() {
31 static_assert(sizeof(pthread_key_t) <= sizeof(data), "size too big");
32 pthread_key_t* key = reinterpret_cast<pthread_key_t*>(&data);
33 int errorcode = pthread_key_create(key, nullptr);
34 assert(errorcode == 0);
35 (void) errorcode;
36}
37
38ThreadLocalImpl::~ThreadLocalImpl() {
39 pthread_key_t* key = reinterpret_cast<pthread_key_t*>(&data);
40 int errorcode = pthread_key_delete(*key);
41 assert(errorcode == 0);
42 (void) errorcode;
43}
44
45void ThreadLocalImpl::setInstance(const void* d) {
46 pthread_key_t* key = reinterpret_cast<pthread_key_t*>(&data);
47 int errorcode = pthread_setspecific(*key, d);
48 assert(errorcode == 0);
49 (void) errorcode;
50}
51
52void *ThreadLocalImpl::getInstance() {
53 pthread_key_t* key = reinterpret_cast<pthread_key_t*>(&data);
54 return pthread_getspecific(*key);
55}
56
57void ThreadLocalImpl::removeInstance() {
58 setInstance(nullptr);
59}
60
Duncan P. N. Exon Smith91d3cfe2016-04-05 20:45:04 +000061}
David Majnemerc175dd2e2014-12-15 01:19:53 +000062#else
Owen Andersonf17f6f02009-06-25 21:58:01 +000063namespace llvm {
64using namespace sys;
Hans Wennborgfabf8bf2013-12-19 20:32:44 +000065ThreadLocalImpl::ThreadLocalImpl() : data() { }
Owen Andersonf17f6f02009-06-25 21:58:01 +000066ThreadLocalImpl::~ThreadLocalImpl() { }
Owen Anderson331c8ab2009-06-26 08:48:03 +000067void ThreadLocalImpl::setInstance(const void* d) { data = const_cast<void*>(d);}
David Majnemer421c89d2014-12-15 01:04:45 +000068void *ThreadLocalImpl::getInstance() { return data; }
Owen Andersoncfc2a572010-07-28 22:49:43 +000069void ThreadLocalImpl::removeInstance() { setInstance(0); }
Duncan P. N. Exon Smith91d3cfe2016-04-05 20:45:04 +000070}
David Majnemerc175dd2e2014-12-15 01:19:53 +000071#endif