Skip to content
This repository was archived by the owner on Jan 23, 2023. It is now read-only.

WIP: Merge System.Threading.Thread into Internal.Runtime.Augments.RuntimeThread. #22032

Closed
wants to merge 5 commits into from

Conversation

filipnavara
Copy link
Member

@filipnavara filipnavara commented Jan 17, 2019

This is just a proof of concept. It will likely break VS Debugger in its current shape, but it depends on how the VS Debugger accesses the _managedThreadId field.

Here's a background on what I am trying to solve. It was already discussed in mono/mono#12470 (comment), but I will restate it here for clarity.

Right now CoreFX implements System.Threading.Thread using calls to Internal.Runtime.Augments.RuntimeThread in CoreLib. The CoreRT implementation is quite clean, but for historical reasons the CoreCLR implementation is not. It's split into RuntimeThread and another internal System.Threading.Thread type in CoreLib. This is an unfortunate situation where two System.Threading.Thread types exist, with different, but overlapping, functionality, in two different assemblies. To add insult to the injury some types in CoreLib reference the System.Threading.Thread directly instead of RuntimeThread, which makes code sharing with CoreRT/Mono difficult.

There are three proposed solutions for a cleanup:

  1. Get rid of System.Threading.Thread in CoreLib and fold its functionality into RuntimeThread. This is implemented by this PR. Additionally, RuntimeThread namespace should be changed to Internal.Threading.RuntimeThread to make it match the convention used for most types like that (not done in this PR and will require changes to CoreRT and CoreFX too).

  2. Keep System.Threading.Thread in CoreLib, but ensure that it is called only from RuntimeThread and unmanaged code. All other code would reference RuntimeThread only.

  3. Move System.Threading.Thread from CoreFX back to CoreLib, including its transitive dependency closure and get rid of RuntimeThread.

Option 2) seems to be least favorable. The only thing going for it is that it won't break VS debugger and it's very limited in scope to the CoreCLR repository. Ultimately it doesn't solve the problem with two classes with the same name though.

Option 3) will require most shuffling of code between repositories.

@jkotas
Copy link
Member

jkotas commented Jan 17, 2019

cc @marek-safar

@@ -14,22 +14,135 @@ namespace Internal.Runtime.Augments
{
public class RuntimeThread : CriticalFinalizerObject
{
/*=========================================================================

Choose a reason for hiding this comment

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

Hmm, there is more code duplicating with CoreFX Thread that I expected

Copy link
Member

Choose a reason for hiding this comment

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

Do you have anything specific you have in mind?

@jkotas
Copy link
Member

jkotas commented Jan 19, 2019

Let's wait for the Environment CoreFX->CoreCLR move to finish first, and then decide which option to take for this one.

@filipnavara
Copy link
Member Author

Sure. This is low-priority one, but I decided to tackle it because it bit me couple of times that the class name resolution didn't work as I expected (ie. using Thread = ... was effectively ignored because the class was in the System.Threading namespace). I'll be happy to help with a solution (whichever one you decide on) once the work on Environment is finished.

@filipnavara
Copy link
Member Author

Any new thoughts about this?

@marek-safar
Copy link

@jkotas @stephentoub could you please advice Filip on future steps

@jkotas
Copy link
Member

jkotas commented Feb 19, 2019

I think we should bring the public System.Threading.Thread with its transitive closure back to CoreLib. @stephentoub @marek-safar Do you agree?

If we go with this plan, the first step should be to identify all files in CoreFX that need to be moved to CoreLib and move them to CoreLib shared partition in the CoreFX repo. (Except Thread.cs itself since it will require refactoring to integrate back to CoreLib.)

@stephentoub
Copy link
Member

stephentoub commented Feb 19, 2019

I think we should bring the public System.Threading.Thread with its transitive closure back to CoreLib. @stephentoub @marek-safar Do you agree?

In theory, yes. It'd be good to see the full transitive list before pulling the trigger. From a quick skim, it looks like the worst of it would be some permissions stuff via AppDomain, which should probably be in Corelib, anyway.

@marek-safar
Copy link

I think we should bring the public System.Threading.Thread with its transitive closure back to CoreLib.

This makes sense, all VM specific types are in CoreLib and this will hopefully make few dependencies simpler as well. I didn't check Thread dependencies chain but it's hopefully not too bad.

@filipnavara
Copy link
Member Author

filipnavara commented Feb 20, 2019

Here's the list:

  • System.Runtime.Extensions\src\System\CannotUnloadAppDomainException.cs
  • System.Runtime.Extensions\src\System\AppDomain.cs
  • System.Runtime.Extensions\src\System\AppDomainSetup.cs
  • System.Runtime.Extensions\src\System\Collections\ArrayList.cs
  • System.Runtime.Extensions\src\System\Security\IPermission.cs
  • System.Runtime.Extensions\src\System\Security\ISecurityEncodable.cs
  • System.Runtime.Extensions\src\System\Security\IStackWalk.cs
  • System.Runtime.Extensions\src\System\Security\PermissionSet.cs
  • System.Runtime.Extensions\src\System\Security\Permissions\PermissionState.cs
  • System.Runtime.Extensions\src\System\Security\SecurityElement.cs
  • System.Security.Principal\src\System\Security\Principal\IIdentity.cs
  • System.Security.Principal\src\System\Security\Principal\IPrincipal.cs
  • System.Security.Principal\src\System\Security\Principal\PrincipalPolicy.cs
  • System.Threading.Thread\src\System\LocalDataStoreSlot.cs
  • System.Threading.Thread\src\System\Threading\CompressedStack.cs
  • System.Threading.Thread\src\System\Threading\Thread.cs
  • System.Threading.Thread\src\System\Threading\Thread.Unix.cs
  • System.Threading.Thread\src\System\Threading\Thread.Windows.cs

@filipnavara
Copy link
Member Author

I tried copying the above files into CoreRT's System.Private.CoreLib to see if it can compile. I've got to the point where only resource strings are missing, so the list above should be fairly complete.

@stephentoub
Copy link
Member

Thanks for the list.

System.Runtime.Extensions\src\System\Collections\ArrayList.cs

Is this actually exposed through API, or could the implementation just use List<object>?

@filipnavara
Copy link
Member Author

filipnavara commented Feb 20, 2019

Is this actually exposed through API, or could the implementation just use List<object>?

Unfortunately it is exposed by SecurityElement. It's the biggest dependency of them all.

@jkotas
Copy link
Member

jkotas commented Feb 20, 2019

ArrayList is old sibling of Hashtable. I do not feel bad about having it in CoreLib.

@filipnavara
Copy link
Member Author

I'm going to close this PR, but feel free to continue discussion.

Is anyone planning to work on it? I'm notoriously bad at multi-repository code transfers, but if you are willing to bear with me I can try to reproduce the same PR sequence as for the Environment transfer.

@jkotas
Copy link
Member

jkotas commented Feb 20, 2019

We will be glad if you can help.

@stephentoub
Copy link
Member

And if you need help and get stuck, please let me know; I'll be happy to.

@filipnavara
Copy link
Member Author

Alright. I will get to work later today.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants