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