xref: /onnv-gate/usr/src/lib/libc/port/gen/rand.c (revision 6812:febeba71273d)
10Sstevel@tonic-gate /*
20Sstevel@tonic-gate  * CDDL HEADER START
30Sstevel@tonic-gate  *
40Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*6812Sraf  * Common Development and Distribution License (the "License").
6*6812Sraf  * You may not use this file except in compliance with the License.
70Sstevel@tonic-gate  *
80Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
90Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
100Sstevel@tonic-gate  * See the License for the specific language governing permissions
110Sstevel@tonic-gate  * and limitations under the License.
120Sstevel@tonic-gate  *
130Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
140Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
150Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
160Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
170Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
180Sstevel@tonic-gate  *
190Sstevel@tonic-gate  * CDDL HEADER END
200Sstevel@tonic-gate  */
21*6812Sraf 
220Sstevel@tonic-gate /*
23*6812Sraf  * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
240Sstevel@tonic-gate  * Use is subject to license terms.
250Sstevel@tonic-gate  */
260Sstevel@tonic-gate 
270Sstevel@tonic-gate /*	Copyright (c) 1988 AT&T	*/
280Sstevel@tonic-gate /*	  All Rights Reserved  	*/
290Sstevel@tonic-gate 
300Sstevel@tonic-gate /*
310Sstevel@tonic-gate  * University Copyright- Copyright (c) 1982, 1986, 1988
320Sstevel@tonic-gate  * The Regents of the University of California
330Sstevel@tonic-gate  * All Rights Reserved
340Sstevel@tonic-gate  *
350Sstevel@tonic-gate  * University Acknowledgment- Portions of this document are derived from
360Sstevel@tonic-gate  * software developed by the University of California, Berkeley, and its
370Sstevel@tonic-gate  * contributors.
380Sstevel@tonic-gate  */
390Sstevel@tonic-gate 
400Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
410Sstevel@tonic-gate 
42*6812Sraf #include "lint.h"
430Sstevel@tonic-gate #include "thr_uberdata.h"
440Sstevel@tonic-gate #include <stdlib.h>
450Sstevel@tonic-gate #include <atomic.h>
460Sstevel@tonic-gate 
470Sstevel@tonic-gate /*
480Sstevel@tonic-gate  * We need atomic_cas_uint() protection because a multithreaded process
490Sstevel@tonic-gate  * may be calling rand() from different threads and because multiple
500Sstevel@tonic-gate  * threads may be calling rand_r() using a pointer to the same seed.
510Sstevel@tonic-gate  */
520Sstevel@tonic-gate 
530Sstevel@tonic-gate static uint_t rand_seed = 1;
540Sstevel@tonic-gate 
550Sstevel@tonic-gate #define	NEXT_SEED(s)	((s) * 1103515245 + 12345)
560Sstevel@tonic-gate #define	NEXT_VALUE(s)	(((s) >> 16) & 0x7fff)
570Sstevel@tonic-gate 
580Sstevel@tonic-gate static int
rand_mt(uint_t * seed)590Sstevel@tonic-gate rand_mt(uint_t *seed)
600Sstevel@tonic-gate {
610Sstevel@tonic-gate 	uint_t old_seed;
620Sstevel@tonic-gate 	uint_t new_seed;
630Sstevel@tonic-gate 
640Sstevel@tonic-gate 	for (;;) {
650Sstevel@tonic-gate 		/* force reload on every iteration */
660Sstevel@tonic-gate 		old_seed = *(volatile uint_t *)seed;
670Sstevel@tonic-gate 		new_seed = NEXT_SEED(old_seed);
680Sstevel@tonic-gate 		if (atomic_cas_uint(seed, old_seed, new_seed) == old_seed)
690Sstevel@tonic-gate 			return (NEXT_VALUE(new_seed));
700Sstevel@tonic-gate 		SMT_PAUSE();
710Sstevel@tonic-gate 	}
720Sstevel@tonic-gate }
730Sstevel@tonic-gate 
740Sstevel@tonic-gate int
rand_r(uint_t * seed)750Sstevel@tonic-gate rand_r(uint_t *seed)
760Sstevel@tonic-gate {
770Sstevel@tonic-gate 	if (curthread->ul_uberdata->uberflags.uf_mt)
780Sstevel@tonic-gate 		return (rand_mt(seed));
790Sstevel@tonic-gate 	return (NEXT_VALUE(*seed = NEXT_SEED(*seed)));
800Sstevel@tonic-gate }
810Sstevel@tonic-gate 
820Sstevel@tonic-gate void
srand(uint_t seed)830Sstevel@tonic-gate srand(uint_t seed)
840Sstevel@tonic-gate {
850Sstevel@tonic-gate 	rand_seed = seed;
860Sstevel@tonic-gate }
870Sstevel@tonic-gate 
880Sstevel@tonic-gate int
rand(void)890Sstevel@tonic-gate rand(void)
900Sstevel@tonic-gate {
910Sstevel@tonic-gate 	return (rand_r(&rand_seed));
920Sstevel@tonic-gate }
93