(root)/
glibc-2.38/
sysdeps/
pthread/
tst-cond23.c
       1  /* Copyright (C) 2008-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 <errno.h>
      19  #include <pthread.h>
      20  #include <stdio.h>
      21  #include <time.h>
      22  #include <unistd.h>
      23  
      24  
      25  #if defined _POSIX_CLOCK_SELECTION && _POSIX_CLOCK_SELECTION >= 0
      26  static int
      27  check (pthread_condattr_t *condattr, int pshared, clockid_t cl)
      28  {
      29    clockid_t cl2;
      30    if (pthread_condattr_getclock (condattr, &cl2) != 0)
      31      {
      32        puts ("condattr_getclock failed");
      33        return 1;
      34      }
      35    if (cl != cl2)
      36      {
      37        printf ("condattr_getclock returned wrong value: %d, expected %d\n",
      38  	      (int) cl2, (int) cl);
      39        return 1;
      40      }
      41  
      42    int p;
      43    if (pthread_condattr_getpshared (condattr, &p) != 0)
      44      {
      45        puts ("condattr_getpshared failed");
      46        return 1;
      47      }
      48    else if (p != pshared)
      49      {
      50        printf ("condattr_getpshared returned wrong value: %d, expected %d\n",
      51  	      p, pshared);
      52        return 1;
      53      }
      54  
      55    return 0;
      56  }
      57  
      58  static int
      59  run_test (clockid_t cl)
      60  {
      61    pthread_condattr_t condattr;
      62  
      63    printf ("clock = %d\n", (int) cl);
      64  
      65    if (pthread_condattr_init (&condattr) != 0)
      66      {
      67        puts ("condattr_init failed");
      68        return 1;
      69      }
      70  
      71    if (check (&condattr, PTHREAD_PROCESS_PRIVATE, CLOCK_REALTIME))
      72      return 1;
      73  
      74    if (pthread_condattr_setpshared (&condattr, PTHREAD_PROCESS_SHARED) != 0)
      75      {
      76        puts ("1st condattr_setpshared failed");
      77        return 1;
      78      }
      79  
      80    if (check (&condattr, PTHREAD_PROCESS_SHARED, CLOCK_REALTIME))
      81      return 1;
      82  
      83    if (pthread_condattr_setclock (&condattr, cl) != 0)
      84      {
      85        puts ("1st condattr_setclock failed");
      86        return 1;
      87      }
      88  
      89    if (check (&condattr, PTHREAD_PROCESS_SHARED, cl))
      90      return 1;
      91  
      92    if (pthread_condattr_setpshared (&condattr, PTHREAD_PROCESS_PRIVATE) != 0)
      93      {
      94        puts ("2nd condattr_setpshared failed");
      95        return 1;
      96      }
      97  
      98    if (check (&condattr, PTHREAD_PROCESS_PRIVATE, cl))
      99      return 1;
     100  
     101    if (pthread_condattr_setclock (&condattr, CLOCK_REALTIME) != 0)
     102      {
     103        puts ("2nd condattr_setclock failed");
     104        return 1;
     105      }
     106  
     107    if (check (&condattr, PTHREAD_PROCESS_PRIVATE, CLOCK_REALTIME))
     108      return 1;
     109  
     110    if (pthread_condattr_setclock (&condattr, cl) != 0)
     111      {
     112        puts ("3rd condattr_setclock failed");
     113        return 1;
     114      }
     115  
     116    if (check (&condattr, PTHREAD_PROCESS_PRIVATE, cl))
     117      return 1;
     118  
     119    if (pthread_condattr_setpshared (&condattr, PTHREAD_PROCESS_SHARED) != 0)
     120      {
     121        puts ("3rd condattr_setpshared failed");
     122        return 1;
     123      }
     124  
     125    if (check (&condattr, PTHREAD_PROCESS_SHARED, cl))
     126      return 1;
     127  
     128    if (pthread_condattr_setclock (&condattr, CLOCK_REALTIME) != 0)
     129      {
     130        puts ("4th condattr_setclock failed");
     131        return 1;
     132      }
     133  
     134    if (check (&condattr, PTHREAD_PROCESS_SHARED, CLOCK_REALTIME))
     135      return 1;
     136  
     137    if (pthread_condattr_destroy (&condattr) != 0)
     138      {
     139        puts ("condattr_destroy failed");
     140        return 1;
     141      }
     142  
     143    return 0;
     144  }
     145  #endif
     146  
     147  
     148  static int
     149  do_test (void)
     150  {
     151  #if !defined _POSIX_CLOCK_SELECTION || _POSIX_CLOCK_SELECTION == -1
     152  
     153    puts ("_POSIX_CLOCK_SELECTION not supported, test skipped");
     154    return 0;
     155  
     156  #else
     157  
     158    int res = run_test (CLOCK_REALTIME);
     159  
     160  # if defined _POSIX_MONOTONIC_CLOCK && _POSIX_MONOTONIC_CLOCK >= 0
     161  #  if _POSIX_MONOTONIC_CLOCK == 0
     162    int e = sysconf (_SC_MONOTONIC_CLOCK);
     163    if (e < 0)
     164      puts ("CLOCK_MONOTONIC not supported");
     165    else if (e == 0)
     166      {
     167        puts ("sysconf (_SC_MONOTONIC_CLOCK) must not return 0");
     168        res = 1;
     169      }
     170    else
     171  #  endif
     172      res |= run_test (CLOCK_MONOTONIC);
     173  # else
     174    puts ("_POSIX_MONOTONIC_CLOCK not defined");
     175  # endif
     176  
     177    return res;
     178  #endif
     179  }
     180  
     181  #define TEST_FUNCTION do_test ()
     182  #include "../test-skeleton.c"