Coverage Report

Created: 2020-09-22 08:39

/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.55k
{
23
2.55k
}
24
25
// Exclusive ownership
26
27
void
28
__shared_mutex_base::lock()
29
4.28M
{
30
4.28M
    unique_lock<mutex> lk(__mut_);
31
4.28M
    while (__state_ & __write_entered_)
32
6
        __gate1_.wait(lk);
33
4.28M
    __state_ |= __write_entered_;
34
4.28M
    while (__state_ & __n_readers_)
35
6
        __gate2_.wait(lk);
36
4.28M
}
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
4.28M
{
53
4.28M
    lock_guard<mutex> _(__mut_);
54
4.28M
    __state_ = 0;
55
4.28M
    __gate1_.notify_all();
56
4.28M
}
57
58
// Shared ownership
59
60
void
61
__shared_mutex_base::lock_shared()
62
6.26M
{
63
6.26M
    unique_lock<mutex> lk(__mut_);
64
6.26M
    while (
(__state_ & __write_entered_)6.26M
|| (__state_ & __n_readers_) == __n_readers_)
65
7
        __gate1_.wait(lk);
66
6.26M
    unsigned num_readers = (__state_ & __n_readers_) + 1;
67
6.26M
    __state_ &= ~__n_readers_;
68
6.26M
    __state_ |= num_readers;
69
6.26M
}
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
6.26M
{
89
6.26M
    lock_guard<mutex> _(__mut_);
90
6.26M
    unsigned num_readers = (__state_ & __n_readers_) - 1;
91
6.26M
    __state_ &= ~__n_readers_;
92
6.26M
    __state_ |= num_readers;
93
6.26M
    if (__state_ & __write_entered_)
94
6
    {
95
6
        if (num_readers == 0)
96
6
            __gate2_.notify_one();
97
6
    }
98
6.26M
    else
99
6.26M
    {
100
6.26M
        if (num_readers == __n_readers_ - 1)
101
0
            __gate1_.notify_one();
102
6.26M
    }
103
6.26M
}
104
105
106
// Shared Timed Mutex
107
// These routines are here for ABI stability
108
2.55k
shared_timed_mutex::shared_timed_mutex() : __base() {}
109
4.28M
void shared_timed_mutex::lock()     { return __base.lock(); }
110
0
bool shared_timed_mutex::try_lock() { return __base.try_lock(); }
111
4.28M
void shared_timed_mutex::unlock()   { return __base.unlock(); }
112
6.26M
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
6.26M
void shared_timed_mutex::unlock_shared() { return __base.unlock_shared(); }
115
116
_LIBCPP_END_NAMESPACE_STD
117
118
#endif // !_LIBCPP_HAS_NO_THREADS