Back to index

glibc  2.9
tst-key4.c
Go to the documentation of this file.
00001 /* Copyright (C) 2003, 2004 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 #ifdef PTHREAD_KEYS_MAX
00028 const int max = PTHREAD_KEYS_MAX;
00029 #else
00030 const int max = _POSIX_THREAD_KEYS_MAX;
00031 #endif
00032 static pthread_key_t *keys;
00033 
00034 
00035 static void *
00036 tf1 (void *arg)
00037 {
00038   int i;
00039   for (i = 0; i < max; ++i)
00040     if (pthread_setspecific (keys[i], (void *) (long int) (i + 1)) != 0)
00041       {
00042        puts ("setspecific failed");
00043        exit (1);
00044       }
00045 
00046   return NULL;
00047 }
00048 
00049 
00050 static void *
00051 tf2 (void *arg)
00052 {
00053   int i;
00054   for (i = 0; i < max; ++i)
00055     if (pthread_getspecific (keys[i]) != NULL)
00056       {
00057        printf ("getspecific for key %d not NULL\n", i);
00058        exit (1);
00059       }
00060 
00061   return NULL;
00062 }
00063 
00064 
00065 static int
00066 do_test (void)
00067 {
00068   keys = alloca (max * sizeof (pthread_key_t));
00069 
00070   int i;
00071   for (i = 0; i < max; ++i)
00072     if (pthread_key_create (&keys[i], NULL) != 0)
00073       {
00074        puts ("key_create failed");
00075        exit (1);
00076       }
00077 
00078   pthread_attr_t a;
00079 
00080   if (pthread_attr_init (&a) != 0)
00081     {
00082       puts ("attr_init failed");
00083       exit (1);
00084     }
00085 
00086   if (pthread_attr_setstacksize (&a, 1 * 1024 * 1024) != 0)
00087     {
00088       puts ("attr_setstacksize failed");
00089       return 1;
00090     }
00091 
00092   for (i = 0; i < 10; ++i)
00093     {
00094       int j;
00095 #define N 2
00096       pthread_t th[N];
00097       for (j = 0; j < N; ++j)
00098        if (pthread_create (&th[j], NULL, tf1, NULL) != 0)
00099          {
00100            puts ("1st create failed");
00101            exit (1);
00102          }
00103 
00104       for (j = 0; j < N; ++j)
00105        if (pthread_join (th[j], NULL) != 0)
00106          {
00107            puts ("1st join failed");
00108            exit (1);
00109          }
00110 
00111       for (j = 0; j < N; ++j)
00112        if (pthread_create (&th[j], NULL, tf2, NULL) != 0)
00113          {
00114            puts ("2nd create failed");
00115            exit (1);
00116          }
00117 
00118       for (j = 0; j < N; ++j)
00119        if (pthread_join (th[j], NULL) != 0)
00120          {
00121            puts ("2nd join failed");
00122            exit (1);
00123          }
00124     }
00125 
00126   if (pthread_attr_destroy (&a) != 0)
00127     {
00128       puts ("attr_destroy failed");
00129       exit (1);
00130     }
00131 
00132   return 0;
00133 }
00134 
00135 
00136 #define TEST_FUNCTION do_test ()
00137 #include "../test-skeleton.c"