Debian Codegraph @

crt/s2n/utils/s2n_atomic.c

raw symbols SCIP
1 /*
2 * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License").
5 * You may not use this file except in compliance with the License.
6 * A copy of the License is located at
7 *
8 * http://aws.amazon.com/apache2.0
9 *
10 * or in the "license" file accompanying this file. This file is distributed
11 * on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
12 * express or implied. See the License for the specific language governing
13 * permissions and limitations under the License.
14 */
15
16 #include "utils/s2n_atomic.h"
17
18 #include <signal.h>
19
20 #include "utils/s2n_safety.h"
21
22 static sig_atomic_t set_val = true;
23 static sig_atomic_t clear_val = false;
24
25 S2N_RESULT s2n_atomic_init()
26 {
27 #if S2N_ATOMIC_SUPPORTED && S2N_THREAD_SANITIZER
28 RESULT_ENSURE(__atomic_always_lock_free(sizeof(s2n_atomic_flag), NULL), S2N_ERR_ATOMIC);
29 #endif
30 return S2N_RESULT_OK;
31 }
32
33 void s2n_atomic_flag_set(s2n_atomic_flag *var)
34 {
35 #if S2N_ATOMIC_SUPPORTED && S2N_THREAD_SANITIZER
36 __atomic_store(&var->val, &set_val, __ATOMIC_RELAXED);
37 #else
38 var->val = set_val;
39 #endif
40 }
41
42 void s2n_atomic_flag_clear(s2n_atomic_flag *var)
43 {
44 #if S2N_ATOMIC_SUPPORTED && S2N_THREAD_SANITIZER
45 __atomic_store(&var->val, &clear_val, __ATOMIC_RELAXED);
46 #else
47 var->val = clear_val;
48 #endif
49 }
50
51 bool s2n_atomic_flag_test(s2n_atomic_flag *var)
52 {
53 #if S2N_ATOMIC_SUPPORTED && S2N_THREAD_SANITIZER
54 sig_atomic_t result = 0;
55 __atomic_load(&var->val, &result, __ATOMIC_RELAXED);
56 return result;
57 #else
58 return var->val;
59 #endif
60 }