少年修仙传客户端基础资源
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
#include "os/c-api/il2cpp-config-platforms.h"
 
#if IL2CPP_THREADS_PTHREAD && !IL2CPP_TINY_WITHOUT_DEBUGGER
 
#include "MutexImpl.h"
#include "PosixHelpers.h"
#include "os/Thread.h"
 
namespace il2cpp
{
namespace os
{
    MutexImpl::MutexImpl()
        : posix::PosixWaitObject(kMutex)
        , m_OwningThread(NULL)
        , m_RecursionCount(0)
    {
        // For a mutex, 1 means unlocked.
        m_Count = 1;
    }
 
    void MutexImpl::Lock(bool interruptible)
    {
        TryLock(posix::kNoTimeout, interruptible);
    }
 
    bool MutexImpl::TryLock(uint32_t milliseconds, bool interruptible)
    {
        Thread* currentThread = Thread::GetCurrentThread();
        if (m_OwningThread == currentThread)
        {
            IL2CPP_ASSERT(m_Count == 0);
            ++m_RecursionCount;
            return true;
        }
 
        if (Wait(milliseconds, interruptible) == kWaitStatusSuccess)
        {
            m_OwningThread = currentThread;
            m_RecursionCount = 1;
            return true;
        }
 
        return false;
    }
 
    void MutexImpl::Unlock()
    {
        IL2CPP_ASSERT(m_OwningThread == Thread::GetCurrentThread());
 
        // Undo one locking level.
        --m_RecursionCount;
        if (m_RecursionCount > 0)
        {
            // Still locked.
            return;
        }
 
        // Ok, we're releasing the mutex. Lock and signal. We don't absolutely
        // need the lock as we are already owning the mutex here but play it safe.
        posix::PosixAutoLock lock(&m_Mutex);
 
        IL2CPP_ASSERT(m_Count == 0);
        m_Count = 1; // Unintuitive but 1 means unlocked.
        m_OwningThread = NULL;
 
        // Signal condition so that either a thread that's already waiting or a thread that
        // comes around later and waits can claim the mutex.
        if (HaveWaitingThreads())
            pthread_cond_signal(&m_Condition);
    }
}
}
 
#endif