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