diff --git a/monkey/monkey_island/cc/event_queue/__init__.py b/monkey/monkey_island/cc/event_queue/__init__.py index 6eab2b6ac..1a9ec662b 100644 --- a/monkey/monkey_island/cc/event_queue/__init__.py +++ b/monkey/monkey_island/cc/event_queue/__init__.py @@ -1,3 +1,4 @@ from .types import IslandEventSubscriber from .i_island_event_queue import IIslandEventQueue, IslandEventTopic from .pypubsub_island_event_queue import PyPubSubIslandEventQueue +from .locking_island_event_queue_decorator import LockingIslandEventQueueDecorator diff --git a/monkey/monkey_island/cc/event_queue/locking_island_event_queue_decorator.py b/monkey/monkey_island/cc/event_queue/locking_island_event_queue_decorator.py new file mode 100644 index 000000000..da20ef3b1 --- /dev/null +++ b/monkey/monkey_island/cc/event_queue/locking_island_event_queue_decorator.py @@ -0,0 +1,20 @@ +from threading import Lock + +from . import IIslandEventQueue, IslandEventSubscriber, IslandEventTopic + + +class LockingIslandEventQueueDecorator(IIslandEventQueue): + """ + Makes an IIslandEventQueue thread-safe by locking publish() + """ + + def __init__(self, island_event_queue: IIslandEventQueue): + self._lock = Lock() + self._island_event_queue = island_event_queue + + def subscribe(self, topic: IslandEventTopic, subscriber: IslandEventSubscriber): + self._island_event_queue.subscribe(topic, subscriber) + + def publish(self, topic: IslandEventTopic, **kwargs): + with self._lock: + self._island_event_queue.publish(topic, **kwargs)