Skip to content

Commit

Permalink
add ThreadSafeBlockingListenerList
Browse files Browse the repository at this point in the history
This allows the use of ScopedLock instead of ScopedTryLock in listener calls
  • Loading branch information
benkuper committed Oct 19, 2024
1 parent 5179f4e commit 3bf2771
Showing 1 changed file with 10 additions and 3 deletions.
13 changes: 10 additions & 3 deletions modules/juce_core/containers/juce_ListenerList.h
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,8 @@ namespace juce
@tags{Core}
*/
template <typename ListenerClass,
typename ArrayType = Array<ListenerClass*>>
typename ArrayType = Array<ListenerClass*>,
typename LockMode = ScopedTryLock>
class ListenerList
{
public:
Expand Down Expand Up @@ -230,13 +231,16 @@ class ListenerList
Callback&& callback)
{
#if JUCE_ASSERTIONS_ENABLED_OR_LOGGED
const ScopedTryLock callCheckedExcludingLock (*callCheckedExcludingMutex);
const LockMode callCheckedExcludingLock (*callCheckedExcludingMutex);

// If you hit this assertion it means you're trying to call the listeners from multiple
// threads concurrently. If you need to do this either use a LightweightListenerList, for a
// lock free option, or a ThreadSafeListenerList if you also need the extra guarantees
// provided by ListenerList. See the class descriptions for more details.
jassert (callCheckedExcludingLock.isLocked());
if (std::is_same<ScopedTryLock, LockMode>())
{
jassert(((const ScopedTryLock*)&callCheckedExcludingLock)->isLocked());
}
#endif

if (! initialised())
Expand Down Expand Up @@ -415,6 +419,9 @@ class ListenerList
*/
template <typename ListenerClass>
using ThreadSafeListenerList = ListenerList<ListenerClass, Array<ListenerClass*, CriticalSection>>;
template <typename ListenerClass>
using ThreadSafeBlockingListenerList = ListenerList<ListenerClass, Array<ListenerClass*, CriticalSection>, ScopedLock>;


//==============================================================================
/**
Expand Down

0 comments on commit 3bf2771

Please sign in to comment.