• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

STEllAR-GROUP / hpx / #882

31 Aug 2023 07:44PM UTC coverage: 41.798% (-44.7%) from 86.546%
#882

push

19442 of 46514 relevant lines covered (41.8%)

126375.38 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

38.46
/libs/core/synchronization/src/mutex.cpp
1
//  Copyright (c) 2007-2023 Hartmut Kaiser
2
//  Copyright (c) 2013-2015 Agustin Berge
3
//
4
//  SPDX-License-Identifier: BSL-1.0
5
//  Distributed under the Boost Software License, Version 1.0. (See accompanying
6
//  file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
7

8
#include <hpx/assert.hpp>
9
#include <hpx/modules/coroutines.hpp>
10
#include <hpx/modules/errors.hpp>
11
#include <hpx/modules/itt_notify.hpp>
12
#include <hpx/modules/lock_registration.hpp>
13
#include <hpx/modules/threading_base.hpp>
14
#include <hpx/modules/timing.hpp>
15
#include <hpx/synchronization/condition_variable.hpp>
16
#include <hpx/synchronization/mutex.hpp>
17
#include <hpx/synchronization/spinlock.hpp>
18

19
#include <mutex>
20
#include <utility>
21

22
namespace hpx {
23

24
    ///////////////////////////////////////////////////////////////////////////
25
#if HPX_HAVE_ITTNOTIFY != 0
26
    mutex::mutex(char const* const description)
27
      : owner_id_(threads::invalid_thread_id)
28
    {
29
        HPX_ITT_SYNC_CREATE(this, "hpx::mutex", description);
30
        HPX_ITT_SYNC_RENAME(this, "hpx::mutex");
31
    }
32
#endif
33

34
#if HPX_HAVE_ITTNOTIFY != 0
35
    mutex::~mutex()
36
    {
37
        HPX_ITT_SYNC_DESTROY(this);
38
    }
39
#else
40
    mutex::~mutex() = default;
41
#endif
10,135✔
42

43
    void mutex::lock(char const* description, error_code& ec)
44
    {
49,738✔
45
        HPX_ASSERT(threads::get_self_ptr() != nullptr);
46

47
        HPX_ITT_SYNC_PREPARE(this);
48
        std::unique_lock<mutex_type> l(mtx_);
49

49,738✔
50
        threads::thread_id_type const self_id = threads::get_self_id();
51
        if (owner_id_ == self_id)
49,738✔
52
        {
49,738✔
53
            HPX_ITT_SYNC_CANCEL(this);
54
            l.unlock();
55
            HPX_THROWS_IF(ec, hpx::error::deadlock, description,
×
56
                "The calling thread already owns the mutex");
×
57
            return;
58
        }
×
59

60
        while (owner_id_ != threads::invalid_thread_id)
61
        {
49,755✔
62
            cond_.wait(l, ec);
63
            if (ec)
17✔
64
            {
17✔
65
                HPX_ITT_SYNC_CANCEL(this);
66
                return;
67
            }
68
        }
69

70
        util::register_lock(this);
71
        HPX_ITT_SYNC_ACQUIRED(this);
72
        owner_id_ = self_id;
73
    }
49,738✔
74

75
    bool mutex::try_lock(char const* /* description */, error_code& /* ec */)
76
    {
×
77
        HPX_ASSERT(threads::get_self_ptr() != nullptr);
78

79
        HPX_ITT_SYNC_PREPARE(this);
80
        std::unique_lock<mutex_type> l(mtx_);
81

×
82
        if (owner_id_ != threads::invalid_thread_id)
83
        {
×
84
            HPX_ITT_SYNC_CANCEL(this);
85
            return false;
86
        }
87

88
        threads::thread_id_type const self_id = threads::get_self_id();
89
        util::register_lock(this);
×
90
        HPX_ITT_SYNC_ACQUIRED(this);
91
        owner_id_ = self_id;
92
        return true;
×
93
    }
×
94

95
    void mutex::unlock(error_code& ec)
96
    {
49,738✔
97
        HPX_ASSERT(threads::get_self_ptr() != nullptr);
98

99
        HPX_ITT_SYNC_RELEASING(this);
100
        // Unregister lock early as the lock guard below may suspend.
101
        util::unregister_lock(this);
102
        std::unique_lock<mutex_type> l(mtx_);
103

49,738✔
104
        threads::thread_id_type const self_id = threads::get_self_id();
105
        if (HPX_UNLIKELY(owner_id_ != self_id))
49,738✔
106
        {
49,738✔
107
            l.unlock();
108
            HPX_THROWS_IF(ec, hpx::error::lock_error, "mutex::unlock",
×
109
                "The calling thread does not own the mutex");
×
110
            return;
111
        }
112

113
        HPX_ITT_SYNC_RELEASED(this);
114
        owner_id_ = threads::invalid_thread_id;
115

49,738✔
116
        {
117
            [[maybe_unused]] util::ignore_while_checking il(&l);
118

119
            // Failing to release lock 'this->mtx' in function
120
#if defined(HPX_MSVC)
121
#pragma warning(push)
122
#pragma warning(disable : 26115)
123
#endif
124

125
            cond_.notify_one(HPX_MOVE(l), threads::thread_priority::boost, ec);
126
            il.reset_owns_registration();
99,476✔
127

128
#if defined(HPX_MSVC)
129
#pragma warning(pop)
130
#endif
131
        }
132
    }
133

134
    ///////////////////////////////////////////////////////////////////////////
135
    timed_mutex::timed_mutex(char const* const description)
×
136
      : mutex(description)
137
    {
138
    }
×
139

140
    timed_mutex::~timed_mutex() = default;
×
141

142
    bool timed_mutex::try_lock_until(
×
143
        hpx::chrono::steady_time_point const& abs_time,
144
        char const* /* description */, error_code& ec)
145
    {
146
        HPX_ASSERT(threads::get_self_ptr() != nullptr);
147

148
        HPX_ITT_SYNC_PREPARE(this);
149
        std::unique_lock<mutex_type> l(mtx_);
×
150

151
        threads::thread_id_type const self_id = threads::get_self_id();
×
152
        if (owner_id_ != threads::invalid_thread_id)
×
153
        {
154
            threads::thread_restart_state const reason =
155
                cond_.wait_until(l, abs_time, ec);
×
156
            if (ec)
×
157
            {
158
                HPX_ITT_SYNC_CANCEL(this);
159
                return false;
160
            }
161

162
            if (reason == threads::thread_restart_state::timeout)    //-V110
×
163
            {
164
                HPX_ITT_SYNC_CANCEL(this);
165
                return false;
166
            }
167

168
            if (owner_id_ != threads::invalid_thread_id)    //-V110
×
169
            {
170
                HPX_ITT_SYNC_CANCEL(this);
171
                return false;
172
            }
173
        }
174

175
        util::register_lock(this);
176
        HPX_ITT_SYNC_ACQUIRED(this);
177
        owner_id_ = self_id;
×
178
        return true;
×
179
    }
180
}    // namespace hpx
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc