Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 3 additions & 20 deletions Core/Libraries/Source/WWVegas/WWLib/mutex.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -90,26 +90,12 @@ MutexClass::LockClass::~LockClass()

// ----------------------------------------------------------------------------

CriticalSectionClass::CriticalSectionClass() : handle(nullptr), locked(false)
CriticalSectionClass::CriticalSectionClass() : locked(false)
{
#ifdef _UNIX
//assert(0);
#else
// Allocate with proper alignment for CRITICAL_SECTION
// On 64-bit: requires 8-byte alignment, on 32-bit: requires 4-byte alignment
#if defined(_WIN64) || defined(__LP64__)
size_t alignment = 8;
#else
size_t alignment = 4;
#endif

handle = _aligned_malloc(sizeof(CRITICAL_SECTION), alignment);
if (handle != nullptr) {
InitializeCriticalSection((CRITICAL_SECTION*)handle);
}
else {
WWASSERT(false); // Allocation failed
}
InitializeCriticalSection((CRITICAL_SECTION*)handle);
#endif
}

Expand All @@ -119,10 +105,7 @@ CriticalSectionClass::~CriticalSectionClass()
//assert(0);
#else
WWASSERT(!locked); // Can't delete locked critical section!
if (handle != nullptr) {
DeleteCriticalSection((CRITICAL_SECTION*)handle);
_aligned_free(handle);
}
DeleteCriticalSection((CRITICAL_SECTION*)handle);
#endif
}

Expand Down
5 changes: 4 additions & 1 deletion Core/Libraries/Source/WWVegas/WWLib/mutex.h
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,10 @@ class MutexClass

class CriticalSectionClass
{
void* handle;
// Inline storage for CRITICAL_SECTION to avoid heap allocation entirely.
// CRITICAL_SECTION is 40 bytes on x64 and 24 bytes on x86; 40 bytes with
// 8-byte alignment is sufficient for both platforms.
alignas(8) char handle[40];
unsigned locked;

// Lock and unlock are private so that you can't use them directly. Use LockClass as a sentry instead!
Expand Down
Loading