• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /* Test program, used by the intl-thread-3 test.
2    Copyright (C) 2005-2007, 2009-2010, 2013, 2018-2019 Free Software Foundation, Inc.
3 
4    This program is free software: you can redistribute it and/or modify
5    it under the terms of the GNU General Public License as published by
6    the Free Software Foundation; either version 3 of the License, or
7    (at your option) any later version.
8 
9    This program is distributed in the hope that it will be useful,
10    but WITHOUT ANY WARRANTY; without even the implied warranty of
11    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12    GNU General Public License for more details.
13 
14    You should have received a copy of the GNU General Public License
15    along with this program.  If not, see <https://www.gnu.org/licenses/>.  */
16 
17 /* Written by Bruno Haible <haible@clisp.cons.org>, 2005.  */
18 
19 #ifdef HAVE_CONFIG_H
20 # include <config.h>
21 #endif
22 
23 #include <locale.h>
24 #include <stdlib.h>
25 #include <stdio.h>
26 #include <string.h>
27 
28 #if USE_POSIX_THREADS && HAVE_WORKING_USELOCALE
29 
30 #include <pthread.h>
31 
32 #if USE_SYSTEM_LIBINTL
33 # include <libintl.h>
34 #else
35 /* Make sure we use the included libintl, not the system's one. */
36 # undef _LIBINTL_H
37 # include "libgnuintl.h"
38 #endif
39 
40 /* Name of locale to use in thread1.  */
41 const char *locale_name_1;
42 /* Name of locale to use in thread2.  */
43 const char *locale_name_2;
44 
45 /* Set to 1 if the program is not behaving correctly.  */
46 int result;
47 
48 /* Denotes which thread should run next.  */
49 int flipflop;
50 /* Lock and wait queue used to switch between the threads.  */
51 pthread_mutex_t lock;
52 pthread_cond_t waitqueue;
53 
54 /* Waits until the flipflop has a given value.
55    Before the call, the lock is unlocked.  After the call, it is locked.  */
56 static void
waitfor(int value)57 waitfor (int value)
58 {
59   if (pthread_mutex_lock (&lock))
60     exit (10);
61   while (flipflop != value)
62     if (pthread_cond_wait (&waitqueue, &lock))
63       exit (11);
64 }
65 
66 /* Sets the flipflop to a given value.
67    Before the call, the lock is locked.  After the call, it is unlocked.  */
68 static void
setto(int value)69 setto (int value)
70 {
71   flipflop = value;
72   if (pthread_cond_signal (&waitqueue))
73     exit (20);
74   if (pthread_mutex_unlock (&lock))
75     exit (21);
76 }
77 
78 void *
thread1_execution(void * arg)79 thread1_execution (void *arg)
80 {
81   char *s;
82 
83   waitfor (1);
84   uselocale (newlocale (LC_ALL_MASK, locale_name_1, NULL));
85   setto (2);
86 
87   /* Here we expect output in ISO-8859-1.  */
88 
89   waitfor (1);
90   s = gettext ("cheese");
91   puts (s);
92   if (strcmp (s, "K\344se"))
93     {
94       fprintf (stderr, "thread 1 call 1 returned: %s\n", s);
95       result = 1;
96     }
97   setto (2);
98 
99   waitfor (1);
100   s = gettext ("cheese");
101   puts (s);
102   if (strcmp (s, "K\344se"))
103     {
104       fprintf (stderr, "thread 1 call 2 returned: %s\n", s);
105       result = 1;
106     }
107   setto (2);
108 
109   return NULL;
110 }
111 
112 void *
thread2_execution(void * arg)113 thread2_execution (void *arg)
114 {
115   char *s;
116 
117   waitfor (2);
118   uselocale (newlocale (LC_ALL_MASK, locale_name_2, NULL));
119   setto (1);
120 
121   /* Here we expect output in UTF-8.  */
122 
123   waitfor (2);
124   s = gettext ("cheese");
125   puts (s);
126   if (strcmp (s, "K\303\244se"))
127     {
128       fprintf (stderr, "thread 2 call 1 returned: %s\n", s);
129       result = 1;
130     }
131   setto (1);
132 
133   waitfor (2);
134   s = gettext ("cheese");
135   puts (s);
136   if (strcmp (s, "K\303\244se"))
137     {
138       fprintf (stderr, "thread 2 call 2 returned: %s\n", s);
139       result = 1;
140     }
141   setto (1);
142 
143   return NULL;
144 }
145 
146 int
main(int argc,char * argv[])147 main (int argc, char *argv[])
148 {
149   pthread_t thread1;
150   pthread_t thread2;
151 
152   locale_name_1 = argv[1];
153   locale_name_2 = argv[2];
154 
155   unsetenv ("LANGUAGE");
156   unsetenv ("OUTPUT_CHARSET");
157   textdomain ("tstthread");
158   bindtextdomain ("tstthread", "in-th-3");
159   result = 0;
160 
161   flipflop = 1;
162   if (pthread_mutex_init (&lock, NULL))
163     exit (2);
164   if (pthread_cond_init (&waitqueue, NULL))
165     exit (2);
166   if (pthread_create (&thread1, NULL, &thread1_execution, NULL))
167     exit (2);
168   if (pthread_create (&thread2, NULL, &thread2_execution, NULL))
169     exit (2);
170   if (pthread_join (thread2, NULL))
171     exit (3);
172 
173   return result;
174 }
175 
176 #else
177 
178 /* This test is not executed.  */
179 
180 int
main(void)181 main (void)
182 {
183   return 77;
184 }
185 
186 #endif
187