[add] first

This commit is contained in:
2023-10-08 10:24:48 +08:00
commit b1ae0510a9
1048 changed files with 3254361 additions and 0 deletions

View File

@@ -0,0 +1,31 @@
#pragma once
#if IL2CPP_THREADS_WIN32
#include "utils/NonCopyable.h"
#include "WindowsHeaders.h"
class FastMutexImpl;
namespace il2cpp
{
namespace os
{
class ConditionVariableImpl : public il2cpp::utils::NonCopyable
{
public:
ConditionVariableImpl();
~ConditionVariableImpl();
int Wait(FastMutexImpl* lock);
int TimedWait(FastMutexImpl* lock, uint32_t timeout_ms);
void Broadcast();
void Signal();
private:
CONDITION_VARIABLE m_ConditionVariable;
};
}
}
#endif

View File

@@ -0,0 +1,9 @@
#pragma once
namespace il2cpp
{
namespace os
{
void InitializeDllMain();
}
}

View File

@@ -0,0 +1,33 @@
#pragma once
#if IL2CPP_THREADS_WIN32
#include "os/ErrorCodes.h"
#include "os/WaitStatus.h"
#include "utils/NonCopyable.h"
#include "WindowsHeaders.h"
namespace il2cpp
{
namespace os
{
class EventImpl : public il2cpp::utils::NonCopyable
{
public:
EventImpl(bool manualReset = false, bool signaled = false);
~EventImpl();
ErrorCode Set();
ErrorCode Reset();
WaitStatus Wait(bool interruptible);
WaitStatus Wait(uint32_t ms, bool interruptible);
void* GetOSHandle();
private:
HANDLE m_Event;
};
}
}
#endif

View File

@@ -0,0 +1,19 @@
#pragma once
#if IL2CPP_THREADS_WIN32
#include "ReaderWriterLockImpl.h"
namespace il2cpp
{
namespace os
{
class FastReaderReaderWriterLockImpl : public ReaderWriterLockImpl
{
// Windows ReaderWriterLockImpl uses the Win32 SlimReaderWriterLock
// which is fast for our purposes
};
}
}
#endif

View File

@@ -0,0 +1,64 @@
#pragma once
#if IL2CPP_THREADS_WIN32
#include "os/ErrorCodes.h"
#include "os/WaitStatus.h"
#include "utils/NonCopyable.h"
#include "WindowsHeaders.h"
namespace il2cpp
{
namespace os
{
class MutexImpl : public il2cpp::utils::NonCopyable
{
public:
MutexImpl();
~MutexImpl();
void Lock(bool interruptible);
bool TryLock(uint32_t milliseconds, bool interruptible);
void Unlock();
void* GetOSHandle();
private:
HANDLE m_MutexHandle;
};
class FastMutexImpl
{
public:
FastMutexImpl()
{
InitializeCriticalSection(&m_CritialSection);
}
~FastMutexImpl()
{
DeleteCriticalSection(&m_CritialSection);
}
void Lock()
{
EnterCriticalSection(&m_CritialSection);
}
void Unlock()
{
LeaveCriticalSection(&m_CritialSection);
}
CRITICAL_SECTION* GetOSHandle()
{
return &m_CritialSection;
}
private:
CRITICAL_SECTION m_CritialSection;
};
}
}
#endif

View File

@@ -0,0 +1,51 @@
#pragma once
#if IL2CPP_THREADS_WIN32
#include "WindowsHeaders.h"
namespace il2cpp
{
namespace os
{
class ReaderWriterLockImpl
{
public:
ReaderWriterLockImpl()
{
InitializeSRWLock(&m_Lock);
}
void LockExclusive()
{
AcquireSRWLockExclusive(&m_Lock);
}
void LockShared()
{
AcquireSRWLockShared(&m_Lock);
}
void ReleaseExclusive()
{
ReleaseSRWLockExclusive(&m_Lock);
}
void ReleaseShared()
{
ReleaseSRWLockShared(&m_Lock);
}
PSRWLOCK GetOSHandle()
{
return &m_Lock;
}
private:
SRWLOCK m_Lock;
};
}
}
#endif

View File

@@ -0,0 +1,30 @@
#pragma once
#if IL2CPP_THREADS_WIN32
#include "os/ErrorCodes.h"
#include "os/WaitStatus.h"
#include "utils/NonCopyable.h"
#include "WindowsHeaders.h"
namespace il2cpp
{
namespace os
{
class SemaphoreImpl : public il2cpp::utils::NonCopyable
{
public:
SemaphoreImpl(int32_t initialValue, int32_t maximumValue);
~SemaphoreImpl();
bool Post(int32_t releaseCount, int32_t* previousCount = NULL);
WaitStatus Wait(bool interruptible);
WaitStatus Wait(uint32_t ms, bool interruptible);
void* GetOSHandle();
private:
HANDLE m_Handle;
};
}
}
#endif

View File

@@ -0,0 +1,132 @@
#pragma once
#if IL2CPP_TARGET_WINDOWS
#include <string>
#include <vector>
#include <stdint.h>
#include "os/Socket.h"
#include "os/ErrorCodes.h"
#include "os/WaitStatus.h"
#include "utils/Expected.h"
#include "utils/NonCopyable.h"
struct sockaddr;
namespace il2cpp
{
namespace os
{
class SocketImpl : public il2cpp::utils::NonCopyable
{
public:
/// Handle for socket. Must be large enough to hold a pointer.
typedef uint64_t SocketDescriptor;
SocketImpl(ThreadStatusCallback thread_status_callback);
~SocketImpl();
inline SocketDescriptor GetDescriptor()
{
return _fd;
}
ErrorCode GetLastError() const;
WaitStatus Create(SocketDescriptor fd, int32_t family, int32_t type, int32_t protocol);
WaitStatus Create(AddressFamily family, SocketType type, ProtocolType protocol);
WaitStatus Close();
bool IsClosed() { return (_fd == -1); }
WaitStatus SetBlocking(bool blocking);
WaitStatus Listen(int32_t blacklog);
WaitStatus Bind(const char *path);
WaitStatus Bind(const char *address, uint16_t port);
WaitStatus Bind(uint32_t address, uint16_t port);
WaitStatus Bind(uint8_t address[ipv6AddressSize], uint32_t scope, uint16_t port);
WaitStatus Connect(const char *path);
WaitStatus Connect(uint32_t address, uint16_t port);
WaitStatus Connect(uint8_t address[ipv6AddressSize], uint32_t scope, uint16_t port);
WaitStatus Disconnect(bool reuse);
WaitStatus Shutdown(int32_t how);
WaitStatus GetLocalEndPointInfo(EndPointInfo &info);
WaitStatus GetRemoteEndPointInfo(EndPointInfo &info);
WaitStatus Receive(const uint8_t *data, int32_t count, os::SocketFlags flags, int32_t *len);
WaitStatus ReceiveFromInternal(const uint8_t *data, size_t count, int32_t flags, int32_t *len, struct sockaddr *from, int32_t *fromlen);
WaitStatus Send(const uint8_t *data, int32_t count, os::SocketFlags flags, int32_t *len);
WaitStatus SendArray(WSABuf *wsabufs, int32_t count, int32_t *sent, SocketFlags c_flags);
WaitStatus ReceiveArray(WSABuf *wsabufs, int32_t count, int32_t *len, SocketFlags c_flags);
WaitStatus SendTo(uint32_t address, uint16_t port, const uint8_t *data, int32_t count, os::SocketFlags flags, int32_t *len);
utils::Expected<WaitStatus> SendTo(const char *path, const uint8_t *data, int32_t count, os::SocketFlags flags, int32_t *len);
WaitStatus SendTo(uint8_t address[ipv6AddressSize], uint32_t scope, uint16_t port, const uint8_t *data, int32_t count, os::SocketFlags flags, int32_t *len);
WaitStatus RecvFrom(uint32_t address, uint16_t port, const uint8_t *data, int32_t count, os::SocketFlags flags, int32_t *len, os::EndPointInfo &ep);
utils::Expected<WaitStatus> RecvFrom(const char *path, const uint8_t *data, int32_t count, os::SocketFlags flags, int32_t *len, os::EndPointInfo &ep);
WaitStatus RecvFrom(uint8_t address[ipv6AddressSize], uint32_t scope, uint16_t port, const uint8_t *data, int32_t count, os::SocketFlags flags, int32_t *len, os::EndPointInfo &ep);
WaitStatus Available(int32_t *amount);
WaitStatus Accept(os::Socket **socket);
WaitStatus Ioctl(int32_t command, const uint8_t *in_data, int32_t in_len, uint8_t *out_data, int32_t out_len, int32_t *written);
WaitStatus GetSocketOption(SocketOptionLevel level, SocketOptionName name, uint8_t *buffer, int32_t *length);
WaitStatus GetSocketOptionFull(SocketOptionLevel level, SocketOptionName name, int32_t *first, int32_t *second);
WaitStatus SetSocketOption(SocketOptionLevel level, SocketOptionName name, int32_t value);
WaitStatus SetSocketOptionLinger(SocketOptionLevel level, SocketOptionName name, bool enabled, int32_t seconds);
WaitStatus SetSocketOptionArray(SocketOptionLevel level, SocketOptionName name, const uint8_t *buffer, int32_t length);
WaitStatus SetSocketOptionMembership(SocketOptionLevel level, SocketOptionName name, uint32_t group_address, uint32_t local_address);
#if IL2CPP_SUPPORT_IPV6
WaitStatus SetSocketOptionMembership(SocketOptionLevel level, SocketOptionName name, IPv6Address ipv6, uint64_t interfaceOffset);
#endif
WaitStatus SendFile(const char *filename, TransmitFileBuffers *buffers, TransmitFileOptions options);
static WaitStatus Poll(std::vector<PollRequest> &requests, int32_t count, int32_t timeout, int32_t *result, int32_t *error);
static WaitStatus Poll(std::vector<PollRequest> &requests, int32_t timeout, int32_t *result, int32_t *error);
static WaitStatus Poll(PollRequest& request, int32_t timeout, int32_t *result, int32_t *error);
static WaitStatus GetHostName(std::string &name);
static WaitStatus GetHostByName(const std::string &host, std::string &name, std::vector<std::string> &aliases, std::vector<std::string> &addresses);
static WaitStatus GetHostByName(const std::string &host, std::string &name, int32_t &family, std::vector<std::string> &aliases, std::vector<void*> &addr_list, int32_t &addr_size);
static WaitStatus GetHostByAddr(const std::string &address, std::string &name, std::vector<std::string> &aliases, std::vector<std::string> &addr_list);
static void Startup();
static void Cleanup();
private:
bool _is_valid;
SocketDescriptor _fd;
int32_t _domain;
int32_t _type;
int32_t _protocol;
ErrorCode _saved_error;
int32_t _still_readable;
ThreadStatusCallback _thread_status_callback;
void StoreLastError();
void StoreLastError(int32_t error_no);
WaitStatus ConnectInternal(struct sockaddr *sa, int32_t sa_size);
WaitStatus SendToInternal(struct sockaddr *sa, int32_t sa_size, const uint8_t *data, int32_t count, os::SocketFlags flags, int32_t *len);
WaitStatus RecvFromInternal(struct sockaddr* sa, int32_t sa_size, const uint8_t* data, int32_t count, os::SocketFlags flags, int32_t* len, os::EndPointInfo& ep);
WaitStatus SetSocketOptionInternal(int32_t level, int32_t name, const void *value, int32_t len);
};
}
}
#endif

View File

@@ -0,0 +1,74 @@
#pragma once
#if !IL2CPP_THREADS_STD && IL2CPP_THREADS_WIN32
#include "os/ErrorCodes.h"
#include "os/Thread.h"
#include "os/WaitStatus.h"
#include "utils/NonCopyable.h"
#include "WindowsHeaders.h"
#define IL2CPP_DEFAULT_STACK_SIZE ( 1 * 1024 * 1024) // default .NET stacksize is 1mb
namespace il2cpp
{
namespace os
{
class ThreadImpl : public il2cpp::utils::NonCopyable
{
public:
ThreadImpl();
~ThreadImpl();
size_t Id();
ErrorCode Run(Thread::StartFunc func, void* arg, int64_t affinityMask);
void SetName(const char* name);
void SetPriority(ThreadPriority priority);
ThreadPriority GetPriority();
void SetStackSize(size_t newsize)
{
// only makes sense if it's called BEFORE the thread has been created
IL2CPP_ASSERT(m_ThreadHandle == NULL);
// if newsize is zero we use the per-platform default value for size of stack
if (newsize == 0)
{
newsize = IL2CPP_DEFAULT_STACK_SIZE;
}
m_StackSize = newsize;
}
static int GetMaxStackSize();
void QueueUserAPC(Thread::APCFunc func, void* context);
ApartmentState GetApartment();
ApartmentState GetExplicitApartment();
ApartmentState SetApartment(ApartmentState state);
void SetExplicitApartment(ApartmentState state);
static void Sleep(uint32_t ms, bool interruptible);
static size_t CurrentThreadId();
static ThreadImpl* CreateForCurrentThread();
static bool YieldInternal();
#if IL2CPP_HAS_NATIVE_THREAD_CLEANUP
static void SetNativeThreadCleanup(Thread::ThreadCleanupFunc cleanupFunction);
static void RegisterCurrentThreadForCleanup(void* arg);
static void UnregisterCurrentThreadForCleanup();
static void OnCurrentThreadExiting();
#endif
private:
HANDLE m_ThreadHandle;
volatile DWORD m_ThreadId;
SIZE_T m_StackSize;
ApartmentState m_ApartmentState;
ThreadPriority m_Priority;
};
}
}
#endif

View File

@@ -0,0 +1,100 @@
#pragma once
#include "il2cpp-config.h"
#if IL2CPP_THREADS_WIN32
#include "os/c-api/il2cpp-config-platforms.h"
#include "os/ErrorCodes.h"
#include "utils/NonCopyable.h"
// We declare windows APIs here instead of including windows system headers
// to avoid leaking windows system headers to all of il2cpp and il2cpp-generated
// code.
// On UWP, old Windows SDKs (10586 and older) did not have support for Tls* functions
// so instead we used forwarded them to Fls* equivalents. Using Tls* functions directly
// with these old SDKs causes linker errors.
#define IL2CPP_USE_WINRT_FLS_WORKAROUND (IL2CPP_TARGET_WINRT && WINDOWS_SDK_BUILD_VERSION <= 10586)
#if !IL2CPP_USE_WINRT_FLS_WORKAROUND
extern "C" {
__declspec(dllimport) unsigned long __stdcall TlsAlloc(void);
__declspec(dllimport) void* __stdcall TlsGetValue(unsigned long dwTlsIndex);
__declspec(dllimport) int __stdcall TlsSetValue(unsigned long dwTlsIndex, void* lpTlsValue);
__declspec(dllimport) int __stdcall TlsFree(unsigned long dwTlsIndex);
__declspec(dllimport) unsigned long __stdcall GetLastError(void);
}
#else
extern "C" {
typedef void (__stdcall* PFLS_CALLBACK_FUNCTION)(void* lpFlsData);
__declspec(dllimport) unsigned long __stdcall FlsAlloc(PFLS_CALLBACK_FUNCTION callback);
__declspec(dllimport) void* __stdcall FlsGetValue(unsigned long dwTlsIndex);
__declspec(dllimport) int __stdcall FlsSetValue(unsigned long dwTlsIndex, void* lpTlsValue);
__declspec(dllimport) int __stdcall FlsFree(unsigned long dwTlsIndex);
__declspec(dllimport) unsigned long __stdcall GetLastError(void);
}
#define TlsAlloc() FlsAlloc(NULL)
#define TlsGetValue FlsGetValue
#define TlsSetValue FlsSetValue
#define TlsFree FlsFree
#endif
#define IL2CPP_TLS_OUT_OF_INDEXES ((unsigned long)0xFFFFFFFF)
namespace il2cpp
{
namespace os
{
class ThreadLocalValueImpl : public il2cpp::utils::NonCopyable
{
public:
inline ThreadLocalValueImpl()
{
m_Index = TlsAlloc();
IL2CPP_ASSERT(m_Index != IL2CPP_TLS_OUT_OF_INDEXES);
}
inline ~ThreadLocalValueImpl()
{
bool success = TlsFree(m_Index);
NO_UNUSED_WARNING(success);
IL2CPP_ASSERT(success);
}
inline ErrorCode SetValue(void* value)
{
if (TlsSetValue(m_Index, value) == false)
return static_cast<ErrorCode>(GetLastError());
return kErrorCodeSuccess;
}
inline ErrorCode GetValue(void** value)
{
*value = TlsGetValue(m_Index);
if (*value)
return kErrorCodeSuccess;
unsigned long lastError = GetLastError();
if (lastError == 0)
return kErrorCodeSuccess;
return static_cast<ErrorCode>(lastError);
}
private:
unsigned long m_Index;
};
}
}
#if IL2CPP_USE_WINRT_FLS_WORKAROUND
#undef TlsAlloc
#undef TlsGetValue
#undef TlsSetValue
#undef TlsFree
#endif
#endif // IL2CPP_THREADS_WIN32

View File

@@ -0,0 +1,32 @@
#pragma once
#if IL2CPP_TARGET_WINDOWS
#pragma once
#ifndef NOMINMAX
#define NOMINMAX
#endif
#ifndef WIN32_LEAN_AND_MEAN
#define WIN32_LEAN_AND_MEAN 1
#endif
#define INC_OLE2 1
#include <Windows.h>
#include <Objidl.h>
#if IL2CPP_TARGET_WINDOWS_DESKTOP || IL2CPP_TARGET_WINRT || IL2CPP_TARGET_WINDOWS_GAMES
#include <wincrypt.h>
#include <winsock2.h>
#include <Iphlpapi.h>
#endif
#if !IL2CPP_TARGET_WINDOWS_DESKTOP
#include <bcrypt.h>
#include <roapi.h>
#include <robuffer.h>
#include <winstring.h>
#endif
#define LINK_TO_WINDOWSRUNTIME_LIBS (!IL2CPP_TARGET_WINDOWS_DESKTOP)
#endif

View File

@@ -0,0 +1,41 @@
#pragma once
#if IL2CPP_TARGET_WINDOWS
#include "WindowsHeaders.h"
#include "os/WaitStatus.h"
#if IL2CPP_TARGET_WINRT
#include "os/WinRT/Win32ApiWinRTEmulation.h"
#endif
#if IL2CPP_TARGET_XBOXONE
#include "os/XboxOne/Win32ApiXboxEmulation.h"
#endif
#if IL2CPP_TARGET_WINDOWS_GAMES
#include "os/WindowsGames/Win32ApiWindowsGamesEmulation.h"
#endif
#if IL2CPP_TARGET_WINRT || IL2CPP_TARGET_XBOXONE
#include "os/WinRT/Win32ApiSharedEmulation.h"
#endif
#include <vector>
namespace il2cpp
{
namespace os
{
namespace win
{
// Wait for a release of the given handle in way that can be interrupted by APCs.
WaitStatus WaitForSingleObjectAndAccountForAPCs(HANDLE handle, uint32_t ms, bool interruptible);
int32_t WaitForAnyObjectAndAccountForAPCs(const std::vector<HANDLE>& handles, uint32_t ms, bool interruptible);
bool WaitForAllObjectsAndAccountForAPCs(const std::vector<HANDLE>& handles, uint32_t ms, bool interruptible);
}
}
}
#endif // IL2CPP_TARGET_WINDOWS