Back to index

glibc  2.9
tst-key3.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 <pthread.h>
00021 #include <stdio.h>
00022 #include <unistd.h>
00023 
00024 #define N 2
00025 
00026 
00027 static int cnt0;
00028 static void
00029 f0 (void *p)
00030 {
00031   ++cnt0;
00032 }
00033 
00034 
00035 static int cnt1;
00036 static void
00037 f1 (void *p)
00038 {
00039   ++cnt1;
00040 }
00041 
00042 
00043 static void (*fcts[N]) (void *) =
00044 {
00045   f0,
00046   f1
00047 };
00048 
00049 
00050 static pthread_barrier_t b;
00051 
00052 
00053 static void *
00054 tf (void *arg)
00055 {
00056   pthread_key_t *key = (pthread_key_t *) arg;
00057 
00058   if (pthread_setspecific (*key, (void *) -1l) != 0)
00059     {
00060       write (2, "setspecific failed\n", 19);
00061       _exit (1);
00062     }
00063 
00064   pthread_barrier_wait (&b);
00065 
00066   const struct timespec t = { .tv_sec = 1000, .tv_nsec = 0 };
00067   while (1)
00068     nanosleep (&t, NULL);
00069 
00070   /* NOTREACHED */
00071   return NULL;
00072 }
00073 
00074 
00075 int
00076 do_test (void)
00077 {
00078   pthread_key_t keys[N];
00079 
00080   int i;
00081   for (i = 0; i < N; ++i)
00082     if (pthread_key_create (&keys[i], fcts[i]) != 0)
00083       {
00084        write (2, "key_create failed\n", 18);
00085        _exit (1);
00086       }
00087 
00088   if (pthread_barrier_init (&b, NULL, 2) != 0)
00089     {
00090       write (2, "barrier_init failed\n", 20);
00091       _exit (1);
00092     }
00093 
00094   pthread_t th;
00095   if (pthread_create (&th, NULL, tf, &keys[1]) != 0)
00096     {
00097       write (2, "create failed\n", 14);
00098       _exit (1);
00099     }
00100 
00101   pthread_barrier_wait (&b);
00102 
00103   if (pthread_cancel (th) != 0)
00104     {
00105       write (2, "cancel failed\n", 14);
00106       _exit (1);
00107     }
00108 
00109   void *status;
00110   if (pthread_join (th, &status) != 0)
00111     {
00112       write (2, "join failed\n", 12);
00113       _exit (1);
00114     }
00115 
00116   if (status != PTHREAD_CANCELED)
00117     {
00118       write (2, "thread not canceled\n", 20);
00119       _exit (1);
00120     }
00121 
00122   /* Note that the TSD destructors not necessarily have to have
00123      finished by the time pthread_join returns.  At least according to
00124      POSIX.  We implement the stronger requirement that they indeed
00125      have run and therefore these tests succeed.  */
00126   if (cnt0 != 0)
00127     {
00128       write (2, "cnt0 != 0\n", 10);
00129       _exit (1);
00130     }
00131 
00132   if (cnt1 != 1)
00133     {
00134       write (2, "cnt1 != 1\n", 10);
00135       _exit (1);
00136     }
00137 
00138   for (i = 0; i < N; ++i)
00139     if (pthread_key_delete (keys[i]) != 0)
00140       {
00141        write (2, "key_delete failed\n", 18);
00142        _exit (1);
00143       }
00144 
00145   if (pthread_barrier_destroy (&b) != 0)
00146     {
00147       write (2, "barrier_destroy failed\n", 23);
00148       _exit (1);
00149     }
00150 
00151   return 0;
00152 }
00153 
00154 
00155 #define TEST_FUNCTION do_test ()
00156 #include "../test-skeleton.c"