summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJohannes Schanda <schanda@itestra.de>2013-03-06 16:49:43 +0100
committerJohannes Schanda <schanda@itestra.de>2013-03-06 16:49:43 +0100
commit7e94c8a7651a88e2197673ae74a7e8d121d11605 (patch)
treea1ef2f9b32afd3ca802e70dcab73f83a81db4eca
parent69ee45f2fc4fe323b5c0ae381e5092ef27d9e08b (diff)
downloadgenivi-common-api-runtime-7e94c8a7651a88e2197673ae74a7e8d121d11605.tar.gz
Improve locking in eventgpt_6_1_pre
-rw-r--r--src/CommonAPI/Event.h17
1 files changed, 12 insertions, 5 deletions
diff --git a/src/CommonAPI/Event.h b/src/CommonAPI/Event.h
index e79d491..956ea8d 100644
--- a/src/CommonAPI/Event.h
+++ b/src/CommonAPI/Event.h
@@ -79,13 +79,16 @@ typename Event<_Arguments...>::Subscription Event<_Arguments...>::subscribe(List
template <typename... _Arguments>
typename Event<_Arguments...>::Subscription Event<_Arguments...>::subscribeCancellableListener(CancellableListener listener) {
- const bool firstListenerAdded = listenersList_.empty();
+ listenerListMutex_.lock();
+ const bool firstListenerAdded = listenersList_.empty();
listenersList_.emplace_front(std::move(listener));
Subscription listenerSubscription = listenersList_.begin();
+ listenerListMutex_.unlock();
- if (firstListenerAdded)
+ if (firstListenerAdded) {
onFirstListenerAdded(*listenerSubscription);
+ }
onListenerAdded(*listenerSubscription);
@@ -98,13 +101,14 @@ void Event<_Arguments...>::unsubscribe(Subscription listenerSubscription) {
listenerListMutex_.lock();
listenersList_.erase(listenerSubscription);
+ const bool lastListenerRemoved = listenersList_.empty();
listenerListMutex_.unlock();
onListenerRemoved(cancellableListener);
- const bool lastListenerRemoved = listenersList_.empty();
- if (lastListenerRemoved)
+ if (lastListenerRemoved) {
onLastListenerRemoved(cancellableListener);
+ }
}
template <typename... _Arguments>
@@ -121,9 +125,12 @@ SubscriptionStatus Event<_Arguments...>::notifyListeners(const _Arguments&... ev
} else
iterator++;
}
+
+ const bool lEmpty = listenersList_.empty();
+
listenerListMutex_.unlock();
- return listenersList_.empty() ? SubscriptionStatus::CANCEL : SubscriptionStatus::RETAIN;
+ return lEmpty ? SubscriptionStatus::CANCEL : SubscriptionStatus::RETAIN;
}
template <typename... _Arguments>