Back to index

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