s2n_atomic.h 1.5 KB

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