blob: fb1c11f6b4148127eceafc306ea90adda2150f58 [file] [log] [blame]
Adam Langley53b272a2015-06-04 17:45:09 -07001/* 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
17#include <stdlib.h>
18
19#include <openssl/type_check.h>
20
21
22#if !defined(OPENSSL_C11_ATOMIC)
23
Robert Sloanc9abfe42018-11-26 12:19:07 -080024OPENSSL_STATIC_ASSERT((CRYPTO_refcount_t)-1 == CRYPTO_REFCOUNT_MAX,
25 "CRYPTO_REFCOUNT_MAX is incorrect");
Adam Langley53b272a2015-06-04 17:45:09 -070026
27static struct CRYPTO_STATIC_MUTEX g_refcount_lock = CRYPTO_STATIC_MUTEX_INIT;
28
29void CRYPTO_refcount_inc(CRYPTO_refcount_t *count) {
30 CRYPTO_STATIC_MUTEX_lock_write(&g_refcount_lock);
31 if (*count < CRYPTO_REFCOUNT_MAX) {
32 (*count)++;
33 }
David Benjamind316cba2016-06-02 16:17:39 -040034 CRYPTO_STATIC_MUTEX_unlock_write(&g_refcount_lock);
Adam Langley53b272a2015-06-04 17:45:09 -070035}
36
37int CRYPTO_refcount_dec_and_test_zero(CRYPTO_refcount_t *count) {
38 int ret;
39
40 CRYPTO_STATIC_MUTEX_lock_write(&g_refcount_lock);
41 if (*count == 0) {
42 abort();
43 }
44 if (*count < CRYPTO_REFCOUNT_MAX) {
45 (*count)--;
46 }
47 ret = (*count == 0);
David Benjamind316cba2016-06-02 16:17:39 -040048 CRYPTO_STATIC_MUTEX_unlock_write(&g_refcount_lock);
Adam Langley53b272a2015-06-04 17:45:09 -070049
50 return ret;
51}
52
Robert Sloan8f860b12017-08-28 07:37:06 -070053#endif // OPENSSL_C11_ATOMIC