Re: [PATCHES] Compiling libpq with VisualC
От | Magnus Hagander |
---|---|
Тема | Re: [PATCHES] Compiling libpq with VisualC |
Дата | |
Msg-id | 6BCB9D8A16AC4241919521715F4D8BCE34BC53@algol.sollentuna.se обсуждение исходный текст |
Ответы |
Re: [pgsql-hackers-win32] [PATCHES] Compiling libpq with
|
Список | pgsql-hackers |
> >>What is the recommended way to create mutex objects > (CreateMutex) from > >>Win32 libraries? There must be a clean way like there is > in pthreads. > >> > >> > > > >A mutex is inherently a global object. CreateMutex(NULL, > FALSE, NULL) > >will return a handle to an unowned mutex. > > > > > > > That's not the problem. Under pthread, it's possible to > initialize a mutex from compile time: > > static pthread_mutex_t init_mutex = PTHREAD_MUTEX_INITIALIZER; > > This means that the mutex is immediately valid, no races with > the initialization. I couldn't find an equivalent Win32 feature. AFAIK, there is no such thing on Win32. The clean way is probably to rqeuire the library to export a function InitialyzeFooLibrary() that does it (like Winsock does with requiring WSAStartup()). To do something like it though, you can use a named mutex. Then doing, in pseudocode: if (CreateMutex(...,"my_unique_mutex_name") == ERROR_ALREADY_EXISTS) OpenMutex(...,"my_unique_mutex_name") Assuming nobody closes the mutex between your attempt to create and open (which shouldn't happen if you just ignore closing it until process exit), this should be safe. Store the HANDLE to the Mutex in TLS, and have each thread do the create/open when it needs the mutex (e.g. wrap the wait on the mutex in a function/macro that will create/open the mutex if it's INVALID_HANDLE_VALUE, which you assign it to by default). You need a unique name for the mutex, since it's not per-process but per-sessino. But that can easily be constructed from the pid. //Magnus
В списке pgsql-hackers по дате отправления: