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

mavlink / MAVSDK / 14788758985

02 May 2025 04:35AM UTC coverage: 44.324% (+0.1%) from 44.223%
14788758985

Pull #2542

github

web-flow
Merge 764620054 into 2cf24f244
Pull Request #2542: Fixing thread sanitizer issues

262 of 372 new or added lines in 17 files covered. (70.43%)

77 existing lines in 8 files now uncovered.

14760 of 33300 relevant lines covered (44.32%)

294.54 hits per line

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

75.58
/src/mavsdk/core/call_every_handler.cpp
1
#include "call_every_handler.h"
2

3
#include <utility>
4
#include <algorithm>
5

6
namespace mavsdk {
7

8
CallEveryHandler::CallEveryHandler(Time& time) : _time(time) {}
92✔
9

10
CallEveryHandler::Cookie CallEveryHandler::add(std::function<void()> callback, double interval_s)
165✔
11
{
12
    auto new_entry = Entry{};
165✔
13
    new_entry.callback = std::move(callback);
165✔
14
    auto before = _time.steady_time();
165✔
15
    // Make sure it gets run straightaway. The epsilon seemed not enough, so
16
    // we use the arbitrary value of 1 ms.
17
    _time.shift_steady_time_by(before, -interval_s - 0.001);
165✔
18
    new_entry.last_time = before;
165✔
19
    new_entry.interval_s = interval_s;
165✔
20
    new_entry.cookie = _next_cookie++;
165✔
21

22
    // Try to acquire the lock and add directly to _entries
23
    if (_entries_mutex.try_lock()) {
165✔
24
        _entries.push_back(new_entry);
165✔
25
        _entries_mutex.unlock();
165✔
26
    } else {
27
        // If we couldn't acquire the lock, add to _add_list
NEW
28
        std::lock_guard<std::mutex> lock(_add_mutex);
×
NEW
29
        _add_list.push_back(new_entry);
×
NEW
30
    }
×
31

32
    return new_entry.cookie;
330✔
33
}
165✔
34

35
void CallEveryHandler::change(double interval_s, Cookie cookie)
3✔
36
{
37
    // Try to acquire the lock and change directly in _entries
38
    if (_entries_mutex.try_lock()) {
3✔
39
        auto it = std::find_if(_entries.begin(), _entries.end(), [&](const Entry& entry) {
3✔
40
            return entry.cookie == cookie;
4✔
41
        });
42
        if (it != _entries.end()) {
3✔
43
            it->interval_s = interval_s;
3✔
44
        }
45
        _entries_mutex.unlock();
3✔
46
    } else {
47
        // If we couldn't acquire the lock, we'll have to try again later
48
        // For simplicity, we don't queue changes
NEW
49
        std::lock_guard<std::mutex> lock(_change_mutex);
×
NEW
50
        auto entry = Entry{};
×
NEW
51
        entry.interval_s = interval_s;
×
NEW
52
        entry.cookie = cookie;
×
NEW
53
        _change_list.push_back(entry);
×
UNCOV
54
    }
×
55
}
3✔
56

57
void CallEveryHandler::reset(Cookie cookie)
1✔
58
{
59
    // Try to acquire the lock and reset directly in _entries
60
    if (_entries_mutex.try_lock()) {
1✔
61
        auto it = std::find_if(_entries.begin(), _entries.end(), [&](const Entry& entry) {
1✔
62
            return entry.cookie == cookie;
1✔
63
        });
64
        if (it != _entries.end()) {
1✔
65
            it->last_time = _time.steady_time();
1✔
66
        }
67
        _entries_mutex.unlock();
1✔
68
    } else {
69
        // If we couldn't acquire the lock, we presumably just ran it this instance anyway.
70
    }
71
}
1✔
72

73
void CallEveryHandler::remove(Cookie cookie)
171✔
74
{
75
    // Try to acquire the lock and remove directly from _entries
76
    if (_entries_mutex.try_lock()) {
171✔
77
        auto it = std::find_if(_entries.begin(), _entries.end(), [&](auto& timeout) {
171✔
78
            return timeout.cookie == cookie;
114✔
79
        });
80

81
        if (it != _entries.end()) {
171✔
82
            _entries.erase(it);
74✔
83
        }
84
        _entries_mutex.unlock();
171✔
85
    } else {
86
        // If we couldn't acquire the lock, add to _remove_list
NEW
87
        std::lock_guard<std::mutex> lock(_remove_mutex);
×
NEW
88
        _remove_list.push_back(cookie);
×
UNCOV
89
    }
×
90
}
171✔
91

92
void CallEveryHandler::run_once()
21,229✔
93
{
94
    // First, get the lists under the lists mutex
95
    std::vector<Entry> add_list_copy;
21,229✔
96
    std::vector<Entry> change_list_copy;
20,520✔
97
    std::vector<Cookie> remove_list_copy;
19,513✔
98

99
    {
100
        std::lock_guard<std::mutex> lock(_add_mutex);
20,326✔
101
        add_list_copy = std::move(_add_list);
20,344✔
102
        _add_list.clear();
20,826✔
103
    }
20,565✔
104
    {
105
        std::lock_guard<std::mutex> lock(_remove_mutex);
20,000✔
106
        remove_list_copy = std::move(_remove_list);
20,317✔
107
        _remove_list.clear();
20,572✔
108
    }
20,932✔
109
    {
110
        std::lock_guard<std::mutex> lock(_change_mutex);
20,180✔
111
        change_list_copy = std::move(_change_list);
20,418✔
112
        _change_list.clear();
20,106✔
113
    }
20,368✔
114

115
    // Then process the entries under the entries mutex
116
    {
117
        std::lock_guard<std::mutex> lock(_entries_mutex);
20,307✔
118

119
        // Process add list
120
        for (auto& entry : add_list_copy) {
20,205✔
NEW
121
            _entries.push_back(entry);
×
122
        }
123

124
        // Process remove list
125
        for (const auto& cookie : remove_list_copy) {
20,172✔
NEW
126
            auto it = std::find_if(_entries.begin(), _entries.end(), [&](auto& timeout) {
×
NEW
127
                return timeout.cookie == cookie;
×
128
            });
129

NEW
130
            if (it != _entries.end()) {
×
NEW
131
                _entries.erase(it);
×
132
            }
133
        }
134

135
        // Process change list
136
        for (const auto& change : change_list_copy) {
20,395✔
NEW
137
            auto it = std::find_if(_entries.begin(), _entries.end(), [&](const Entry& entry) {
×
NEW
138
                return entry.cookie == change.cookie;
×
139
            });
NEW
140
            if (it != _entries.end()) {
×
NEW
141
                it->interval_s = change.interval_s;
×
142
            }
143
        }
144

145
        // Process entries and collect callbacks that need to be executed
146
        for (auto& entry : _entries) {
45,006✔
147
            if (_time.elapsed_since_s(entry.last_time) > double(entry.interval_s)) {
25,791✔
148
                _time.shift_steady_time_by(entry.last_time, double(entry.interval_s));
410✔
149

150
                if (entry.callback) {
410✔
151
                    entry.callback();
410✔
152
                }
153
            }
154
        }
155
    }
21,286✔
156
}
21,361✔
157

158
} // namespace mavsdk
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

© 2026 Coveralls, Inc