Coverage Report

Created: 2019-07-24 05:18

/Users/buildslave/jenkins/workspace/clang-stage2-coverage-R/llvm/lib/Support/RWMutex.cpp
Line
Count
Source
1
//===- RWMutex.cpp - Reader/Writer 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::RWMutex class.
10
//
11
//===----------------------------------------------------------------------===//
12
13
#include "llvm/Support/Allocator.h"
14
#include "llvm/Support/RWMutex.h"
15
#include "llvm/Config/config.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
25
using namespace llvm;
26
using namespace sys;
27
28
RWMutexImpl::RWMutexImpl() = default;
29
RWMutexImpl::~RWMutexImpl() = default;
30
31
bool RWMutexImpl::reader_acquire() { return true; }
32
bool RWMutexImpl::reader_release() { return true; }
33
bool RWMutexImpl::writer_acquire() { return true; }
34
bool RWMutexImpl::writer_release() { return true; }
35
36
#else
37
38
#if defined(HAVE_PTHREAD_H) && defined(HAVE_PTHREAD_RWLOCK_INIT)
39
40
#include <cassert>
41
#include <cstdlib>
42
#include <pthread.h>
43
44
using namespace llvm;
45
using namespace sys;
46
47
// Construct a RWMutex using pthread calls
48
RWMutexImpl::RWMutexImpl()
49
120k
{
50
120k
  // Declare the pthread_rwlock data structures
51
120k
  pthread_rwlock_t* rwlock =
52
120k
    static_cast<pthread_rwlock_t*>(safe_malloc(sizeof(pthread_rwlock_t)));
53
120k
54
120k
#ifdef __APPLE__
55
120k
  // Workaround a bug/mis-feature in Darwin's pthread_rwlock_init.
56
120k
  bzero(rwlock, sizeof(pthread_rwlock_t));
57
120k
#endif
58
120k
59
120k
  // Initialize the rwlock
60
120k
  int errorcode = pthread_rwlock_init(rwlock, nullptr);
61
120k
  (void)errorcode;
62
120k
  assert(errorcode == 0);
63
120k
64
120k
  // Assign the data member
65
120k
  data_ = rwlock;
66
120k
}
67
68
// Destruct a RWMutex
69
RWMutexImpl::~RWMutexImpl()
70
114k
{
71
114k
  pthread_rwlock_t* rwlock = static_cast<pthread_rwlock_t*>(data_);
72
114k
  assert(rwlock != nullptr);
73
114k
  pthread_rwlock_destroy(rwlock);
74
114k
  free(rwlock);
75
114k
}
76
77
bool
78
RWMutexImpl::reader_acquire()
79
167M
{
80
167M
  pthread_rwlock_t* rwlock = static_cast<pthread_rwlock_t*>(data_);
81
167M
  assert(rwlock != nullptr);
82
167M
83
167M
  int errorcode = pthread_rwlock_rdlock(rwlock);
84
167M
  return errorcode == 0;
85
167M
}
86
87
bool
88
RWMutexImpl::reader_release()
89
167M
{
90
167M
  pthread_rwlock_t* rwlock = static_cast<pthread_rwlock_t*>(data_);
91
167M
  assert(rwlock != nullptr);
92
167M
93
167M
  int errorcode = pthread_rwlock_unlock(rwlock);
94
167M
  return errorcode == 0;
95
167M
}
96
97
bool
98
RWMutexImpl::writer_acquire()
99
33.0M
{
100
33.0M
  pthread_rwlock_t* rwlock = static_cast<pthread_rwlock_t*>(data_);
101
33.0M
  assert(rwlock != nullptr);
102
33.0M
103
33.0M
  int errorcode = pthread_rwlock_wrlock(rwlock);
104
33.0M
  return errorcode == 0;
105
33.0M
}
106
107
bool
108
RWMutexImpl::writer_release()
109
33.0M
{
110
33.0M
  pthread_rwlock_t* rwlock = static_cast<pthread_rwlock_t*>(data_);
111
33.0M
  assert(rwlock != nullptr);
112
33.0M
113
33.0M
  int errorcode = pthread_rwlock_unlock(rwlock);
114
33.0M
  return errorcode == 0;
115
33.0M
}
116
117
#elif defined(LLVM_ON_UNIX)
118
#include "Unix/RWMutex.inc"
119
#elif defined( _WIN32)
120
#include "Windows/RWMutex.inc"
121
#else
122
#warning Neither LLVM_ON_UNIX nor _WIN32 was set in Support/Mutex.cpp
123
#endif
124
#endif