LearnWithTouch/App/LearnWithTouch_ios/Libraries/libil2cpp/include/utils/CallOnce.h
2019-07-01 14:33:21 +02:00

45 lines
960 B
C++

#pragma once
#include "NonCopyable.h"
#include "../os/Atomic.h"
#include "../os/Mutex.h"
namespace il2cpp
{
namespace utils
{
typedef void (*CallOnceFunc) (void* arg);
struct OnceFlag : NonCopyable
{
OnceFlag() : m_Flag(NULL)
{
}
friend void CallOnce(OnceFlag& flag, CallOnceFunc func, void* arg);
bool IsSet()
{
return il2cpp::os::Atomic::ReadPointer(&m_Flag) ? true : false;
}
private:
void* m_Flag;
il2cpp::os::FastMutex m_Mutex;
};
inline void CallOnce(OnceFlag& flag, CallOnceFunc func, void* arg)
{
if (!il2cpp::os::Atomic::ReadPointer(&flag.m_Flag))
{
os::FastAutoLock lock(&flag.m_Mutex);
if (!il2cpp::os::Atomic::ReadPointer(&flag.m_Flag))
{
func(arg);
il2cpp::os::Atomic::ExchangePointer(&flag.m_Flag, (void*)1);
}
}
}
}
}