Skip to content
This repository was archived by the owner on Nov 13, 2026. It is now read-only.

Refactor explicit FLS usage in the runtime - #832

Merged
janvorli merged 1 commit into
dotnet:masterfrom
janvorli:thread-exit-callback
Feb 11, 2016
Merged

janvorli merged 1 commit into
dotnet:masterfrom
janvorli:thread-exit-callback

Conversation

@janvorli

@janvorli janvorli commented Feb 9, 2016

Copy link
Copy Markdown
Member

This change replaces explicit FLS usage for thread shutdown notification in the runtime into a
general thread shutdown callback registration mechanism.
The implementation using the FLS on Windows is moved to the PalRedhawkWinMin.cpp.
On Unix, it is implemented using TLS, since there are no fibers.

@janvorli

janvorli commented Feb 9, 2016

Copy link
Copy Markdown
Member Author

@jkotas, @smosier Can you take a look please?

@janvorli

janvorli commented Feb 9, 2016

Copy link
Copy Markdown
Member Author

Please note that setting the FLS value to the current thread pointer is now done before initializing the thread. I believe the change doesn't introduce any functional problem.

@janvorli

janvorli commented Feb 9, 2016

Copy link
Copy Markdown
Member Author

@smosier I was trying to preserve the saveguard checks in the ThreadStore::AttachCurrentThread, but if we don't care about failing fast in case a thread is registered with a fiber and ThreadStore::AttachCurrentThread is called on another fiber and we are ok just doing nothing in that case, I could simplify it further by removing the isFirstAttach parameter of the PalAttachThread and returning from the ThreadStore::AttachCurrentThread without doing anything when the pAttachingThread->IsInitialized() is true.

@janvorli

janvorli commented Feb 9, 2016

Copy link
Copy Markdown
Member Author

Implements #640

Comment thread src/Native/Runtime/PalRedhawk.h Outdated

REDHAWK_PALIMPORT UInt32 REDHAWK_PALAPI PalCompatibleWaitAny(UInt32_BOOL alertable, UInt32 timeout, UInt32 count, HANDLE* pHandles, UInt32_BOOL allowReentrantWait);

REDHAWK_PALIMPORT bool REDHAWK_PALAPI PalRegisterThreadShutdownCallback(ThreadShutdownCallback callback);

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I look at this PAL just as some platform specific code factored into separate files. I do not look at it as independent piece.

I think it would be ok to just call extern C method with given name from the PAL, and not have this registration.

@jkotas

jkotas commented Feb 9, 2016

Copy link
Copy Markdown
Member

LGTM otherwise

@smosier

smosier commented Feb 9, 2016

Copy link
Copy Markdown
Contributor

The original point of this fiber business was move the thread detach code to a place where the Win32 loader lock was not held. It's been too long for me to remember much more detail, but I will dig into it and get back to you. Do we have to worry about anything like the loader lock on unix?

@janvorli

janvorli commented Feb 9, 2016

Copy link
Copy Markdown
Member Author

@smosier There is not such issue on Unix.

@janvorli
janvorli force-pushed the thread-exit-callback branch from 03e7456 to 92b5433 Compare February 9, 2016 22:25
@janvorli

janvorli commented Feb 9, 2016

Copy link
Copy Markdown
Member Author

@jkotas, @smosier I have changed few things:

  1. Removed the callback registration as you've suggested
  2. Placed the registration code to PalInit
  3. Removed the constructor attribute from the PalInit and add call to it to InitDLL instead so that it is called both on Windows and Unix

@jkotas

jkotas commented Feb 9, 2016

Copy link
Copy Markdown
Member

Link error on Ubuntu

@janvorli
janvorli force-pushed the thread-exit-callback branch from 92b5433 to 3146b1b Compare February 9, 2016 23:00
@janvorli

janvorli commented Feb 9, 2016

Copy link
Copy Markdown
Member Author

@jkotas, the PalInit in PalRedhawkUnix.cpp was missing REDHAWK_PALEXPORT and REDHAWK_PALAPI annotation.

Comment thread src/Native/Runtime/threadstore.cpp Outdated
void ThreadStore::DetachCurrentThread()
{
//
// Note: we call this when each *fiber* is destroyed, because we receive that notification outside

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Shouldn't the comment be updated to remove the reference to fiber?

@janvorli

Copy link
Copy Markdown
Member Author

@jkotas, @smosier I have removed the isFirstAttach parameter from the PalAttachThread and submitted it as a separate commit so that you can compare it to the previous version.


if (attachedThread != NULL)
{
ASSERT_UNCONDITIONALLY("PalAttachThread called multiple times for the same thread");

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Nit - Regular assert may be more appropriate here.

@jkotas

jkotas commented Feb 10, 2016

Copy link
Copy Markdown
Member

LGTM

@janvorli
janvorli force-pushed the thread-exit-callback branch from 13a0910 to fe5bc43 Compare February 10, 2016 16:57
Comment thread src/Native/Runtime/startup.cpp Outdated

bool InitDLL(HANDLE hPalInstance)
{
if (!PalInit())

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Calling PalInit here will mean that it gets called twice in the PN runtime, which calls PalInit from DllMain. The reason for PN to do it this way was that we considered the runtime to be three separate parts: unmanaged (C++), managed (C#), and PAL. DllMain was responsible for initializing all 3 in the proper order. Putting the PalInit here will conflate the C++ and PAL pieces together and require some corresponding PN-side changes. Could we call PalInit from __initialize_runtime instead?

@smosier

smosier commented Feb 10, 2016

Copy link
Copy Markdown
Contributor

In general, I'd feel more comfortable about the FLS changes if you were to at least prototype the corresponding changes in the "app PAL" used by PN. This would help us understand if there are any unique issues there which might affect the form of these changes.

if (!PalQueryProcessorTopology())
return false;

g_flsIndex = FlsAlloc(FiberDetachCallback);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Please add a comment about what we're doing with FLS here. Basically something along the lines of "we use fiber detach callbacks to run our thread shutdown code because the fiber detach callback is made without the OS loader lock" is what I'd like to see.

This change abstracts the FLS usage for thread shutdown notification in PAL into a
general thread shutdown callback registration mechanism. It also implements its
Unix version.
@janvorli
janvorli force-pushed the thread-exit-callback branch from fe5bc43 to 8d00b43 Compare February 10, 2016 19:49
@janvorli

Copy link
Copy Markdown
Member Author

@smosier I've reflected all your feedback. I am going to try to create the prototype change that you've asked for.

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.

5 participants