1 ////////////////////////////////////////////////////////////////////////////
3 // Copyright 2016 Realm Inc.
5 // Licensed under the Apache License, Version 2.0 (the "License");
6 // you may not use this file except in compliance with the License.
7 // You may obtain a copy of the License at
9 // http://www.apache.org/licenses/LICENSE-2.0
11 // Unless required by applicable law or agreed to in writing, software
12 // distributed under the License is distributed on an "AS IS" BASIS,
13 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 // See the License for the specific language governing permissions and
15 // limitations under the License.
17 ////////////////////////////////////////////////////////////////////////////
19 #include "collection_notifications.hpp"
21 #include "impl/collection_notifier.hpp"
23 using namespace realm;
24 using namespace realm::_impl;
26 NotificationToken::NotificationToken(std::shared_ptr<_impl::CollectionNotifier> notifier, uint64_t token)
27 : m_notifier(std::move(notifier)), m_token(token)
31 NotificationToken::~NotificationToken()
33 // m_notifier itself (and not just the pointed-to thing) needs to be accessed
34 // atomically to ensure that there are no data races when the token is
35 // destroyed after being modified on a different thread.
36 // This is needed despite the token not being thread-safe in general as
37 // users find it very surprising for obj-c objects to care about what
38 // thread they are deallocated on.
39 if (auto notifier = m_notifier.exchange({})) {
40 notifier->remove_callback(m_token);
44 NotificationToken::NotificationToken(NotificationToken&&) = default;
46 NotificationToken& NotificationToken::operator=(realm::NotificationToken&& rgt)
49 if (auto notifier = m_notifier.exchange({})) {
50 notifier->remove_callback(m_token);
52 m_notifier = std::move(rgt.m_notifier);
53 m_token = rgt.m_token;
58 void NotificationToken::suppress_next()
60 m_notifier.load()->suppress_next_notification(m_token);