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

mavlink / MAVSDK / 14793244879

02 May 2025 10:11AM UTC coverage: 44.318% (+0.1%) from 44.223%
14793244879

Pull #2542

github

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

254 of 364 new or added lines in 18 files covered. (69.78%)

77 existing lines in 8 files now uncovered.

14756 of 33296 relevant lines covered (44.32%)

291.36 hits per line

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

73.42
/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,300✔
93
{
94
    std::lock_guard<std::mutex> lock(_entries_mutex);
21,300✔
95

96
    {
97
        // Process add list
98
        std::lock_guard<std::mutex> add_lock(_add_mutex);
20,806✔
99
        for (auto& entry : _add_list) {
20,890✔
NEW
100
            _entries.push_back(entry);
×
101
        }
102
        _add_list.clear();
20,971✔
103
    }
21,139✔
104
    {
105
        // Process remove list
106
        std::lock_guard<std::mutex> remove_lock(_remove_mutex);
20,586✔
107
        for (const auto& cookie : _remove_list) {
20,710✔
NEW
108
            auto it = std::find_if(_entries.begin(), _entries.end(), [&](auto& timeout) {
×
NEW
109
                return timeout.cookie == cookie;
×
110
            });
111

NEW
112
            if (it != _entries.end()) {
×
NEW
113
                _entries.erase(it);
×
114
            }
115
        }
116
        _remove_list.clear();
20,625✔
117
    }
20,993✔
118
    {
119
        // Process change list
120
        std::lock_guard<std::mutex> change_lock(_change_mutex);
20,676✔
121
        for (const auto& change : _change_list) {
20,721✔
NEW
122
            auto it = std::find_if(_entries.begin(), _entries.end(), [&](const Entry& entry) {
×
NEW
123
                return entry.cookie == change.cookie;
×
124
            });
NEW
125
            if (it != _entries.end()) {
×
NEW
126
                it->interval_s = change.interval_s;
×
127
            }
128
        }
129
        _change_list.clear();
20,130✔
130
    }
20,634✔
131

132
    // Process entries and collect callbacks that need to be executed
133
    for (auto& entry : _entries) {
46,288✔
134
        if (_time.elapsed_since_s(entry.last_time) > double(entry.interval_s)) {
25,928✔
135
            _time.shift_steady_time_by(entry.last_time, double(entry.interval_s));
411✔
136

137
            if (entry.callback) {
410✔
138
                entry.callback();
411✔
139
            }
140
        }
141
    }
142
}
21,384✔
143

144
} // 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