2010-11-29 20:44:50 +01:00
|
|
|
//=== llvm/Support/Unix/ThreadLocal.inc - Unix Thread Local Data -*- C++ -*-===//
|
2010-11-29 19:16:10 +01:00
|
|
|
//
|
2019-01-19 09:50:56 +01:00
|
|
|
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
|
|
|
|
// See https://llvm.org/LICENSE.txt for license information.
|
|
|
|
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
|
2010-11-29 19:16:10 +01:00
|
|
|
//
|
2009-06-25 23:58:01 +02:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// This file implements the Unix specific (non-pthread) ThreadLocal class.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//=== WARNING: Implementation here must contain only generic UNIX code that
|
|
|
|
//=== is guaranteed to work on *all* UNIX variants.
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2018-04-30 16:59:11 +02:00
|
|
|
#include "llvm/Config/config.h"
|
|
|
|
|
2014-12-15 02:19:53 +01:00
|
|
|
#if defined(HAVE_PTHREAD_H) && defined(HAVE_PTHREAD_GETSPECIFIC)
|
|
|
|
|
|
|
|
#include <cassert>
|
|
|
|
#include <pthread.h>
|
2016-04-05 22:45:04 +02:00
|
|
|
#include <stdlib.h>
|
2014-12-15 02:19:53 +01:00
|
|
|
|
|
|
|
namespace llvm {
|
|
|
|
using namespace sys;
|
|
|
|
|
|
|
|
ThreadLocalImpl::ThreadLocalImpl() : data() {
|
|
|
|
static_assert(sizeof(pthread_key_t) <= sizeof(data), "size too big");
|
|
|
|
pthread_key_t* key = reinterpret_cast<pthread_key_t*>(&data);
|
|
|
|
int errorcode = pthread_key_create(key, nullptr);
|
|
|
|
assert(errorcode == 0);
|
|
|
|
(void) errorcode;
|
|
|
|
}
|
|
|
|
|
|
|
|
ThreadLocalImpl::~ThreadLocalImpl() {
|
|
|
|
pthread_key_t* key = reinterpret_cast<pthread_key_t*>(&data);
|
|
|
|
int errorcode = pthread_key_delete(*key);
|
|
|
|
assert(errorcode == 0);
|
|
|
|
(void) errorcode;
|
|
|
|
}
|
|
|
|
|
|
|
|
void ThreadLocalImpl::setInstance(const void* d) {
|
|
|
|
pthread_key_t* key = reinterpret_cast<pthread_key_t*>(&data);
|
|
|
|
int errorcode = pthread_setspecific(*key, d);
|
|
|
|
assert(errorcode == 0);
|
|
|
|
(void) errorcode;
|
|
|
|
}
|
|
|
|
|
|
|
|
void *ThreadLocalImpl::getInstance() {
|
|
|
|
pthread_key_t* key = reinterpret_cast<pthread_key_t*>(&data);
|
|
|
|
return pthread_getspecific(*key);
|
|
|
|
}
|
|
|
|
|
|
|
|
void ThreadLocalImpl::removeInstance() {
|
|
|
|
setInstance(nullptr);
|
|
|
|
}
|
|
|
|
|
2016-04-05 22:45:04 +02:00
|
|
|
}
|
2014-12-15 02:19:53 +01:00
|
|
|
#else
|
2009-06-25 23:58:01 +02:00
|
|
|
namespace llvm {
|
|
|
|
using namespace sys;
|
2013-12-19 21:32:44 +01:00
|
|
|
ThreadLocalImpl::ThreadLocalImpl() : data() { }
|
2009-06-25 23:58:01 +02:00
|
|
|
ThreadLocalImpl::~ThreadLocalImpl() { }
|
2009-06-26 10:48:03 +02:00
|
|
|
void ThreadLocalImpl::setInstance(const void* d) { data = const_cast<void*>(d);}
|
2014-12-15 02:04:45 +01:00
|
|
|
void *ThreadLocalImpl::getInstance() { return data; }
|
2010-07-29 00:49:43 +02:00
|
|
|
void ThreadLocalImpl::removeInstance() { setInstance(0); }
|
2016-04-05 22:45:04 +02:00
|
|
|
}
|
2014-12-15 02:19:53 +01:00
|
|
|
#endif
|