Skip to content

Commit

Permalink
Update rng.hpp
Browse files Browse the repository at this point in the history
  • Loading branch information
KRM7 committed Sep 12, 2023
1 parent 25477bb commit dc28782
Showing 1 changed file with 12 additions and 10 deletions.
22 changes: 12 additions & 10 deletions src/utility/rng.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -194,9 +194,9 @@ namespace gapp::rng
/** Set a new seed for the generator. Thread-safe. */
static void seed(std::uint64_t seed)
{
std::scoped_lock _{ tls_generators_.lock };
std::scoped_lock _{ tls_generators().lock };
global_generator_.seed(seed);
for (Generator* generator : tls_generators_.list)
for (Generator* generator : tls_generators().list)
{
*generator = global_generator_.jump();
}
Expand All @@ -213,31 +213,33 @@ namespace gapp::rng

struct RegisteredGenerator
{
RegisteredGenerator()
RegisteredGenerator() noexcept
{
std::scoped_lock _{ tls_generators_.lock };
std::scoped_lock _{ tls_generators().lock };
instance = global_generator_.jump();
tls_generators_.list.push_back(std::addressof(instance));
tls_generators().list.push_back(std::addressof(instance));
}

~RegisteredGenerator() noexcept
{
std::scoped_lock _{ tls_generators_.lock };
std::erase(tls_generators_.list, std::addressof(instance));
std::scoped_lock _{ tls_generators().lock };
std::erase(tls_generators().list, std::addressof(instance));
}

Generator instance{ 0 };
};

struct GeneratorList
{
std::mutex lock;
detail::spinlock lock;
std::vector<Generator*> list;
};

GAPP_API inline static GeneratorList& tls_generators_ = *new GeneratorList;
static GeneratorList& tls_generators() { static GeneratorList l; return l; }

//GAPP_API inline static GeneratorList& tls_generators_ = *new GeneratorList;
GAPP_API inline static constinit Xoroshiro128p global_generator_{ GAPP_SEED };
alignas(128) inline static thread_local RegisteredGenerator generator_;
alignas(128) inline static thread_local RegisteredGenerator generator_{};
};


Expand Down

0 comments on commit dc28782

Please sign in to comment.