Skip to content

Commit 057e20b

Browse files
committed
Fix race condition then registering/unregistering callbacks
Adding and removing callbacks needs to be protected by UpdateMutex too, otherwise a different thread calling UpdateLEDs() might cause a segfault.
1 parent 8eecbd9 commit 057e20b

1 file changed

Lines changed: 8 additions & 0 deletions

File tree

RGBController/RGBController.cpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1601,12 +1601,16 @@ void RGBController::SetMode(int mode)
16011601

16021602
void RGBController::RegisterUpdateCallback(RGBControllerCallback new_callback, void * new_callback_arg)
16031603
{
1604+
UpdateMutex.lock();
16041605
UpdateCallbacks.push_back(new_callback);
16051606
UpdateCallbackArgs.push_back(new_callback_arg);
1607+
UpdateMutex.unlock();
16061608
}
16071609

16081610
void RGBController::UnregisterUpdateCallback(void * callback_arg)
16091611
{
1612+
UpdateMutex.lock();
1613+
16101614
for(unsigned int callback_idx = 0; callback_idx < UpdateCallbackArgs.size(); callback_idx++ )
16111615
{
16121616
if(UpdateCallbackArgs[callback_idx] == callback_arg)
@@ -1617,12 +1621,16 @@ void RGBController::UnregisterUpdateCallback(void * callback_arg)
16171621
break;
16181622
}
16191623
}
1624+
1625+
UpdateMutex.unlock();
16201626
}
16211627

16221628
void RGBController::ClearCallbacks()
16231629
{
1630+
UpdateMutex.lock();
16241631
UpdateCallbacks.clear();
16251632
UpdateCallbackArgs.clear();
1633+
UpdateMutex.unlock();
16261634
}
16271635

16281636
void RGBController::SignalUpdate()

0 commit comments

Comments
 (0)