少年修仙传客户端基础资源
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
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
#include "os/c-api/il2cpp-config-platforms.h"
#include "os/Thread.h"
 
#if IL2CPP_SUPPORT_THREADS
 
#include "os/Mutex.h"
#include "os/ThreadLocalValue.h"
#if IL2CPP_THREADS_STD
#include "os/Std/ThreadImpl.h"
#elif IL2CPP_TARGET_WINDOWS
#include "os/Win32/ThreadImpl.h"
#elif IL2CPP_THREADS_PTHREAD
#include "os/Posix/ThreadImpl.h"
#else
#include "os/ThreadImpl.h"
#endif
 
#include "utils/dynamic_array.h"
 
#include <limits>
 
namespace il2cpp
{
namespace os
{
/// TLS variable referring to current thread.
    static ThreadLocalValue s_CurrentThread;
 
    // TLS variable referring to whether this thread is currently executing Thread::Shutdown
    // It is thread local for thread safety
    static ThreadLocalValue s_IsCleaningUpThreads;
 
    static FastMutex s_AliveThreadsMutex;
    static il2cpp::utils::dynamic_array<Thread*> s_AliveThreads;
 
    int64_t Thread::s_DefaultAffinityMask = kThreadAffinityAll;
 
    static bool GetIsCleaningUpThreads()
    {
        void* value = NULL;
        s_IsCleaningUpThreads.GetValue(&value);
        return reinterpret_cast<intptr_t>(value) != 0;
    }
 
    static void SetIsCleaningUpThreads(bool value)
    {
        s_IsCleaningUpThreads.SetValue(reinterpret_cast<void*>(static_cast<intptr_t>(value)));
    }
 
    Thread::Thread()
        : m_Thread(new ThreadImpl())
        , m_State(kThreadCreated)
        , m_ThreadExitedEvent(true) // Manual reset event
        , m_CleanupFunc(NULL)
        , m_CleanupFuncArg(NULL)
    {
        FastAutoLock lock(&s_AliveThreadsMutex);
        s_AliveThreads.push_back(this);
    }
 
    Thread::Thread(ThreadImpl* thread)
        : m_Thread(thread)
        , m_State(kThreadRunning)
        , m_CleanupFunc(NULL)
        , m_CleanupFuncArg(NULL)
    {
        FastAutoLock lock(&s_AliveThreadsMutex);
        s_AliveThreads.push_back(this);
    }
 
    Thread::~Thread()
    {
        delete m_Thread;
 
        if (!GetIsCleaningUpThreads())
        {
            FastAutoLock lock(&s_AliveThreadsMutex);
            size_t count = s_AliveThreads.size();
            for (size_t i = 0; i < count; i++)
            {
                if (s_AliveThreads[i] == this)
                {
                    s_AliveThreads.erase_swap_back(&s_AliveThreads[i]);
                    break;
                }
            }
        }
    }
 
    void Thread::Init()
    {
        Thread* thread = GetOrCreateCurrentThread();
        if (thread->GetApartment() == kApartmentStateUnknown)
            thread->SetApartment(kApartmentStateInMTA);
    }
 
    void Thread::Shutdown()
    {
        Thread* thread = GetCurrentThread();
        thread->SetApartment(kApartmentStateUnknown);
 
        SetIsCleaningUpThreads(true);
 
        FastAutoLock lock(&s_AliveThreadsMutex);
        size_t count = s_AliveThreads.size();
        for (size_t i = 0; i < count; i++)
            delete s_AliveThreads[i];
 
        s_AliveThreads.clear();
        SetIsCleaningUpThreads(false);
    }
 
    Thread::ThreadId Thread::Id()
    {
        return m_Thread->Id();
    }
 
    void Thread::SetName(const char* name)
    {
        m_Thread->SetName(name);
    }
 
    void Thread::SetPriority(ThreadPriority priority)
    {
        m_Thread->SetPriority(priority);
    }
 
    ThreadPriority Thread::GetPriority()
    {
        return m_Thread->GetPriority();
    }
 
    void Thread::SetStackSize(size_t stackSize)
    {
        m_Thread->SetStackSize(stackSize);
    }
 
    int Thread::GetMaxStackSize()
    {
        return ThreadImpl::GetMaxStackSize();
    }
 
    struct StartData
    {
        Thread* thread;
        Thread::StartFunc startFunction;
        void* startFunctionArgument;
    };
 
/// Wrapper for the user's thread start function. Sets s_CurrentThread.
    void Thread::RunWrapper(void* arg)
    {
        StartData* data = reinterpret_cast<StartData*>(arg);
 
        // Store thread reference.
        Thread* thread = data->thread;
 
        const ApartmentState apartment = thread->GetExplicitApartment();
        if (apartment != kApartmentStateUnknown)
        {
            thread->SetExplicitApartment(kApartmentStateUnknown);
            thread->SetApartment(apartment);
        }
 
        s_CurrentThread.SetValue(thread);
 
        // Get rid of StartData.
        StartFunc startFunction = data->startFunction;
        void* startFunctionArgument = data->startFunctionArgument;
        delete data;
 
        // Make sure thread exit event is not signaled.
        thread->m_ThreadExitedEvent.Reset();
 
        // Run user thread start function.
        thread->m_State = kThreadRunning;
        startFunction(startFunctionArgument);
        thread->m_State = kThreadExited;
 
        thread->SetApartment(kApartmentStateUnknown);
 
        CleanupFunc cleanupFunc = thread->m_CleanupFunc;
        void* cleanupFuncArg = thread->m_CleanupFuncArg;
 
        // Signal that we've finished execution.
        thread->m_ThreadExitedEvent.Set();
 
        if (cleanupFunc)
            cleanupFunc(cleanupFuncArg);
    }
 
    ErrorCode Thread::Run(StartFunc func, void* arg)
    {
        IL2CPP_ASSERT(m_State == kThreadCreated || m_State == kThreadExited);
 
        StartData* startData = new StartData;
        startData->startFunction = func;
        startData->startFunctionArgument = arg;
        startData->thread = this;
 
        return m_Thread->Run(RunWrapper, startData, s_DefaultAffinityMask);
    }
 
    WaitStatus Thread::Join()
    {
        IL2CPP_ASSERT(this != GetCurrentThread() && "Trying to join the current thread will deadlock");
        return Join(std::numeric_limits<uint32_t>::max());
    }
 
    WaitStatus Thread::Join(uint32_t ms)
    {
        // Wait for thread exit event.
        if (m_ThreadExitedEvent.Wait(ms, true) != kWaitStatusSuccess)
            return kWaitStatusFailure;
 
        return kWaitStatusSuccess;
    }
 
    void Thread::QueueUserAPC(APCFunc func, void* context)
    {
        m_Thread->QueueUserAPC(func, context);
    }
 
    ApartmentState Thread::GetApartment()
    {
#if IL2CPP_THREAD_IMPL_HAS_COM_APARTMENTS
        return m_Thread->GetApartment();
#else
        return kApartmentStateUnknown;
#endif
    }
 
    ApartmentState Thread::GetExplicitApartment()
    {
#if IL2CPP_THREAD_IMPL_HAS_COM_APARTMENTS
        return m_Thread->GetExplicitApartment();
#else
        return kApartmentStateUnknown;
#endif
    }
 
    ApartmentState Thread::SetApartment(ApartmentState state)
    {
#if IL2CPP_THREAD_IMPL_HAS_COM_APARTMENTS
        return m_Thread->SetApartment(state);
#else
        NO_UNUSED_WARNING(state);
        return GetApartment();
#endif
    }
 
    void Thread::SetExplicitApartment(ApartmentState state)
    {
#if IL2CPP_THREAD_IMPL_HAS_COM_APARTMENTS
        m_Thread->SetExplicitApartment(state);
#else
        NO_UNUSED_WARNING(state);
#endif
    }
 
    void Thread::Sleep(uint32_t milliseconds, bool interruptible)
    {
        ThreadImpl::Sleep(milliseconds, interruptible);
    }
 
    size_t Thread::CurrentThreadId()
    {
        return ThreadImpl::CurrentThreadId();
    }
 
    Thread* Thread::GetCurrentThread()
    {
        void* value;
        s_CurrentThread.GetValue(&value);
        IL2CPP_ASSERT(value != NULL);
        return reinterpret_cast<Thread*>(value);
    }
 
    bool Thread::HasCurrentThread()
    {
        void* value;
        s_CurrentThread.GetValue(&value);
        return value != NULL;
    }
 
    Thread* Thread::GetOrCreateCurrentThread()
    {
        Thread* thread = NULL;
        s_CurrentThread.GetValue(reinterpret_cast<void**>(&thread));
        if (thread)
            return thread;
 
        thread = new Thread(ThreadImpl::CreateForCurrentThread());
        s_CurrentThread.SetValue(thread);
 
        return thread;
    }
 
    void Thread::DetachCurrentThread()
    {
        // PTHREAD cleanup isn't deterministic: it could be that our thread local variables get cleaned up before thread clean up routine runs
#if IL2CPP_DEBUG && !IL2CPP_THREADS_PTHREAD
        void* value;
        s_CurrentThread.GetValue(&value);
        IL2CPP_ASSERT(value != NULL);
#endif
 
        s_CurrentThread.SetValue(NULL);
    }
 
    bool Thread::YieldInternal()
    {
        return ThreadImpl::YieldInternal();
    }
 
    void Thread::SetDefaultAffinityMask(int64_t affinityMask)
    {
        s_DefaultAffinityMask = affinityMask;
    }
 
#if IL2CPP_HAS_NATIVE_THREAD_CLEANUP
 
    void Thread::SetNativeThreadCleanup(ThreadCleanupFunc cleanupFunction)
    {
        ThreadImpl::SetNativeThreadCleanup(cleanupFunction);
    }
 
    void Thread::RegisterCurrentThreadForCleanup(void* arg)
    {
        ThreadImpl::RegisterCurrentThreadForCleanup(arg);
    }
 
    void Thread::UnregisterCurrentThreadForCleanup()
    {
        ThreadImpl::UnregisterCurrentThreadForCleanup();
    }
 
    void Thread::SignalExited()
    {
        m_ThreadExitedEvent.Set();
    }
 
#endif
}
}
 
#else
 
#include <limits.h>
 
namespace il2cpp
{
namespace os
{
    int64_t Thread::s_DefaultAffinityMask = -1;
 
    Thread::Thread()
    {
    }
 
    Thread::~Thread()
    {
    }
 
    void Thread::Init()
    {
    }
 
    void Thread::Shutdown()
    {
    }
 
    Thread::ThreadId Thread::Id()
    {
        return 0;
    }
 
    void Thread::SetName(const char* name)
    {
    }
 
    void Thread::SetPriority(ThreadPriority priority)
    {
    }
 
    ThreadPriority Thread::GetPriority()
    {
        return kThreadPriorityLowest;
    }
 
    void Thread::SetStackSize(size_t stackSize)
    {
    }
 
    int Thread::GetMaxStackSize()
    {
        return INT_MAX;
    }
 
    void Thread::RunWrapper(void* arg)
    {
    }
 
    ErrorCode Thread::Run(StartFunc func, void* arg)
    {
        IL2CPP_ASSERT(0 && "Threads are not enabled for this platform.");
        return kErrorCodeSuccess;
    }
 
    WaitStatus Thread::Join()
    {
        IL2CPP_ASSERT(0 && "Threads are not enabled for this platform.");
        return kWaitStatusSuccess;
    }
 
    WaitStatus Thread::Join(uint32_t ms)
    {
        IL2CPP_ASSERT(0 && "Threads are not enabled for this platform.");
        return kWaitStatusSuccess;
    }
 
    void Thread::QueueUserAPC(APCFunc func, void* context)
    {
    }
 
    ApartmentState Thread::GetApartment()
    {
        return kApartmentStateUnknown;
    }
 
    ApartmentState Thread::GetExplicitApartment()
    {
        return kApartmentStateUnknown;
    }
 
    ApartmentState Thread::SetApartment(ApartmentState state)
    {
        return kApartmentStateUnknown;
    }
 
    void Thread::SetExplicitApartment(ApartmentState state)
    {
    }
 
    void Thread::Sleep(uint32_t milliseconds, bool interruptible)
    {
    }
 
    size_t Thread::CurrentThreadId()
    {
        return 0;
    }
 
    Thread* Thread::GetCurrentThread()
    {
        return NULL;
    }
 
    Thread* Thread::GetOrCreateCurrentThread()
    {
        return NULL;
    }
 
    void Thread::DetachCurrentThread()
    {
    }
 
    bool Thread::YieldInternal()
    {
        return false;
    }
 
    void Thread::SetDefaultAffinityMask(int64_t affinityMask)
    {
        s_DefaultAffinityMask = affinityMask;
    }
 
#if IL2CPP_HAS_NATIVE_THREAD_CLEANUP
 
    void Thread::SetNativeThreadCleanup(ThreadCleanupFunc cleanupFunction)
    {
    }
 
    void Thread::RegisterCurrentThreadForCleanup(void* arg)
    {
    }
 
    void Thread::UnregisterCurrentThreadForCleanup()
    {
    }
 
    void Thread::SignalExited()
    {
    }
 
#endif
}
}
 
#endif