Re: [PATCHES] Compiling libpq with VisualC

From: "Magnus Hagander" <mha(at)sollentuna(dot)net>
To: "Manfred Spraul" <manfred(at)colorfullife(dot)com>, <pgsql(at)mohawksoft(dot)com>
Cc: "Bruce Momjian" <pgman(at)candle(dot)pha(dot)pa(dot)us>, "Andreas Pflug" <pgadmin(at)pse-consulting(dot)de>, "PostgreSQL Win32 port list" <pgsql-hackers-win32(at)postgresql(dot)org>, "PostgreSQL-development" <pgsql-hackers(at)postgresql(dot)org>
Subject: Re: [PATCHES] Compiling libpq with VisualC
Date: 2004-06-14 09:07:49
Message-ID: 6BCB9D8A16AC4241919521715F4D8BCE34BC53@algol.sollentuna.se
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-hackers pgsql-hackers-win32

> >>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

Responses

Browse pgsql-hackers by date

  From Date Subject
Next Message Andreas Pflug 2004-06-14 09:28:48 Re: [pgsql-hackers-win32] [PATCHES] Compiling libpq with
Previous Message Mark Kirkwood 2004-06-14 08:52:25 Re: I just got it: PostgreSQL Application Server -- a

Browse pgsql-hackers-win32 by date

  From Date Subject
Next Message Andreas Pflug 2004-06-14 09:28:48 Re: [pgsql-hackers-win32] [PATCHES] Compiling libpq with
Previous Message Magnus Hagander 2004-06-14 07:41:57 Re: Installer