[sre] Make creation of dynamic method synchronized
authorVlad Brezae <brezaevlad@gmail.com>
Tue, 23 Jul 2019 16:03:02 +0000 (19:03 +0300)
committerMarek Safar <marek.safar@gmail.com>
Wed, 24 Jul 2019 12:31:51 +0000 (14:31 +0200)
commitd0b18850e433c4feda15d950851be59d500eaf4c
tree223c62f54a2cb49a6cbe0a986d92d3ecc42644c2
parent8807eac82a81def90c861f2510a90f9c8a8ddebe
[sre] Make creation of dynamic method synchronized

Before, reflection_create_dynamic_method could end up being called by two threads simultaneously. At the end of the method the ilgen field of the MonoReflectionDynamicMethod object is cleared, to enable its collection since it is no longer needed. The problem with this is that it can confuse the other thread compiling the dynamic method, making it see that there is no body.

We could either bother making this clearing work with multiple threads (and I'm not 100% convinced that the rest of the code is thread safe), disable the clearing of ilgen (which would make us use more memory) or go with the simple approach of locking on this DynamicMethod so only one thread is compiling it at one time.

Commit migrated from https://github.com/mono/mono/commit/71630cce754551e2f0414d59afb8824e7e0b0cfc
src/mono/mono/metadata/sre.c
src/mono/netcore/CoreFX.issues.rsp
src/mono/netcore/System.Private.CoreLib/src/System.Reflection.Emit/DynamicMethod.cs