Back to index

glibc  2.9
tst-tsd1.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 <string.h>
00023 
00024 
00025 static int
00026 do_test (void)
00027 {
00028   pthread_key_t key1;
00029   pthread_key_t key2;
00030   void *value;
00031   int result = 0;
00032   int err;
00033 
00034   err = pthread_key_create (&key1, NULL);
00035   if (err != 0)
00036     {
00037       printf ("1st key_create failed: %s\n", strerror (err));
00038       return 1;
00039     }
00040 
00041   /* Initial value must be NULL.  */
00042   value = pthread_getspecific (key1);
00043   if (value != NULL)
00044     {
00045       puts ("1st getspecific != NULL");
00046       result = 1;
00047     }
00048 
00049   err = pthread_setspecific (key1, (void *) -2l);
00050   if (err != 0)
00051     {
00052       printf ("1st setspecific failed: %s\n", strerror (err));
00053       return 1;
00054     }
00055 
00056   value = pthread_getspecific (key1);
00057   if (value == NULL)
00058     {
00059       puts ("2nd getspecific == NULL\n");
00060       result = 1;
00061     }
00062   else if (value != (void *) -2l)
00063     {
00064       puts ("2nd getspecific != -2l\n");
00065       result = 1;
00066     }
00067 
00068   err = pthread_setspecific (key1, (void *) -3l);
00069   if (err != 0)
00070     {
00071       printf ("2nd setspecific failed: %s\n", strerror (err));
00072       return 1;
00073     }
00074 
00075   value = pthread_getspecific (key1);
00076   if (value == NULL)
00077     {
00078       puts ("3rd getspecific == NULL\n");
00079       result = 1;
00080     }
00081   else if (value != (void *) -3l)
00082     {
00083       puts ("3rd getspecific != -2l\n");
00084       result = 1;
00085     }
00086 
00087   err = pthread_key_delete (key1);
00088   if (err != 0)
00089     {
00090       printf ("key_delete failed: %s\n", strerror (err));
00091       result = 1;
00092     }
00093 
00094 
00095   err = pthread_key_create (&key2, NULL);
00096   if (err != 0)
00097     {
00098       printf ("2nd key_create failed: %s\n", strerror (err));
00099       return 1;
00100     }
00101 
00102   if (key1 != key2)
00103     puts ("key1 != key2; no more tests performed");
00104   else
00105     {
00106       value = pthread_getspecific (key2);
00107       if (value != NULL)
00108        {
00109          puts ("4th getspecific != NULL");
00110          result = 1;
00111        }
00112     }
00113 
00114   return result;
00115 }
00116 
00117 #define TEST_FUNCTION do_test ()
00118 #include "../test-skeleton.c"