Back to index

glibc  2.9
tst-cond9.c
Go to the documentation of this file.
00001 /* Copyright (C) 2003 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 <stdlib.h>
00024 #include <time.h>
00025 #include <sys/time.h>
00026 
00027 
00028 static pthread_cond_t cond = PTHREAD_COND_INITIALIZER;
00029 static pthread_mutex_t mut = PTHREAD_ERRORCHECK_MUTEX_INITIALIZER_NP;
00030 
00031 
00032 static void *
00033 tf (void *arg)
00034 {
00035   int err = pthread_cond_wait (&cond, &mut);
00036   if (err == 0)
00037     {
00038       puts ("cond_wait did not fail");
00039       exit (1);
00040     }
00041 
00042   if (err != EPERM)
00043     {
00044       printf ("cond_wait didn't return EPERM but %d\n", err);
00045       exit (1);
00046     }
00047 
00048 
00049   /* Current time.  */
00050   struct timeval tv;
00051   (void) gettimeofday (&tv, NULL);
00052   /* +1000 seconds in correct format.  */
00053   struct timespec ts;
00054   TIMEVAL_TO_TIMESPEC (&tv, &ts);
00055   ts.tv_sec += 1000;
00056 
00057   err = pthread_cond_timedwait (&cond, &mut, &ts);
00058   if (err == 0)
00059     {
00060       puts ("cond_timedwait did not fail");
00061       exit (1);
00062     }
00063 
00064   if (err != EPERM)
00065     {
00066       printf ("cond_timedwait didn't return EPERM but %d\n", err);
00067       exit (1);
00068     }
00069 
00070   return (void *) 1l;
00071 }
00072 
00073 
00074 static int
00075 do_test (void)
00076 {
00077   pthread_t th;
00078   int err;
00079 
00080   printf ("&cond = %p\n&mut = %p\n", &cond, &mut);
00081 
00082   err = pthread_cond_wait (&cond, &mut);
00083   if (err == 0)
00084     {
00085       puts ("cond_wait did not fail");
00086       exit (1);
00087     }
00088 
00089   if (err != EPERM)
00090     {
00091       printf ("cond_wait didn't return EPERM but %d\n", err);
00092       exit (1);
00093     }
00094 
00095 
00096   /* Current time.  */
00097   struct timeval tv;
00098   (void) gettimeofday (&tv, NULL);
00099   /* +1000 seconds in correct format.  */
00100   struct timespec ts;
00101   TIMEVAL_TO_TIMESPEC (&tv, &ts);
00102   ts.tv_sec += 1000;
00103 
00104   err = pthread_cond_timedwait (&cond, &mut, &ts);
00105   if (err == 0)
00106     {
00107       puts ("cond_timedwait did not fail");
00108       exit (1);
00109     }
00110 
00111   if (err != EPERM)
00112     {
00113       printf ("cond_timedwait didn't return EPERM but %d\n", err);
00114       exit (1);
00115     }
00116 
00117   if (pthread_mutex_lock (&mut) != 0)
00118     {
00119       puts ("parent: mutex_lock failed");
00120       exit (1);
00121     }
00122 
00123   puts ("creating thread");
00124 
00125   if (pthread_create (&th, NULL, tf, NULL) != 0)
00126     {
00127       puts ("create failed");
00128       exit (1);
00129     }
00130 
00131   void *r;
00132   if (pthread_join (th, &r) != 0)
00133     {
00134       puts ("join failed");
00135       exit (1);
00136     }
00137   if (r != (void *) 1l)
00138     {
00139       puts ("thread has wrong return value");
00140       exit (1);
00141     }
00142 
00143   puts ("done");
00144 
00145   return 0;
00146 }
00147 
00148 
00149 #define TEST_FUNCTION do_test ()
00150 #include "../test-skeleton.c"