Avoid race condition between domain unload and threadpool. (#3491)
* Avoid race condition between domain unload and threadpool.
Previous logic only ensured that domain->threadpool_jobs was 0.
There was a race condition as follows:
* worker jobs are queued into ThreadPoolDomain outstanding_request's
* domain unload starts
* domain->threadpool_jobs is 0 so unload proceeds
* queued jobs are processed from the ThreadPoolDomain outstanding_request's
* the domain can be set/entered by worker threads since
mono_domain_set only fails for domains with state of MONO_APPDOMAIN_UNLOADED,
while our domain is in the MONO_APPDOMAIN_UNLOADING state
* jobs are now being unexpectedly run in domain that is being shutdown
* Fix a hang when unloading the domain.
This change applies the diff from ludovic-henry to correct the hang.
https://gist.github.com/ludovic-henry/
6c8a4bc8951091c0ef36df2e52d5e32e