| 1 | /* |
| 2 | * LTTng - Non thread-safe reference counting |
| 3 | * |
| 4 | * Copyright 2013, 2014 Jérémie Galarneau <jeremie.galarneau@efficios.com> |
| 5 | * |
| 6 | * SPDX-License-Identifier: LGPL-2.1-only |
| 7 | * |
| 8 | */ |
| 9 | |
| 10 | #ifndef LTTNG_REF_INTERNAL_H |
| 11 | #define LTTNG_REF_INTERNAL_H |
| 12 | |
| 13 | #include <common/macros.hpp> |
| 14 | |
| 15 | #include <urcu/urcu.h> |
| 16 | |
| 17 | using lttng_release_func = void (*)(void *); |
| 18 | |
| 19 | struct lttng_ref { |
| 20 | unsigned long count; |
| 21 | lttng_release_func release; |
| 22 | }; |
| 23 | |
| 24 | static inline void lttng_ref_init(struct lttng_ref *ref, lttng_release_func release) |
| 25 | { |
| 26 | LTTNG_ASSERT(ref); |
| 27 | ref->count = 1; |
| 28 | ref->release = release; |
| 29 | } |
| 30 | |
| 31 | static inline void lttng_ref_get(struct lttng_ref *ref) |
| 32 | { |
| 33 | LTTNG_ASSERT(ref); |
| 34 | ref->count++; |
| 35 | /* Overflow check. */ |
| 36 | LTTNG_ASSERT(ref->count); |
| 37 | } |
| 38 | |
| 39 | static inline void lttng_ref_put(struct lttng_ref *ref) |
| 40 | { |
| 41 | LTTNG_ASSERT(ref); |
| 42 | /* Underflow check. */ |
| 43 | LTTNG_ASSERT(ref->count); |
| 44 | if (caa_unlikely((--ref->count) == 0)) { |
| 45 | ref->release(ref); |
| 46 | } |
| 47 | } |
| 48 | |
| 49 | #endif /* LTTNG_REF_INTERNAL_H */ |