1 /* Test program, used by the intl-thread-2 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
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
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 *
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 waitfor (1);
88 s = gettext ("beauty");
89 puts (s);
90 if (strcmp (s, "beaut\303\251"))
91 {
92 fprintf (stderr, "thread 1 call 1 returned: %s\n", s);
93 result = 1;
94 }
95 setto (2);
96
97 waitfor (1);
98 s = gettext ("beauty");
99 puts (s);
100 if (strcmp (s, "beaut\303\251"))
101 {
102 fprintf (stderr, "thread 1 call 2 returned: %s\n", s);
103 result = 1;
104 }
105 setto (2);
106
107 return NULL;
108 }
109
110 void *
111 thread2_execution (void *arg)
112 {
113 char *s;
114
115 waitfor (2);
116 uselocale (newlocale (LC_ALL_MASK, locale_name_2, NULL));
117 setto (1);
118
119 waitfor (2);
120 s = gettext ("beauty");
121 puts (s);
122 if (strcmp (s, "Sch\303\266nheit"))
123 {
124 fprintf (stderr, "thread 2 call 1 returned: %s\n", s);
125 result = 1;
126 }
127 setto (1);
128
129 waitfor (2);
130 s = gettext ("beauty");
131 puts (s);
132 if (strcmp (s, "Sch\303\266nheit"))
133 {
134 fprintf (stderr, "thread 2 call 2 returned: %s\n", s);
135 result = 1;
136 }
137 setto (1);
138
139 return NULL;
140 }
141
142 int
143 main (int argc, char *argv[])
144 {
145 pthread_t thread1;
146 pthread_t thread2;
147
148 locale_name_1 = argv[1];
149 locale_name_2 = argv[2];
150
151 unsetenv ("LANGUAGE");
152 unsetenv ("OUTPUT_CHARSET");
153 textdomain ("tstthread");
154 bindtextdomain ("tstthread", "in-th-2");
155 result = 0;
156
157 flipflop = 1;
158 if (pthread_mutex_init (&lock, NULL))
159 exit (2);
160 if (pthread_cond_init (&waitqueue, NULL))
161 exit (2);
162 if (pthread_create (&thread1, NULL, &thread1_execution, NULL))
163 exit (2);
164 if (pthread_create (&thread2, NULL, &thread2_execution, NULL))
165 exit (2);
166 if (pthread_join (thread2, NULL))
167 exit (3);
168
169 return result;
170 }
171
172 #else
173
174 /* This test is not executed. */
175
176 int
177 main (void)
178 {
179 return 77;
180 }
181
182 #endif