1 /* Return the internal lock used by nl_langinfo.
2 Copyright (C) 2019-2024 Free Software Foundation, Inc.
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.
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.
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/>. */
17 /* Written by Bruno Haible <bruno@clisp.org>, 2019-2020. */
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
26 /* This declaration is solely to ensure that after preprocessing
27 this file is never empty. */
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. */
36 /* Prohibit renaming this symbol. */
37 # undef gl_get_nl_langinfo_lock
39 /* Macro for exporting a symbol (function, not variable) defined in this file,
40 when compiled into a shared library. */
41 # ifndef SHLIB_EXPORTED
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)
48 # define SHLIB_EXPORTED
52 # if defined _WIN32 && !defined __CYGWIN__
54 # define WIN32_LEAN_AND_MEAN /* avoid including junk */
57 # include "windows-initguard.h"
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. */
62 /* Make sure the function gets exported from DLLs. */
63 SHLIB_EXPORTED CRITICAL_SECTION
*gl_get_nl_langinfo_lock (void);
65 static glwthread_initguard_t guard
= GLWTHREAD_INITGUARD_INIT
;
66 static CRITICAL_SECTION lock
;
68 /* Returns the internal lock used by nl_langinfo. */
70 gl_get_nl_langinfo_lock (void)
74 if (InterlockedIncrement (&guard
.started
) == 0)
76 /* This thread is the first one to need the lock. Initialize it. */
77 InitializeCriticalSection (&lock
);
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. */
93 # elif HAVE_PTHREAD_API
97 static pthread_mutex_t mutex
= PTHREAD_MUTEX_INITIALIZER
;
99 /* Make sure the function gets exported from shared libraries. */
100 SHLIB_EXPORTED pthread_mutex_t
*gl_get_nl_langinfo_lock (void);
102 /* Returns the internal lock used by nl_langinfo. */
104 gl_get_nl_langinfo_lock (void)
109 # elif HAVE_THREADS_H
111 # include <threads.h>
114 static int volatile init_needed
= 1;
115 static once_flag init_once
= ONCE_FLAG_INIT
;
121 if (mtx_init (&mutex
, mtx_plain
) != thrd_success
)
126 /* Make sure the function gets exported from shared libraries. */
127 SHLIB_EXPORTED mtx_t
*gl_get_nl_langinfo_lock (void);
129 /* Returns the internal lock used by nl_langinfo. */
131 gl_get_nl_langinfo_lock (void)
134 call_once (&init_once
, atomic_init
);
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
146 # define IMP(x) _imp__##x
148 void * IMP(gl_get_nl_langinfo_lock
) = &gl_get_nl_langinfo_lock
;