diff --git a/Source/Engine/Platform/ConditionVariable.h b/Source/Engine/Platform/ConditionVariable.h index f9a3de919..6486c4d9d 100644 --- a/Source/Engine/Platform/ConditionVariable.h +++ b/Source/Engine/Platform/ConditionVariable.h @@ -4,12 +4,10 @@ #if PLATFORM_WINDOWS || PLATFORM_UWP || PLATFORM_XBOX_ONE || PLATFORM_XBOX_SCARLETT #include "Win32/Win32ConditionVariable.h" -#elif PLATFORM_LINUX || PLATFORM_ANDROID || PLATFORM_PS4 || PLATFORM_PS5 +#elif PLATFORM_LINUX || PLATFORM_ANDROID || PLATFORM_PS4 || PLATFORM_PS5 || PLATFORM_MAC #include "Unix/UnixConditionVariable.h" #elif PLATFORM_SWITCH #include "Platforms/Switch/Engine/Platform/SwitchConditionVariable.h" -#elif PLATFORM_MAC -#include "Mac/MacConditionVariable.h" #else #error Missing Condition Variable implementation! #endif diff --git a/Source/Engine/Platform/CriticalSection.h b/Source/Engine/Platform/CriticalSection.h index 7b0ed971b..3f0c8b418 100644 --- a/Source/Engine/Platform/CriticalSection.h +++ b/Source/Engine/Platform/CriticalSection.h @@ -4,12 +4,10 @@ #if PLATFORM_WINDOWS || PLATFORM_UWP || PLATFORM_XBOX_ONE || PLATFORM_XBOX_SCARLETT #include "Win32/Win32CriticalSection.h" -#elif PLATFORM_LINUX || PLATFORM_ANDROID || PLATFORM_PS4 || PLATFORM_PS5 +#elif PLATFORM_LINUX || PLATFORM_ANDROID || PLATFORM_PS4 || PLATFORM_PS5 || PLATFORM_MAC #include "Unix/UnixCriticalSection.h" #elif PLATFORM_SWITCH #include "Platforms/Switch/Engine/Platform/SwitchCriticalSection.h" -#elif PLATFORM_MAC -#include "Mac/MacCriticalSection.h" #else #error Missing Critical Section implementation! #endif diff --git a/Source/Engine/Platform/Defines.h b/Source/Engine/Platform/Defines.h index f7f8ddf35..4e1646b8f 100644 --- a/Source/Engine/Platform/Defines.h +++ b/Source/Engine/Platform/Defines.h @@ -192,8 +192,8 @@ API_ENUM() enum class ArchitectureType // Platform family defines #define PLATFORM_WINDOWS_FAMILY (PLATFORM_WINDOWS || PLATFORM_UWP || PLATFORM_XBOX_ONE || PLATFORM_XBOX_SCARLETT) #define PLATFORM_MICROSOFT_FAMILY (PLATFORM_WINDOWS_FAMILY) -#define PLATFORM_UNIX_FAMILY (PLATFORM_LINUX || PLATFORM_ANDROID || PLATFORM_PS4 || PLATFORM_PS5) #define PLATFORM_APPLE_FAMILY (PLATFORM_MAC || PLATFORM_IOS) +#define PLATFORM_UNIX_FAMILY (PLATFORM_LINUX || PLATFORM_ANDROID || PLATFORM_PS4 || PLATFORM_PS5 || PLATFORM_APPLE_FAMILY) // SIMD defines #if defined(__i386__) || defined(_M_IX86) || defined(__x86_64__) || defined(_M_X64) || defined(__SSE2__) diff --git a/Source/Engine/Platform/File.h b/Source/Engine/Platform/File.h index d7df5444c..62a8b8341 100644 --- a/Source/Engine/Platform/File.h +++ b/Source/Engine/Platform/File.h @@ -4,14 +4,12 @@ #if PLATFORM_WINDOWS || PLATFORM_UWP || PLATFORM_XBOX_ONE || PLATFORM_XBOX_SCARLETT #include "Win32/Win32File.h" -#elif PLATFORM_LINUX || PLATFORM_PS4 || PLATFORM_PS5 +#elif PLATFORM_LINUX || PLATFORM_PS4 || PLATFORM_PS5 || PLATFORM_MAC #include "Unix/UnixFile.h" #elif PLATFORM_ANDROID #include "Android/AndroidFile.h" #elif PLATFORM_SWITCH #include "Platforms/Switch/Engine/Platform/SwitchFile.h" -#elif PLATFORM_MAC -#include "Mac/MacFile.h" #else #error Missing File implementation! #endif diff --git a/Source/Engine/Platform/Mac/MacConditionVariable.h b/Source/Engine/Platform/Mac/MacConditionVariable.h deleted file mode 100644 index c1d60184d..000000000 --- a/Source/Engine/Platform/Mac/MacConditionVariable.h +++ /dev/null @@ -1,91 +0,0 @@ -// Copyright (c) 2012-2021 Wojciech Figat. All rights reserved. - -#pragma once - -#if PLATFORM_MAC - -#include "MacCriticalSection.h" -#include -#include - -/// -/// Mac implementation of a condition variables. Condition variables are synchronization primitives that enable threads to wait until a particular condition occurs. Condition variables enable threads to atomically release a lock and enter the sleeping state. -/// -class FLAXENGINE_API MacConditionVariable -{ -private: - - pthread_cond_t _cond; - -private: - - MacConditionVariable(const MacConditionVariable&); - MacConditionVariable& operator=(const MacConditionVariable&); - -public: - - /// - /// Initializes a new instance of the class. - /// - MacConditionVariable() - { - pthread_cond_init(&_cond, nullptr); - } - - /// - /// Finalizes an instance of the class. - /// - ~MacConditionVariable() - { - pthread_cond_destroy(&_cond); - } - -public: - - /// - /// Blocks the current thread execution until the condition variable is woken up. - /// - /// The critical section locked by the current thread. - void Wait(const MacCriticalSection& lock) - { - pthread_cond_wait(&_cond, lock._mutexPtr); - } - - /// - /// Blocks the current thread execution until the condition variable is woken up or after the specified timeout duration. - /// - /// The critical section locked by the current thread. - /// The time-out interval, in milliseconds. If the time-out interval elapses, the function re-acquires the critical section and returns zero. If timeout is zero, the function tests the states of the specified objects and returns immediately. If timeout is INFINITE, the function's time-out interval never elapses. - /// If the function succeeds, the return value is true, otherwise, if the function fails or the time-out interval elapses, the return value is false. - bool Wait(const MacCriticalSection& lock, const int32 timeout) - { - struct timeval tv; - struct timespec ts; - - gettimeofday(&tv, NULL); - ts.tv_sec = time(NULL) + timeout / 1000; - ts.tv_nsec = tv.tv_usec * 1000 + 1000 * 1000 * (timeout % 1000); - ts.tv_sec += ts.tv_nsec / (1000 * 1000 * 1000); - ts.tv_nsec %= (1000 * 1000 * 1000); - - return pthread_cond_timedwait(&_cond, lock._mutexPtr, &ts) == 0; - } - - /// - /// Notifies one waiting thread. - /// - void NotifyOne() - { - pthread_cond_signal(&_cond); - } - - /// - /// Notifies all waiting threads. - /// - void NotifyAll() - { - pthread_cond_broadcast(&_cond); - } -}; - -#endif diff --git a/Source/Engine/Platform/Mac/MacCriticalSection.h b/Source/Engine/Platform/Mac/MacCriticalSection.h deleted file mode 100644 index e6254706f..000000000 --- a/Source/Engine/Platform/Mac/MacCriticalSection.h +++ /dev/null @@ -1,91 +0,0 @@ -// Copyright (c) 2012-2021 Wojciech Figat. All rights reserved. - -#pragma once - -#if PLATFORM_MAC - -#include "Engine/Platform/Platform.h" -#include - -class MacConditionVariable; - -/// -/// Mac implementation of a critical section. -/// -class FLAXENGINE_API MacCriticalSection -{ - friend MacConditionVariable; -private: - - pthread_mutex_t _mutex; - pthread_mutex_t* _mutexPtr; -#if BUILD_DEBUG - pthread_t _owningThreadId; -#endif - -private: - - MacCriticalSection(const MacCriticalSection&); - MacCriticalSection& operator=(const MacCriticalSection&); - -public: - - /// - /// Initializes a new instance of the class. - /// - MacCriticalSection() - { - pthread_mutexattr_t attributes; - pthread_mutexattr_init(&attributes); - pthread_mutexattr_settype(&attributes, PTHREAD_MUTEX_RECURSIVE); - pthread_mutex_init(&_mutex, &attributes); - pthread_mutexattr_destroy(&attributes); - _mutexPtr = &_mutex; -#if BUILD_DEBUG - _owningThreadId = 0; -#endif - } - - /// - /// Finalizes an instance of the class. - /// - ~MacCriticalSection() - { - pthread_mutex_destroy(&_mutex); - } - -public: - - /// - /// Locks the critical section. - /// - void Lock() const - { - pthread_mutex_lock(_mutexPtr); -#if BUILD_DEBUG - ((MacCriticalSection*)this)->_owningThreadId = pthread_self(); -#endif - } - - /// - /// Attempts to enter a critical section without blocking. If the call is successful, the calling thread takes ownership of the critical section. - /// - /// True if calling thread took ownership of the critical section. - bool TryLock() const - { - return pthread_mutex_trylock(_mutexPtr) == 0; - } - - /// - /// Releases the lock on the critical section. - /// - void Unlock() const - { -#if BUILD_DEBUG - ((MacCriticalSection*)this)->_owningThreadId = 0; -#endif - pthread_mutex_unlock(_mutexPtr); - } -}; - -#endif diff --git a/Source/Engine/Platform/Mac/MacDefines.h b/Source/Engine/Platform/Mac/MacDefines.h index c56284643..3931de79f 100644 --- a/Source/Engine/Platform/Mac/MacDefines.h +++ b/Source/Engine/Platform/Mac/MacDefines.h @@ -4,6 +4,8 @@ #if PLATFORM_MAC +#include "../Unix/UnixDefines.h" + // Platform description #define PLATFORM_64BITS 1 #define PLATFORM_ARCH_X64 1 @@ -13,7 +15,5 @@ #define PLATFORM_CACHE_LINE_SIZE 128 #define PLATFORM_HAS_HEADLESS_MODE 1 #define PLATFORM_DEBUG_BREAK __builtin_trap() -#define PLATFORM_LINE_TERMINATOR "\n" -#define PLATFORM_TEXT_IS_CHAR16 1 #endif diff --git a/Source/Engine/Platform/Mac/MacFile.cpp b/Source/Engine/Platform/Mac/MacFile.cpp deleted file mode 100644 index 2e8456d9e..000000000 --- a/Source/Engine/Platform/Mac/MacFile.cpp +++ /dev/null @@ -1,153 +0,0 @@ -// Copyright (c) 2012-2021 Wojciech Figat. All rights reserved. - -#if PLATFORM_MAC - -#include "../File.h" -#include "Engine/Core/Log.h" -#include "Engine/Core/Types/String.h" -#include "Engine/Core/Types/DateTime.h" -#include "Engine/Core/Types/TimeSpan.h" -#include "Engine/Utilities/StringConverter.h" -#include "Engine/Core/Log.h" -#include -#include -#include -#include -#include - -MacFile::MacFile(int32 handle) - : _handle(handle) -{ -} - -MacFile::~MacFile() -{ - Close(); -} - -MacFile* MacFile::Open(const StringView& path, FileMode mode, FileAccess access, FileShare share) -{ - int flags = O_CLOEXEC; - switch (access) - { - case FileAccess::Read: - flags |= O_RDONLY; - break; - case FileAccess::Write: - flags |= O_WRONLY; - break; - case FileAccess::ReadWrite: - flags |= O_RDWR; - break; - default: ; - } - switch (mode) - { - case FileMode::CreateAlways: - flags |= O_CREAT | O_TRUNC; - break; - case FileMode::CreateNew: - flags |= O_CREAT | O_EXCL; - break; - case FileMode::OpenAlways: - break; - case FileMode::OpenExisting: - break; - case FileMode::TruncateExisting: - flags |= O_TRUNC; - break; - default: ; - } - - mode_t omode = S_IRUSR | S_IWUSR; - if ((uint32)share & (uint32)FileShare::Delete) - omode |= 0; - if ((uint32)share & (uint32)FileShare::Read) - omode |= (mode_t)(S_IRGRP | S_IROTH); - if ((uint32)share & (uint32)FileShare::Write) - omode |= (mode_t)(S_IWGRP | S_IWOTH); - if ((uint32)share & (uint32)FileShare::Delete) - omode |= 0; - - const StringAsANSI<> pathANSI(*path, path.Length()); - auto handle = open(pathANSI.Get(), flags, omode); - if (handle == -1) - { - return nullptr; - } - - return New(handle); -} - -bool MacFile::Read(void* buffer, uint32 bytesToRead, uint32* bytesRead) -{ - const ssize_t tmp = read(_handle, buffer, bytesToRead); - if (tmp != -1) - { - if (bytesRead) - *bytesRead = tmp; - return false; - } - if (bytesRead) - *bytesRead = 0; - return true; -} - -bool MacFile::Write(const void* buffer, uint32 bytesToWrite, uint32* bytesWritten) -{ - const ssize_t tmp = write(_handle, buffer, bytesToWrite); - if (tmp != -1) - { - if (bytesWritten) - *bytesWritten = tmp; - return false; - } - if (bytesWritten) - *bytesWritten = 0; - return true; -} - -void MacFile::Close() -{ - if (_handle != -1) - { - close(_handle); - _handle = -1; - } -} - -uint32 MacFile::GetSize() const -{ - struct stat fileInfo; - fstat(_handle, &fileInfo); - return fileInfo.st_size; -} - -DateTime MacFile::GetLastWriteTime() const -{ - struct stat fileInfo; - if (fstat(_handle, &fileInfo) == -1) - { - return DateTime::MinValue(); - } - const TimeSpan timeSinceEpoch(0, 0, fileInfo.st_mtime); - const DateTime MacEpoch(1970, 1, 1); - return MacEpoch + timeSinceEpoch; -} - -uint32 MacFile::GetPosition() const -{ - return lseek(_handle, 0, SEEK_CUR); -} - -void MacFile::SetPosition(uint32 seek) -{ - lseek(_handle, seek, SEEK_SET); -} - -bool MacFile::IsOpened() const -{ - return _handle != -1; -} - -#endif diff --git a/Source/Engine/Platform/Mac/MacFile.h b/Source/Engine/Platform/Mac/MacFile.h deleted file mode 100644 index 8e6741db1..000000000 --- a/Source/Engine/Platform/Mac/MacFile.h +++ /dev/null @@ -1,56 +0,0 @@ -// Copyright (c) 2012-2021 Wojciech Figat. All rights reserved. - -#pragma once - -#if PLATFORM_MAC - -#include "Engine/Platform/Base/FileBase.h" - -/// -/// Mac platform file object implementation. -/// -class MacFile : public FileBase -{ -protected: - - int32 _handle; - -public: - - /// - /// Initializes a new instance of the class. - /// - /// The handle. - MacFile(int32 handle); - - /// - /// Finalizes an instance of the class. - /// - ~MacFile(); - -public: - - /// - /// Creates or opens a file. - /// - /// The name of the file to be created or opened. - /// An action to take on a file that exists or does not exist. - /// The requested access to the file. - /// The requested sharing mode of the file. - /// Opened file handle or null if cannot. - static MacFile* Open(const StringView& path, FileMode mode, FileAccess access = FileAccess::ReadWrite, FileShare share = FileShare::None); - -public: - - // [FileBase] - bool Read(void* buffer, uint32 bytesToRead, uint32* bytesRead = nullptr) override; - bool Write(const void* buffer, uint32 bytesToWrite, uint32* bytesWritten = nullptr) override; - void Close() override; - uint32 GetSize() const override; - DateTime GetLastWriteTime() const override; - uint32 GetPosition() const override; - void SetPosition(uint32 seek) override; - bool IsOpened() const override; -}; - -#endif diff --git a/Source/Engine/Platform/Mac/MacNetwork.cpp b/Source/Engine/Platform/Mac/MacNetwork.cpp deleted file mode 100644 index 11eeccb26..000000000 --- a/Source/Engine/Platform/Mac/MacNetwork.cpp +++ /dev/null @@ -1,7 +0,0 @@ -// Copyright (c) 2012-2021 Wojciech Figat. All rights reserved. - -#if PLATFORM_MAC - -// TODO: networking on Mac - -#endif diff --git a/Source/Engine/Platform/Mac/MacNetwork.h b/Source/Engine/Platform/Mac/MacNetwork.h deleted file mode 100644 index 1e8c16cd3..000000000 --- a/Source/Engine/Platform/Mac/MacNetwork.h +++ /dev/null @@ -1,27 +0,0 @@ -// Copyright (c) 2012-2021 Wojciech Figat. All rights reserved. - -#pragma once - -#if PLATFORM_MAC - -#include "Engine/Platform/Base/NetworkBase.h" - -class FLAXENGINE_API MacNetwork : public NetworkBase -{ -public: - - // [NetworkBase] - static bool CreateSocket(NetworkSocket& socket, NetworkProtocol proto, NetworkIPVersion ipv); - static bool DestroySocket(NetworkSocket& socket); - static bool SetSocketOption(NetworkSocket& socket, NetworkSocketOption option, int32 value); - static bool GetSocketOption(NetworkSocket& socket, NetworkSocketOption option, int32& value); - static bool ConnectSocket(NetworkSocket& socket, NetworkEndPoint& endPoint); - static bool BindSocket(NetworkSocket& socket, NetworkEndPoint& endPoint); - static bool Listen(NetworkSocket& socket, uint16 queueSize); - static bool Accept(NetworkSocket& serverSocket, NetworkSocket& newSocket, NetworkEndPoint& newEndPoint); - static int32 WriteSocket(NetworkSocket socket, byte* data, uint32 length, NetworkEndPoint* endPoint = nullptr); - static int32 ReadSocket(NetworkSocket socket, byte* buffer, uint32 bufferSize, NetworkEndPoint* endPoint = nullptr); - static bool CreateEndPoint(const String& address, const String& port, NetworkIPVersion ipv, NetworkEndPoint& endPoint, bool bindable = true); -}; - -#endif diff --git a/Source/Engine/Platform/Mac/MacPlatform.cpp b/Source/Engine/Platform/Mac/MacPlatform.cpp index 5fb09ecf4..baa9ea739 100644 --- a/Source/Engine/Platform/Mac/MacPlatform.cpp +++ b/Source/Engine/Platform/Mac/MacPlatform.cpp @@ -80,50 +80,6 @@ public: typedef uint16_t offset_t; #define align_mem_up(num, align) (((num) + ((align) - 1)) & ~((align) - 1)) -void* MacPlatform::Allocate(uint64 size, uint64 alignment) -{ - void* ptr = nullptr; - - // Alignment always has to be power of two - ASSERT_LOW_LAYER((alignment & (alignment - 1)) == 0); - - if (alignment && size) - { - uint32_t pad = sizeof(offset_t) + (alignment - 1); - void* p = malloc(size + pad); - if (p) - { - // Add the offset size to malloc's pointer - ptr = (void*)align_mem_up(((uintptr_t)p + sizeof(offset_t)), alignment); - - // Calculate the offset and store it behind aligned pointer - *((offset_t*)ptr - 1) = (offset_t)((uintptr_t)ptr - (uintptr_t)p); - } -#if COMPILE_WITH_PROFILER - OnMemoryAlloc(ptr, size); -#endif - } - return ptr; -} - -void MacPlatform::Free(void* ptr) -{ - if (ptr) - { -#if COMPILE_WITH_PROFILER - OnMemoryFree(ptr); -#endif - // Walk backwards from the passed-in pointer to get the pointer offset - offset_t offset = *((offset_t*)ptr - 1); - - // Get original pointer - void* p = (void*)((uint8_t*)ptr - offset); - - // Free memory - free(p); - } -} - bool MacPlatform::Is64BitPlatform() { return PLATFORM_64BITS; @@ -237,7 +193,7 @@ void MacPlatform::GetUTCTime(int32& year, int32& month, int32& dayOfWeek, int32& bool MacPlatform::Init() { - if (PlatformBase::Init()) + if (UnixPlatform::Init()) return true; // Init timing diff --git a/Source/Engine/Platform/Mac/MacPlatform.h b/Source/Engine/Platform/Mac/MacPlatform.h index 8edccb832..86bb07e63 100644 --- a/Source/Engine/Platform/Mac/MacPlatform.h +++ b/Source/Engine/Platform/Mac/MacPlatform.h @@ -9,11 +9,11 @@ /// /// The Mac platform implementation and application management utilities. /// -class FLAXENGINE_API MacPlatform : public PlatformBase +class FLAXENGINE_API MacPlatform : public UnixPlatform { public: - // [PlatformBase] + // [UnixPlatform] FORCE_INLINE static void MemoryBarrier() { __sync_synchronize(); @@ -66,14 +66,11 @@ public: { __builtin_prefetch(static_cast(ptr)); } - static void* Allocate(uint64 size, uint64 alignment); - static void Free(void* ptr); static bool Is64BitPlatform(); static CPUInfo GetCPUInfo(); static int32 GetCacheLineSize(); static MemoryStats GetMemoryStats(); static ProcessMemoryStats GetProcessMemoryStats(); - static uint64 GetCurrentProcessId(); static uint64 GetCurrentThreadID(); static void SetThreadPriority(ThreadPriority priority); static void SetThreadAffinityMask(uint64 affinityMask); diff --git a/Source/Engine/Platform/Mac/MacStringUtils.cpp b/Source/Engine/Platform/Mac/MacStringUtils.cpp deleted file mode 100644 index f7a6ca15c..000000000 --- a/Source/Engine/Platform/Mac/MacStringUtils.cpp +++ /dev/null @@ -1,16 +0,0 @@ -// Copyright (c) 2012-2021 Wojciech Figat. All rights reserved. - -#if PLATFORM_MAC - -#include "Engine/Platform/StringUtils.h" -#include -#include -#include -#include -#include - -// Reuse Unix-impl -#define PLATFORM_UNIX 1 -#include "../Unix/UnixStringUtils.cpp" - -#endif diff --git a/Source/Engine/Platform/Mac/MacThread.cpp b/Source/Engine/Platform/Mac/MacThread.cpp deleted file mode 100644 index edfe7a128..000000000 --- a/Source/Engine/Platform/Mac/MacThread.cpp +++ /dev/null @@ -1,68 +0,0 @@ -// Copyright (c) 2012-2021 Wojciech Figat. All rights reserved. - -#if PLATFORM_MAC - -#include "MacThread.h" -#include "Engine/Core/Log.h" -#include "Engine/Threading/IRunnable.h" -#include "Engine/Threading/ThreadRegistry.h" - -MacThread::MacThread(IRunnable* runnable, const String& name, ThreadPriority priority) - : ThreadBase(runnable, name, priority) - , _thread(0) -{ -} - -MacThread::~MacThread() -{ - ASSERT(_thread == 0); -} - -MacThread* MacThread::Create(IRunnable* runnable, const String& name, ThreadPriority priority, uint32 stackSize) -{ - auto thread = New(runnable, name, priority); - pthread_attr_t attr; - pthread_attr_init(&attr); - if (stackSize != 0) - pthread_attr_setstacksize(&attr, stackSize); - const int32 result = pthread_create(&thread->_thread, &attr, ThreadProc, thread); - if (result != 0) - { - LOG(Warning, "Failed to spawn a thread. Result code: {0}", result); - Delete(thread); - return nullptr; - } - thread->SetPriorityInternal(thread->GetPriority()); - return thread; -} - -void MacThread::Join() -{ - pthread_join(_thread, nullptr); -} - -void* MacThread::ThreadProc(void* pThis) -{ - auto thread = (MacThread*)pThis; - const int32 exitCode = thread->Run(); - return (void*)(uintptr)exitCode; -} - -void MacThread::ClearHandleInternal() -{ - _thread = 0; -} - -void MacThread::SetPriorityInternal(ThreadPriority priority) -{ - // TODO: impl this -} - -void MacThread::KillInternal(bool waitForJoin) -{ - if (waitForJoin) - pthread_join(_thread, nullptr); - pthread_kill(_thread, SIGKILL); -} - -#endif diff --git a/Source/Engine/Platform/Mac/MacThread.h b/Source/Engine/Platform/Mac/MacThread.h index bc1b87deb..1b8b51f82 100644 --- a/Source/Engine/Platform/Mac/MacThread.h +++ b/Source/Engine/Platform/Mac/MacThread.h @@ -4,34 +4,72 @@ #if PLATFORM_MAC -#include "../Base/ThreadBase.h" +#include "../Unix/UnixThread.h" +#include /// /// Thread object for Mac platform. /// -class MacThread : public ThreadBase +class MacThread : public UnixThread { -protected: - - pthread_t _thread; - public: - MacThread(IRunnable* runnable, const String& name, ThreadPriority priority); - ~MacThread(); - static MacThread* Create(IRunnable* runnable, const String& name, ThreadPriority priority = ThreadPriority::Normal, uint32 stackSize = 0); + /// + /// Initializes a new instance of the class. + /// + /// The runnable. + /// The thread name. + /// The thread priority. + MacThread(IRunnable* runnable, const String& name, ThreadPriority priority) + : UnixThread(runnable, name, priority) + { + } + +public: - // [ThreadBase] - void Join() override; + /// + /// Factory method to create a thread with the specified stack size and thread priority + /// + /// The runnable object to execute + /// Name of the thread + /// Tells the thread whether it needs to adjust its priority or not. Defaults to normal priority + /// The size of the stack to create. 0 means use the current thread's stack size + /// Pointer to the new thread or null if cannot create it + static MacThread* Create(IRunnable* runnable, const String& name, ThreadPriority priority = ThreadPriority::Normal, uint32 stackSize = 0) + { + return (MacThread*)Setup(New(runnable, name, priority), stackSize); + } protected: - static void* ThreadProc(void* pThis); - - // [ThreadBase] - void ClearHandleInternal() override; - void SetPriorityInternal(ThreadPriority priority) override; - void KillInternal(bool waitForJoin) override; + // [UnixThread] + int32 GetThreadPriority(ThreadPriority priority) override + { + switch (priority) + { + case ThreadPriority::Highest: + return 45; + case ThreadPriority::AboveNormal: + return 37; + case ThreadPriority::Normal: + return 31; + case ThreadPriority::BelowNormal: + return 25; + case ThreadPriority::Lowest: + return 20; + } + return 31; + } + int32 Start(pthread_attr_t& attr) override + { + return pthread_create(&_thread, &attr, ThreadProc, this); + } + void KillInternal(bool waitForJoin) override + { + if (waitForJoin) + pthread_join(_thread, nullptr); + pthread_kill(_thread, SIGKILL); + } }; #endif diff --git a/Source/Engine/Platform/Network.h b/Source/Engine/Platform/Network.h index 03435c4e0..93983cefa 100644 --- a/Source/Engine/Platform/Network.h +++ b/Source/Engine/Platform/Network.h @@ -4,7 +4,7 @@ #if PLATFORM_WINDOWS || PLATFORM_UWP || PLATFORM_XBOX_ONE || PLATFORM_XBOX_SCARLETT #include "Win32/Win32Network.h" -#elif PLATFORM_LINUX || PLATFORM_ANDROID +#elif PLATFORM_LINUX || PLATFORM_ANDROID || PLATFORM_MAC #include "Unix/UnixNetwork.h" #elif PLATFORM_PS4 #include "Platforms/PS4/Engine/Platform/PS4Network.h" diff --git a/Source/Engine/Platform/Platform.Build.cs b/Source/Engine/Platform/Platform.Build.cs index 05f6078b5..9f72d6aae 100644 --- a/Source/Engine/Platform/Platform.Build.cs +++ b/Source/Engine/Platform/Platform.Build.cs @@ -78,6 +78,7 @@ public class Platform : EngineModule options.SourcePaths.Add(Path.Combine(Globals.EngineRoot, "Source", "Platforms", "Switch", "Engine", "Platform")); break; case TargetPlatform.Mac: + options.SourcePaths.Add(Path.Combine(FolderPath, "Unix")); options.SourcePaths.Add(Path.Combine(FolderPath, "Mac")); break; default: throw new InvalidPlatformException(options.Platform.Target); diff --git a/Source/Engine/Platform/Types.h b/Source/Engine/Platform/Types.h index e82bdac71..3af24d092 100644 --- a/Source/Engine/Platform/Types.h +++ b/Source/Engine/Platform/Types.h @@ -231,24 +231,24 @@ typedef UserBase User; class ClipboardBase; typedef ClipboardBase Clipboard; -class MacCriticalSection; -typedef MacCriticalSection CriticalSection; -class MacConditionVariable; -typedef MacConditionVariable ConditionVariable; +class UnixCriticalSection; +typedef UnixCriticalSection CriticalSection; +class UnixConditionVariable; +typedef UnixConditionVariable ConditionVariable; class MacFileSystem; typedef MacFileSystem FileSystem; class FileSystemWatcherBase; typedef FileSystemWatcherBase FileSystemWatcher; -class MacFile; -typedef MacFile File; +class UnixFile; +typedef UnixFile File; class MacPlatform; typedef MacPlatform Platform; class MacThread; typedef MacThread Thread; class MacWindow; typedef MacWindow Window; -class MacNetwork; -typedef MacNetwork Network; +class UnixNetwork; +typedef UnixNetwork Network; class UserBase; typedef UserBase User; diff --git a/Source/Engine/Platform/Unix/UnixConditionVariable.h b/Source/Engine/Platform/Unix/UnixConditionVariable.h index 76e6eaadf..e9a502fd6 100644 --- a/Source/Engine/Platform/Unix/UnixConditionVariable.h +++ b/Source/Engine/Platform/Unix/UnixConditionVariable.h @@ -17,8 +17,6 @@ private: pthread_cond_t _cond; -private: - UnixConditionVariable(const UnixConditionVariable&); UnixConditionVariable& operator=(const UnixConditionVariable&); @@ -61,13 +59,11 @@ public: { struct timeval tv; struct timespec ts; - gettimeofday(&tv, NULL); ts.tv_sec = time(NULL) + timeout / 1000; ts.tv_nsec = tv.tv_usec * 1000 + 1000 * 1000 * (timeout % 1000); ts.tv_sec += ts.tv_nsec / (1000 * 1000 * 1000); ts.tv_nsec %= (1000 * 1000 * 1000); - return pthread_cond_timedwait(&_cond, lock._mutexPtr, &ts) == 0; } diff --git a/Source/Engine/Platform/Unix/UnixCriticalSection.h b/Source/Engine/Platform/Unix/UnixCriticalSection.h index 232b50818..0f65defc0 100644 --- a/Source/Engine/Platform/Unix/UnixCriticalSection.h +++ b/Source/Engine/Platform/Unix/UnixCriticalSection.h @@ -15,7 +15,6 @@ class UnixConditionVariable; class FLAXENGINE_API UnixCriticalSection { friend UnixConditionVariable; - private: pthread_mutex_t _mutex; @@ -24,8 +23,6 @@ private: pthread_t _owningThreadId; #endif -private: - UnixCriticalSection(const UnixCriticalSection&); UnixCriticalSection& operator=(const UnixCriticalSection&); diff --git a/Source/Engine/Platform/Unix/UnixNetwork.cpp b/Source/Engine/Platform/Unix/UnixNetwork.cpp index f305d042e..eb4c29d3f 100644 --- a/Source/Engine/Platform/Unix/UnixNetwork.cpp +++ b/Source/Engine/Platform/Unix/UnixNetwork.cpp @@ -56,7 +56,9 @@ static void TranslateSockOptToNative(NetworkSocketOption option, int32* level, i SOCKOPT(NetworkSocketOption::NoDelay, IPPROTO_TCP, TCP_NODELAY) #endif SOCKOPT(NetworkSocketOption::IPv6Only, IPPROTO_IPV6, IPV6_V6ONLY) +#ifdef IP_MTU SOCKOPT(NetworkSocketOption::Mtu, IPPROTO_IP, IP_MTU) +#endif SOCKOPT(NetworkSocketOption::Type, SOL_SOCKET, SO_TYPE) #undef SOCKOPT default: diff --git a/Source/Tools/Flax.Build/Platforms/Mac/MacPlatform.cs b/Source/Tools/Flax.Build/Platforms/Mac/MacPlatform.cs index 7a1051ae8..9d26f38bf 100644 --- a/Source/Tools/Flax.Build/Platforms/Mac/MacPlatform.cs +++ b/Source/Tools/Flax.Build/Platforms/Mac/MacPlatform.cs @@ -10,8 +10,8 @@ namespace Flax.Build.Platforms /// /// The build platform for all Mac systems. /// - /// - public sealed class MacPlatform : Platform + /// + public sealed class MacPlatform : UnixPlatform { /// public override TargetPlatform Target => TargetPlatform.Mac; @@ -22,24 +22,15 @@ namespace Flax.Build.Platforms /// public override bool HasSharedLibrarySupport => true; - /// - public override string ExecutableFileExtension => string.Empty; - /// public override string SharedLibraryFileExtension => ".dylib"; - /// - public override string StaticLibraryFileExtension => ".a"; - /// public override string ProgramDatabaseFileExtension => ".dSYM"; /// public override string SharedLibraryFilePrefix => string.Empty; - /// - public override string StaticLibraryFilePrefix => "lib"; - /// public override ProjectFormat DefaultProjectFormat => ProjectFormat.XCode; @@ -75,5 +66,15 @@ namespace Flax.Build.Platforms { return new MacToolchain(this, architecture); } + + /// + public override bool CanBuildPlatform(TargetPlatform platform) + { + switch (platform) + { + case TargetPlatform.Mac: return HasRequiredSDKsInstalled; + default: return false; + } + } } } diff --git a/Source/Tools/Flax.Build/Platforms/Mac/MacToolchain.cs b/Source/Tools/Flax.Build/Platforms/Mac/MacToolchain.cs index 8f3f39e55..9682f6f7b 100644 --- a/Source/Tools/Flax.Build/Platforms/Mac/MacToolchain.cs +++ b/Source/Tools/Flax.Build/Platforms/Mac/MacToolchain.cs @@ -11,14 +11,13 @@ namespace Flax.Build.Platforms /// /// The build toolchain for all Mac systems. /// - /// - public sealed class MacToolchain : Toolchain + /// + public sealed class MacToolchain : UnixToolchain { private string MinMacOSXVer = "10.14"; public string ToolchainPath; public string SdkPath; - public string ClangPath; public string LinkerPath; public string ArchiverPath; @@ -47,7 +46,7 @@ namespace Flax.Build.Platforms ClangPath = Path.Combine(ToolchainPath, "usr/bin/clang++"); LinkerPath = Path.Combine(ToolchainPath, "usr/bin/clang++"); ArchiverPath = Path.Combine(ToolchainPath, "usr/bin/libtool"); - var clangVersion = UnixToolchain.GetClangVersion(ClangPath); + ClangVersion = GetClangVersion(ClangPath); SdkPath = Path.Combine(SdkPath, "SDKs"); var sdks = Directory.GetDirectories(SdkPath); var sdkPrefix = "MacOSX"; @@ -75,19 +74,13 @@ namespace Flax.Build.Platforms SdkPath = bestSdk; // Setup system paths - SystemIncludePaths.Add(Path.Combine(ToolchainPath, "usr/include")); - SystemIncludePaths.Add(Path.Combine(ToolchainPath, "usr/include/c++/v1")); - SystemIncludePaths.Add(Path.Combine(ToolchainPath, "usr/lib/clang", clangVersion.ToString(3), "include")); - SystemIncludePaths.Add(Path.Combine(SdkPath, "usr/include")); + //SystemIncludePaths.Add(Path.Combine(ToolchainPath, "usr/include")); + //SystemIncludePaths.Add(Path.Combine(ToolchainPath, "usr/include/c++/v1")); + //SystemIncludePaths.Add(Path.Combine(ToolchainPath, "usr/lib/clang", ClangVersion.ToString(3), "include")); + //SystemIncludePaths.Add(Path.Combine(SdkPath, "usr/include")); SystemLibraryPaths.Add(Path.Combine(SdkPath, "usr/lib")); } - /// - public override string DllExport => "__attribute__((__visibility__(\\\"default\\\")))"; - - /// - public override string DllImport => ""; - /// public override void LogInfo() { diff --git a/Source/Tools/Flax.Build/Platforms/Unix/UnixToolchain.cs b/Source/Tools/Flax.Build/Platforms/Unix/UnixToolchain.cs index f4f4a25f2..e7b116254 100644 --- a/Source/Tools/Flax.Build/Platforms/Unix/UnixToolchain.cs +++ b/Source/Tools/Flax.Build/Platforms/Unix/UnixToolchain.cs @@ -71,13 +71,15 @@ namespace Flax.Build.Platforms /// /// The platform. /// The target architecture. - /// The root folder for the toolchains installation. + /// The root folder for the toolchains installation. If null, then platform must specify tools paths manually. /// The system compiler to use. Null if use toolset root. /// The custom toolchain folder location in directory. If nul the architecture name will be used. - protected UnixToolchain(UnixPlatform platform, TargetArchitecture architecture, string toolchainRoots, string systemCompiler, string toolchainSubDir = null) + protected UnixToolchain(UnixPlatform platform, TargetArchitecture architecture, string toolchainRoots = null, string systemCompiler = null, string toolchainSubDir = null) : base(platform, architecture) { ArchitectureName = GetToolchainName(platform.Target, architecture); + if (toolchainRoots == null) + return; // Build paths if (systemCompiler != null) @@ -198,6 +200,12 @@ namespace Flax.Build.Platforms case TargetArchitecture.ARM64: return "aarch64-linux-android"; default: throw new InvalidArchitectureException(architecture); } + case TargetPlatform.Mac: + switch (architecture) + { + case TargetArchitecture.x64: return "x86_64-apple-macos" + Configuration.MacOSXMinVer; + default: throw new InvalidArchitectureException(architecture); + } default: throw new InvalidPlatformException(platform); } }