Skip to content
This repository was archived by the owner on Dec 29, 2022. It is now read-only.

[linux-port] Cross-platform critical section - #235

Merged
ehsannas merged 1 commit into
google:linuxfrom
pow2clk:pow2clk-fixup
May 31, 2018
Merged

ehsannas merged 1 commit into
google:linuxfrom
pow2clk:pow2clk-fixup

Conversation

@pow2clk

@pow2clk pow2clk commented May 31, 2018

Copy link
Copy Markdown

InitializeCriticalSection, DeleteCriticalSection, EnterCriticalSection,
and LeaveCriticalSection serve to create a mutual exclusion section of
code, but they are stubbed out currently for non-win platforms.

There is multi-platform support for the same functionality in LLVM
in the form of SmartMutex. This switches from the Windows specific
to the abstraction, allowing the same code to work on other platforms.

Fixes half of #198

@AppVeyorBot

Copy link
Copy Markdown

@pow2clk

pow2clk commented May 31, 2018

Copy link
Copy Markdown
Author

Okay well I learned that clang-spirv-tests doesn't replace a simple dxc run test. This means this has a rather larger prerequisite. Either an ugly violation of hierarchy or a conversion to explicit loading like Windows does.

InitializeCriticalSection, DeleteCriticalSection, EnterCriticalSection,
and LeaveCriticalSection serve to create a mutual exclusion section of
code, but they are stubbed out currently for non-win platforms.

There is multi-platform support for the same functionality in LLVM
in the form of SmartMutex. This switches from the Windows specific
to the abstraction, allowing the same code to work on other platforms.

Fixes half of google#198
@pow2clk

pow2clk commented May 31, 2018

Copy link
Copy Markdown
Author

I hit upon a simpler solution that ignores the larger problem based on the fact that I am completely certain that "dxil.dll" will never be found successfully on a linux system. So the function that boils down to if it is or ever was found will always be false and the premature use of a critical section can be avoided.

This ignores two issues:
dxil.dll is not currently built for any windows or non-windows build I've ever seen. I can't figure out when it was last built or what happened to it. Currently, what would have gone there is built inside dxcompiler.. Should this check even exist then?

the critical section/ mutex is initialized as part of the initialization of the dxcompiler library, which is completely bypassed by linux currently. So far we've managed to make do without it, but at the least it represents a significant difference between the overall behavior of the linux and windows build. There are options to make this work similarly on linux. It might be worth it only to make it work more like Windows does for the sake of fewer surprises. The root of the problem seems to be that Linux dynamically links dxcompiler instead of loading it. I haven't conclusively identified why the cmake files do that yet.

@AppVeyorBot

Copy link
Copy Markdown

@ehsannas

Copy link
Copy Markdown

Given the following code by MS:

MutexImpl::MutexImpl(bool /*recursive*/)
{
C_ASSERT(sizeof(data_) == sizeof(CRITICAL_SECTION));
// data_ = new CRITICAL_SECTION; // HLSL Change
InitializeCriticalSection((LPCRITICAL_SECTION)data_);
}
MutexImpl::~MutexImpl()
{
DeleteCriticalSection((LPCRITICAL_SECTION)data_);
// delete (LPCRITICAL_SECTION)data_; // HLSL Change
// data_ = 0; // HLSL Change
}
bool
MutexImpl::acquire()
{
EnterCriticalSection((LPCRITICAL_SECTION)data_);
return true;
}
bool
MutexImpl::release()
{
LeaveCriticalSection((LPCRITICAL_SECTION)data_);
return true;
}

and that lock() and unlock() call the impl's acquire() and release(), your change looks very reasonable to me.

#define OutputDebugStringA(msg) fputs(msg, stderr)
#define OutputDebugFormatA(...) fprintf(stderr, __VA_ARGS__)

// Note: This will *disable* the CRITICAL_SECTION structures in the code.

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yay!

@ehsannas

Copy link
Copy Markdown

LGTM. I'd just wait a bit and see if MS responds to the issue . If we don't get a response soon, we can just merge this change and improve it in a future CL.

@pow2clk

pow2clk commented May 31, 2018

Copy link
Copy Markdown
Author

Yeah. The microsoft change to the original class puzzled me. It wouldn't have been strictly necessary for this change, but there's another static Mutex lurking somewhere that gets initialized as a static object, so before the TLS Malloc gets set, so it fails if it has to allocate anything.

@ehsannas ehsannas left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM.

@ehsannas
ehsannas merged commit e9ce03b into google:linux May 31, 2018
@pow2clk
pow2clk deleted the pow2clk-fixup branch June 6, 2018 09:50
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants