views:

52

answers:

3

I'm trying to understand in a bit more detail how a OS loaderlock is used in relation to the loading and unloading of DLL's in Windows.

I understand that every loaded DLL get notified when a new thread is created/destroyed and or a new DLL is loaded/unloaded.

So does that mean that the DllMain function is run inside a lock and no other thread can access it while it is running, and if you were to create another thread in that function, you could hang the process or even the OS?

Is my understanding correct?

Is there some article somewhere that explain this?

A: 

that is correct.

Any such execution is illegal because it can lead to deadlocks and to use of DLLs before they have been initialized by the operating system's loader.

More information can be found here: LoaderLock MDA (MSDN Website)

Mervin
A: 

A deadlock can happen when two threads try to acquire two locks in different sequence.

  • Thread A gets lock A and then tries to get lock B
  • Meanwhile thread B gets lock B and then tries to get lock A

A thread that's running DllMain has already acquired an implicit O/S lock: therefore they (Microsoft) reckon that it may be unsafe for that thread to try to acquire any other, second lock (e.g. because a different thread might already own that lock and be currently blocked on the implicit O/S lock).

ChrisW
A: 

Raymond Chen explains this.

JdeBP
Link doesn't seem to work...
Tony