Back to index

glibc  2.9
tst-attr1.c
Go to the documentation of this file.
00001 /* pthread_getattr_np test.
00002    Copyright (C) 2003 Free Software Foundation, Inc.
00003    This file is part of the GNU C Library.
00004    Contributed by Jakub Jelinek <jakub@redhat.com>, 2003.
00005 
00006    The GNU C Library is free software; you can redistribute it and/or
00007    modify it under the terms of the GNU Lesser General Public
00008    License as published by the Free Software Foundation; either
00009    version 2.1 of the License, or (at your option) any later version.
00010 
00011    The GNU C Library is distributed in the hope that it will be useful,
00012    but WITHOUT ANY WARRANTY; without even the implied warranty of
00013    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00014    Lesser General Public License for more details.
00015 
00016    You should have received a copy of the GNU Lesser General Public
00017    License along with the GNU C Library; if not, write to the Free
00018    Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
00019    02111-1307 USA.  */
00020 
00021 #include <errno.h>
00022 #include <error.h>
00023 #include <pthread.h>
00024 #include <stdio.h>
00025 #include <stdlib.h>
00026 #include <string.h>
00027 #include <unistd.h>
00028 
00029 #include <stackinfo.h>
00030 
00031 static void *
00032 tf (void *arg)
00033 {
00034   pthread_attr_t a, *ap, a2;
00035   int err;
00036   void *result = NULL;
00037 
00038   if (arg == NULL)
00039     {
00040       ap = &a2;
00041       err = pthread_attr_init (ap);
00042       if (err)
00043         {
00044           error (0, err, "pthread_attr_init failed");
00045           return tf;
00046         }
00047     }
00048   else
00049     ap = (pthread_attr_t *) arg;
00050 
00051   err = pthread_getattr_np (pthread_self (), &a);
00052   if (err)
00053     {
00054       error (0, err, "pthread_getattr_np failed");
00055       result = tf;
00056     }
00057 
00058   int detachstate1, detachstate2;
00059   err = pthread_attr_getdetachstate (&a, &detachstate1);
00060   if (err)
00061     {
00062       error (0, err, "pthread_attr_getdetachstate failed");
00063       result = tf;
00064     }
00065   else
00066     {
00067       err = pthread_attr_getdetachstate (ap, &detachstate2);
00068       if (err)
00069        {
00070          error (0, err, "pthread_attr_getdetachstate failed");
00071          result = tf;
00072        }
00073       else if (detachstate1 != detachstate2)
00074        {
00075          error (0, 0, "detachstate differs %d != %d",
00076                detachstate1, detachstate2);
00077          result = tf;
00078        }
00079     }
00080 
00081   void *stackaddr;
00082   size_t stacksize;
00083   err = pthread_attr_getstack (&a, &stackaddr, &stacksize);
00084   if (err)
00085     {
00086       error (0, err, "pthread_attr_getstack failed");
00087       result = tf;
00088     }
00089   else if ((void *) &a < stackaddr
00090           || (void *) &a >= stackaddr + stacksize)
00091     {
00092       error (0, 0, "pthread_attr_getstack returned range does not cover thread's stack");
00093       result = tf;
00094     }
00095   else
00096     printf ("thread stack %p-%p (0x%zx)\n", stackaddr, stackaddr + stacksize,
00097            stacksize);
00098 
00099   size_t guardsize1, guardsize2;
00100   err = pthread_attr_getguardsize (&a, &guardsize1);
00101   if (err)
00102     {
00103       error (0, err, "pthread_attr_getguardsize failed");
00104       result = tf;
00105     }
00106   else
00107     {
00108       err = pthread_attr_getguardsize (ap, &guardsize2);
00109       if (err)
00110        {
00111          error (0, err, "pthread_attr_getguardsize failed");
00112          result = tf;
00113        }
00114       else if (guardsize1 != guardsize2)
00115        {
00116          error (0, 0, "guardsize differs %zd != %zd",
00117                guardsize1, guardsize2);
00118          result = tf;
00119        }
00120       else
00121        printf ("thread guardsize %zd\n", guardsize1);
00122     }
00123 
00124   int scope1, scope2;
00125   err = pthread_attr_getscope (&a, &scope1);
00126   if (err)
00127     {
00128       error (0, err, "pthread_attr_getscope failed");
00129       result = tf;
00130     }
00131   else
00132     {
00133       err = pthread_attr_getscope (ap, &scope2);
00134       if (err)
00135        {
00136          error (0, err, "pthread_attr_getscope failed");
00137          result = tf;
00138        }
00139       else if (scope1 != scope2)
00140        {
00141          error (0, 0, "scope differs %d != %d",
00142                scope1, scope2);
00143          result = tf;
00144        }
00145     }
00146 
00147   err = pthread_attr_destroy (&a);
00148   if (err)
00149     {
00150       error (0, err, "pthread_attr_destroy failed");
00151       result = tf;
00152     }
00153 
00154   if (ap == &a2)
00155     {
00156       err = pthread_attr_destroy (ap);
00157       if (err)
00158        {
00159          error (0, err, "pthread_attr_destroy failed");
00160          result = tf;
00161        }
00162     }
00163 
00164   return result;
00165 }
00166 
00167 
00168 static int
00169 do_test (void)
00170 {
00171   int result = 0;
00172   pthread_attr_t a;
00173 
00174   int err = pthread_attr_init (&a);
00175   if (err)
00176     {
00177       error (0, err, "pthread_attr_init failed");
00178       result = 1;
00179     }
00180 
00181   err = pthread_attr_destroy (&a);
00182   if (err)
00183     {
00184       error (0, err, "pthread_attr_destroy failed");
00185       result = 1;
00186     }
00187 
00188   err = pthread_getattr_np (pthread_self (), &a);
00189   if (err)
00190     {
00191       error (0, err, "pthread_getattr_np failed");
00192       result = 1;
00193     }
00194 
00195   int detachstate;
00196   err = pthread_attr_getdetachstate (&a, &detachstate);
00197   if (err)
00198     {
00199       error (0, err, "pthread_attr_getdetachstate failed");
00200       result = 1;
00201     }
00202   else if (detachstate != PTHREAD_CREATE_JOINABLE)
00203     {
00204       error (0, 0, "initial thread not joinable");
00205       result = 1;
00206     }
00207 
00208   void *stackaddr;
00209   size_t stacksize;
00210   err = pthread_attr_getstack (&a, &stackaddr, &stacksize);
00211   if (err)
00212     {
00213       error (0, err, "pthread_attr_getstack failed");
00214       result = 1;
00215     }
00216   else if ((void *) &a < stackaddr
00217           || (void *) &a >= stackaddr + stacksize)
00218     {
00219       error (0, 0, "pthread_attr_getstack returned range does not cover main's stack");
00220       result = 1;
00221     }
00222   else
00223     printf ("initial thread stack %p-%p (0x%zx)\n", stackaddr,
00224            stackaddr + stacksize, stacksize);
00225 
00226   size_t guardsize;
00227   err = pthread_attr_getguardsize (&a, &guardsize);
00228   if (err)
00229     {
00230       error (0, err, "pthread_attr_getguardsize failed");
00231       result = 1;
00232     }
00233   else if (guardsize != 0)
00234     {
00235       error (0, 0, "pthread_attr_getguardsize returned %zd != 0",
00236             guardsize);
00237       result = 1;
00238     }
00239 
00240   int scope;
00241   err = pthread_attr_getscope (&a, &scope);
00242   if (err)
00243     {
00244       error (0, err, "pthread_attr_getscope failed");
00245       result = 1;
00246     }
00247   else if (scope != PTHREAD_SCOPE_SYSTEM)
00248     {
00249       error (0, 0, "pthread_attr_getscope returned %d != PTHREAD_SCOPE_SYSTEM",
00250             scope);
00251       result = 1;
00252     }
00253 
00254   int inheritsched;
00255   err = pthread_attr_getinheritsched (&a, &inheritsched);
00256   if (err)
00257     {
00258       error (0, err, "pthread_attr_getinheritsched failed");
00259       result = 1;
00260     }
00261   else if (inheritsched != PTHREAD_INHERIT_SCHED)
00262     {
00263       error (0, 0, "pthread_attr_getinheritsched returned %d != PTHREAD_INHERIT_SCHED",
00264             inheritsched);
00265       result = 1;
00266     }
00267 
00268   err = pthread_attr_destroy (&a);
00269   if (err)
00270     {
00271       error (0, err, "pthread_attr_destroy failed");
00272       result = 1;
00273     }
00274 
00275   pthread_t th;
00276   err = pthread_create (&th, NULL, tf, NULL);
00277   if (err)
00278     {
00279       error (0, err, "pthread_create #1 failed");
00280       result = 1;
00281     }
00282   else
00283     {
00284       void *ret;
00285       err = pthread_join (th, &ret);
00286       if (err)
00287        {
00288          error (0, err, "pthread_join #1 failed");
00289          result = 1;
00290        }
00291       else if (ret != NULL)
00292         result = 1;
00293     }
00294 
00295   err = pthread_attr_init (&a);
00296   if (err)
00297     {
00298       error (0, err, "pthread_attr_init failed");
00299       result = 1;
00300     }
00301 
00302   err = pthread_create (&th, &a, tf, &a);
00303   if (err)
00304     {
00305       error (0, err, "pthread_create #2 failed");
00306       result = 1;
00307     }
00308   else
00309     {
00310       void *ret;
00311       err = pthread_join (th, &ret);
00312       if (err)
00313        {
00314          error (0, err, "pthread_join #2 failed");
00315          result = 1;
00316        }
00317       else if (ret != NULL)
00318         result = 1;
00319     }
00320 
00321   err = pthread_attr_setguardsize (&a, 16 * sysconf (_SC_PAGESIZE));
00322   if (err)
00323     {
00324       error (0, err, "pthread_attr_setguardsize failed");
00325       result = 1;
00326     }
00327 
00328   err = pthread_create (&th, &a, tf, &a);
00329   if (err)
00330     {
00331       error (0, err, "pthread_create #3 failed");
00332       result = 1;
00333     }
00334   else
00335     {
00336       void *ret;
00337       err = pthread_join (th, &ret);
00338       if (err)
00339        {
00340          error (0, err, "pthread_join #3 failed");
00341          result = 1;
00342        }
00343       else if (ret != NULL)
00344         result = 1;
00345     }
00346 
00347   err = pthread_attr_destroy (&a);
00348   if (err)
00349     {
00350       error (0, err, "pthread_attr_destroy failed");
00351       result = 1;
00352     }
00353 
00354   return result;
00355 }
00356 
00357 #define TEST_FUNCTION do_test ()
00358 #include "../test-skeleton.c"