(root)/
glibc-2.38/
sysdeps/
pthread/
tst-cond-except.c
       1  /* Verify that exception table for pthread_cond_wait is correct.
       2     Copyright (C) 2012-2023 Free Software Foundation, Inc.
       3     This file is part of the GNU C Library.
       4  
       5     The GNU C Library is free software; you can redistribute it and/or
       6     modify it under the terms of the GNU Lesser General Public
       7     License as published by the Free Software Foundation; either
       8     version 2.1 of the License, or (at your option) any later version.
       9  
      10     The GNU C Library is distributed in the hope that it will be useful,
      11     but WITHOUT ANY WARRANTY; without even the implied warranty of
      12     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
      13     Lesser General Public License for more details.
      14  
      15     You should have received a copy of the GNU Lesser General Public
      16     License along with the GNU C Library; if not, see
      17     <https://www.gnu.org/licenses/>.  */
      18  
      19  #include <pthread.h>
      20  #include <stdio.h>
      21  #include <stdint.h>
      22  #include <string.h>
      23  #include <unistd.h>
      24  
      25  pthread_mutex_t mutex;
      26  pthread_cond_t cond;
      27  
      28  #define CHECK_RETURN_VAL_OR_FAIL(ret,str) \
      29    ({ if ((ret) != 0) \
      30         { \
      31           printf ("%s failed: %s\n", (str), strerror (ret)); \
      32           ret = 1; \
      33           goto out; \
      34         } \
      35    })
      36  
      37  
      38  void
      39  clean (void *arg)
      40  {
      41    puts ("clean: Unlocking mutex...");
      42    pthread_mutex_unlock ((pthread_mutex_t *) arg);
      43    puts ("clean: Mutex unlocked...");
      44  }
      45  
      46  void *
      47  thr (void *arg)
      48  {
      49    int ret = 0;
      50    pthread_mutexattr_t mutexAttr;
      51    ret = pthread_mutexattr_init (&mutexAttr);
      52    CHECK_RETURN_VAL_OR_FAIL (ret, "pthread_mutexattr_init");
      53  
      54    ret = pthread_mutexattr_setprotocol (&mutexAttr, PTHREAD_PRIO_INHERIT);
      55    CHECK_RETURN_VAL_OR_FAIL (ret, "pthread_mutexattr_setprotocol");
      56  
      57    ret = pthread_mutex_init (&mutex, &mutexAttr);
      58    CHECK_RETURN_VAL_OR_FAIL (ret, "pthread_mutex_init");
      59  
      60    ret = pthread_cond_init (&cond, 0);
      61    CHECK_RETURN_VAL_OR_FAIL (ret, "pthread_cond_init");
      62  
      63    puts ("th: Init done, entering wait...");
      64  
      65    pthread_cleanup_push (clean, (void *) &mutex);
      66    ret = pthread_mutex_lock (&mutex);
      67    CHECK_RETURN_VAL_OR_FAIL (ret, "pthread_mutex_lock");
      68    while (1)
      69      {
      70        ret = pthread_cond_wait (&cond, &mutex);
      71        CHECK_RETURN_VAL_OR_FAIL (ret, "pthread_cond_wait");
      72      }
      73    pthread_cleanup_pop (1);
      74  
      75  out:
      76    return (void *) (uintptr_t) ret;
      77  }
      78  
      79  int
      80  do_test (void)
      81  {
      82    pthread_t thread;
      83    int ret = 0;
      84    void *thr_ret = 0;
      85    ret = pthread_create (&thread, 0, thr, &thr_ret);
      86    CHECK_RETURN_VAL_OR_FAIL (ret, "pthread_create");
      87  
      88    puts ("main: Thread created, waiting a bit...");
      89    sleep (2);
      90  
      91    puts ("main: Cancelling thread...");
      92    ret = pthread_cancel (thread);
      93    CHECK_RETURN_VAL_OR_FAIL (ret, "pthread_cancel");
      94  
      95    puts ("main: Joining th...");
      96    ret = pthread_join (thread, NULL);
      97    CHECK_RETURN_VAL_OR_FAIL (ret, "pthread_join");
      98  
      99    if (thr_ret != NULL)
     100      return 1;
     101  
     102    puts ("main: Joined thread, done!");
     103  
     104  out:
     105    return ret;
     106  }
     107  
     108  #define TEST_FUNCTION do_test ()
     109  #include "../test-skeleton.c"