Back to index

glibc  2.9
tst-join3.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 <sys/time.h>
00026 
00027 
00028 static pthread_mutex_t lock = PTHREAD_MUTEX_INITIALIZER;
00029 
00030 
00031 static void *
00032 tf (void *arg)
00033 {
00034   if (pthread_mutex_lock (&lock) != 0)
00035     {
00036       puts ("child: mutex_lock failed");
00037       return NULL;
00038     }
00039 
00040   return (void *) 42l;
00041 }
00042 
00043 
00044 static int
00045 do_test (void)
00046 {
00047   pthread_t th;
00048 
00049   if (pthread_mutex_lock (&lock) != 0)
00050     {
00051       puts ("mutex_lock failed");
00052       exit (1);
00053     }
00054 
00055   if (pthread_create (&th, NULL, tf, NULL) != 0)
00056     {
00057       puts ("mutex_create failed");
00058       exit (1);
00059     }
00060 
00061   void *status;
00062   struct timespec ts;
00063   struct timeval tv;
00064   (void) gettimeofday (&tv, NULL);
00065   TIMEVAL_TO_TIMESPEC (&tv, &ts);
00066   ts.tv_nsec += 200000000;
00067   if (ts.tv_nsec >= 1000000000)
00068     {
00069       ts.tv_nsec -= 1000000000;
00070       ++ts.tv_sec;
00071     }
00072   int val = pthread_timedjoin_np (th, &status, &ts);
00073   if (val == 0)
00074     {
00075       puts ("1st timedjoin succeeded");
00076       exit (1);
00077     }
00078   else if (val != ETIMEDOUT)
00079     {
00080       puts ("1st timedjoin didn't return ETIMEDOUT");
00081       exit (1);
00082     }
00083 
00084   if (pthread_mutex_unlock (&lock) != 0)
00085     {
00086       puts ("mutex_unlock failed");
00087       exit (1);
00088     }
00089 
00090   while (1)
00091     {
00092       (void) gettimeofday (&tv, NULL);
00093       TIMEVAL_TO_TIMESPEC (&tv, &ts);
00094       ts.tv_nsec += 200000000;
00095       if (ts.tv_nsec >= 1000000000)
00096        {
00097          ts.tv_nsec -= 1000000000;
00098          ++ts.tv_sec;
00099        }
00100 
00101       val = pthread_timedjoin_np (th, &status, &ts);
00102       if (val == 0)
00103        break;
00104 
00105       if (val != ETIMEDOUT)
00106        {
00107          printf ("timedjoin returned %s (%d), expected only 0 or ETIMEDOUT\n",
00108                 strerror (val), val);
00109          exit (1);
00110        }
00111     }
00112 
00113   if (status != (void *) 42l)
00114     {
00115       printf ("return value %p, expected %p\n", status, (void *) 42l);
00116       exit (1);
00117     }
00118 
00119   return 0;
00120 }
00121 
00122 #define TEST_FUNCTION do_test ()
00123 #include "../test-skeleton.c"