Coverage Report

Created: 2019-07-24 05:18

/Users/buildslave/jenkins/workspace/clang-stage2-coverage-R/llvm/lib/Support/Mutex.cpp
Line
Count
Source
1
//===- Mutex.cpp - Mutual Exclusion Lock ------------------------*- 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 llvm::sys::Mutex class.
10
//
11
//===----------------------------------------------------------------------===//
12
13
#include "llvm/Support/Mutex.h"
14
#include "llvm/Config/config.h"
15
#include "llvm/Support/ErrorHandling.h"
16
17
//===----------------------------------------------------------------------===//
18
//=== WARNING: Implementation here must contain only TRULY operating system
19
//===          independent code.
20
//===----------------------------------------------------------------------===//
21
22
#if !defined(LLVM_ENABLE_THREADS) || LLVM_ENABLE_THREADS == 0
23
// Define all methods as no-ops if threading is explicitly disabled
24
namespace llvm {
25
using namespace sys;
26
MutexImpl::MutexImpl( bool recursive) { }
27
MutexImpl::~MutexImpl() { }
28
bool MutexImpl::acquire() { return true; }
29
bool MutexImpl::release() { return true; }
30
bool MutexImpl::tryacquire() { return true; }
31
}
32
#else
33
34
#if defined(HAVE_PTHREAD_H) && defined(HAVE_PTHREAD_MUTEX_LOCK)
35
36
#include <cassert>
37
#include <pthread.h>
38
#include <stdlib.h>
39
40
namespace llvm {
41
using namespace sys;
42
43
// Construct a Mutex using pthread calls
44
MutexImpl::MutexImpl( bool recursive)
45
  : data_(nullptr)
46
560k
{
47
560k
  // Declare the pthread_mutex data structures
48
560k
  pthread_mutex_t* mutex =
49
560k
    static_cast<pthread_mutex_t*>(safe_malloc(sizeof(pthread_mutex_t)));
50
560k
51
560k
  pthread_mutexattr_t attr;
52
560k
53
560k
  // Initialize the mutex attributes
54
560k
  int errorcode = pthread_mutexattr_init(&attr);
55
560k
  assert(errorcode == 0); (void)errorcode;
56
560k
57
560k
  // Initialize the mutex as a recursive mutex, if requested, or normal
58
560k
  // otherwise.
59
560k
  int kind = ( recursive  ? PTHREAD_MUTEX_RECURSIVE : PTHREAD_MUTEX_NORMAL );
60
560k
  errorcode = pthread_mutexattr_settype(&attr, kind);
61
560k
  assert(errorcode == 0);
62
560k
63
560k
  // Initialize the mutex
64
560k
  errorcode = pthread_mutex_init(mutex, &attr);
65
560k
  assert(errorcode == 0);
66
560k
67
560k
  // Destroy the attributes
68
560k
  errorcode = pthread_mutexattr_destroy(&attr);
69
560k
  assert(errorcode == 0);
70
560k
71
560k
  // Assign the data member
72
560k
  data_ = mutex;
73
560k
}
74
75
// Destruct a Mutex
76
MutexImpl::~MutexImpl()
77
408k
{
78
408k
  pthread_mutex_t* mutex = static_cast<pthread_mutex_t*>(data_);
79
408k
  assert(mutex != nullptr);
80
408k
  pthread_mutex_destroy(mutex);
81
408k
  free(mutex);
82
408k
}
83
84
bool
85
MutexImpl::acquire()
86
2.07M
{
87
2.07M
  pthread_mutex_t* mutex = static_cast<pthread_mutex_t*>(data_);
88
2.07M
  assert(mutex != nullptr);
89
2.07M
90
2.07M
  int errorcode = pthread_mutex_lock(mutex);
91
2.07M
  return errorcode == 0;
92
2.07M
}
93
94
bool
95
MutexImpl::release()
96
2.07M
{
97
2.07M
  pthread_mutex_t* mutex = static_cast<pthread_mutex_t*>(data_);
98
2.07M
  assert(mutex != nullptr);
99
2.07M
100
2.07M
  int errorcode = pthread_mutex_unlock(mutex);
101
2.07M
  return errorcode == 0;
102
2.07M
}
103
104
bool
105
MutexImpl::tryacquire()
106
6
{
107
6
  pthread_mutex_t* mutex = static_cast<pthread_mutex_t*>(data_);
108
6
  assert(mutex != nullptr);
109
6
110
6
  int errorcode = pthread_mutex_trylock(mutex);
111
6
  return errorcode == 0;
112
6
}
113
114
}
115
116
#elif defined(LLVM_ON_UNIX)
117
#include "Unix/Mutex.inc"
118
#elif defined( _WIN32)
119
#include "Windows/Mutex.inc"
120
#else
121
#warning Neither LLVM_ON_UNIX nor _WIN32 was set in Support/Mutex.cpp
122
#endif
123
#endif