少年修仙传客户端基础资源
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
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
/**
 * \file
 * Threadpool for all concurrent GC work.
 *
 * Copyright (C) 2015 Xamarin Inc
 *
 * Licensed under the MIT license. See LICENSE file in the project root for full license information.
 */
 
#include "config.h"
#ifdef HAVE_SGEN_GC
 
#include "mono/sgen/sgen-gc.h"
#include "mono/sgen/sgen-thread-pool.h"
#include "mono/sgen/sgen-client.h"
#include "mono/utils/mono-os-mutex.h"
 
static mono_mutex_t lock;
static mono_cond_t work_cond;
static mono_cond_t done_cond;
 
static int threads_num;
static MonoNativeThreadId threads [SGEN_THREADPOOL_MAX_NUM_THREADS];
static int threads_context [SGEN_THREADPOOL_MAX_NUM_THREADS];
 
static volatile gboolean threadpool_shutdown;
static volatile int threads_finished;
 
static int contexts_num;
static SgenThreadPoolContext pool_contexts [SGEN_THREADPOOL_MAX_NUM_CONTEXTS];
 
enum {
    STATE_WAITING,
    STATE_IN_PROGRESS,
    STATE_DONE
};
 
/* Assumes that the lock is held. */
static SgenThreadPoolJob*
get_job_and_set_in_progress (SgenThreadPoolContext *context)
{
    for (size_t i = 0; i < context->job_queue.next_slot; ++i) {
        SgenThreadPoolJob *job = (SgenThreadPoolJob *)context->job_queue.data [i];
        if (job->state == STATE_WAITING) {
            job->state = STATE_IN_PROGRESS;
            return job;
        }
    }
    return NULL;
}
 
/* Assumes that the lock is held. */
static ssize_t
find_job_in_queue (SgenThreadPoolContext *context, SgenThreadPoolJob *job)
{
    for (ssize_t i = 0; i < context->job_queue.next_slot; ++i) {
        if (context->job_queue.data [i] == job)
            return i;
    }
    return -1;
}
 
/* Assumes that the lock is held. */
static void
remove_job (SgenThreadPoolContext *context, SgenThreadPoolJob *job)
{
    ssize_t index;
    SGEN_ASSERT (0, job->state == STATE_DONE, "Why are we removing a job that's not done?");
    index = find_job_in_queue (context, job);
    SGEN_ASSERT (0, index >= 0, "Why is the job we're trying to remove not in the queue?");
    context->job_queue.data [index] = NULL;
    sgen_pointer_queue_remove_nulls (&context->job_queue);
    sgen_thread_pool_job_free (job);
}
 
static gboolean
continue_idle_job (SgenThreadPoolContext *context, void *thread_data)
{
    if (!context->continue_idle_job_func)
        return FALSE;
    return context->continue_idle_job_func (thread_data, context - pool_contexts);
}
 
static gboolean
should_work (SgenThreadPoolContext *context, void *thread_data)
{
    if (!context->should_work_func)
        return TRUE;
    return context->should_work_func (thread_data);
}
 
/*
 * Tells whether we should lock and attempt to get work from
 * a higher priority context.
 */
static gboolean
has_priority_work (int worker_index, int current_context)
{
    int i;
 
    for (i = 0; i < current_context; i++) {
        SgenThreadPoolContext *context = &pool_contexts [i];
        void *thread_data;
 
        if (worker_index >= context->num_threads)
            continue;
        thread_data = (context->thread_datas) ? context->thread_datas [worker_index] : NULL;
        if (!should_work (context, thread_data))
            continue;
        if (context->job_queue.next_slot > 0)
            return TRUE;
        if (continue_idle_job (context, thread_data))
            return TRUE;
    }
 
    /* Return if job enqueued on current context. Jobs have priority over idle work */
    if (pool_contexts [current_context].job_queue.next_slot > 0)
        return TRUE;
 
    return FALSE;
}
 
/*
 * Gets the highest priority work. If there is none, it waits
 * for work_cond. Should always be called with lock held.
 */
static void
get_work (int worker_index, int *work_context, int *do_idle, SgenThreadPoolJob **job)
{
    while (!threadpool_shutdown) {
        int i;
 
        for (i = 0; i < contexts_num; i++) {
            SgenThreadPoolContext *context = &pool_contexts [i];
            void *thread_data;
 
            if (worker_index >= context->num_threads)
                continue;
            thread_data = (context->thread_datas) ? context->thread_datas [worker_index] : NULL;
 
            if (!should_work (context, thread_data))
                continue;
 
            /*
             * It's important that we check the continue idle flag with the lock held.
             * Suppose we didn't check with the lock held, and the result is FALSE.  The
             * main thread might then set continue idle and signal us before we can take
             * the lock, and we'd lose the signal.
             */
            *do_idle = continue_idle_job (context, thread_data);
            *job = get_job_and_set_in_progress (context);
 
            if (*job || *do_idle) {
                *work_context = i;
                return;
            }
        }
 
        /*
         * Nothing to do on any context
         * pthread_cond_wait() can return successfully despite the condition
         * not being signalled, so we have to run this in a loop until we
         * really have work to do.
         */
        mono_os_cond_wait (&work_cond, &lock);
    }
}
 
static mono_native_thread_return_t
thread_func (void *data)
{
    int worker_index = (int)(gsize)data;
    int current_context;
    void *thread_data = NULL;
 
    sgen_client_thread_register_worker ();
 
    for (current_context = 0; current_context < contexts_num; current_context++) {
        if (worker_index >= pool_contexts [current_context].num_threads ||
                !pool_contexts [current_context].thread_init_func)
            break;
 
        thread_data = (pool_contexts [current_context].thread_datas) ? pool_contexts [current_context].thread_datas [worker_index] : NULL;
        pool_contexts [current_context].thread_init_func (thread_data);
    }
 
    current_context = 0;
 
    mono_os_mutex_lock (&lock);
    for (;;) {
        gboolean do_idle = FALSE;
        SgenThreadPoolJob *job = NULL;
        SgenThreadPoolContext *context = NULL;
 
        threads_context [worker_index] = -1;
        get_work (worker_index, &current_context, &do_idle, &job);
        threads_context [worker_index] = current_context;
 
        if (!threadpool_shutdown) {
            context = &pool_contexts [current_context];
            thread_data = (context->thread_datas) ? context->thread_datas [worker_index] : NULL;
        }
 
        mono_os_mutex_unlock (&lock);
 
        if (job) {
            job->func (thread_data, job);
 
            mono_os_mutex_lock (&lock);
 
            SGEN_ASSERT (0, job->state == STATE_IN_PROGRESS, "The job should still be in progress.");
            job->state = STATE_DONE;
            remove_job (context, job);
            /*
             * Only the main GC thread will ever wait on the done condition, so we don't
             * have to broadcast.
             */
            mono_os_cond_signal (&done_cond);
        } else if (do_idle) {
            SGEN_ASSERT (0, context->idle_job_func, "Why do we have idle work when there's no idle job function?");
            do {
                context->idle_job_func (thread_data);
                do_idle = continue_idle_job (context, thread_data);
            } while (do_idle && !has_priority_work (worker_index, current_context));
 
            mono_os_mutex_lock (&lock);
 
            if (!do_idle)
                mono_os_cond_signal (&done_cond);
        } else {
            SGEN_ASSERT (0, threadpool_shutdown, "Why did we unlock if no jobs and not shutting down?");
            mono_os_mutex_lock (&lock);
            threads_finished++;
            mono_os_cond_signal (&done_cond);
            mono_os_mutex_unlock (&lock);
            return 0;
        }
    }
 
    return (mono_native_thread_return_t)0;
}
 
int
sgen_thread_pool_create_context (int num_threads, SgenThreadPoolThreadInitFunc init_func, SgenThreadPoolIdleJobFunc idle_func, SgenThreadPoolContinueIdleJobFunc continue_idle_func, SgenThreadPoolShouldWorkFunc should_work_func, void **thread_datas)
{
    int context_id = contexts_num;
 
    SGEN_ASSERT (0, contexts_num < SGEN_THREADPOOL_MAX_NUM_CONTEXTS, "Maximum sgen thread pool contexts reached");
 
    pool_contexts [context_id].thread_init_func = init_func;
    pool_contexts [context_id].idle_job_func = idle_func;
    pool_contexts [context_id].continue_idle_job_func = continue_idle_func;
    pool_contexts [context_id].should_work_func = should_work_func;
    pool_contexts [context_id].thread_datas = thread_datas;
 
    SGEN_ASSERT (0, num_threads <= SGEN_THREADPOOL_MAX_NUM_THREADS, "Maximum sgen thread pool threads exceeded");
 
    pool_contexts [context_id].num_threads = num_threads;
 
    sgen_pointer_queue_init (&pool_contexts [contexts_num].job_queue, 0);
 
    contexts_num++;
 
    return context_id;
}
 
void
sgen_thread_pool_start (void)
{
    int i;
 
    for (i = 0; i < contexts_num; i++) {
        if (threads_num < pool_contexts [i].num_threads)
            threads_num = pool_contexts [i].num_threads;
    }
 
    if (!threads_num)
        return;
 
    mono_os_mutex_init (&lock);
    mono_os_cond_init (&work_cond);
    mono_os_cond_init (&done_cond);
 
    threads_finished = 0;
    threadpool_shutdown = FALSE;
 
    for (i = 0; i < threads_num; i++) {
        mono_native_thread_create (&threads [i], thread_func, (void*)(gsize)i);
    }
}
 
void
sgen_thread_pool_shutdown (void)
{
    if (!threads_num)
        return;
 
    mono_os_mutex_lock (&lock);
    threadpool_shutdown = TRUE;
    mono_os_cond_broadcast (&work_cond);
    while (threads_finished < threads_num)
        mono_os_cond_wait (&done_cond, &lock);
    mono_os_mutex_unlock (&lock);
 
    mono_os_mutex_destroy (&lock);
    mono_os_cond_destroy (&work_cond);
    mono_os_cond_destroy (&done_cond);
 
    for (int i = 0; i < threads_num; i++) {
        mono_threads_add_joinable_thread ((gpointer)threads [i]);
    }
}
 
SgenThreadPoolJob*
sgen_thread_pool_job_alloc (const char *name, SgenThreadPoolJobFunc func, size_t size)
{
    SgenThreadPoolJob *job = (SgenThreadPoolJob *)sgen_alloc_internal_dynamic (size, INTERNAL_MEM_THREAD_POOL_JOB, TRUE);
    job->name = name;
    job->size = size;
    job->state = STATE_WAITING;
    job->func = func;
    return job;
}
 
void
sgen_thread_pool_job_free (SgenThreadPoolJob *job)
{
    sgen_free_internal_dynamic (job, job->size, INTERNAL_MEM_THREAD_POOL_JOB);
}
 
void
sgen_thread_pool_job_enqueue (int context_id, SgenThreadPoolJob *job)
{
    mono_os_mutex_lock (&lock);
 
    sgen_pointer_queue_add (&pool_contexts [context_id].job_queue, job);
    mono_os_cond_broadcast (&work_cond);
 
    mono_os_mutex_unlock (&lock);
}
 
void
sgen_thread_pool_job_wait (int context_id, SgenThreadPoolJob *job)
{
    SGEN_ASSERT (0, job, "Where's the job?");
 
    mono_os_mutex_lock (&lock);
 
    while (find_job_in_queue (&pool_contexts [context_id], job) >= 0)
        mono_os_cond_wait (&done_cond, &lock);
 
    mono_os_mutex_unlock (&lock);
}
 
void
sgen_thread_pool_idle_signal (int context_id)
{
    SGEN_ASSERT (0, pool_contexts [context_id].idle_job_func, "Why are we signaling idle without an idle function?");
 
    mono_os_mutex_lock (&lock);
 
    if (pool_contexts [context_id].continue_idle_job_func (NULL, context_id))
        mono_os_cond_broadcast (&work_cond);
 
    mono_os_mutex_unlock (&lock);
}
 
void
sgen_thread_pool_idle_wait (int context_id, SgenThreadPoolContinueIdleWaitFunc continue_wait)
{
    SGEN_ASSERT (0, pool_contexts [context_id].idle_job_func, "Why are we waiting for idle without an idle function?");
 
    mono_os_mutex_lock (&lock);
 
    while (continue_wait (context_id, threads_context))
        mono_os_cond_wait (&done_cond, &lock);
 
    mono_os_mutex_unlock (&lock);
}
 
void
sgen_thread_pool_wait_for_all_jobs (int context_id)
{
    mono_os_mutex_lock (&lock);
 
    while (!sgen_pointer_queue_is_empty (&pool_contexts [context_id].job_queue))
        mono_os_cond_wait (&done_cond, &lock);
 
    mono_os_mutex_unlock (&lock);
}
 
/* Return 0 if is not a thread pool thread or the thread number otherwise */
int
sgen_thread_pool_is_thread_pool_thread (MonoNativeThreadId some_thread)
{
    int i;
 
    for (i = 0; i < threads_num; i++) {
        if (some_thread == threads [i])
            return i + 1;
    }
 
    return 0;
}
 
#endif