Coverage Report

Created: 2022-07-16 07:03

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