Back to index

glibc  2.9
tst-key2.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 void *
00051 tf (void *arg)
00052 {
00053   pthread_key_t *key = (pthread_key_t *) arg;
00054 
00055   if (pthread_setspecific (*key, (void *) -1l) != 0)
00056     {
00057       write (2, "setspecific failed\n", 19);
00058       _exit (1);
00059     }
00060 
00061   return NULL;
00062 }
00063 
00064 
00065 int
00066 do_test (void)
00067 {
00068   pthread_key_t keys[N];
00069 
00070   int i;
00071   for (i = 0; i < N; ++i)
00072     if (pthread_key_create (&keys[i], fcts[i]) != 0)
00073       {
00074        write (2, "key_create failed\n", 18);
00075        _exit (1);
00076       }
00077 
00078   pthread_t th;
00079   if (pthread_create (&th, NULL, tf, &keys[1]) != 0)
00080     {
00081       write (2, "create failed\n", 14);
00082       _exit (1);
00083     }
00084 
00085   if (pthread_join (th, NULL) != 0)
00086     {
00087       write (2, "join failed\n", 12);
00088       _exit (1);
00089     }
00090 
00091   if (cnt0 != 0)
00092     {
00093       write (2, "cnt0 != 0\n", 10);
00094       _exit (1);
00095     }
00096 
00097   if (cnt1 != 1)
00098     {
00099       write (2, "cnt1 != 1\n", 10);
00100       _exit (1);
00101     }
00102 
00103   for (i = 0; i < N; ++i)
00104     if (pthread_key_delete (keys[i]) != 0)
00105       {
00106        write (2, "key_delete failed\n", 18);
00107        _exit (1);
00108       }
00109 
00110   return 0;
00111 }
00112 
00113 
00114 #define TEST_FUNCTION do_test ()
00115 #include "../test-skeleton.c"