Skip to content

Commit

Permalink
ListenerList: Add a thread safe ListenerList type
Browse files Browse the repository at this point in the history
  • Loading branch information
Anthony-Nicholls committed Jun 7, 2024
1 parent 0dfff14 commit c4d5ffa
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 9 deletions.
18 changes: 12 additions & 6 deletions modules/juce_core/containers/juce_ListenerList.h
Original file line number Diff line number Diff line change
Expand Up @@ -73,10 +73,7 @@ namespace juce
guaranteed that no more listeners will be called.
By default a ListenerList is not thread safe. If thread-safety is required,
you can provide a thread-safe Array type as the second type parameter e.g.
@code
using ThreadSafeList = ListenerList<MyListenerType, Array<MyListenerType*, CriticalSection>>;
@endcode
use the ThreadSafeListenerList type.
When calling listeners the iteration can be escaped early by using a
"BailOutChecker". A BailOutChecker is a type that has a public member function
Expand All @@ -87,8 +84,8 @@ namespace juce
@tags{Core}
*/
template <class ListenerClass,
class ArrayType = Array<ListenerClass*>>
template <typename ListenerClass,
typename ArrayType = Array<ListenerClass*>>
class ListenerList
{
public:
Expand Down Expand Up @@ -402,4 +399,13 @@ class ListenerList
JUCE_DECLARE_NON_COPYABLE (ListenerList)
};

//==============================================================================
/**
A thread safe version of the ListenerList class.
@see ListenerList
*/
template <typename ListenerClass>
using ThreadSafeListenerList = ListenerList<ListenerClass, Array<ListenerClass*, CriticalSection>>;

} // namespace juce
2 changes: 1 addition & 1 deletion modules/juce_core/threads/juce_Thread.h
Original file line number Diff line number Diff line change
Expand Up @@ -602,7 +602,7 @@ class JUCE_API Thread
uint32 affinityMask = 0;
bool deleteOnThreadEnd = false;
std::atomic<bool> shouldExit { false };
ListenerList<Listener, Array<Listener*, CriticalSection>> listeners;
ThreadSafeListenerList<Listener> listeners;

#if JUCE_ANDROID || JUCE_LINUX || JUCE_BSD
std::atomic<Priority> priority;
Expand Down
2 changes: 1 addition & 1 deletion modules/juce_core/threads/juce_ThreadPool.h
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ class JUCE_API ThreadPoolJob
String jobName;
ThreadPool* pool = nullptr;
std::atomic<bool> shouldStop { false }, isActive { false }, shouldBeDeleted { false };
ListenerList<Thread::Listener, Array<Thread::Listener*, CriticalSection>> listeners;
ThreadSafeListenerList<Thread::Listener> listeners;

JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (ThreadPoolJob)
};
Expand Down
2 changes: 1 addition & 1 deletion modules/juce_events/timers/juce_Timer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ class ShutdownDetector : private DeletedAtShutdown
}

private:
using ListenerListType = ListenerList<Listener, Array<Listener*, CriticalSection>>;
using ListenerListType = ThreadSafeListenerList<Listener>;

// By having a static ListenerList it can outlive the ShutdownDetector instance preventing
// issues for objects trying to remove themselves after the instance has been deleted
Expand Down

0 comments on commit c4d5ffa

Please sign in to comment.