tst-gettext5.c 3.6 KB

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