2010-11-30 03:44:50 +08:00
|
|
|
//=== llvm/Support/Unix/ThreadLocal.inc - Unix Thread Local Data -*- C++ -*-===//
|
2010-11-30 02:16:10 +08:00
|
|
|
//
|
2019-01-19 16:50:56 +08: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-30 02:16:10 +08:00
|
|
|
//
|
2009-06-26 05:58:01 +08: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 22:59:11 +08:00
|
|
|
#include "llvm/Config/config.h"
|
|
|
|
|
2014-12-15 09:19:53 +08:00
|
|
|
#if defined(HAVE_PTHREAD_H) && defined(HAVE_PTHREAD_GETSPECIFIC)
|
|
|
|
|
|
|
|
#include <cassert>
|
|
|
|
#include <pthread.h>
|
2016-04-06 04:45:04 +08:00
|
|
|
#include <stdlib.h>
|
2014-12-15 09:19:53 +08: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-06 04:45:04 +08:00
|
|
|
}
|
2014-12-15 09:19:53 +08:00
|
|
|
#else
|
2009-06-26 05:58:01 +08:00
|
|
|
namespace llvm {
|
|
|
|
using namespace sys;
|
2013-12-20 04:32:44 +08:00
|
|
|
ThreadLocalImpl::ThreadLocalImpl() : data() { }
|
2009-06-26 05:58:01 +08:00
|
|
|
ThreadLocalImpl::~ThreadLocalImpl() { }
|
2009-06-26 16:48:03 +08:00
|
|
|
void ThreadLocalImpl::setInstance(const void* d) { data = const_cast<void*>(d);}
|
2014-12-15 09:04:45 +08:00
|
|
|
void *ThreadLocalImpl::getInstance() { return data; }
|
2010-07-29 06:49:43 +08:00
|
|
|
void ThreadLocalImpl::removeInstance() { setInstance(0); }
|
2016-04-06 04:45:04 +08:00
|
|
|
}
|
2014-12-15 09:19:53 +08:00
|
|
|
#endif
|