The main goal of these changes is to support checking if some data has been changed over time. This is used by the WIP simulation nodes during baking to detect which attributes have to be stored in every frame because they have changed. By using a combination of a weak user count and a version counter, it is possible to detect that an attribute (or any data controlled by implicit sharing) has not been changed with O(1) memory and time. It's still possible that the data has been changed multiple times and is the same in the end and beginning of course. That wouldn't be detected using this mechanism. The `ImplicitSharingInfo` struct has a new weak user count. A weak reference is one that does not keep the referenced data alive, but makes sure that the `ImplicitSharingInfo` itself is not deleted. If some piece of data has one strong and multiple weak users, it is still mutable. If the strong user count goes down to zero, the referenced data is freed. Remaining weak users can check for this condition using `is_expired`. This is a bit similar to `std::weak_ptr` but there is an important difference: a weak user can not become a strong user while one can create a `shared_ptr` from a `weak_ptr`. This restriction is necessary, because some code might be changing the referenced data assuming that it is the only owner. If another thread suddenly adds a new owner, the data would be shared again and the first thread would not have been allowed to modify the data in the first place. There is also a new integer version counter in `ImplicitSharingInfo`. It is incremented whenever some code wants to modify the referenced data. Obviously, this can only be done when the data is not shared because then it would be immutable. By comparing an old and new version number of the same sharing info, one can check if the data has been modified. One has to keep a weak reference to the sharing info together with the old version number to ensure that the new sharing info is still the same as the old one. Without this, it can happen that the sharing info was freed and a new one was allocated at the same pointer address. Using a strong reference for this purpose does not work, because then the data would never be modified because it's shared.
115 lines
3.0 KiB
C++
115 lines
3.0 KiB
C++
/* SPDX-License-Identifier: Apache-2.0 */
|
|
|
|
#include "MEM_guardedalloc.h"
|
|
|
|
#include "BLI_implicit_sharing_ptr.hh"
|
|
|
|
#include "testing/testing.h"
|
|
|
|
namespace blender::tests {
|
|
|
|
class ImplicitlySharedData : public ImplicitSharingMixin {
|
|
public:
|
|
ImplicitSharingPtr<ImplicitlySharedData> copy() const
|
|
{
|
|
return MEM_new<ImplicitlySharedData>(__func__);
|
|
}
|
|
|
|
void delete_self() override
|
|
{
|
|
MEM_delete(this);
|
|
}
|
|
};
|
|
|
|
class SharedDataContainer {
|
|
private:
|
|
ImplicitSharingPtr<ImplicitlySharedData> data_;
|
|
|
|
public:
|
|
SharedDataContainer() : data_(MEM_new<ImplicitlySharedData>(__func__)) {}
|
|
|
|
const ImplicitSharingInfo *sharing_info() const
|
|
{
|
|
return data_.get();
|
|
}
|
|
|
|
const ImplicitlySharedData *get_for_read() const
|
|
{
|
|
return data_.get();
|
|
}
|
|
|
|
ImplicitlySharedData *get_for_write()
|
|
{
|
|
if (!data_) {
|
|
return nullptr;
|
|
}
|
|
if (data_->is_mutable()) {
|
|
data_->tag_ensured_mutable();
|
|
return data_.get();
|
|
}
|
|
data_ = data_->copy();
|
|
return data_.get();
|
|
}
|
|
};
|
|
|
|
TEST(implicit_sharing, CopyOnWriteAccess)
|
|
{
|
|
/* Create the initial data. */
|
|
SharedDataContainer a;
|
|
EXPECT_NE(a.get_for_read(), nullptr);
|
|
|
|
/* a and b share the same underlying data now. */
|
|
SharedDataContainer b = a;
|
|
EXPECT_EQ(a.get_for_read(), b.get_for_read());
|
|
|
|
/* c now shares the data with a and b. */
|
|
SharedDataContainer c = a;
|
|
EXPECT_EQ(b.get_for_read(), c.get_for_read());
|
|
|
|
/* Retrieving write access on b should make a copy because the data is shared. */
|
|
ImplicitlySharedData *data_b1 = b.get_for_write();
|
|
EXPECT_NE(data_b1, nullptr);
|
|
EXPECT_EQ(data_b1, b.get_for_read());
|
|
EXPECT_NE(data_b1, a.get_for_read());
|
|
EXPECT_NE(data_b1, c.get_for_read());
|
|
|
|
/* Retrieving the same write access again should *not* make another copy. */
|
|
ImplicitlySharedData *data_b2 = b.get_for_write();
|
|
EXPECT_EQ(data_b1, data_b2);
|
|
|
|
/* Moving b should also move the data. b then does not have ownership anymore. Since the data in
|
|
* b only had one owner, the data is still mutable now that d is the owner. */
|
|
SharedDataContainer d = std::move(b);
|
|
EXPECT_EQ(b.get_for_read(), nullptr);
|
|
EXPECT_EQ(b.get_for_write(), nullptr);
|
|
EXPECT_EQ(d.get_for_read(), data_b1);
|
|
EXPECT_EQ(d.get_for_write(), data_b1);
|
|
}
|
|
|
|
TEST(implicit_sharing, WeakUser)
|
|
{
|
|
SharedDataContainer a;
|
|
const ImplicitSharingInfo *sharing_info = a.sharing_info();
|
|
EXPECT_FALSE(sharing_info->is_expired());
|
|
EXPECT_TRUE(sharing_info->is_mutable());
|
|
sharing_info->add_weak_user();
|
|
EXPECT_FALSE(sharing_info->is_expired());
|
|
EXPECT_TRUE(sharing_info->is_mutable());
|
|
a = {};
|
|
EXPECT_TRUE(sharing_info->is_expired());
|
|
sharing_info->remove_weak_user_and_delete_if_last();
|
|
}
|
|
|
|
TEST(implicit_sharing, Version)
|
|
{
|
|
SharedDataContainer a;
|
|
const ImplicitSharingInfo *sharing_info = a.sharing_info();
|
|
const int old_version = sharing_info->version();
|
|
a.get_for_read();
|
|
EXPECT_EQ(old_version, sharing_info->version());
|
|
a.get_for_write();
|
|
EXPECT_LT(old_version, sharing_info->version());
|
|
}
|
|
|
|
} // namespace blender::tests
|