Coverage Report

Created: 2019-07-24 05:18

/Users/buildslave/jenkins/workspace/clang-stage2-coverage-R/llvm/lib/Support/Unix/ThreadLocal.inc
Line
Count
Source
1
//=== llvm/Support/Unix/ThreadLocal.inc - Unix Thread Local Data -*- C++ -*-===//
2
//
3
// 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
6
//
7
//===----------------------------------------------------------------------===//
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
18
#include "llvm/Config/config.h"
19
20
#if defined(HAVE_PTHREAD_H) && defined(HAVE_PTHREAD_GETSPECIFIC)
21
22
#include <cassert>
23
#include <pthread.h>
24
#include <stdlib.h>
25
26
namespace llvm {
27
using namespace sys;
28
29
2.68k
ThreadLocalImpl::ThreadLocalImpl() : data() {
30
2.68k
  static_assert(sizeof(pthread_key_t) <= sizeof(data), "size too big");
31
2.68k
  pthread_key_t* key = reinterpret_cast<pthread_key_t*>(&data);
32
2.68k
  int errorcode = pthread_key_create(key, nullptr);
33
2.68k
  assert(errorcode == 0);
34
2.68k
  (void) errorcode;
35
2.68k
}
36
37
655
ThreadLocalImpl::~ThreadLocalImpl() {
38
655
  pthread_key_t* key = reinterpret_cast<pthread_key_t*>(&data);
39
655
  int errorcode = pthread_key_delete(*key);
40
655
  assert(errorcode == 0);
41
655
  (void) errorcode;
42
655
}
43
44
10.4k
void ThreadLocalImpl::setInstance(const void* d) {
45
10.4k
  pthread_key_t* key = reinterpret_cast<pthread_key_t*>(&data);
46
10.4k
  int errorcode = pthread_setspecific(*key, d);
47
10.4k
  assert(errorcode == 0);
48
10.4k
  (void) errorcode;
49
10.4k
}
50
51
18.5k
void *ThreadLocalImpl::getInstance() {
52
18.5k
  pthread_key_t* key = reinterpret_cast<pthread_key_t*>(&data);
53
18.5k
  return pthread_getspecific(*key);
54
18.5k
}
55
56
2
void ThreadLocalImpl::removeInstance() {
57
2
  setInstance(nullptr);
58
2
}
59
60
}
61
#else
62
namespace llvm {
63
using namespace sys;
64
ThreadLocalImpl::ThreadLocalImpl() : data() { }
65
ThreadLocalImpl::~ThreadLocalImpl() { }
66
void ThreadLocalImpl::setInstance(const void* d) { data = const_cast<void*>(d);}
67
void *ThreadLocalImpl::getInstance() { return data; }
68
void ThreadLocalImpl::removeInstance() { setInstance(0); }
69
}
70
#endif