Debian Codegraph @

lib/mbtowc-lock.h

raw
1 /* Use the internal lock used by mbrtowc and mbrtoc32.
2 Copyright (C) 2019-2026 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 /* Use a lock, so that no two threads can invoke mbtowc at the same time. */
20
21 static inline int
22 mbtowc_unlocked (wchar_t *pwc, const char *p, size_t m)
23 {
24 /* Put the hidden internal state of mbtowc into an initial state.
25 This is needed at least with glibc, uClibc, and MSVC CRT.
26 See <https://sourceware.org/PR9674>. */
27 mbtowc (NULL, NULL, 0);
28
29 return mbtowc (pwc, p, m);
30 }
31
32 /* Prohibit renaming this symbol. */
33 #undef gl_get_mbtowc_lock
34
35 #if AVOID_ANY_THREADS || GNULIB_MBRTOWC_SINGLE_THREAD
36
37 /* All uses of this function are in a single thread. No locking needed. */
38
39 static int
40 mbtowc_with_lock (wchar_t *pwc, const char *p, size_t m)
41 {
42 return mbtowc_unlocked (pwc, p, m);
43 }
44
45 #elif defined _WIN32 && !defined __CYGWIN__
46
47 extern __declspec(dllimport) CRITICAL_SECTION *gl_get_mbtowc_lock (void);
48
49 static int
50 mbtowc_with_lock (wchar_t *pwc, const char *p, size_t m)
51 {
52 CRITICAL_SECTION *lock = gl_get_mbtowc_lock ();
53
54 EnterCriticalSection (lock);
55 int ret = mbtowc_unlocked (pwc, p, m);
56 LeaveCriticalSection (lock);
57
58 return ret;
59 }
60
61 #elif HAVE_PTHREAD_API /* AIX, Cygwin */
62
63 extern
64 # if defined _WIN32 || defined __CYGWIN__
65 __declspec(dllimport)
66 # endif
67 pthread_mutex_t *gl_get_mbtowc_lock (void);
68
69 # if HAVE_WEAK_SYMBOLS
70
71 /* Avoid the need to link with '-lpthread'. */
72 # pragma weak pthread_mutex_lock
73 # pragma weak pthread_mutex_unlock
74
75 /* Determine whether libpthread is in use. */
76 # pragma weak pthread_mutexattr_gettype
77 /* See the comments in lock.h. */
78 # define pthread_in_use() \
79 (pthread_mutexattr_gettype != NULL || c11_threads_in_use ())
80
81 # else
82 # define pthread_in_use() 1
83 # endif
84
85 static int
86 mbtowc_with_lock (wchar_t *pwc, const char *p, size_t m)
87 {
88 if (pthread_in_use())
89 {
90 pthread_mutex_t *lock = gl_get_mbtowc_lock ();
91
92 if (pthread_mutex_lock (lock))
93 abort ();
94 int ret = mbtowc_unlocked (pwc, p, m);
95 if (pthread_mutex_unlock (lock))
96 abort ();
97
98 return ret;
99 }
100 else
101 return mbtowc_unlocked (pwc, p, m);
102 }
103
104 #elif HAVE_THREADS_H
105
106 extern mtx_t *gl_get_mbtowc_lock (void);
107
108 static int
109 mbtowc_with_lock (wchar_t *pwc, const char *p, size_t m)
110 {
111 mtx_t *lock = gl_get_mbtowc_lock ();
112
113 if (mtx_lock (lock) != thrd_success)
114 abort ();
115 int ret = mbtowc_unlocked (pwc, p, m);
116 if (mtx_unlock (lock) != thrd_success)
117 abort ();
118
119 return ret;
120 }
121
122 #endif