xref: /freebsd/contrib/unbound/compat/arc4_lock.c (revision ff8258499cd4a5a2a522c985cd7592a7904d973f)
1*ff825849SDag-Erling Smørgrav /* arc4_lock.c - global lock for arc4random
2*ff825849SDag-Erling Smørgrav *
3*ff825849SDag-Erling Smørgrav  * Copyright (c) 2014, NLnet Labs. All rights reserved.
4*ff825849SDag-Erling Smørgrav  *
5*ff825849SDag-Erling Smørgrav  * This software is open source.
6*ff825849SDag-Erling Smørgrav  *
7*ff825849SDag-Erling Smørgrav  * Redistribution and use in source and binary forms, with or without
8*ff825849SDag-Erling Smørgrav  * modification, are permitted provided that the following conditions
9*ff825849SDag-Erling Smørgrav  * are met:
10*ff825849SDag-Erling Smørgrav  *
11*ff825849SDag-Erling Smørgrav  * Redistributions of source code must retain the above copyright notice,
12*ff825849SDag-Erling Smørgrav  * this list of conditions and the following disclaimer.
13*ff825849SDag-Erling Smørgrav  *
14*ff825849SDag-Erling Smørgrav  * Redistributions in binary form must reproduce the above copyright notice,
15*ff825849SDag-Erling Smørgrav  * this list of conditions and the following disclaimer in the documentation
16*ff825849SDag-Erling Smørgrav  * and/or other materials provided with the distribution.
17*ff825849SDag-Erling Smørgrav  *
18*ff825849SDag-Erling Smørgrav  * Neither the name of the NLNET LABS nor the names of its contributors may
19*ff825849SDag-Erling Smørgrav  * be used to endorse or promote products derived from this software without
20*ff825849SDag-Erling Smørgrav  * specific prior written permission.
21*ff825849SDag-Erling Smørgrav  *
22*ff825849SDag-Erling Smørgrav  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
23*ff825849SDag-Erling Smørgrav  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
24*ff825849SDag-Erling Smørgrav  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
25*ff825849SDag-Erling Smørgrav  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
26*ff825849SDag-Erling Smørgrav  * HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
27*ff825849SDag-Erling Smørgrav  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
28*ff825849SDag-Erling Smørgrav  * TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
29*ff825849SDag-Erling Smørgrav  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
30*ff825849SDag-Erling Smørgrav  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
31*ff825849SDag-Erling Smørgrav  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
32*ff825849SDag-Erling Smørgrav  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33*ff825849SDag-Erling Smørgrav  */
34*ff825849SDag-Erling Smørgrav #include "config.h"
35*ff825849SDag-Erling Smørgrav #define LOCKRET(func) func
36*ff825849SDag-Erling Smørgrav #include "util/locks.h"
37*ff825849SDag-Erling Smørgrav 
38*ff825849SDag-Erling Smørgrav void _ARC4_LOCK(void);
39*ff825849SDag-Erling Smørgrav void _ARC4_UNLOCK(void);
40*ff825849SDag-Erling Smørgrav 
41*ff825849SDag-Erling Smørgrav #ifdef THREADS_DISABLED
42*ff825849SDag-Erling Smørgrav void _ARC4_LOCK(void)
43*ff825849SDag-Erling Smørgrav {
44*ff825849SDag-Erling Smørgrav }
45*ff825849SDag-Erling Smørgrav 
46*ff825849SDag-Erling Smørgrav void _ARC4_UNLOCK(void)
47*ff825849SDag-Erling Smørgrav {
48*ff825849SDag-Erling Smørgrav }
49*ff825849SDag-Erling Smørgrav #else /* !THREADS_DISABLED */
50*ff825849SDag-Erling Smørgrav 
51*ff825849SDag-Erling Smørgrav static lock_quick_t arc4lock;
52*ff825849SDag-Erling Smørgrav static int arc4lockinit = 0;
53*ff825849SDag-Erling Smørgrav 
54*ff825849SDag-Erling Smørgrav void _ARC4_LOCK(void)
55*ff825849SDag-Erling Smørgrav {
56*ff825849SDag-Erling Smørgrav 	if(!arc4lockinit) {
57*ff825849SDag-Erling Smørgrav 		arc4lockinit = 1;
58*ff825849SDag-Erling Smørgrav 		lock_quick_init(&arc4lock);
59*ff825849SDag-Erling Smørgrav 	}
60*ff825849SDag-Erling Smørgrav 	lock_quick_lock(&arc4lock);
61*ff825849SDag-Erling Smørgrav }
62*ff825849SDag-Erling Smørgrav 
63*ff825849SDag-Erling Smørgrav void _ARC4_UNLOCK(void)
64*ff825849SDag-Erling Smørgrav {
65*ff825849SDag-Erling Smørgrav 	lock_quick_unlock(&arc4lock);
66*ff825849SDag-Erling Smørgrav }
67*ff825849SDag-Erling Smørgrav #endif /* THREADS_DISABLED */
68