-
-
Notifications
You must be signed in to change notification settings - Fork 13
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
System.Threading.Lock #15
Comments
Hi Mark, I'm glad if you find my libraries useful or interesting enough to contribute to it. Basically I'm open to collaborations, though for my CoreLibraries I have a very strict constraint not to have any dependencies to any other packages. I don't reference even In my other libraries I don't stick to this rule so much, so for example, my Drawing.Core library references If you still would like to improve KGySoft.CoreLibraries, feel free to add the functionality without introducing a new dependency (preferably, by forking the Net9 branch). You can place a comment with your name and/or a link to your original repository if you wish, this is how I also give attribution. But I can also understand if such an option is not that desirable for you. |
I think we can do that but I feel like it would still need to be in its own assembly due to https://github.com/MarkCiliaVincenti/Backport.System.Threading.Lock/blob/master/Backport.System.Threading.Lock/Properties/AssemblyInfo.cs It would therefore more or less be a case of copying and pasting in the source files in a new class library. |
That's why I acknowledged that I understand if you don't find my constraints appealing. I see the following options:
Side note: Just noticed the |
That's the issue. We don't want to put a TypeForwardedTo in your assembly either, and if we do this in its own assembly you're going to need to release it as its own nuget package, unless I'm missing something. |
Now I checked why did you do that. When I backport something from the Also, I don't think And as you have the But as I mentioned, you can freely create a fork for private usage and add whatever is useful for you in any way. |
There are two ways to use the library. One way for those on net5 or later is to use the System.Threading.Lock. The type forwarding is very important. Suppose my library is used by another library that targets net8 and net9. This library is then used by another library that targets only net8. Then finally a solution targeting net9 uses the latter library. What would happen is that it would not find the code it expects to find when it comes to locking. This was how the library started but then there was the problem with thread aborts so I catered for it in a less elegant way, using the lock factory |
Again, I will never backport a System type publicly because it can cause conflicts as noted above. Note that Tim also posted it as an internal type in his example.
It's because a public type is missing in a higher version of targeted platforms in your library, which exists in a lower version. (Anecdotical side note again: sometimes you cannot be prepared for similar conflicts with extension methods, i. e. when .NET introduces the same extension method that you already had. Even in this case you shouldn't remove your existing public method to keep binary compatibility but you can remove the If I ever will improve the existing locks in my library, I would add I cannot promise that I can do it for .NET 9 because my priority is one of my other libraries now so I would maybe improve my locks in a later version only. If the reason of your offer and willingness is that you use some locking types from my libraries, I still say that feel free to improve them, but if you add new dependencies or public backported |
Since KGySoft.CoreLibraries is targeting pre-.NET 5.0, we cannot use the original backport. Therefore if you're interested we could copy these two in: And then set them to internal. We could then add something like this to the csproj:
|
Actually I don't think that those are necessary. As I mentioned, there is no performance benefit in the backport for pre-.NET 9 at all so I simply would not add it to the library. Instead, I would refactor the locks like this: // the general change in classes with locker objects:
- private readonly object syncRoot = new object();
+ private readonly SyncObj syncRoot = SynchronizationHelper.CreateLock(); Where #if NET9_0_OR_GREATER
global using SyncObj = System.Threading.Lock;
#else
global using SyncObj = object;
#endif And the helper class could be as simple as this one: internal static class SynchronizationHelper
{
internal static SyncObj CreateLock() => new SyncObj();
} The few explicit #if NET9_0_OR_GREATER
internal static void Lock(SyncObj syncRoot) => syncRoot.Enter();
internal static void Unlock(SyncObj syncRoot) => syncRoot.Exit();
#else
internal static void Lock(SyncObj syncRoot) => Monitor.Enter(syncRoot);
internal static void Unlock(SyncObj syncRoot) => Monitor.Exit(syncRoot);
#endif And basically that's it. I don't use |
That would work, although you may want to aggressively inline there. Of course you're not enabling all the functionality in and have to use a different interface, but it could work. I guess it's over to you then. Thanks for your insight and detailed explanations, learned a lot. |
Would you be interested if I wrote a PR bringing in a dependency to my library Backport.System.Threading.Lock that will give you better locking performance on .NET 9.0+?
The text was updated successfully, but these errors were encountered: