Blame intl/tst-gettext4.c

Packit 6c4009
/* Test that gettext() in multithreaded applications works correctly if
Packit 6c4009
   different threads operate in different locales with the same encoding.
Packit 6c4009
   Copyright (C) 2005-2018 Free Software Foundation, Inc.
Packit 6c4009
   This file is part of the GNU C Library.
Packit 6c4009
   Contributed by Bruno Haible <bruno@clisp.org>, 2005.
Packit 6c4009
Packit 6c4009
   The GNU C Library is free software; you can redistribute it and/or
Packit 6c4009
   modify it under the terms of the GNU Lesser General Public
Packit 6c4009
   License as published by the Free Software Foundation; either
Packit 6c4009
   version 2.1 of the License, or (at your option) any later version.
Packit 6c4009
Packit 6c4009
   The GNU C Library is distributed in the hope that it will be useful,
Packit 6c4009
   but WITHOUT ANY WARRANTY; without even the implied warranty of
Packit 6c4009
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Packit 6c4009
   Lesser General Public License for more details.
Packit 6c4009
Packit 6c4009
   You should have received a copy of the GNU Lesser General Public
Packit 6c4009
   License along with the GNU C Library; if not, see
Packit 6c4009
   <http://www.gnu.org/licenses/>.  */
Packit 6c4009
Packit 6c4009
#include <libintl.h>
Packit 6c4009
#include <locale.h>
Packit 6c4009
#include <pthread.h>
Packit 6c4009
#include <stdio.h>
Packit 6c4009
#include <stdlib.h>
Packit 6c4009
#include <string.h>
Packit 6c4009
Packit 6c4009
/* Set to 1 if the program is not behaving correctly.  */
Packit 6c4009
int result;
Packit 6c4009
Packit 6c4009
/* Denotes which thread should run next.  */
Packit 6c4009
int flipflop;
Packit 6c4009
/* Lock and wait queue used to switch between the threads.  */
Packit 6c4009
pthread_mutex_t lock;
Packit 6c4009
pthread_cond_t waitqueue;
Packit 6c4009
Packit 6c4009
/* Waits until the flipflop has a given value.
Packit 6c4009
   Before the call, the lock is unlocked.  After the call, it is locked.  */
Packit 6c4009
static void
Packit 6c4009
waitfor (int value)
Packit 6c4009
{
Packit 6c4009
  if (pthread_mutex_lock (&lock))
Packit 6c4009
    exit (10);
Packit 6c4009
  while (flipflop != value)
Packit 6c4009
    if (pthread_cond_wait (&waitqueue, &lock))
Packit 6c4009
      exit (11);
Packit 6c4009
}
Packit 6c4009
Packit 6c4009
/* Sets the flipflop to a given value.
Packit 6c4009
   Before the call, the lock is locked.  After the call, it is unlocked.  */
Packit 6c4009
static void
Packit 6c4009
setto (int value)
Packit 6c4009
{
Packit 6c4009
  flipflop = value;
Packit 6c4009
  if (pthread_cond_signal (&waitqueue))
Packit 6c4009
    exit (20);
Packit 6c4009
  if (pthread_mutex_unlock (&lock))
Packit 6c4009
    exit (21);
Packit 6c4009
}
Packit 6c4009
Packit 6c4009
void *
Packit 6c4009
thread1_execution (void *arg)
Packit 6c4009
{
Packit 6c4009
  char *s;
Packit 6c4009
Packit 6c4009
  waitfor (1);
Packit 6c4009
  uselocale (newlocale (LC_ALL_MASK, "de_DE.ISO-8859-1", NULL));
Packit 6c4009
  setto (2);
Packit 6c4009
Packit 6c4009
  waitfor (1);
Packit 6c4009
  s = gettext ("beauty");
Packit 6c4009
  puts (s);
Packit 6c4009
  if (strcmp (s, "Sch\366nheit"))
Packit 6c4009
    {
Packit 6c4009
      fprintf (stderr, "thread 1 call 1 returned: %s\n", s);
Packit 6c4009
      result = 1;
Packit 6c4009
    }
Packit 6c4009
  setto (2);
Packit 6c4009
Packit 6c4009
  waitfor (1);
Packit 6c4009
  s = gettext ("beauty");
Packit 6c4009
  puts (s);
Packit 6c4009
  if (strcmp (s, "Sch\366nheit"))
Packit 6c4009
    {
Packit 6c4009
      fprintf (stderr, "thread 1 call 2 returned: %s\n", s);
Packit 6c4009
      result = 1;
Packit 6c4009
    }
Packit 6c4009
  setto (2);
Packit 6c4009
Packit 6c4009
  return NULL;
Packit 6c4009
}
Packit 6c4009
Packit 6c4009
void *
Packit 6c4009
thread2_execution (void *arg)
Packit 6c4009
{
Packit 6c4009
  char *s;
Packit 6c4009
Packit 6c4009
  waitfor (2);
Packit 6c4009
  uselocale (newlocale (LC_ALL_MASK, "fr_FR.ISO-8859-1", NULL));
Packit 6c4009
  setto (1);
Packit 6c4009
Packit 6c4009
  waitfor (2);
Packit 6c4009
  s = gettext ("beauty");
Packit 6c4009
  puts (s);
Packit 6c4009
  if (strcmp (s, "beaut\351"))
Packit 6c4009
    {
Packit 6c4009
      fprintf (stderr, "thread 2 call 1 returned: %s\n", s);
Packit 6c4009
      result = 1;
Packit 6c4009
    }
Packit 6c4009
  setto (1);
Packit 6c4009
Packit 6c4009
  waitfor (2);
Packit 6c4009
  s = gettext ("beauty");
Packit 6c4009
  puts (s);
Packit 6c4009
  if (strcmp (s, "beaut\351"))
Packit 6c4009
    {
Packit 6c4009
      fprintf (stderr, "thread 2 call 2 returned: %s\n", s);
Packit 6c4009
      result = 1;
Packit 6c4009
    }
Packit 6c4009
  setto (1);
Packit 6c4009
Packit 6c4009
  return NULL;
Packit 6c4009
}
Packit 6c4009
Packit 6c4009
int
Packit 6c4009
main (void)
Packit 6c4009
{
Packit 6c4009
  pthread_t thread1;
Packit 6c4009
  pthread_t thread2;
Packit 6c4009
Packit 6c4009
  unsetenv ("LANGUAGE");
Packit 6c4009
  unsetenv ("OUTPUT_CHARSET");
Packit 6c4009
  textdomain ("multithread");
Packit 6c4009
  bindtextdomain ("multithread", OBJPFX "domaindir");
Packit 6c4009
  result = 0;
Packit 6c4009
Packit 6c4009
  flipflop = 1;
Packit 6c4009
  if (pthread_mutex_init (&lock, NULL))
Packit 6c4009
    exit (2);
Packit 6c4009
  if (pthread_cond_init (&waitqueue, NULL))
Packit 6c4009
    exit (2);
Packit 6c4009
  if (pthread_create (&thread1, NULL, &thread1_execution, NULL))
Packit 6c4009
    exit (2);
Packit 6c4009
  if (pthread_create (&thread2, NULL, &thread2_execution, NULL))
Packit 6c4009
    exit (2);
Packit 6c4009
  if (pthread_join (thread2, NULL))
Packit 6c4009
    exit (3);
Packit 6c4009
Packit 6c4009
  return result;
Packit 6c4009
}