Browse Source

Make random state thread-local.

tags/v1.0.0
Andrew Belt 5 years ago
parent
commit
8d63d198ea
4 changed files with 16 additions and 12 deletions
  1. +7
    -7
      include/plugin.hpp
  2. +3
    -1
      include/random.hpp
  3. +5
    -1
      src/engine/Engine.cpp
  4. +1
    -3
      src/random.cpp

+ 7
- 7
include/plugin.hpp View File

@@ -14,13 +14,6 @@ namespace rack {
namespace plugin {


struct Update {
std::string pluginSlug;
std::string version;
std::string changelogUrl;
};


void init();
void destroy();
void logIn(const std::string &email, const std::string &password);
@@ -36,6 +29,13 @@ std::string normalizeTag(const std::string &tag);
bool isSlugValid(const std::string &slug);


struct Update {
std::string pluginSlug;
std::string version;
std::string changelogUrl;
};


extern const std::set<std::string> allowedTags;
extern std::vector<Plugin*> plugins;



+ 3
- 1
include/random.hpp View File

@@ -11,7 +11,9 @@ namespace rack {
namespace random {


/** Seeds the RNG with the current time */
/** Initializes the thread-local RNG state.
Must call per-thread, otherwise the RNG will always return 0.
*/
void init();
/** Returns a uniform random uint32_t from 0 to UINT32_MAX */
uint32_t u32();


+ 5
- 1
src/engine/Engine.cpp View File

@@ -150,6 +150,7 @@ struct EngineWorker {

void start() {
thread = std::thread([&] {
random::init();
run();
});
}
@@ -436,7 +437,10 @@ static void Engine_run(Engine *that) {

void Engine::start() {
internal->running = true;
internal->thread = std::thread(Engine_run, this);
internal->thread = std::thread([&] {
random::init();
Engine_run(this);
});
}

void Engine::stop() {


+ 1
- 3
src/random.cpp View File

@@ -11,7 +11,7 @@ namespace random {
// xoroshiro128+
// from http://xoroshiro.di.unimi.it/xoroshiro128plus.c

static uint64_t xoroshiro128plus_state[2] = {};
thread_local uint64_t xoroshiro128plus_state[2];

static uint64_t rotl(const uint64_t x, int k) {
return (x << k) | (x >> (64 - k));
@@ -30,8 +30,6 @@ static uint64_t xoroshiro128plus_next(void) {
}

void init() {
// Only allow the seed to be initialized once during the lifetime of the program.
assert(xoroshiro128plus_state[0] == 0 && xoroshiro128plus_state[1] == 0);
struct timeval tv;
gettimeofday(&tv, NULL);
xoroshiro128plus_state[0] = tv.tv_sec;


Loading…
Cancel
Save