1/* Test program, used by the gettext-6 test.
2   Copyright (C) 2005-2006 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 2, or (at your option)
7   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, write to the Free Software Foundation,
16   Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.  */
17
18/* Written by Bruno Haible <haible@clisp.cons.org>, 2005.  */
19
20#ifdef HAVE_CONFIG_H
21# include <config.h>
22#endif
23
24#include <locale.h>
25#include <stdlib.h>
26#include <stdio.h>
27#include <string.h>
28
29#if USE_POSIX_THREADS && ((__GLIBC__ > 2) || (__GLIBC__ == 2 && __GLIBC_MINOR__ >= 3))
30
31#include <pthread.h>
32#include "setenv.h"
33
34/* Make sure we use the included libintl, not the system's one. */
35#undef _LIBINTL_H
36#include "libgnuintl.h"
37
38/* Set to 1 if the program is not behaving correctly.  */
39int result;
40
41/* Denotes which thread should run next.  */
42int flipflop;
43/* Lock and wait queue used to switch between the threads.  */
44pthread_mutex_t lock;
45pthread_cond_t waitqueue;
46
47/* Waits until the flipflop has a given value.
48   Before the call, the lock is unlocked.  After the call, it is locked.  */
49static void
50waitfor (int value)
51{
52  if (pthread_mutex_lock (&lock))
53    exit (10);
54  while (flipflop != value)
55    if (pthread_cond_wait (&waitqueue, &lock))
56      exit (11);
57}
58
59/* Sets the flipflop to a given value.
60   Before the call, the lock is locked.  After the call, it is unlocked.  */
61static void
62setto (int value)
63{
64  flipflop = value;
65  if (pthread_cond_signal (&waitqueue))
66    exit (20);
67  if (pthread_mutex_unlock (&lock))
68    exit (21);
69}
70
71void *
72thread1_execution (void *arg)
73{
74  char *s;
75
76  waitfor (1);
77  uselocale (newlocale (LC_ALL_MASK, "de_DE", NULL));
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 1 returned: %s\n", s);
86      result = 1;
87    }
88  setto (2);
89
90  waitfor (1);
91  s = gettext ("beauty");
92  puts (s);
93  if (strcmp (s, "Sch\366nheit"))
94    {
95      fprintf (stderr, "thread 1 call 2 returned: %s\n", s);
96      result = 1;
97    }
98  setto (2);
99
100  return NULL;
101}
102
103void *
104thread2_execution (void *arg)
105{
106  char *s;
107
108  waitfor (2);
109  uselocale (newlocale (LC_ALL_MASK, "fr_FR", NULL));
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 1 returned: %s\n", s);
118      result = 1;
119    }
120  setto (1);
121
122  waitfor (2);
123  s = gettext ("beauty");
124  puts (s);
125  if (strcmp (s, "beaut\351"))
126    {
127      fprintf (stderr, "thread 2 call 2 returned: %s\n", s);
128      result = 1;
129    }
130  setto (1);
131
132  return NULL;
133}
134
135int
136main (void)
137{
138  pthread_t thread1;
139  pthread_t thread2;
140
141  unsetenv ("LANGUAGE");
142  unsetenv ("OUTPUT_CHARSET");
143  textdomain ("tstthread");
144  bindtextdomain ("tstthread", ".");
145  result = 0;
146
147  flipflop = 1;
148  if (pthread_mutex_init (&lock, NULL))
149    exit (2);
150  if (pthread_cond_init (&waitqueue, NULL))
151    exit (2);
152  if (pthread_create (&thread1, NULL, &thread1_execution, NULL))
153    exit (2);
154  if (pthread_create (&thread2, NULL, &thread2_execution, NULL))
155    exit (2);
156  if (pthread_join (thread2, NULL))
157    exit (3);
158
159  return result;
160}
161
162#else
163
164/* This test is not executed.  */
165
166int
167main (void)
168{
169  return 77;
170}
171
172#endif
173