Обсуждение: thread-safety: gmtime_r(), localtime_r()

Поиск
Список
Период
Сортировка

thread-safety: gmtime_r(), localtime_r()

От
Peter Eisentraut
Дата:
Here is a patch for using gmtime_r() and localtime_r() instead of 
gmtime() and localtime(), for thread-safety.

There are a few affected calls in libpq and ecpg's libpgtypes, which are 
probably effectively bugs, because those libraries already claim to be 
thread-safe.

There is one affected call in the backend.  Most of the backend 
otherwise uses the custom functions pg_gmtime() and pg_localtime(), 
which are implemented differently.

Some portability fun: gmtime_r() and localtime_r() are in POSIX but are 
not available on Windows.  Windows has functions gmtime_s() and 
localtime_s() that can fulfill the same purpose, so we can add some 
small wrappers around them.  (Note that these *_s() functions are also
different from the *_s() functions in the bounds-checking extension of
C11.  We are not using those here.)

MinGW exposes neither *_r() nor *_s() by default.  You can get at the
POSIX-style *_r() functions by defining _POSIX_C_SOURCE appropriately
before including <time.h>.  (There is apparently probably also a way to 
get at the Windows-style *_s() functions by supplying some additional 
options or defines.  But we might as well just use the POSIX ones.)
Вложения

Re: thread-safety: gmtime_r(), localtime_r()

От
Stepan Neretin
Дата:

On Thu, Jun 27, 2024 at 1:42 AM Peter Eisentraut <peter@eisentraut.org> wrote:
Here is a patch for using gmtime_r() and localtime_r() instead of
gmtime() and localtime(), for thread-safety.

There are a few affected calls in libpq and ecpg's libpgtypes, which are
probably effectively bugs, because those libraries already claim to be
thread-safe.

There is one affected call in the backend.  Most of the backend
otherwise uses the custom functions pg_gmtime() and pg_localtime(),
which are implemented differently.

Some portability fun: gmtime_r() and localtime_r() are in POSIX but are
not available on Windows.  Windows has functions gmtime_s() and
localtime_s() that can fulfill the same purpose, so we can add some
small wrappers around them.  (Note that these *_s() functions are also
different from the *_s() functions in the bounds-checking extension of
C11.  We are not using those here.)

MinGW exposes neither *_r() nor *_s() by default.  You can get at the
POSIX-style *_r() functions by defining _POSIX_C_SOURCE appropriately
before including <time.h>.  (There is apparently probably also a way to
get at the Windows-style *_s() functions by supplying some additional
options or defines.  But we might as well just use the POSIX ones.)


Hi! Looks good to me.
But why you don`t change localtime function at all places? 
For example:
src/bin/pg_controldata/pg_controldata.c
src/bin/pg_dump/pg_backup_archiver.c
src/bin/initdb/findtimezone.c
Best regards, Stepan Neretin.
 

Re: thread-safety: gmtime_r(), localtime_r()

От
Peter Eisentraut
Дата:
On 27.06.24 06:47, Stepan Neretin wrote:
> Hi! Looks good to me.
> But why you don`t change localtime function at all places?
> For example:
> src/bin/pg_controldata/pg_controldata.c
> src/bin/pg_dump/pg_backup_archiver.c
> src/bin/initdb/findtimezone.c

At the moment, I am focusing on the components that are already meant to 
be thread-safe (libpq, ecpg libs) and the ones we are actively looking 
at maybe converting (backend).  I don't intend at this point to convert 
all other code to use only thread-safe APIs.