Back to index

glibc  2.9
tst-cond23.c
Go to the documentation of this file.
00001 /* Copyright (C) 2008 Free Software Foundation, Inc.
00002    This file is part of the GNU C Library.
00003    Contributed by Jakub Jelinek <jakub@redhat.com>, 2008.
00004 
00005    The GNU C Library is free software; you can redistribute it and/or
00006    modify it under the terms of the GNU Lesser General Public
00007    License as published by the Free Software Foundation; either
00008    version 2.1 of the License, or (at your option) any later version.
00009 
00010    The GNU C Library is distributed in the hope that it will be useful,
00011    but WITHOUT ANY WARRANTY; without even the implied warranty of
00012    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00013    Lesser General Public License for more details.
00014 
00015    You should have received a copy of the GNU Lesser General Public
00016    License along with the GNU C Library; if not, write to the Free
00017    Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
00018    02111-1307 USA.  */
00019 
00020 #include <errno.h>
00021 #include <pthread.h>
00022 #include <stdio.h>
00023 #include <time.h>
00024 #include <unistd.h>
00025 
00026 
00027 #if defined _POSIX_CLOCK_SELECTION && _POSIX_CLOCK_SELECTION >= 0
00028 static int
00029 check (pthread_condattr_t *condattr, int pshared, clockid_t cl)
00030 {
00031   clockid_t cl2;
00032   if (pthread_condattr_getclock (condattr, &cl2) != 0)
00033     {
00034       puts ("condattr_getclock failed");
00035       return 1;
00036     }
00037   if (cl != cl2)
00038     {
00039       printf ("condattr_getclock returned wrong value: %d, expected %d\n",
00040              (int) cl2, (int) cl);
00041       return 1;
00042     }
00043 
00044   int p;
00045   if (pthread_condattr_getpshared (condattr, &p) != 0)
00046     {
00047       puts ("condattr_getpshared failed");
00048       return 1;
00049     }
00050   else if (p != pshared)
00051     {
00052       printf ("condattr_getpshared returned wrong value: %d, expected %d\n",
00053              p, pshared);
00054       return 1;
00055     }
00056 
00057   return 0;
00058 }
00059 
00060 static int
00061 run_test (clockid_t cl)
00062 {
00063   pthread_condattr_t condattr;
00064 
00065   printf ("clock = %d\n", (int) cl);
00066 
00067   if (pthread_condattr_init (&condattr) != 0)
00068     {
00069       puts ("condattr_init failed");
00070       return 1;
00071     }
00072 
00073   if (check (&condattr, PTHREAD_PROCESS_PRIVATE, CLOCK_REALTIME))
00074     return 1;
00075 
00076   if (pthread_condattr_setpshared (&condattr, PTHREAD_PROCESS_SHARED) != 0)
00077     {
00078       puts ("1st condattr_setpshared failed");
00079       return 1;
00080     }
00081 
00082   if (check (&condattr, PTHREAD_PROCESS_SHARED, CLOCK_REALTIME))
00083     return 1;
00084 
00085   if (pthread_condattr_setclock (&condattr, cl) != 0)
00086     {
00087       puts ("1st condattr_setclock failed");
00088       return 1;
00089     }
00090 
00091   if (check (&condattr, PTHREAD_PROCESS_SHARED, cl))
00092     return 1;
00093 
00094   if (pthread_condattr_setpshared (&condattr, PTHREAD_PROCESS_PRIVATE) != 0)
00095     {
00096       puts ("2nd condattr_setpshared failed");
00097       return 1;
00098     }
00099 
00100   if (check (&condattr, PTHREAD_PROCESS_PRIVATE, cl))
00101     return 1;
00102 
00103   if (pthread_condattr_setclock (&condattr, CLOCK_REALTIME) != 0)
00104     {
00105       puts ("2nd condattr_setclock failed");
00106       return 1;
00107     }
00108 
00109   if (check (&condattr, PTHREAD_PROCESS_PRIVATE, CLOCK_REALTIME))
00110     return 1;
00111 
00112   if (pthread_condattr_setclock (&condattr, cl) != 0)
00113     {
00114       puts ("3rd condattr_setclock failed");
00115       return 1;
00116     }
00117 
00118   if (check (&condattr, PTHREAD_PROCESS_PRIVATE, cl))
00119     return 1;
00120 
00121   if (pthread_condattr_setpshared (&condattr, PTHREAD_PROCESS_SHARED) != 0)
00122     {
00123       puts ("3rd condattr_setpshared failed");
00124       return 1;
00125     }
00126 
00127   if (check (&condattr, PTHREAD_PROCESS_SHARED, cl))
00128     return 1;
00129 
00130   if (pthread_condattr_setclock (&condattr, CLOCK_REALTIME) != 0)
00131     {
00132       puts ("4th condattr_setclock failed");
00133       return 1;
00134     }
00135 
00136   if (check (&condattr, PTHREAD_PROCESS_SHARED, CLOCK_REALTIME))
00137     return 1;
00138 
00139   if (pthread_condattr_destroy (&condattr) != 0)
00140     {
00141       puts ("condattr_destroy failed");
00142       return 1;
00143     }
00144 
00145   return 0;
00146 }
00147 #endif
00148 
00149 
00150 static int
00151 do_test (void)
00152 {
00153 #if !defined _POSIX_CLOCK_SELECTION || _POSIX_CLOCK_SELECTION == -1
00154 
00155   puts ("_POSIX_CLOCK_SELECTION not supported, test skipped");
00156   return 0;
00157 
00158 #else
00159 
00160   int res = run_test (CLOCK_REALTIME);
00161 
00162 # if defined _POSIX_MONOTONIC_CLOCK && _POSIX_MONOTONIC_CLOCK >= 0
00163 #  if _POSIX_MONOTONIC_CLOCK == 0
00164   int e = sysconf (_SC_MONOTONIC_CLOCK);
00165   if (e < 0)
00166     puts ("CLOCK_MONOTONIC not supported");
00167   else if (e == 0)
00168     {
00169       puts ("sysconf (_SC_MONOTONIC_CLOCK) must not return 0");
00170       res = 1;
00171     }
00172   else
00173 #  endif
00174     res |= run_test (CLOCK_MONOTONIC);
00175 # else
00176   puts ("_POSIX_MONOTONIC_CLOCK not defined");
00177 # endif
00178 
00179   return res;
00180 #endif
00181 }
00182 
00183 #define TEST_FUNCTION do_test ()
00184 #include "../test-skeleton.c"