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