Back to index

glibc  2.9
tst-tsd3.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 <limits.h>
00021 #include <pthread.h>
00022 #include <stdio.h>
00023 #include <stdlib.h>
00024 #include <unistd.h>
00025 
00026 
00027 static pthread_key_t key1;
00028 static pthread_key_t key2;
00029 
00030 
00031 static int left;
00032 
00033 
00034 static void
00035 destr1 (void *arg)
00036 {
00037   if (--left > 0)
00038     {
00039       puts ("set key2");
00040 
00041       if (pthread_setspecific (key2, (void *) 1l) != 0)
00042        {
00043          puts ("destr1: setspecific failed");
00044          exit (1);
00045        }
00046     }
00047 }
00048 
00049 
00050 static void
00051 destr2 (void *arg)
00052 {
00053   if (--left > 0)
00054     {
00055       puts ("set key1");
00056 
00057       if (pthread_setspecific (key1, (void *) 1l) != 0)
00058        {
00059          puts ("destr2: setspecific failed");
00060          exit (1);
00061        }
00062     }
00063 }
00064 
00065 
00066 static void *
00067 tf (void *arg)
00068 {
00069   /* Let the destructors work.  */
00070   left = 7;
00071 
00072   if (pthread_setspecific (key1, (void *) 1l) != 0
00073       || pthread_setspecific (key2, (void *) 1l) != 0)
00074     {
00075       puts ("tf: setspecific failed");
00076       exit (1);
00077     }
00078 
00079   return NULL;
00080 }
00081 
00082 
00083 static int
00084 do_test (void)
00085 {
00086   /* Allocate two keys, both with destructors.  */
00087   if (pthread_key_create (&key1, destr1) != 0
00088       || pthread_key_create (&key2, destr2) != 0)
00089     {
00090       puts ("key_create failed");
00091       return 1;
00092     }
00093 
00094   pthread_t th;
00095   if (pthread_create (&th, NULL, tf, NULL) != 0)
00096     {
00097       puts ("create failed");
00098       return 1;
00099     }
00100 
00101   if (pthread_join (th, NULL) != 0)
00102     {
00103       puts ("join failed");
00104       return 1;
00105     }
00106 
00107   if (left != 0)
00108     {
00109       printf ("left == %d\n", left);
00110       return 1;
00111     }
00112 
00113   if (pthread_getspecific (key1) != NULL)
00114     {
00115       puts ("key1 data != NULL");
00116       return 1;
00117     }
00118   if (pthread_getspecific (key2) != NULL)
00119     {
00120       puts ("key2 data != NULL");
00121       return 1;
00122     }
00123 
00124   return 0;
00125 }
00126 
00127 
00128 #define TEST_FUNCTION do_test ()
00129 #include "../test-skeleton.c"