mbtowc-lock.h 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. /* Use the internal lock used by mbrtowc and mbrtoc32.
  2. Copyright (C) 2019-2021 Free Software Foundation, Inc.
  3. This program is free software: you can redistribute it and/or modify
  4. it under the terms of the GNU Lesser General Public License as published by
  5. the Free Software Foundation; either version 3 of the License, or
  6. (at your option) any later version.
  7. This program is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. GNU Lesser General Public License for more details.
  11. You should have received a copy of the GNU Lesser General Public License
  12. along with this program. If not, see <https://www.gnu.org/licenses/>. */
  13. /* Written by Bruno Haible <bruno@clisp.org>, 2019-2020. */
  14. /* Use a lock, so that no two threads can invoke mbtowc at the same time. */
  15. static inline int
  16. mbtowc_unlocked (wchar_t *pwc, const char *p, size_t m)
  17. {
  18. /* Put the hidden internal state of mbtowc into its initial state.
  19. This is needed at least with glibc, uClibc, and MSVC CRT.
  20. See <https://sourceware.org/bugzilla/show_bug.cgi?id=9674>. */
  21. mbtowc (NULL, NULL, 0);
  22. return mbtowc (pwc, p, m);
  23. }
  24. /* Prohibit renaming this symbol. */
  25. #undef gl_get_mbtowc_lock
  26. #if defined _WIN32 && !defined __CYGWIN__
  27. extern __declspec(dllimport) CRITICAL_SECTION *gl_get_mbtowc_lock (void);
  28. static int
  29. mbtowc_with_lock (wchar_t *pwc, const char *p, size_t m)
  30. {
  31. CRITICAL_SECTION *lock = gl_get_mbtowc_lock ();
  32. int ret;
  33. EnterCriticalSection (lock);
  34. ret = mbtowc_unlocked (pwc, p, m);
  35. LeaveCriticalSection (lock);
  36. return ret;
  37. }
  38. #elif HAVE_PTHREAD_API /* AIX, IRIX, Cygwin */
  39. extern
  40. # if defined _WIN32 || defined __CYGWIN__
  41. __declspec(dllimport)
  42. # endif
  43. pthread_mutex_t *gl_get_mbtowc_lock (void);
  44. # if HAVE_WEAK_SYMBOLS /* IRIX */
  45. /* Avoid the need to link with '-lpthread'. */
  46. # pragma weak pthread_mutex_lock
  47. # pragma weak pthread_mutex_unlock
  48. /* Determine whether libpthread is in use. */
  49. # pragma weak pthread_mutexattr_gettype
  50. /* See the comments in lock.h. */
  51. # define pthread_in_use() \
  52. (pthread_mutexattr_gettype != NULL || c11_threads_in_use ())
  53. # else
  54. # define pthread_in_use() 1
  55. # endif
  56. static int
  57. mbtowc_with_lock (wchar_t *pwc, const char *p, size_t m)
  58. {
  59. if (pthread_in_use())
  60. {
  61. pthread_mutex_t *lock = gl_get_mbtowc_lock ();
  62. int ret;
  63. if (pthread_mutex_lock (lock))
  64. abort ();
  65. ret = mbtowc_unlocked (pwc, p, m);
  66. if (pthread_mutex_unlock (lock))
  67. abort ();
  68. return ret;
  69. }
  70. else
  71. return mbtowc_unlocked (pwc, p, m);
  72. }
  73. #elif HAVE_THREADS_H
  74. extern mtx_t *gl_get_mbtowc_lock (void);
  75. static int
  76. mbtowc_with_lock (wchar_t *pwc, const char *p, size_t m)
  77. {
  78. mtx_t *lock = gl_get_mbtowc_lock ();
  79. int ret;
  80. if (mtx_lock (lock) != thrd_success)
  81. abort ();
  82. ret = mbtowc_unlocked (pwc, p, m);
  83. if (mtx_unlock (lock) != thrd_success)
  84. abort ();
  85. return ret;
  86. }
  87. #endif