Re: [HACKERS] [PATCHES] Compiling libpq with VisualC - Mailing list pgsql-hackers-win32

From Gary Doades
Subject Re: [HACKERS] [PATCHES] Compiling libpq with VisualC
Date
Msg-id 40CDF596.16134.1021004B@localhost
Whole thread Raw
List pgsql-hackers-win32
On 13 Jun 2004 at 19:57, Bruce Momjian wrote:

>
> Yes, but consider that two threads could both call it, with one perhaps
> using the first value, and the second overwriting the first.  Obviously
> not something we want.
>

Every Windows DLL has (or can have) a DllMain function. This is called
everytime a Dll is attached to (loaded) by a process or thread. You can
put any one-time initialisation in this function and guarantee that it is
only called by the first process/thread that loads the dll.

BOOL WINAPI DllMain(
  HINSTANCE hinstDLL,
  DWORD fdwReason,
  LPVOID lpvReserved
);

Parameters:

hinstDLL
[in] Handle to the DLL module. The value is the base address of the
DLL. The HINSTANCE of a DLL is the same as the HMODULE of the
DLL, so hinstDLL can be used in calls to functions that require a
module handle.

fdwReason
[in] Indicates why the DLL entry-point function is being called. This
parameter can be one of the following values. Value Meaning

DLL_PROCESS_ATTACH The DLL is being loaded into the virtual
address space of the current process as a result of the process starting
up or as a result of a call to LoadLibrary. DLLs can use this opportunity
to initialize any instance data or to use the TlsAlloc function to allocate
a thread local storage (TLS) index.

DLL_THREAD_ATTACH The current process is creating a new thread.
When this occurs, the system calls the entry-point function of all DLLs
currently attached to the process. The call is made in the context of the
new thread. DLLs can use this opportunity to initialize a TLS slot for the
thread. A thread calling the DLL entry-point function with
DLL_PROCESS_ATTACH does not call the DLL entry-point function
with DLL_THREAD_ATTACH.
Note that a DLL's entry-point function is called with this value only by
threads created after the DLL is loaded by the process. When a DLL is
loaded using LoadLibrary, existing threads do not call the entry-point
function of the newly loaded DLL.

DLL_THREAD_DETACH A thread is exiting cleanly. If the DLL has
stored a pointer to allocated memory in a TLS slot, it should use this
opportunity to free the memory. The system calls the entry-point
function of all currently loaded DLLs with this value. The call is made in
the context of the exiting thread.

DLL_PROCESS_DETACH The DLL is being unloaded from the virtual
address space of the calling process as a result of unsuccessfully
loading the DLL, termination of the process, or a call to FreeLibrary.
The DLL can use this opportunity to call the TlsFree function to free any
TLS indices allocated by using TlsAlloc and to free any thread local
data.
Note that the thread that receives the DLL_PROCESS_DETACH
notification is not necessarily the same thread that received the
DLL_PROCESS_ATTACH notification.

lpvReserved
[in] If fdwReason is DLL_PROCESS_ATTACH, lpvReserved is NULL
for dynamic loads and non-NULL for static loads.
If fdwReason is DLL_PROCESS_DETACH, lpvReserved is NULL if
DllMain has been called by using FreeLibrary and non-NULL if DllMain
has been called during process termination.


Regards,
Gary


pgsql-hackers-win32 by date:

Previous
From: Tom Lane
Date:
Subject: Re: pg_ctl vs. Windows locking
Next
From: Thomas Kellerer
Date:
Subject: Re: pg_ctl vs. Windows locking