Merge pull request #2043 from RincewindsHat/cleanup/leftovers
[monitoring-plugins.git] / gl / nl_langinfo-lock.c
blob5a248ed8d1083b09a17f09f167aef8d386dafd65
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. */
19 #include <config.h>
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. */
28 typedef int dummy;
30 #else
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
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
52 # if defined _WIN32 && !defined __CYGWIN__
54 # define WIN32_LEAN_AND_MEAN /* avoid including junk */
55 # include <windows.h>
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. */
69 CRITICAL_SECTION *
70 gl_get_nl_langinfo_lock (void)
72 if (!guard.done)
74 if (InterlockedIncrement (&guard.started) == 0)
76 /* This thread is the first one to need the lock. Initialize it. */
77 InitializeCriticalSection (&lock);
78 guard.done = 1;
80 else
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);
90 return &lock;
93 # elif HAVE_PTHREAD_API
95 # include <pthread.h>
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. */
103 pthread_mutex_t *
104 gl_get_nl_langinfo_lock (void)
106 return &mutex;
109 # elif HAVE_THREADS_H
111 # include <threads.h>
112 # include <stdlib.h>
114 static int volatile init_needed = 1;
115 static once_flag init_once = ONCE_FLAG_INIT;
116 static mtx_t mutex;
118 static void
119 atomic_init (void)
121 if (mtx_init (&mutex, mtx_plain) != thrd_success)
122 abort ();
123 init_needed = 0;
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. */
130 mtx_t *
131 gl_get_nl_langinfo_lock (void)
133 if (init_needed)
134 call_once (&init_once, atomic_init);
135 return &mutex;
138 # endif
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
151 #endif