blob: a402ae980424e37c656b34ed2a4366c4fae4fef5 [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//
Chandler Carruth2946cd72019-01-19 08:50:56 +00003// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
Michael J. Spencer447762d2010-11-29 18:16:10 +00006//
Owen Andersonf17f6f02009-06-25 21:58:01 +00007//===----------------------------------------------------------------------===//
8//
9// This file implements the Unix specific (non-pthread) ThreadLocal class.
10//
11//===----------------------------------------------------------------------===//
12
13//===----------------------------------------------------------------------===//
14//=== WARNING: Implementation here must contain only generic UNIX code that
15//=== is guaranteed to work on *all* UNIX variants.
16//===----------------------------------------------------------------------===//
17
Nico Weber432a3882018-04-30 14:59:11 +000018#include "llvm/Config/config.h"
19
David Majnemerc175dd2e2014-12-15 01:19:53 +000020#if defined(HAVE_PTHREAD_H) && defined(HAVE_PTHREAD_GETSPECIFIC)
21
22#include <cassert>
23#include <pthread.h>
Duncan P. N. Exon Smith91d3cfe2016-04-05 20:45:04 +000024#include <stdlib.h>
David Majnemerc175dd2e2014-12-15 01:19:53 +000025
26namespace llvm {
27using namespace sys;
28
29ThreadLocalImpl::ThreadLocalImpl() : data() {
30 static_assert(sizeof(pthread_key_t) <= sizeof(data), "size too big");
31 pthread_key_t* key = reinterpret_cast<pthread_key_t*>(&data);
32 int errorcode = pthread_key_create(key, nullptr);
33 assert(errorcode == 0);
34 (void) errorcode;
35}
36
37ThreadLocalImpl::~ThreadLocalImpl() {
38 pthread_key_t* key = reinterpret_cast<pthread_key_t*>(&data);
39 int errorcode = pthread_key_delete(*key);
40 assert(errorcode == 0);
41 (void) errorcode;
42}
43
44void ThreadLocalImpl::setInstance(const void* d) {
45 pthread_key_t* key = reinterpret_cast<pthread_key_t*>(&data);
46 int errorcode = pthread_setspecific(*key, d);
47 assert(errorcode == 0);
48 (void) errorcode;
49}
50
51void *ThreadLocalImpl::getInstance() {
52 pthread_key_t* key = reinterpret_cast<pthread_key_t*>(&data);
53 return pthread_getspecific(*key);
54}
55
56void ThreadLocalImpl::removeInstance() {
57 setInstance(nullptr);
58}
59
Duncan P. N. Exon Smith91d3cfe2016-04-05 20:45:04 +000060}
David Majnemerc175dd2e2014-12-15 01:19:53 +000061#else
Owen Andersonf17f6f02009-06-25 21:58:01 +000062namespace llvm {
63using namespace sys;
Hans Wennborgfabf8bf2013-12-19 20:32:44 +000064ThreadLocalImpl::ThreadLocalImpl() : data() { }
Owen Andersonf17f6f02009-06-25 21:58:01 +000065ThreadLocalImpl::~ThreadLocalImpl() { }
Owen Anderson331c8ab2009-06-26 08:48:03 +000066void ThreadLocalImpl::setInstance(const void* d) { data = const_cast<void*>(d);}
David Majnemer421c89d2014-12-15 01:04:45 +000067void *ThreadLocalImpl::getInstance() { return data; }
Owen Andersoncfc2a572010-07-28 22:49:43 +000068void ThreadLocalImpl::removeInstance() { setInstance(0); }
Duncan P. N. Exon Smith91d3cfe2016-04-05 20:45:04 +000069}
David Majnemerc175dd2e2014-12-15 01:19:53 +000070#endif