diff options
Diffstat (limited to 'source/blender/blenlib')
-rw-r--r-- | source/blender/blenlib/BLI_rand.h | 8 | ||||
-rw-r--r-- | source/blender/blenlib/intern/rand.c | 26 |
2 files changed, 34 insertions, 0 deletions
diff --git a/source/blender/blenlib/BLI_rand.h b/source/blender/blenlib/BLI_rand.h index c86b3aea503..879af446469 100644 --- a/source/blender/blenlib/BLI_rand.h +++ b/source/blender/blenlib/BLI_rand.h @@ -40,6 +40,9 @@ struct RNG; typedef struct RNG RNG; +struct RNG_THREAD_ARRAY; +typedef struct RNG_THREAD_ARRAY RNG_THREAD_ARRAY; + struct RNG *BLI_rng_new(unsigned int seed); struct RNG *BLI_rng_new_srandom(unsigned int seed); void BLI_rng_free(struct RNG *rng) ATTR_NONNULL(1); @@ -89,4 +92,9 @@ int BLI_thread_rand(int thread) ATTR_WARN_UNUSED_RESULT; /** Allows up to BLENDER_MAX_THREADS threads to address */ float BLI_thread_frand(int thread) ATTR_WARN_UNUSED_RESULT; +/** array versions for thread safe random generation */ +RNG_THREAD_ARRAY *BLI_rng_threaded_new(void); +void BLI_rng_threaded_free(struct RNG_THREAD_ARRAY *rngarr) ATTR_NONNULL(1); +int BLI_rng_thread_rand(RNG_THREAD_ARRAY *rngarr, int thread) ATTR_WARN_UNUSED_RESULT; + #endif /* __BLI_RAND_H__ */ diff --git a/source/blender/blenlib/intern/rand.c b/source/blender/blenlib/intern/rand.c index 3dff0b31091..70780f9d47d 100644 --- a/source/blender/blenlib/intern/rand.c +++ b/source/blender/blenlib/intern/rand.c @@ -245,3 +245,29 @@ float BLI_thread_frand(int thread) return BLI_rng_get_float(&rng_tab[thread]); } +struct RNG_THREAD_ARRAY { + RNG rng_tab[BLENDER_MAX_THREADS]; +}; + +RNG_THREAD_ARRAY *BLI_rng_threaded_new(void) +{ + unsigned int i; + RNG_THREAD_ARRAY *rngarr = MEM_mallocN(sizeof(RNG_THREAD_ARRAY), "random_array"); + + for (i = 0; i < BLENDER_MAX_THREADS; i++) { + BLI_rng_srandom(&rngarr->rng_tab[i], (unsigned int)(rngarr->rng_tab[i].X * 257)); + } + + return rngarr; +} + +void BLI_rng_threaded_free(struct RNG_THREAD_ARRAY *rngarr) +{ + MEM_freeN(rngarr); +} + +int BLI_rng_thread_rand(RNG_THREAD_ARRAY *rngarr, int thread) +{ + return BLI_rng_get_int(&rngarr->rng_tab[thread]); +} + |