tst-gettext4.c 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. /* Test that gettext() in multithreaded applications works correctly if
  2. different threads operate in different locales with the same encoding.
  3. Copyright (C) 2005-2026 Free Software Foundation, Inc.
  4. This file is part of the GNU C Library.
  5. The GNU C Library is free software; you can redistribute it and/or
  6. modify it under the terms of the GNU Lesser General Public
  7. License as published by the Free Software Foundation; either
  8. version 2.1 of the License, or (at your option) any later version.
  9. The GNU C Library 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 GNU
  12. Lesser General Public License for more details.
  13. You should have received a copy of the GNU Lesser General Public
  14. License along with the GNU C Library; if not, see
  15. <https://www.gnu.org/licenses/>. */
  16. #include <libintl.h>
  17. #include <locale.h>
  18. #include <pthread.h>
  19. #include <stdio.h>
  20. #include <stdlib.h>
  21. #include <string.h>
  22. /* Set to 1 if the program is not behaving correctly. */
  23. int result;
  24. /* Denotes which thread should run next. */
  25. int flipflop;
  26. /* Lock and wait queue used to switch between the threads. */
  27. pthread_mutex_t lock;
  28. pthread_cond_t waitqueue;
  29. /* Waits until the flipflop has a given value.
  30. Before the call, the lock is unlocked. After the call, it is locked. */
  31. static void
  32. waitfor (int value)
  33. {
  34. if (pthread_mutex_lock (&lock))
  35. exit (10);
  36. while (flipflop != value)
  37. if (pthread_cond_wait (&waitqueue, &lock))
  38. exit (11);
  39. }
  40. /* Sets the flipflop to a given value.
  41. Before the call, the lock is locked. After the call, it is unlocked. */
  42. static void
  43. setto (int value)
  44. {
  45. flipflop = value;
  46. if (pthread_cond_signal (&waitqueue))
  47. exit (20);
  48. if (pthread_mutex_unlock (&lock))
  49. exit (21);
  50. }
  51. void *
  52. thread1_execution (void *arg)
  53. {
  54. char *s;
  55. waitfor (1);
  56. uselocale (newlocale (LC_ALL_MASK, "de_DE.ISO-8859-1", NULL));
  57. setto (2);
  58. waitfor (1);
  59. s = gettext ("beauty");
  60. puts (s);
  61. if (strcmp (s, "Sch\366nheit"))
  62. {
  63. fprintf (stderr, "thread 1 call 1 returned: %s\n", s);
  64. result = 1;
  65. }
  66. setto (2);
  67. waitfor (1);
  68. s = gettext ("beauty");
  69. puts (s);
  70. if (strcmp (s, "Sch\366nheit"))
  71. {
  72. fprintf (stderr, "thread 1 call 2 returned: %s\n", s);
  73. result = 1;
  74. }
  75. setto (2);
  76. return NULL;
  77. }
  78. void *
  79. thread2_execution (void *arg)
  80. {
  81. char *s;
  82. waitfor (2);
  83. uselocale (newlocale (LC_ALL_MASK, "fr_FR.ISO-8859-1", NULL));
  84. setto (1);
  85. waitfor (2);
  86. s = gettext ("beauty");
  87. puts (s);
  88. if (strcmp (s, "beaut\351"))
  89. {
  90. fprintf (stderr, "thread 2 call 1 returned: %s\n", s);
  91. result = 1;
  92. }
  93. setto (1);
  94. waitfor (2);
  95. s = gettext ("beauty");
  96. puts (s);
  97. if (strcmp (s, "beaut\351"))
  98. {
  99. fprintf (stderr, "thread 2 call 2 returned: %s\n", s);
  100. result = 1;
  101. }
  102. setto (1);
  103. return NULL;
  104. }
  105. int
  106. main (void)
  107. {
  108. pthread_t thread1;
  109. pthread_t thread2;
  110. unsetenv ("LANGUAGE");
  111. unsetenv ("OUTPUT_CHARSET");
  112. textdomain ("multithread");
  113. bindtextdomain ("multithread", OBJPFX "domaindir");
  114. result = 0;
  115. flipflop = 1;
  116. if (pthread_mutex_init (&lock, NULL))
  117. exit (2);
  118. if (pthread_cond_init (&waitqueue, NULL))
  119. exit (2);
  120. if (pthread_create (&thread1, NULL, &thread1_execution, NULL))
  121. exit (2);
  122. if (pthread_create (&thread2, NULL, &thread2_execution, NULL))
  123. exit (2);
  124. if (pthread_join (thread2, NULL))
  125. exit (3);
  126. return result;
  127. }