tst-gettext4.c 3.5 KB

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