mirror of
https://github.com/LNH-team/pico-launcher.git
synced 2026-06-02 17:16:57 +02:00
Initial commit
This commit is contained in:
41
arm9/source/rng/ThreadSafeRandomGenerator.h
Normal file
41
arm9/source/rng/ThreadSafeRandomGenerator.h
Normal file
@@ -0,0 +1,41 @@
|
||||
#pragma once
|
||||
#include <memory>
|
||||
#include <libtwl/rtos/rtosMutex.h>
|
||||
#include "RandomGenerator.h"
|
||||
|
||||
template <class TGenerator>
|
||||
class ThreadSafeRandomGenerator : public RandomGenerator
|
||||
{
|
||||
public:
|
||||
template <typename ...Args>
|
||||
explicit ThreadSafeRandomGenerator(Args && ...args)
|
||||
: _generator(std::forward<Args>(args)...)
|
||||
{
|
||||
rtos_createMutex(&_mutex);
|
||||
}
|
||||
|
||||
void Seed(u64 seed) override
|
||||
{
|
||||
rtos_lockMutex(&_mutex);
|
||||
{
|
||||
_generator.Seed(seed);
|
||||
}
|
||||
rtos_unlockMutex(&_mutex);
|
||||
}
|
||||
|
||||
protected:
|
||||
u32 Next() override
|
||||
{
|
||||
u32 result;
|
||||
rtos_lockMutex(&_mutex);
|
||||
{
|
||||
result = ((RandomGenerator&)_generator).Next();
|
||||
}
|
||||
rtos_unlockMutex(&_mutex);
|
||||
return result;
|
||||
}
|
||||
|
||||
private:
|
||||
rtos_mutex_t _mutex;
|
||||
TGenerator _generator;
|
||||
};
|
||||
Reference in New Issue
Block a user