Back to index

glibc  2.9
tst-cond5.c
Go to the documentation of this file.
00001 /* Copyright (C) 2002 Free Software Foundation, Inc.
00002    This file is part of the GNU C Library.
00003    Contributed by Ulrich Drepper <drepper@redhat.com>, 2002.
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 <string.h>
00025 #include <time.h>
00026 #include <sys/time.h>
00027 
00028 
00029 static pthread_mutex_t mut;
00030 static pthread_cond_t cond = PTHREAD_COND_INITIALIZER;
00031 
00032 
00033 static int
00034 do_test (void)
00035 {
00036   pthread_mutexattr_t ma;
00037   int err;
00038   struct timespec ts;
00039   struct timeval tv;
00040 
00041   if (pthread_mutexattr_init (&ma) != 0)
00042     {
00043       puts ("mutexattr_init failed");
00044       exit (1);
00045     }
00046 
00047   if (pthread_mutexattr_settype (&ma, PTHREAD_MUTEX_ERRORCHECK) != 0)
00048     {
00049       puts ("mutexattr_settype failed");
00050       exit (1);
00051     }
00052 
00053   if (pthread_mutex_init (&mut, &ma) != 0)
00054     {
00055       puts ("mutex_init failed");
00056       exit (1);
00057     }
00058 
00059   /* Get the mutex.  */
00060   if (pthread_mutex_lock (&mut) != 0)
00061     {
00062       puts ("mutex_lock failed");
00063       exit (1);
00064     }
00065 
00066   /* Waiting for the condition will fail.  But we want the timeout here.  */
00067   if (gettimeofday (&tv, NULL) != 0)
00068     {
00069       puts ("gettimeofday failed");
00070       exit (1);
00071     }
00072 
00073   TIMEVAL_TO_TIMESPEC (&tv, &ts);
00074   ts.tv_nsec += 500000000;
00075   if (ts.tv_nsec >= 1000000000)
00076     {
00077       ts.tv_nsec -= 1000000000;
00078       ++ts.tv_sec;
00079     }
00080   err = pthread_cond_timedwait (&cond, &mut, &ts);
00081   if (err == 0)
00082     {
00083       /* This could in theory happen but here without any signal and
00084         additional waiter it should not.  */
00085       puts ("cond_timedwait succeeded");
00086       exit (1);
00087     }
00088   else if (err != ETIMEDOUT)
00089     {
00090       printf ("cond_timedwait returned with %s\n", strerror (err));
00091       exit (1);
00092     }
00093 
00094   err = pthread_mutex_unlock (&mut);
00095   if (err != 0)
00096     {
00097       printf ("mutex_unlock failed: %s\n", strerror (err));
00098       exit (1);
00099     }
00100 
00101   return 0;
00102 }
00103 
00104 
00105 #define TEST_FUNCTION do_test ()
00106 #include "../test-skeleton.c"