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