Skip to content

Commit

Permalink
Make CollectionParent::generate_key() threadsafe
Browse files Browse the repository at this point in the history
  • Loading branch information
jedelbo committed May 15, 2023
1 parent 69814d7 commit ab8da70
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 2 deletions.
4 changes: 3 additions & 1 deletion src/realm/collection_parent.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -262,11 +262,13 @@ CollectionBasePtr CollectionParent::get_collection_ptr(ColKey col_key) const
}


int64_t CollectionParent::generate_key(size_t sz) const
int64_t CollectionParent::generate_key(size_t sz)
{
static std::mt19937 gen32;
static std::mutex mutex;

int64_t key;
const std::lock_guard<std::mutex> lock(mutex);
do {
if (sz < 0x10) {
key = int8_t(gen32());
Expand Down
2 changes: 1 addition & 1 deletion src/realm/collection_parent.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,7 @@ class CollectionParent : public std::enable_shared_from_this<CollectionParent> {
SetBasePtr get_setbase_ptr(ColKey col_key) const;
CollectionBasePtr get_collection_ptr(ColKey col_key) const;

int64_t generate_key(size_t sz) const;
static int64_t generate_key(size_t sz);
};

} // namespace realm
Expand Down

0 comments on commit ab8da70

Please sign in to comment.