summaryrefslogtreecommitdiff
path: root/components/utilities/libadt/ref/ref.h
blob: da700e4a1ce19f7b9e07c26fffa6495ffeb861c2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
/*
 * Copyright (c) 2006-2023, RT-Thread Development Team
 *
 * SPDX-License-Identifier: Apache-2.0
 *
 * Change Logs:
 * Date           Author       Notes
 * 2022-3-1       zhouxiaohu   first version
 * 2023-5-18      GuEe-GUI     implemented by rtatomic
 */

#ifndef __UTIL_REF_H__
#define __UTIL_REF_H__

#include <rtatomic.h>

/**
 * struct ref must be embedded in an object.
 * it acts as a reference counter for the object.
 */
struct rt_ref
{
    rt_atomic_t refcount;
};

#define RT_REF_INIT(n)  { .refcount = n, }

rt_inline void rt_ref_init(struct rt_ref *r)
{
    rt_atomic_store(&r->refcount, 1);
}

rt_inline unsigned int rt_ref_read(struct rt_ref *r)
{
    return rt_atomic_load(&r->refcount);
}

/**
 * ref_get
 * increment reference counter for object.
 */
rt_inline void rt_ref_get(struct rt_ref *r)
{
    rt_atomic_add(&r->refcount, 1);
}

/**
 * ref_put
 * decrement reference counter for object.
 * If the reference counter is zero, call release().
 *
 * Return 1 means the object's reference counter is zero and release() is called.
 */
rt_inline int rt_ref_put(struct rt_ref *r, void (*release)(struct rt_ref *r))
{
    if (rt_atomic_dec_and_test(&r->refcount))
    {
        release(r);

        return 1;
    }

    return 0;
}

/**
 * ref_get_unless_zero - Increment refcount for object unless it is zero.
 * Return non-zero if the increment succeeded. Otherwise return 0.
 */
rt_inline int rt_ref_get_unless_zero(struct rt_ref *r)
{
    return (int)rt_atomic_inc_not_zero(&r->refcount);
}

#endif /* __UTIL_REF_H__ */