Back to index

glibc  2.9
tst-basic2.c
Go to the documentation of this file.
00001 /* Copyright (C) 2002, 2003, 2004 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 <stdint.h>
00023 #include <stdlib.h>
00024 #include <unistd.h>
00025 
00026 
00027 #define N 20
00028 
00029 static pthread_t th[N];
00030 static pthread_mutex_t lock[N];
00031 
00032 
00033 static void *tf (void *a)
00034 {
00035   uintptr_t idx = (uintptr_t) a;
00036 
00037   pthread_mutex_lock (&lock[idx]);
00038 
00039   return pthread_equal (pthread_self (), th[idx]) ? NULL : (void *) 1l;
00040 }
00041 
00042 
00043 int
00044 do_test (void)
00045 {
00046   if (pthread_equal (pthread_self (), pthread_self ()) == 0)
00047     {
00048       puts ("pthread_equal (pthread_self (), pthread_self ()) failed");
00049       exit (1);
00050     }
00051 
00052   pthread_attr_t at;
00053 
00054   if (pthread_attr_init (&at) != 0)
00055     {
00056       puts ("attr_init failed");
00057       return 1;
00058     }
00059 
00060   if (pthread_attr_setstacksize (&at, 1 * 1024 * 1024) != 0)
00061     {
00062       puts ("attr_setstacksize failed");
00063       return 1;
00064     }
00065 
00066   int i;
00067   for (i = 0; i < N; ++i)
00068     {
00069       if (pthread_mutex_init (&lock[i], NULL) != 0)
00070        {
00071          puts ("mutex_init failed");
00072          exit (1);
00073        }
00074 
00075       if (pthread_mutex_lock (&lock[i]) != 0)
00076        {
00077          puts ("mutex_lock failed");
00078          exit (1);
00079        }
00080 
00081       if (pthread_create (&th[i], &at, tf, (void *) (long int) i) != 0)
00082        {
00083          puts ("create failed");
00084          exit (1);
00085        }
00086 
00087       if (pthread_mutex_unlock (&lock[i]) != 0)
00088        {
00089          puts ("mutex_unlock failed");
00090          exit (1);
00091        }
00092 
00093       printf ("created thread %d\n", i);
00094     }
00095 
00096   if (pthread_attr_destroy (&at) != 0)
00097     {
00098       puts ("attr_destroy failed");
00099       return 1;
00100     }
00101 
00102   int result = 0;
00103   for (i = 0; i < N; ++i)
00104     {
00105       void *r;
00106       int e;
00107       if ((e = pthread_join (th[i], &r)) != 0)
00108        {
00109          printf ("join failed: %d\n", e);
00110          _exit (1);
00111        }
00112       else if (r != NULL)
00113        result = 1;
00114     }
00115 
00116   return 0;
00117 }
00118 
00119 
00120 #define TEST_FUNCTION do_test ()
00121 #include "../test-skeleton.c"