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