1/* Test program, used by the gettext-7 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 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 <http://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 && ((__GLIBC__ > 2) || (__GLIBC__ == 2 && __GLIBC_MINOR__ >= 3))
29
30#include <pthread.h>
31#include "setenv.h"
32
33/* Make sure we use the included libintl, not the system's one. */
34#undef _LIBINTL_H
35#include "libgnuintl.h"
36
37/* Set to 1 if the program is not behaving correctly.  */
38int result;
39
40/* Denotes which thread should run next.  */
41int flipflop;
42/* Lock and wait queue used to switch between the threads.  */
43pthread_mutex_t lock;
44pthread_cond_t waitqueue;
45
46/* Waits until the flipflop has a given value.
47   Before the call, the lock is unlocked.  After the call, it is locked.  */
48static void
49waitfor (int value)
50{
51  if (pthread_mutex_lock (&lock))
52    exit (10);
53  while (flipflop != value)
54    if (pthread_cond_wait (&waitqueue, &lock))
55      exit (11);
56}
57
58/* Sets the flipflop to a given value.
59   Before the call, the lock is locked.  After the call, it is unlocked.  */
60static void
61setto (int value)
62{
63  flipflop = value;
64  if (pthread_cond_signal (&waitqueue))
65    exit (20);
66  if (pthread_mutex_unlock (&lock))
67    exit (21);
68}
69
70void *
71thread1_execution (void *arg)
72{
73  char *s;
74
75  waitfor (1);
76  uselocale (newlocale (LC_ALL_MASK, "de_DE.ISO-8859-1", NULL));
77  setto (2);
78
79  /* Here we expect output in ISO-8859-1.  */
80
81  waitfor (1);
82  s = gettext ("cheese");
83  puts (s);
84  if (strcmp (s, "K\344se"))
85    {
86      fprintf (stderr, "thread 1 call 1 returned: %s\n", s);
87      result = 1;
88    }
89  setto (2);
90
91  waitfor (1);
92  s = gettext ("cheese");
93  puts (s);
94  if (strcmp (s, "K\344se"))
95    {
96      fprintf (stderr, "thread 1 call 2 returned: %s\n", s);
97      result = 1;
98    }
99  setto (2);
100
101  return NULL;
102}
103
104void *
105thread2_execution (void *arg)
106{
107  char *s;
108
109  waitfor (2);
110  uselocale (newlocale (LC_ALL_MASK, "de_DE.UTF-8", NULL));
111  setto (1);
112
113  /* Here we expect output in UTF-8.  */
114
115  waitfor (2);
116  s = gettext ("cheese");
117  puts (s);
118  if (strcmp (s, "K\303\244se"))
119    {
120      fprintf (stderr, "thread 2 call 1 returned: %s\n", s);
121      result = 1;
122    }
123  setto (1);
124
125  waitfor (2);
126  s = gettext ("cheese");
127  puts (s);
128  if (strcmp (s, "K\303\244se"))
129    {
130      fprintf (stderr, "thread 2 call 2 returned: %s\n", s);
131      result = 1;
132    }
133  setto (1);
134
135  return NULL;
136}
137
138int
139main (void)
140{
141  pthread_t thread1;
142  pthread_t thread2;
143
144  unsetenv ("LANGUAGE");
145  unsetenv ("OUTPUT_CHARSET");
146  textdomain ("tstthread");
147  bindtextdomain ("tstthread", ".");
148  result = 0;
149
150  flipflop = 1;
151  if (pthread_mutex_init (&lock, NULL))
152    exit (2);
153  if (pthread_cond_init (&waitqueue, NULL))
154    exit (2);
155  if (pthread_create (&thread1, NULL, &thread1_execution, NULL))
156    exit (2);
157  if (pthread_create (&thread2, NULL, &thread2_execution, NULL))
158    exit (2);
159  if (pthread_join (thread2, NULL))
160    exit (3);
161
162  return result;
163}
164
165#else
166
167/* This test is not executed.  */
168
169int
170main (void)
171{
172  return 77;
173}
174
175#endif
176