Adam Langley | 6f2e733 | 2015-05-15 12:01:29 -0700 | [diff] [blame] | 1 | /* Copyright (c) 2015, Google Inc. |
| 2 | * |
| 3 | * Permission to use, copy, modify, and/or distribute this software for any |
| 4 | * purpose with or without fee is hereby granted, provided that the above |
| 5 | * copyright notice and this permission notice appear in all copies. |
| 6 | * |
| 7 | * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES |
| 8 | * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF |
| 9 | * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY |
| 10 | * SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES |
| 11 | * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION |
| 12 | * OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN |
| 13 | * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. */ |
| 14 | |
| 15 | #include "internal.h" |
| 16 | |
David Benjamin | b7d6320 | 2022-07-26 13:25:02 -0700 | [diff] [blame] | 17 | #include <assert.h> |
Adam Langley | 6f2e733 | 2015-05-15 12:01:29 -0700 | [diff] [blame] | 18 | #include <stdlib.h> |
| 19 | |
Adam Langley | 6f2e733 | 2015-05-15 12:01:29 -0700 | [diff] [blame] | 20 | |
| 21 | #if !defined(OPENSSL_C11_ATOMIC) |
| 22 | |
David Benjamin | b7d6320 | 2022-07-26 13:25:02 -0700 | [diff] [blame] | 23 | static_assert((CRYPTO_refcount_t)-1 == CRYPTO_REFCOUNT_MAX, |
| 24 | "CRYPTO_REFCOUNT_MAX is incorrect"); |
Adam Langley | 6f2e733 | 2015-05-15 12:01:29 -0700 | [diff] [blame] | 25 | |
| 26 | static struct CRYPTO_STATIC_MUTEX g_refcount_lock = CRYPTO_STATIC_MUTEX_INIT; |
| 27 | |
| 28 | void CRYPTO_refcount_inc(CRYPTO_refcount_t *count) { |
| 29 | CRYPTO_STATIC_MUTEX_lock_write(&g_refcount_lock); |
| 30 | if (*count < CRYPTO_REFCOUNT_MAX) { |
| 31 | (*count)++; |
| 32 | } |
David Benjamin | 29270de | 2016-05-24 15:28:36 +0000 | [diff] [blame] | 33 | CRYPTO_STATIC_MUTEX_unlock_write(&g_refcount_lock); |
Adam Langley | 6f2e733 | 2015-05-15 12:01:29 -0700 | [diff] [blame] | 34 | } |
| 35 | |
| 36 | int CRYPTO_refcount_dec_and_test_zero(CRYPTO_refcount_t *count) { |
| 37 | int ret; |
| 38 | |
| 39 | CRYPTO_STATIC_MUTEX_lock_write(&g_refcount_lock); |
| 40 | if (*count == 0) { |
| 41 | abort(); |
| 42 | } |
| 43 | if (*count < CRYPTO_REFCOUNT_MAX) { |
| 44 | (*count)--; |
| 45 | } |
| 46 | ret = (*count == 0); |
David Benjamin | 29270de | 2016-05-24 15:28:36 +0000 | [diff] [blame] | 47 | CRYPTO_STATIC_MUTEX_unlock_write(&g_refcount_lock); |
Adam Langley | 6f2e733 | 2015-05-15 12:01:29 -0700 | [diff] [blame] | 48 | |
| 49 | return ret; |
| 50 | } |
| 51 | |
David Benjamin | 808f832 | 2017-08-18 14:06:02 -0400 | [diff] [blame] | 52 | #endif // OPENSSL_C11_ATOMIC |