Back to index

glibc  2.9
tst-gettext5.c
Go to the documentation of this file.
00001 /* Test that gettext() in multithreaded applications works correctly if
00002    different threads operate in different locales referring to the same
00003    catalog file but with different encodings.
00004    Copyright (C) 2005 Free Software Foundation, Inc.
00005    This file is part of the GNU C Library.
00006    Contributed by Bruno Haible <bruno@clisp.org>, 2005.
00007 
00008    The GNU C Library is free software; you can redistribute it and/or
00009    modify it under the terms of the GNU Lesser General Public
00010    License as published by the Free Software Foundation; either
00011    version 2.1 of the License, or (at your option) any later version.
00012 
00013    The GNU C Library is distributed in the hope that it will be useful,
00014    but WITHOUT ANY WARRANTY; without even the implied warranty of
00015    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00016    Lesser General Public License for more details.
00017 
00018    You should have received a copy of the GNU Lesser General Public
00019    License along with the GNU C Library; if not, write to the Free
00020    Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
00021    02111-1307 USA.  */
00022 
00023 #include <libintl.h>
00024 #include <locale.h>
00025 #include <pthread.h>
00026 #include <stdio.h>
00027 #include <stdlib.h>
00028 #include <string.h>
00029 
00030 /* Set to 1 if the program is not behaving correctly.  */
00031 int result;
00032 
00033 /* Denotes which thread should run next.  */
00034 int flipflop;
00035 /* Lock and wait queue used to switch between the threads.  */
00036 pthread_mutex_t lock;
00037 pthread_cond_t waitqueue;
00038 
00039 /* Waits until the flipflop has a given value.
00040    Before the call, the lock is unlocked.  After the call, it is locked.  */
00041 static void
00042 waitfor (int value)
00043 {
00044   if (pthread_mutex_lock (&lock))
00045     exit (10);
00046   while (flipflop != value)
00047     if (pthread_cond_wait (&waitqueue, &lock))
00048       exit (11);
00049 }
00050 
00051 /* Sets the flipflop to a given value.
00052    Before the call, the lock is locked.  After the call, it is unlocked.  */
00053 static void
00054 setto (int value)
00055 {
00056   flipflop = value;
00057   if (pthread_cond_signal (&waitqueue))
00058     exit (20);
00059   if (pthread_mutex_unlock (&lock))
00060     exit (21);
00061 }
00062 
00063 void *
00064 thread1_execution (void *arg)
00065 {
00066   char *s;
00067 
00068   waitfor (1);
00069   uselocale (newlocale (LC_ALL_MASK, "de_DE.ISO-8859-1", NULL));
00070   setto (2);
00071 
00072   /* Here we expect output in ISO-8859-1.  */
00073 
00074   waitfor (1);
00075   s = gettext ("cheese");
00076   puts (s);
00077   if (strcmp (s, "K\344se"))
00078     {
00079       fprintf (stderr, "thread 1 call 1 returned: %s\n", s);
00080       result = 1;
00081     }
00082   setto (2);
00083 
00084   waitfor (1);
00085   s = gettext ("cheese");
00086   puts (s);
00087   if (strcmp (s, "K\344se"))
00088     {
00089       fprintf (stderr, "thread 1 call 2 returned: %s\n", s);
00090       result = 1;
00091     }
00092   setto (2);
00093 
00094   return NULL;
00095 }
00096 
00097 void *
00098 thread2_execution (void *arg)
00099 {
00100   char *s;
00101 
00102   waitfor (2);
00103   uselocale (newlocale (LC_ALL_MASK, "de_DE.UTF-8", NULL));
00104   setto (1);
00105 
00106   /* Here we expect output in UTF-8.  */
00107 
00108   waitfor (2);
00109   s = gettext ("cheese");
00110   puts (s);
00111   if (strcmp (s, "K\303\244se"))
00112     {
00113       fprintf (stderr, "thread 2 call 1 returned: %s\n", s);
00114       result = 1;
00115     }
00116   setto (1);
00117 
00118   waitfor (2);
00119   s = gettext ("cheese");
00120   puts (s);
00121   if (strcmp (s, "K\303\244se"))
00122     {
00123       fprintf (stderr, "thread 2 call 2 returned: %s\n", s);
00124       result = 1;
00125     }
00126   setto (1);
00127 
00128   return NULL;
00129 }
00130 
00131 int
00132 main (void)
00133 {
00134   pthread_t thread1;
00135   pthread_t thread2;
00136 
00137   unsetenv ("LANGUAGE");
00138   unsetenv ("OUTPUT_CHARSET");
00139   textdomain ("codeset");
00140   bindtextdomain ("codeset", OBJPFX "domaindir");
00141   result = 0;
00142 
00143   flipflop = 1;
00144   if (pthread_mutex_init (&lock, NULL))
00145     exit (2);
00146   if (pthread_cond_init (&waitqueue, NULL))
00147     exit (2);
00148   if (pthread_create (&thread1, NULL, &thread1_execution, NULL))
00149     exit (2);
00150   if (pthread_create (&thread2, NULL, &thread2_execution, NULL))
00151     exit (2);
00152   if (pthread_join (thread2, NULL))
00153     exit (3);
00154 
00155   return result;
00156 }