| 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 | #pragma once |
| 16 | |
| 17 | #include <signal.h> |
| 18 | |
| 19 | #include "utils/s2n_result.h" |
| 20 | |
| 21 | /* s2n-tls allows s2n_send and s2n_recv to be called concurrently. |
| 22 | * There are a handful of values that both methods need to access. |
| 23 | * However, C99 has no concept of concurrency, so provides no atomic data types. |
| 24 | */ |
| 25 | |
| 26 | /* Wrap the underlying value in a struct to encourage developers to only use |
| 27 | * the provided atomic methods. |
| 28 | */ |
| 29 | typedef struct { |
| 30 | /* Traditionally, s2n-tls uses sig_atomic_t for its weak guarantee of |
| 31 | * atomicity for interrupts. |
| 32 | */ |
| 33 | sig_atomic_t val; |
| 34 | } s2n_atomic_flag; |
| 35 | |
| 36 | /* These methods use compiler atomic built-ins if available and lock-free, but otherwise |
| 37 | * rely on setting / clearing a small value generally being atomic in practice. |
| 38 | */ |
| 39 | S2N_RESULT s2n_atomic_init(); |
| 40 | void s2n_atomic_flag_set(s2n_atomic_flag *var); |
| 41 | void s2n_atomic_flag_clear(s2n_atomic_flag *var); |
| 42 | bool s2n_atomic_flag_test(s2n_atomic_flag *var); |