diff --git a/libraries/SRV_Channel/SRV_Channel.h b/libraries/SRV_Channel/SRV_Channel.h index acb6021b89205..ee6e5913e8eb4 100644 --- a/libraries/SRV_Channel/SRV_Channel.h +++ b/libraries/SRV_Channel/SRV_Channel.h @@ -605,6 +605,12 @@ class SRV_Channels { static Bitmask function_mask; static bool initialised; +#if CONFIG_HAL_BOARD != HAL_BOARD_CHIBIOS + // Prevents access to function_mask, functions[].channel_mask, and invalid_mask while they are being rebuilt + // (the race cannot happen on ChibiOS because the rebuild happens on the main thread and no callers can preempt it) + static HAL_Semaphore mask_semaphore; +#endif + // this static arrangement is to avoid having static objects in AP_Param tables static SRV_Channel *channels; static SRV_Channels *_singleton; diff --git a/libraries/SRV_Channel/SRV_Channel_aux.cpp b/libraries/SRV_Channel/SRV_Channel_aux.cpp index 3fb4e922744d7..16000c4da206a 100644 --- a/libraries/SRV_Channel/SRV_Channel_aux.cpp +++ b/libraries/SRV_Channel/SRV_Channel_aux.cpp @@ -211,6 +211,10 @@ void SRV_Channels::update_aux_servo_function(void) if (!channels) { return; } + +#if CONFIG_HAL_BOARD != HAL_BOARD_CHIBIOS + WITH_SEMAPHORE(mask_semaphore); +#endif function_mask.clearall(); for (uint16_t i = 0; i < SRV_Channel::k_nr_aux_servo_functions; i++) { @@ -506,6 +510,9 @@ SRV_Channels::function_assigned(SRV_Channel::Aux_servo_function_t function) if (!initialised) { update_aux_servo_function(); } +#if CONFIG_HAL_BOARD != HAL_BOARD_CHIBIOS + WITH_SEMAPHORE(mask_semaphore); +#endif return function_mask.get(uint16_t(function)); } @@ -556,6 +563,9 @@ bool SRV_Channels::set_aux_channel_default(SRV_Channel::Aux_servo_function_t fun channels[channel].type_setup = false; channels[channel].function.set_and_default(function); channels[channel].aux_servo_function_setup(); +#if CONFIG_HAL_BOARD != HAL_BOARD_CHIBIOS + WITH_SEMAPHORE(mask_semaphore); +#endif function_mask.set((uint16_t)function); if (SRV_Channel::valid_function(function)) { functions[function].channel_mask |= 1U< SRV_Channels::function_mask; +#if CONFIG_HAL_BOARD != HAL_BOARD_CHIBIOS +HAL_Semaphore SRV_Channels::mask_semaphore; +#endif SRV_Channels::srv_function SRV_Channels::functions[SRV_Channel::k_nr_aux_servo_functions]; SRV_Channels::slew_list *SRV_Channels::_slew;