Back to index

glibc  2.9
tst-cond11.c
Go to the documentation of this file.
00001 /* Copyright (C) 2003, 2004 Free Software Foundation, Inc.
00002    This file is part of the GNU C Library.
00003    Contributed by Ulrich Drepper <drepper@redhat.com>, 2003.
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 run_test (clockid_t cl)
00030 {
00031   pthread_condattr_t condattr;
00032   pthread_cond_t cond;
00033   pthread_mutexattr_t mutattr;
00034   pthread_mutex_t mut;
00035 
00036   printf ("clock = %d\n", (int) cl);
00037 
00038   if (pthread_condattr_init (&condattr) != 0)
00039     {
00040       puts ("condattr_init failed");
00041       return 1;
00042     }
00043 
00044   if (pthread_condattr_setclock (&condattr, cl) != 0)
00045     {
00046       puts ("condattr_setclock failed");
00047       return 1;
00048     }
00049 
00050   clockid_t cl2;
00051   if (pthread_condattr_getclock (&condattr, &cl2) != 0)
00052     {
00053       puts ("condattr_getclock failed");
00054       return 1;
00055     }
00056   if (cl != cl2)
00057     {
00058       printf ("condattr_getclock returned wrong value: %d, expected %d\n",
00059              (int) cl2, (int) cl);
00060       return 1;
00061     }
00062 
00063   if (pthread_cond_init (&cond, &condattr) != 0)
00064     {
00065       puts ("cond_init failed");
00066       return 1;
00067     }
00068 
00069   if (pthread_condattr_destroy (&condattr) != 0)
00070     {
00071       puts ("condattr_destroy failed");
00072       return 1;
00073     }
00074 
00075   if (pthread_mutexattr_init (&mutattr) != 0)
00076     {
00077       puts ("mutexattr_init failed");
00078       return 1;
00079     }
00080 
00081   if (pthread_mutexattr_settype (&mutattr, PTHREAD_MUTEX_ERRORCHECK) != 0)
00082     {
00083       puts ("mutexattr_settype failed");
00084       return 1;
00085     }
00086 
00087   if (pthread_mutex_init (&mut, &mutattr) != 0)
00088     {
00089       puts ("mutex_init failed");
00090       return 1;
00091     }
00092 
00093   if (pthread_mutexattr_destroy (&mutattr) != 0)
00094     {
00095       puts ("mutexattr_destroy failed");
00096       return 1;
00097     }
00098 
00099   if (pthread_mutex_lock (&mut) != 0)
00100     {
00101       puts ("mutex_lock failed");
00102       return 1;
00103     }
00104 
00105   if (pthread_mutex_lock (&mut) != EDEADLK)
00106     {
00107       puts ("2nd mutex_lock did not return EDEADLK");
00108       return 1;
00109     }
00110 
00111   struct timespec ts;
00112   if (clock_gettime (cl, &ts) != 0)
00113     {
00114       puts ("clock_gettime failed");
00115       return 1;
00116     }
00117 
00118   /* Wait one second.  */
00119   ++ts.tv_sec;
00120 
00121   int e = pthread_cond_timedwait (&cond, &mut, &ts);
00122   if (e == 0)
00123     {
00124       puts ("cond_timedwait succeeded");
00125       return 1;
00126     }
00127   else if (e != ETIMEDOUT)
00128     {
00129       puts ("cond_timedwait did not return ETIMEDOUT");
00130       return 1;
00131     }
00132 
00133   if (pthread_mutex_unlock (&mut) != 0)
00134     {
00135       puts ("mutex_unlock failed");
00136       return 1;
00137     }
00138 
00139   if (pthread_mutex_destroy (&mut) != 0)
00140     {
00141       puts ("mutex_destroy failed");
00142       return 1;
00143     }
00144 
00145   if (pthread_cond_destroy (&cond) != 0)
00146     {
00147       puts ("cond_destroy failed");
00148       return 1;
00149     }
00150 
00151   return 0;
00152 }
00153 #endif
00154 
00155 
00156 static int
00157 do_test (void)
00158 {
00159 #if !defined _POSIX_CLOCK_SELECTION || _POSIX_CLOCK_SELECTION == -1
00160 
00161   puts ("_POSIX_CLOCK_SELECTION not supported, test skipped");
00162   return 0;
00163 
00164 #else
00165 
00166   int res = run_test (CLOCK_REALTIME);
00167 
00168 # if defined _POSIX_MONOTONIC_CLOCK && _POSIX_MONOTONIC_CLOCK >= 0
00169 #  if _POSIX_MONOTONIC_CLOCK == 0
00170   int e = sysconf (_SC_MONOTONIC_CLOCK);
00171   if (e < 0)
00172     puts ("CLOCK_MONOTONIC not supported");
00173   else if (e == 0)
00174     {
00175       puts ("sysconf (_SC_MONOTONIC_CLOCK) must not return 0");
00176       res = 1;
00177     }
00178   else
00179 #  endif
00180     res |= run_test (CLOCK_MONOTONIC);
00181 # else
00182   puts ("_POSIX_MONOTONIC_CLOCK not defined");
00183 # endif
00184 
00185   return res;
00186 #endif
00187 }
00188 
00189 #define TIMEOUT 3
00190 #define TEST_FUNCTION do_test ()
00191 #include "../test-skeleton.c"