chiark / gitweb /
Apply https://sourceware.org/git/?p=glibc.git;a=commit;h=d5dd6189d506068ed11c8bfa1e1e...
[eglibc.git] / intl / tst-gettext5.c
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 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, write to the Free
20    Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
21    02111-1307 USA.  */
22
23 #include <libintl.h>
24 #include <locale.h>
25 #include <pthread.h>
26 #include <stdio.h>
27 #include <stdlib.h>
28 #include <string.h>
29
30 /* Set to 1 if the program is not behaving correctly.  */
31 int result;
32
33 /* Denotes which thread should run next.  */
34 int flipflop;
35 /* Lock and wait queue used to switch between the threads.  */
36 pthread_mutex_t lock;
37 pthread_cond_t waitqueue;
38
39 /* Waits until the flipflop has a given value.
40    Before the call, the lock is unlocked.  After the call, it is locked.  */
41 static void
42 waitfor (int value)
43 {
44   if (pthread_mutex_lock (&lock))
45     exit (10);
46   while (flipflop != value)
47     if (pthread_cond_wait (&waitqueue, &lock))
48       exit (11);
49 }
50
51 /* Sets the flipflop to a given value.
52    Before the call, the lock is locked.  After the call, it is unlocked.  */
53 static void
54 setto (int value)
55 {
56   flipflop = value;
57   if (pthread_cond_signal (&waitqueue))
58     exit (20);
59   if (pthread_mutex_unlock (&lock))
60     exit (21);
61 }
62
63 void *
64 thread1_execution (void *arg)
65 {
66   char *s;
67
68   waitfor (1);
69   uselocale (newlocale (LC_ALL_MASK, "de_DE.ISO-8859-1", NULL));
70   setto (2);
71
72   /* Here we expect output in ISO-8859-1.  */
73
74   waitfor (1);
75   s = gettext ("cheese");
76   puts (s);
77   if (strcmp (s, "K\344se"))
78     {
79       fprintf (stderr, "thread 1 call 1 returned: %s\n", s);
80       result = 1;
81     }
82   setto (2);
83
84   waitfor (1);
85   s = gettext ("cheese");
86   puts (s);
87   if (strcmp (s, "K\344se"))
88     {
89       fprintf (stderr, "thread 1 call 2 returned: %s\n", s);
90       result = 1;
91     }
92   setto (2);
93
94   return NULL;
95 }
96
97 void *
98 thread2_execution (void *arg)
99 {
100   char *s;
101
102   waitfor (2);
103   uselocale (newlocale (LC_ALL_MASK, "de_DE.UTF-8", NULL));
104   setto (1);
105
106   /* Here we expect output in UTF-8.  */
107
108   waitfor (2);
109   s = gettext ("cheese");
110   puts (s);
111   if (strcmp (s, "K\303\244se"))
112     {
113       fprintf (stderr, "thread 2 call 1 returned: %s\n", s);
114       result = 1;
115     }
116   setto (1);
117
118   waitfor (2);
119   s = gettext ("cheese");
120   puts (s);
121   if (strcmp (s, "K\303\244se"))
122     {
123       fprintf (stderr, "thread 2 call 2 returned: %s\n", s);
124       result = 1;
125     }
126   setto (1);
127
128   return NULL;
129 }
130
131 int
132 main (void)
133 {
134   pthread_t thread1;
135   pthread_t thread2;
136
137   unsetenv ("LANGUAGE");
138   unsetenv ("OUTPUT_CHARSET");
139   textdomain ("codeset");
140   bindtextdomain ("codeset", OBJPFX "domaindir");
141   result = 0;
142
143   flipflop = 1;
144   if (pthread_mutex_init (&lock, NULL))
145     exit (2);
146   if (pthread_cond_init (&waitqueue, NULL))
147     exit (2);
148   if (pthread_create (&thread1, NULL, &thread1_execution, NULL))
149     exit (2);
150   if (pthread_create (&thread2, NULL, &thread2_execution, NULL))
151     exit (2);
152   if (pthread_join (thread2, NULL))
153     exit (3);
154
155   return result;
156 }