(root)/
glibc-2.38/
sysdeps/
pthread/
tst-cond14.c
       1  /* Copyright (C) 2004-2023 Free Software Foundation, Inc.
       2     This file is part of the GNU C Library.
       3  
       4     The GNU C Library is free software; you can redistribute it and/or
       5     modify it under the terms of the GNU Lesser General Public
       6     License as published by the Free Software Foundation; either
       7     version 2.1 of the License, or (at your option) any later version.
       8  
       9     The GNU C Library 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 GNU
      12     Lesser General Public License for more details.
      13  
      14     You should have received a copy of the GNU Lesser General Public
      15     License along with the GNU C Library; if not, see
      16     <https://www.gnu.org/licenses/>.  */
      17  
      18  #include <pthread.h>
      19  #include <stdio.h>
      20  #include <stdlib.h>
      21  #include <string.h>
      22  #include <unistd.h>
      23  
      24  
      25  static pthread_cond_t cond = PTHREAD_COND_INITIALIZER;
      26  static pthread_mutex_t mut = PTHREAD_RECURSIVE_MUTEX_INITIALIZER_NP;
      27  static pthread_mutex_t mut2 = PTHREAD_MUTEX_INITIALIZER;
      28  
      29  static void *
      30  tf (void *p)
      31  {
      32    if (pthread_mutex_lock (&mut) != 0)
      33      {
      34        printf ("%s: 1st mutex_lock failed\n", __func__);
      35        exit (1);
      36      }
      37    if (pthread_mutex_lock (&mut) != 0)
      38      {
      39        printf ("%s: 2nd mutex_lock failed\n", __func__);
      40        exit (1);
      41      }
      42    if (pthread_mutex_lock (&mut) != 0)
      43      {
      44        printf ("%s: 3rd mutex_lock failed\n", __func__);
      45        exit (1);
      46      }
      47  
      48    if (pthread_mutex_unlock (&mut2) != 0)
      49      {
      50        printf ("%s: mutex_unlock failed\n", __func__);
      51        exit (1);
      52      }
      53  
      54    if (pthread_cond_wait (&cond, &mut) != 0)
      55      {
      56        printf ("%s: cond_wait failed\n", __func__);
      57        exit (1);
      58      }
      59  
      60    puts ("child: done");
      61  
      62    return NULL;
      63  }
      64  
      65  
      66  static int
      67  do_test (void)
      68  {
      69    if (pthread_mutex_lock (&mut2) != 0)
      70      {
      71        puts ("1st mutex_lock failed");
      72        return 1;
      73      }
      74  
      75    puts ("parent: create child");
      76  
      77    pthread_t th;
      78    int err = pthread_create (&th, NULL, tf, NULL);
      79    if (err != 0)
      80      {
      81        printf ("parent: cannot create thread: %s\n", strerror (err));
      82        return 1;
      83      }
      84  
      85    /* We have to synchronize with the child.  */
      86    if (pthread_mutex_lock (&mut2) != 0)
      87      {
      88        puts ("2nd mutex_lock failed");
      89        return 1;
      90      }
      91  
      92    /* Give the child to reach to pthread_cond_wait.  */
      93    sleep (1);
      94  
      95    if (pthread_cond_signal (&cond) != 0)
      96      {
      97        puts ("cond_signal failed");
      98        return 1;
      99      }
     100  
     101    err = pthread_join (th, NULL);
     102    if (err != 0)
     103      {
     104        printf ("parent: failed to join: %s\n", strerror (err));
     105        return 1;
     106      }
     107  
     108    puts ("done");
     109  
     110    return 0;
     111  }
     112  
     113  
     114  #define TEST_FUNCTION do_test ()
     115  #include "../test-skeleton.c"