| 1 |
/* Return the internal lock used by nl_langinfo. |
| 2 |
Copyright (C) 2019-2024 Free Software Foundation, Inc. |
| 3 |
|
| 4 |
This file is free software: you can redistribute it and/or modify |
| 5 |
it under the terms of the GNU Lesser General Public License as |
| 6 |
published by the Free Software Foundation; either version 2.1 of the |
| 7 |
License, or (at your option) any later version. |
| 8 |
|
| 9 |
This file is distributed in the hope that it will be useful, |
| 10 |
but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 |
GNU Lesser General Public License for more details. |
| 13 |
|
| 14 |
You should have received a copy of the GNU Lesser General Public License |
| 15 |
along with this program. If not, see <https://www.gnu.org/licenses/>. */ |
| 16 |
|
| 17 |
/* Written by Bruno Haible <[email protected]>, 2019-2020. */ |
| 18 |
|
| 19 |
#include <config.h> |
| 20 |
|
| 21 |
/* The option '--disable-threads' explicitly requests no locking. */ |
| 22 |
/* When it is known that the gl_get_nl_langinfo_lock function is defined |
| 23 |
by a dependency library, it should not be defined here. */ |
| 24 |
#if AVOID_ANY_THREADS || OMIT_NL_LANGINFO_LOCK |
| 25 |
|
| 26 |
/* This declaration is solely to ensure that after preprocessing |
| 27 |
this file is never empty. */ |
| 28 |
typedef int dummy; |
| 29 |
|
| 30 |
#else |
| 31 |
|
| 32 |
/* This file defines the internal lock used by nl_langinfo. |
| 33 |
It is a separate compilation unit, so that only one copy of it is |
| 34 |
present when linking statically. */ |
| 35 |
|
| 36 |
/* Prohibit renaming this symbol. */ |
| 37 |
# undef gl_get_nl_langinfo_lock |
| 38 |
|
| 39 |
/* Macro for exporting a symbol (function, not variable) defined in this file, |
| 40 |
when compiled into a shared library. */ |
| 41 |
# ifndef SHLIB_EXPORTED |
| 42 |
# if HAVE_VISIBILITY |
| 43 |
/* Override the effect of the compiler option '-fvisibility=hidden'. */ |
| 44 |
# define SHLIB_EXPORTED __attribute__((__visibility__("default"))) |
| 45 |
# elif defined _WIN32 || defined __CYGWIN__ |
| 46 |
# define SHLIB_EXPORTED __declspec(dllexport) |
| 47 |
# else |
| 48 |
# define SHLIB_EXPORTED |
| 49 |
# endif |
| 50 |
# endif |
| 51 |
|
| 52 |
# if defined _WIN32 && !defined __CYGWIN__ |
| 53 |
|
| 54 |
# define WIN32_LEAN_AND_MEAN /* avoid including junk */ |
| 55 |
# include <windows.h> |
| 56 |
|
| 57 |
# include "windows-initguard.h" |
| 58 |
|
| 59 |
/* The return type is a 'CRITICAL_SECTION *', not a 'glwthread_mutex_t *', |
| 60 |
because the latter is not guaranteed to be a stable ABI in the future. */ |
| 61 |
|
| 62 |
/* Make sure the function gets exported from DLLs. */ |
| 63 |
SHLIB_EXPORTED CRITICAL_SECTION *gl_get_nl_langinfo_lock (void); |
| 64 |
|
| 65 |
static glwthread_initguard_t guard = GLWTHREAD_INITGUARD_INIT; |
| 66 |
static CRITICAL_SECTION lock; |
| 67 |
|
| 68 |
/* Returns the internal lock used by nl_langinfo. */ |
| 69 |
CRITICAL_SECTION * |
| 70 |
gl_get_nl_langinfo_lock (void) |
| 71 |
{ |
| 72 |
if (!guard.done) |
| 73 |
{ |
| 74 |
if (InterlockedIncrement (&guard.started) == 0) |
| 75 |
{ |
| 76 |
/* This thread is the first one to need the lock. Initialize it. */ |
| 77 |
InitializeCriticalSection (&lock); |
| 78 |
guard.done = 1; |
| 79 |
} |
| 80 |
else |
| 81 |
{ |
| 82 |
/* Don't let guard.started grow and wrap around. */ |
| 83 |
InterlockedDecrement (&guard.started); |
| 84 |
/* Yield the CPU while waiting for another thread to finish |
| 85 |
initializing this mutex. */ |
| 86 |
while (!guard.done) |
| 87 |
Sleep (0); |
| 88 |
} |
| 89 |
} |
| 90 |
return &lock; |
| 91 |
} |
| 92 |
|
| 93 |
# elif HAVE_PTHREAD_API |
| 94 |
|
| 95 |
# include <pthread.h> |
| 96 |
|
| 97 |
static pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER; |
| 98 |
|
| 99 |
/* Make sure the function gets exported from shared libraries. */ |
| 100 |
SHLIB_EXPORTED pthread_mutex_t *gl_get_nl_langinfo_lock (void); |
| 101 |
|
| 102 |
/* Returns the internal lock used by nl_langinfo. */ |
| 103 |
pthread_mutex_t * |
| 104 |
gl_get_nl_langinfo_lock (void) |
| 105 |
{ |
| 106 |
return &mutex; |
| 107 |
} |
| 108 |
|
| 109 |
# elif HAVE_THREADS_H |
| 110 |
|
| 111 |
# include <threads.h> |
| 112 |
# include <stdlib.h> |
| 113 |
|
| 114 |
static int volatile init_needed = 1; |
| 115 |
static once_flag init_once = ONCE_FLAG_INIT; |
| 116 |
static mtx_t mutex; |
| 117 |
|
| 118 |
static void |
| 119 |
atomic_init (void) |
| 120 |
{ |
| 121 |
if (mtx_init (&mutex, mtx_plain) != thrd_success) |
| 122 |
abort (); |
| 123 |
init_needed = 0; |
| 124 |
} |
| 125 |
|
| 126 |
/* Make sure the function gets exported from shared libraries. */ |
| 127 |
SHLIB_EXPORTED mtx_t *gl_get_nl_langinfo_lock (void); |
| 128 |
|
| 129 |
/* Returns the internal lock used by nl_langinfo. */ |
| 130 |
mtx_t * |
| 131 |
gl_get_nl_langinfo_lock (void) |
| 132 |
{ |
| 133 |
if (init_needed) |
| 134 |
call_once (&init_once, atomic_init); |
| 135 |
return &mutex; |
| 136 |
} |
| 137 |
|
| 138 |
# endif |
| 139 |
|
| 140 |
# if (defined _WIN32 || defined __CYGWIN__) && !defined _MSC_VER |
| 141 |
/* Make sure the '__declspec(dllimport)' in nl_langinfo.c does not cause |
| 142 |
a link failure when no DLLs are involved. */ |
| 143 |
# if defined _WIN64 || defined _LP64 |
| 144 |
# define IMP(x) __imp_##x |
| 145 |
# else |
| 146 |
# define IMP(x) _imp__##x |
| 147 |
# endif |
| 148 |
void * IMP(gl_get_nl_langinfo_lock) = &gl_get_nl_langinfo_lock; |
| 149 |
# endif |
| 150 |
|
| 151 |
#endif |