Skip to content

Commit 076548a

Browse files
author
Steven Cartmell
committed
Split find/insert sleep tracker function into two seperate functions.
Throw an error when trying to unlock driver that was not previously locked.
1 parent bd23625 commit 076548a

File tree

1 file changed

+20
-3
lines changed

1 file changed

+20
-3
lines changed

hal/mbed_sleep_manager.c

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -75,9 +75,15 @@ static size_t sleep_tracker_find_index(const char *const filename)
7575
}
7676
}
7777

78-
// If no driver was found currently in the structure, find the first array
79-
// index that hasn't been used by a driver and fill it, then return the
80-
// index.
78+
return -1;
79+
}
80+
81+
static size_t sleep_tracker_add(const char* const filename)
82+
{
83+
char temp[IDENTIFIER_WIDTH];
84+
strncpy(temp, filename, IDENTIFIER_WIDTH);
85+
temp[IDENTIFIER_WIDTH - 1] = '\0';
86+
8187
for (int i = 0; i < STATISTIC_COUNT; ++i) {
8288
if (sleep_stats[i].identifier[0] == '\0') {
8389
core_util_critical_section_enter();
@@ -113,8 +119,14 @@ static void sleep_tracker_print_stats(void)
113119
void sleep_tracker_lock(const char* const filename, int line)
114120
{
115121
const char* const stripped_path = strip_path(filename);
122+
116123
size_t index = sleep_tracker_find_index(stripped_path);
117124

125+
// Entry for this driver does not exist, create one.
126+
if (index == -1) {
127+
index = sleep_tracker_add(filename);
128+
}
129+
118130
core_util_atomic_incr_u8(&sleep_stats[index].count, 1);
119131

120132
printf("LOCK: %s, ln: %i, lock count: %u\r\n", stripped_path, line, deep_sleep_lock);
@@ -125,6 +137,11 @@ void sleep_tracker_unlock(const char* const filename, int line)
125137
const char* const stripped_path = strip_path(filename);
126138
size_t index = sleep_tracker_find_index(stripped_path);
127139

140+
// Entry for this driver does not exist, something went wrong.
141+
if (index == -1) {
142+
error("Unlocking sleep for driver that was not previously locked.");
143+
}
144+
128145
core_util_atomic_decr_u8(&sleep_stats[index].count, 1);
129146

130147
printf("UNLOCK: %s, ln: %i, lock count: %u\r\n", stripped_path, line, deep_sleep_lock);

0 commit comments

Comments
 (0)