Back to index

glibc  2.9
tst-join2.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 
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   int val = pthread_tryjoin_np (th, &status);
00063   if (val == 0)
00064     {
00065       puts ("1st tryjoin succeeded");
00066       exit (1);
00067     }
00068   else if (val != EBUSY)
00069     {
00070       puts ("1st tryjoin didn't return EBUSY");
00071       exit (1);
00072     }
00073 
00074   if (pthread_mutex_unlock (&lock) != 0)
00075     {
00076       puts ("mutex_unlock failed");
00077       exit (1);
00078     }
00079 
00080   while ((val = pthread_tryjoin_np (th, &status)) != 0)
00081     {
00082       if (val != EBUSY)
00083        {
00084          printf ("tryjoin returned %s (%d), expected only 0 or EBUSY\n",
00085                 strerror (val), val);
00086          exit (1);
00087        }
00088 
00089       /* Delay minimally.  */
00090       struct timespec ts = { .tv_sec = 0, .tv_nsec = 10000000 };
00091       nanosleep (&ts, NULL);
00092     }
00093 
00094   if (status != (void *) 42l)
00095     {
00096       printf ("return value %p, expected %p\n", status, (void *) 42l);
00097       exit (1);
00098     }
00099 
00100   return 0;
00101 }
00102 
00103 #define TEST_FUNCTION do_test ()
00104 #include "../test-skeleton.c"