Skip to content

Commit

Permalink
SRV_Channel: build bitmasks thread safe
Browse files Browse the repository at this point in the history
  • Loading branch information
robertlong13 committed Jan 9, 2025
1 parent c57672a commit 315950d
Showing 1 changed file with 28 additions and 10 deletions.
38 changes: 28 additions & 10 deletions libraries/SRV_Channel/SRV_Channel_aux.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -211,23 +211,41 @@ void SRV_Channels::update_aux_servo_function(void)
if (!channels) {
return;
}
function_mask.clearall();

for (uint16_t i = 0; i < SRV_Channel::k_nr_aux_servo_functions; i++) {
functions[i].channel_mask = 0;
}
invalid_mask = 0;

// set auxiliary ranges
// We build up new masks and set them at the end so that they are never
// in the middle of rebuilding if another thread tries to read them.
static Bitmask<SRV_Channel::k_nr_aux_servo_functions> new_function_mask;
new_function_mask.clearall();
uint32_t new_invalid_mask = 0;

// Build up the new function and invalid masks, and set auxiliary ranges
for (uint8_t i = 0; i < NUM_SERVO_CHANNELS; i++) {
if (!channels[i].valid_function()) {
invalid_mask |= 1U<<i;
new_invalid_mask |= 1U<<i;
continue;
}
const uint16_t function = channels[i].function.get();
channels[i].aux_servo_function_setup();
function_mask.set(function);
functions[function].channel_mask |= 1U<<i;
new_function_mask.set(function);
}
function_mask = new_function_mask;
invalid_mask = new_invalid_mask;

// Set the channel masks for each function
// This nested loop runs worst case O(NUM_SERVO_CHANNELS^2), but this only
// runs once per second.
for (uint16_t i = 0; i < SRV_Channel::k_nr_aux_servo_functions; i++) {
if (!new_function_mask.get(i)) {
functions[i].channel_mask = 0;
continue;
}
SRV_Channel::servo_mask_t new_mask = 0;
for (uint8_t j = 0; j < NUM_SERVO_CHANNELS; j++) {
if (channels[j].function.get() == i) {
new_mask |= 1U<<j;
}
}
functions[i].channel_mask = new_mask;
}
initialised = true;
}
Expand Down

0 comments on commit 315950d

Please sign in to comment.