Skip to content

Commit

Permalink
Add locale macro to wrap static-space-using fncs
Browse files Browse the repository at this point in the history
Some functions return a result in a global-to-the-program buffer, or
they have an internal global buffer.  Other threads must be kept from
simultaneously using that function.  This macro is to be used for all
such ones dealing with locales.  Ideally, there would be a separate mutex
for each such buffer space.  But these functions also have to lock the
locale from changing during their execution, and there aren't that many
such functions, and they actually are rarely executed.  So a single lock
will do.

This will allow future commits to have more targeted locking for
functions that don't affect the global locale.
  • Loading branch information
khwilliamson committed May 6, 2021
1 parent fd736b6 commit ccff49d
Showing 1 changed file with 24 additions and 0 deletions.
24 changes: 24 additions & 0 deletions perl.h
Expand Up @@ -6853,6 +6853,30 @@ the plain locale pragma without a parameter (S<C<use locale>>) is in effect.
# endif
#endif

#ifndef LOCALE_LOCK_
# define LOCALE_LOCK_(cond) NOOP
# define LOCALE_UNLOCK_ NOOP
#endif

/* There are some locale-related functions which may need locking only because
* they share some common space across threads, and hence there is the
* potential for a race in accessing that space. Most are because their return
* points to a global static buffer, but some just use some common space
* internally. All functions accessing a given space need to have a critical
* section to prevent any other thread from accessing it at the same time.
* Ideally, there would be a separate mutex for each such space, so that
* another thread isn't unnecessarily blocked. But, most of them need to be
* locked against the locale changing while accessing that space, and it is not
* expected that any will be called frequently, and the locked interval should
* be short, and modern platforms will have reentrant versions (which don't
* lock) for almost all of them, so khw thinks a single mutex should suffice.
* Having a single mutex facilitates that, avoiding potential deadlock
* situations.
*
* This will be a no-op iff the perl is unthreaded. */
#define gwLOCALE_LOCK LOCALE_LOCK_(0)
#define gwLOCALE_UNLOCK LOCALE_UNLOCK_

#ifndef LC_NUMERIC_LOCK
# define LC_NUMERIC_LOCK(cond) NOOP
# define LC_NUMERIC_UNLOCK NOOP
Expand Down

0 comments on commit ccff49d

Please sign in to comment.