少年修仙传客户端基础资源
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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
/**
 * \file
 * Lock free queue.
 *
 * (C) Copyright 2011 Novell, Inc
 *
 * Permission is hereby granted, free of charge, to any person obtaining
 * a copy of this software and associated documentation files (the
 * "Software"), to deal in the Software without restriction, including
 * without limitation the rights to use, copy, modify, merge, publish,
 * distribute, sublicense, and/or sell copies of the Software, and to
 * permit persons to whom the Software is furnished to do so, subject to
 * the following conditions:
 * 
 * The above copyright notice and this permission notice shall be
 * included in all copies or substantial portions of the Software.
 * 
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
 * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
 * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
 * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 */
 
/*
 * This is an implementation of a lock-free queue, as described in
 *
 * Simple, Fast, and Practical Non-Blocking and Blocking
 *   Concurrent Queue Algorithms
 * Maged M. Michael, Michael L. Scott
 * 1995
 *
 * A few slight modifications have been made:
 *
 * We use hazard pointers to rule out the ABA problem, instead of the
 * counter as in the paper.
 *
 * Memory management of the queue entries is done by the caller, not
 * by the queue implementation.  This implies that the dequeue
 * function must return the queue entry, not just the data.
 *
 * Therefore, the dummy entry must never be returned.  We do this by
 * re-enqueuing a new dummy entry after we dequeue one and then
 * retrying the dequeue.  We need more than one dummy because they
 * must be hazardly freed.
 */
 
#include <stdlib.h>
#ifdef HAVE_UNISTD_H
#include <unistd.h>
#endif
 
#include <mono/utils/mono-membar.h>
#include <mono/utils/hazard-pointer.h>
#include <mono/utils/atomic.h>
 
#include <mono/utils/lock-free-queue.h>
 
#define INVALID_NEXT    ((MonoLockFreeQueueNode *volatile)-1)
#define END_MARKER    ((MonoLockFreeQueueNode *volatile)-2)
#define FREE_NEXT    ((MonoLockFreeQueueNode *volatile)-3)
 
/*
 * Initialize a lock-free queue in-place at @q.
 */
void
mono_lock_free_queue_init (MonoLockFreeQueue *q)
{
    int i;
    for (i = 0; i < MONO_LOCK_FREE_QUEUE_NUM_DUMMIES; ++i) {
        q->dummies [i].node.next = (i == 0) ? END_MARKER : FREE_NEXT;
        q->dummies [i].in_use = i == 0 ? 1 : 0;
#ifdef QUEUE_DEBUG
        q->dummies [i].node.in_queue = i == 0 ? TRUE : FALSE;
#endif
    }
 
    q->head = q->tail = &q->dummies [0].node;
    q->has_dummy = 1;
}
 
/*
 * Initialize @node's state. If @poison is TRUE, @node may not be enqueued to a
 * queue - @mono_lock_free_queue_node_unpoison must be called first; otherwise,
 * the node can be enqueued right away.
 *
 * The poisoning feature is mainly intended for ensuring correctness in complex
 * lock-free code that uses the queue. For example, in some code that reuses
 * nodes, nodes can be poisoned when they're dequeued, and then unpoisoned and
 * enqueued in their hazard free callback.
 */
void
mono_lock_free_queue_node_init (MonoLockFreeQueueNode *node, gboolean poison)
{
    node->next = poison ? INVALID_NEXT : FREE_NEXT;
#ifdef QUEUE_DEBUG
    node->in_queue = FALSE;
#endif
}
 
/*
 * Unpoisons @node so that it may be enqueued.
 */
void
mono_lock_free_queue_node_unpoison (MonoLockFreeQueueNode *node)
{
    g_assert (node->next == INVALID_NEXT);
#ifdef QUEUE_DEBUG
    g_assert (!node->in_queue);
#endif
    node->next = FREE_NEXT;
}
 
/*
 * Enqueue @node to @q. @node must have been initialized by a prior call to
 * @mono_lock_free_queue_node_init, and must not be in a poisoned state.
 */
void
mono_lock_free_queue_enqueue (MonoLockFreeQueue *q, MonoLockFreeQueueNode *node)
{
    MonoThreadHazardPointers *hp = mono_hazard_pointer_get ();
    MonoLockFreeQueueNode *tail;
 
#ifdef QUEUE_DEBUG
    g_assert (!node->in_queue);
    node->in_queue = TRUE;
    mono_memory_write_barrier ();
#endif
 
    g_assert (node->next == FREE_NEXT);
    node->next = END_MARKER;
    for (;;) {
        MonoLockFreeQueueNode *next;
 
        tail = (MonoLockFreeQueueNode *) mono_get_hazardous_pointer ((gpointer volatile*)&q->tail, hp, 0);
        mono_memory_read_barrier ();
        /*
         * We never dereference next so we don't need a
         * hazardous load.
         */
        next = tail->next;
        mono_memory_read_barrier ();
 
        /* Are tail and next consistent? */
        if (tail == q->tail) {
            g_assert (next != INVALID_NEXT && next != FREE_NEXT);
            g_assert (next != tail);
 
            if (next == END_MARKER) {
                /*
                 * Here we require that nodes that
                 * have been dequeued don't have
                 * next==END_MARKER.  If they did, we
                 * might append to a node that isn't
                 * in the queue anymore here.
                 */
                if (mono_atomic_cas_ptr ((gpointer volatile*)&tail->next, node, END_MARKER) == END_MARKER)
                    break;
            } else {
                /* Try to advance tail */
                mono_atomic_cas_ptr ((gpointer volatile*)&q->tail, next, tail);
            }
        }
 
        mono_memory_write_barrier ();
        mono_hazard_pointer_clear (hp, 0);
    }
 
    /* Try to advance tail */
    mono_atomic_cas_ptr ((gpointer volatile*)&q->tail, node, tail);
 
    mono_memory_write_barrier ();
    mono_hazard_pointer_clear (hp, 0);
}
 
static void
free_dummy (gpointer _dummy)
{
    MonoLockFreeQueueDummy *dummy = (MonoLockFreeQueueDummy *) _dummy;
    mono_lock_free_queue_node_unpoison (&dummy->node);
    g_assert (dummy->in_use);
    mono_memory_write_barrier ();
    dummy->in_use = 0;
}
 
static MonoLockFreeQueueDummy*
get_dummy (MonoLockFreeQueue *q)
{
    int i;
    for (i = 0; i < MONO_LOCK_FREE_QUEUE_NUM_DUMMIES; ++i) {
        MonoLockFreeQueueDummy *dummy = &q->dummies [i];
 
        if (dummy->in_use)
            continue;
 
        if (mono_atomic_cas_i32 (&dummy->in_use, 1, 0) == 0)
            return dummy;
    }
    return NULL;
}
 
static gboolean
is_dummy (MonoLockFreeQueue *q, MonoLockFreeQueueNode *n)
{
    return n >= &q->dummies [0].node && n <= &q->dummies [MONO_LOCK_FREE_QUEUE_NUM_DUMMIES-1].node;
}
 
static gboolean
try_reenqueue_dummy (MonoLockFreeQueue *q)
{
    MonoLockFreeQueueDummy *dummy;
 
    if (q->has_dummy)
        return FALSE;
 
    dummy = get_dummy (q);
    if (!dummy)
        return FALSE;
 
    if (mono_atomic_cas_i32 (&q->has_dummy, 1, 0) != 0) {
        dummy->in_use = 0;
        return FALSE;
    }
 
    mono_lock_free_queue_enqueue (q, &dummy->node);
 
    return TRUE;
}
 
/*
 * Dequeues a node from @q. Returns NULL if no nodes are available. The returned
 * node is hazardous and must be freed with @mono_thread_hazardous_try_free or
 * @mono_thread_hazardous_queue_free - it must not be freed directly.
 */
MonoLockFreeQueueNode*
mono_lock_free_queue_dequeue (MonoLockFreeQueue *q)
{
    MonoThreadHazardPointers *hp = mono_hazard_pointer_get ();
    MonoLockFreeQueueNode *head;
 
 retry:
    for (;;) {
        MonoLockFreeQueueNode *tail, *next;
 
        head = (MonoLockFreeQueueNode *) mono_get_hazardous_pointer ((gpointer volatile*)&q->head, hp, 0);
        tail = (MonoLockFreeQueueNode*)q->tail;
        mono_memory_read_barrier ();
        next = head->next;
        mono_memory_read_barrier ();
 
        /* Are head, tail and next consistent? */
        if (head == q->head) {
            g_assert (next != INVALID_NEXT && next != FREE_NEXT);
            g_assert (next != head);
 
            /* Is queue empty or tail behind? */
            if (head == tail) {
                if (next == END_MARKER) {
                    /* Queue is empty */
                    mono_hazard_pointer_clear (hp, 0);
 
                    /*
                     * We only continue if we
                     * reenqueue the dummy
                     * ourselves, so as not to
                     * wait for threads that might
                     * not actually run.
                     */
                    if (!is_dummy (q, head) && try_reenqueue_dummy (q))
                        continue;
 
                    return NULL;
                }
 
                /* Try to advance tail */
                mono_atomic_cas_ptr ((gpointer volatile*)&q->tail, next, tail);
            } else {
                g_assert (next != END_MARKER);
                /* Try to dequeue head */
                if (mono_atomic_cas_ptr ((gpointer volatile*)&q->head, next, head) == head)
                    break;
            }
        }
 
        mono_memory_write_barrier ();
        mono_hazard_pointer_clear (hp, 0);
    }
 
    /*
     * The head is dequeued now, so we know it's this thread's
     * responsibility to free it - no other thread can.
     */
    mono_memory_write_barrier ();
    mono_hazard_pointer_clear (hp, 0);
 
    g_assert (head->next);
    /*
     * Setting next here isn't necessary for correctness, but we
     * do it to make sure that we catch dereferencing next in a
     * node that's not in the queue anymore.
     */
    head->next = INVALID_NEXT;
#if QUEUE_DEBUG
    g_assert (head->in_queue);
    head->in_queue = FALSE;
    mono_memory_write_barrier ();
#endif
 
    if (is_dummy (q, head)) {
        g_assert (q->has_dummy);
        q->has_dummy = 0;
        mono_memory_write_barrier ();
        mono_thread_hazardous_try_free (head, free_dummy);
        if (try_reenqueue_dummy (q))
            goto retry;
        return NULL;
    }
 
    /* The caller must hazardously free the node. */
    return head;
}