2.5-18.1
[glibc.git] / intl / tst-gettext5.c
blob498ecab7904b2a382017026e4ff366c32a99b280
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.
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.
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.
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. */
23 #include <libintl.h>
24 #include <locale.h>
25 #include <pthread.h>
26 #include <stdio.h>
27 #include <stdlib.h>
28 #include <string.h>
30 /* Set to 1 if the program is not behaving correctly. */
31 int result;
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;
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)
44 if (pthread_mutex_lock (&lock))
45 exit (10);
46 while (flipflop != value)
47 if (pthread_cond_wait (&waitqueue, &lock))
48 exit (11);
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)
56 flipflop = value;
57 if (pthread_cond_signal (&waitqueue))
58 exit (20);
59 if (pthread_mutex_unlock (&lock))
60 exit (21);
63 void *
64 thread1_execution (void *arg)
66 char *s;
68 waitfor (1);
69 uselocale (newlocale (LC_ALL_MASK, "de_DE.ISO-8859-1", NULL));
70 setto (2);
72 /* Here we expect output in ISO-8859-1. */
74 waitfor (1);
75 s = gettext ("cheese");
76 puts (s);
77 if (strcmp (s, "K\344se"))
79 fprintf (stderr, "thread 1 call 1 returned: %s\n", s);
80 result = 1;
82 setto (2);
84 waitfor (1);
85 s = gettext ("cheese");
86 puts (s);
87 if (strcmp (s, "K\344se"))
89 fprintf (stderr, "thread 1 call 2 returned: %s\n", s);
90 result = 1;
92 setto (2);
94 return NULL;
97 void *
98 thread2_execution (void *arg)
100 char *s;
102 waitfor (2);
103 uselocale (newlocale (LC_ALL_MASK, "de_DE.UTF-8", NULL));
104 setto (1);
106 /* Here we expect output in UTF-8. */
108 waitfor (2);
109 s = gettext ("cheese");
110 puts (s);
111 if (strcmp (s, "K\303\244se"))
113 fprintf (stderr, "thread 2 call 1 returned: %s\n", s);
114 result = 1;
116 setto (1);
118 waitfor (2);
119 s = gettext ("cheese");
120 puts (s);
121 if (strcmp (s, "K\303\244se"))
123 fprintf (stderr, "thread 2 call 2 returned: %s\n", s);
124 result = 1;
126 setto (1);
128 return NULL;
132 main (void)
134 pthread_t thread1;
135 pthread_t thread2;
137 unsetenv ("LANGUAGE");
138 unsetenv ("OUTPUT_CHARSET");
139 textdomain ("codeset");
140 bindtextdomain ("codeset", OBJPFX "domaindir");
141 result = 0;
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);
155 return result;