少年修仙传客户端基础资源
hch
2024-04-01 d01413b00ef631ac20347716b23818b0b811f65f
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
76
77
78
79
80
81
82
83
/**
 * \file
 */
 
#ifndef __MONO_UTILS_REFCOUNT_H__
#define __MONO_UTILS_REFCOUNT_H__
 
#include <glib.h>
#include <config.h>
 
#include "atomic.h"
 
/*
 * Mechanism for ref-counting which tries to be as user-friendly as possible. Instead of being a wrapper around
 * user-provided data, it is embedded into the user data.
 *
 * This introduces some constraints on the MonoRefCount field:
 *  - it needs to be called "ref"
 *  - it cannot be a pointer
 */
 
typedef struct {
    guint32 ref;
    void (*destructor) (gpointer data);
} MonoRefCount;
 
#define mono_refcount_init(v,destructor) do { mono_refcount_initialize (&(v)->ref, (destructor)); } while (0)
#define mono_refcount_inc(v) (mono_refcount_increment (&(v)->ref),(v))
#define mono_refcount_tryinc(v) (mono_refcount_tryincrement (&(v)->ref))
#define mono_refcount_dec(v) do { mono_refcount_decrement (&(v)->ref); } while (0)
 
static inline void
mono_refcount_initialize (MonoRefCount *refcount, void (*destructor) (gpointer data))
{
    refcount->ref = 1;
    refcount->destructor = destructor;
}
 
static inline gboolean
mono_refcount_tryincrement (MonoRefCount *refcount)
{
    guint32 oldref, newref;
 
    g_assert (refcount);
 
    do {
        oldref = refcount->ref;
        if (oldref == 0)
            return FALSE;
 
        newref = oldref + 1;
    } while (mono_atomic_cas_i32 ((gint32*) &refcount->ref, (gint32)newref, (gint32)oldref) != (gint32)oldref);
 
    return TRUE;
}
 
static inline void
mono_refcount_increment (MonoRefCount *refcount)
{
    if (!mono_refcount_tryincrement (refcount))
        g_error ("%s: cannot increment a ref with value 0", __func__);
}
 
static inline void
mono_refcount_decrement (MonoRefCount *refcount)
{
    guint32 oldref, newref;
 
    g_assert (refcount);
 
    do {
        oldref = refcount->ref;
        if (oldref == 0)
            g_error ("%s: cannot decrement a ref with value 0", __func__);
 
        newref = oldref - 1;
    } while (mono_atomic_cas_i32 ((gint32*) &refcount->ref, (gint32)newref, (gint32)oldref) != (gint32)oldref);
 
    if (newref == 0 && refcount->destructor)
        refcount->destructor ((gpointer) refcount);
}
 
#endif /* __MONO_UTILS_REFCOUNT_H__ */