Coverage Report

Created: 2021-09-21 08:58

/Users/buildslave/jenkins/workspace/coverage/llvm-project/libcxx/src/shared_mutex.cpp
Line
Count
Source (jump to first uncovered line)
1
//===---------------------- shared_mutex.cpp ------------------------------===//
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
#include "__config"
10
#ifndef _LIBCPP_HAS_NO_THREADS
11
12
#include "shared_mutex"
13
#if defined(__ELF__) && defined(_LIBCPP_LINK_PTHREAD_LIB)
14
#pragma comment(lib, "pthread")
15
#endif
16
17
_LIBCPP_BEGIN_NAMESPACE_STD
18
19
// Shared Mutex Base
20
__shared_mutex_base::__shared_mutex_base()
21
    : __state_(0)
22
2.91k
{
23
2.91k
}
24
25
// Exclusive ownership
26
27
void
28
__shared_mutex_base::lock()
29
3.13M
{
30
3.13M
    unique_lock<mutex> lk(__mut_);
31
3.13M
    while (__state_ & __write_entered_)
32
3
        __gate1_.wait(lk);
33
3.13M
    __state_ |= __write_entered_;
34
3.13M
    while (__state_ & __n_readers_)
35
2
        __gate2_.wait(lk);
36
3.13M
}
37
38
bool
39
__shared_mutex_base::try_lock()
40
0
{
41
0
    unique_lock<mutex> lk(__mut_);
42
0
    if (__state_ == 0)
43
0
    {
44
0
        __state_ = __write_entered_;
45
0
        return true;
46
0
    }
47
0
    return false;
48
0
}
49
50
void
51
__shared_mutex_base::unlock()
52
3.13M
{
53
3.13M
    lock_guard<mutex> _(__mut_);
54
3.13M
    __state_ = 0;
55
3.13M
    __gate1_.notify_all();
56
3.13M
}
57
58
// Shared ownership
59
60
void
61
__shared_mutex_base::lock_shared()
62
5.42M
{
63
5.42M
    unique_lock<mutex> lk(__mut_);
64
5.42M
    while (
(__state_ & __write_entered_)5.42M
|| (__state_ & __n_readers_) == __n_readers_)
65
3
        __gate1_.wait(lk);
66
5.42M
    unsigned num_readers = (__state_ & __n_readers_) + 1;
67
5.42M
    __state_ &= ~__n_readers_;
68
5.42M
    __state_ |= num_readers;
69
5.42M
}
70
71
bool
72
__shared_mutex_base::try_lock_shared()
73
0
{
74
0
    unique_lock<mutex> lk(__mut_);
75
0
    unsigned num_readers = __state_ & __n_readers_;
76
0
    if (!(__state_ & __write_entered_) && num_readers != __n_readers_)
77
0
    {
78
0
        ++num_readers;
79
0
        __state_ &= ~__n_readers_;
80
0
        __state_ |= num_readers;
81
0
        return true;
82
0
    }
83
0
    return false;
84
0
}
85
86
void
87
__shared_mutex_base::unlock_shared()
88
5.42M
{
89
5.42M
    lock_guard<mutex> _(__mut_);
90
5.42M
    unsigned num_readers = (__state_ & __n_readers_) - 1;
91
5.42M
    __state_ &= ~__n_readers_;
92
5.42M
    __state_ |= num_readers;
93
5.42M
    if (__state_ & __write_entered_)
94
2
    {
95
2
        if (num_readers == 0)
96
2
            __gate2_.notify_one();
97
2
    }
98
5.42M
    else
99
5.42M
    {
100
5.42M
        if (num_readers == __n_readers_ - 1)
101
0
            __gate1_.notify_one();
102
5.42M
    }
103
5.42M
}
104
105
106
// Shared Timed Mutex
107
// These routines are here for ABI stability
108
2.91k
shared_timed_mutex::shared_timed_mutex() : __base() {}
109
3.13M
void shared_timed_mutex::lock()     { return __base.lock(); }
110
0
bool shared_timed_mutex::try_lock() { return __base.try_lock(); }
111
3.13M
void shared_timed_mutex::unlock()   { return __base.unlock(); }
112
5.42M
void shared_timed_mutex::lock_shared() { return __base.lock_shared(); }
113
0
bool shared_timed_mutex::try_lock_shared() { return __base.try_lock_shared(); }
114
5.42M
void shared_timed_mutex::unlock_shared() { return __base.unlock_shared(); }
115
116
_LIBCPP_END_NAMESPACE_STD
117
118
#endif // !_LIBCPP_HAS_NO_THREADS