少年修仙传客户端基础资源
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
/**
 * \file
 * Linearizable property bag.
 *
 * Authors:
 *   Rodrigo Kumpera (kumpera@gmail.com)
 *
 * Licensed under the MIT license. See LICENSE file in the project root for full license information.
 */
#include <mono/metadata/property-bag.h>
#include <mono/utils/atomic.h>
#include <mono/utils/mono-membar.h>
 
/*
 * mono_property_bag_get:
 *
 *   Return the value of the property with TAG or NULL.
 * This doesn't take any locks.
 */
void*
mono_property_bag_get (MonoPropertyBag *bag, int tag)
{
    MonoPropertyBagItem *item;
    
    for (item = bag->head; item && item->tag <= tag; item = item->next) {
        if (item->tag == tag)
            return item;
    }
    return NULL;
}
 
/*
 * mono_property_bag_add:
 *
 *   Store VALUE in the property bag. Return the previous value
 * with the same tag, or NULL. VALUE should point to a structure
 * extending the MonoPropertyBagItem structure with the 'tag'
 * field set.
 * This doesn't take any locks.
 */
void*
mono_property_bag_add (MonoPropertyBag *bag, void *value)
{
    MonoPropertyBagItem *cur, **prev, *item = value;
    int tag = item->tag;
    mono_memory_barrier (); //publish the values in value
 
retry:
    prev = &bag->head;
    while (1) {
        cur = *prev;
        if (!cur || cur->tag > tag) {
            item->next = cur;
            if (mono_atomic_cas_ptr ((void*)prev, item, cur) == cur)
                return item;
            goto retry;
        } else if (cur->tag == tag) {
            return cur;
        } else {
            prev = &cur->next;
        }
    }
    return value;
}