I am modifying a library which is entirely reentrant to add windows support. The problem is the initialization function. The initialization function is wrapped in MUTEX_LOCK(&ssl_lock); and MUTEX_UNLOCK(&ssl_lock); in order to garuntee that everything is initalized only once.
The problem is that I cannot call EnterCriticalSection before InitializeCriticalSection and InitializeCriticalSection is not reentrant. How do I solve this problem?
Depending on what version sof Windows you need to support, you can use the “Init Once” APIs:
If you need to support WinXP/Win Server 2003 or older, you may need to rely on a Mutex object that the kernel can ensure only gets created once. You’ll need to implement some logic when opening/creating the mutex to make sure all threads of execution end up with a valid handle (ie., a loop that attempts to open, then attempts to create until the open or create succeeds).