stdlib: Add single-threaded fast path to rand()

Improve performance of rand() and __random() by adding a single-threaded
fast path.  Bench-random-lock shows about 5x speedup on Neoverse V1.

Reviewed-by: Adhemerval Zanella  <adhemerval.zanella@linaro.org>
This commit is contained in:
Wilco Dijkstra 2024-03-18 15:18:20 +00:00
parent 4734d0f8ad
commit be0cfd848d

View file

@ -51,6 +51,7 @@
SUCH DAMAGE.*/
#include <libc-lock.h>
#include <sys/single_threaded.h>
#include <limits.h>
#include <stddef.h>
#include <stdlib.h>
@ -288,6 +289,12 @@ __random (void)
{
int32_t retval;
if (SINGLE_THREAD_P)
{
(void) __random_r (&unsafe_state, &retval);
return retval;
}
__libc_lock_lock (lock);
(void) __random_r (&unsafe_state, &retval);