Back to index

glibc  2.9
tst-attr3.c
Go to the documentation of this file.
00001 /* pthread_getattr_np test.
00002    Copyright (C) 2003, 2004 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   int inheritsched1, inheritsched2;
00148   err = pthread_attr_getinheritsched (&a, &inheritsched1);
00149   if (err)
00150     {
00151       error (0, err, "pthread_attr_getinheritsched failed");
00152       result = tf;
00153     }
00154   else
00155     {
00156       err = pthread_attr_getinheritsched (ap, &inheritsched2);
00157       if (err)
00158        {
00159          error (0, err, "pthread_attr_getinheritsched failed");
00160          result = tf;
00161        }
00162       else if (inheritsched1 != inheritsched2)
00163        {
00164          error (0, 0, "inheritsched differs %d != %d",
00165                inheritsched1, inheritsched2);
00166          result = tf;
00167        }
00168     }
00169 
00170   cpu_set_t c1, c2;
00171   err = pthread_getaffinity_np (pthread_self (), sizeof (c1), &c1);
00172   if (err == 0)
00173     {
00174       err = pthread_attr_getaffinity_np (&a, sizeof (c2), &c2);
00175       if (err)
00176        {
00177          error (0, err, "pthread_attr_getaffinity_np failed");
00178          result = tf;
00179        }
00180       else if (memcmp (&c1, &c2, sizeof (c1)))
00181        {
00182          error (0, 0, "pthread_attr_getaffinity_np returned different CPU mask than pthread_getattr_np");
00183          result = tf;
00184        }
00185     }
00186 
00187   err = pthread_attr_destroy (&a);
00188   if (err)
00189     {
00190       error (0, err, "pthread_attr_destroy failed");
00191       result = tf;
00192     }
00193 
00194   if (ap == &a2)
00195     {
00196       err = pthread_attr_destroy (ap);
00197       if (err)
00198        {
00199          error (0, err, "pthread_attr_destroy failed");
00200          result = tf;
00201        }
00202     }
00203 
00204   return result;
00205 }
00206 
00207 
00208 static int
00209 do_test (void)
00210 {
00211   int result = 0;
00212   pthread_attr_t a;
00213   cpu_set_t c1, c2;
00214 
00215   int err = pthread_attr_init (&a);
00216   if (err)
00217     {
00218       error (0, err, "pthread_attr_init failed");
00219       result = 1;
00220     }
00221 
00222   err = pthread_attr_getaffinity_np (&a, sizeof (c1), &c1);
00223   if (err && err != ENOSYS)
00224     {
00225       error (0, err, "pthread_attr_getaffinity_np failed");
00226       result = 1;
00227     }
00228 
00229   err = pthread_attr_destroy (&a);
00230   if (err)
00231     {
00232       error (0, err, "pthread_attr_destroy failed");
00233       result = 1;
00234     }
00235 
00236   err = pthread_getattr_np (pthread_self (), &a);
00237   if (err)
00238     {
00239       error (0, err, "pthread_getattr_np failed");
00240       result = 1;
00241     }
00242 
00243   int detachstate;
00244   err = pthread_attr_getdetachstate (&a, &detachstate);
00245   if (err)
00246     {
00247       error (0, err, "pthread_attr_getdetachstate failed");
00248       result = 1;
00249     }
00250   else if (detachstate != PTHREAD_CREATE_JOINABLE)
00251     {
00252       error (0, 0, "initial thread not joinable");
00253       result = 1;
00254     }
00255 
00256   void *stackaddr;
00257   size_t stacksize;
00258   err = pthread_attr_getstack (&a, &stackaddr, &stacksize);
00259   if (err)
00260     {
00261       error (0, err, "pthread_attr_getstack failed");
00262       result = 1;
00263     }
00264   else if ((void *) &a < stackaddr
00265           || (void *) &a >= stackaddr + stacksize)
00266     {
00267       error (0, 0, "pthread_attr_getstack returned range does not cover main's stack");
00268       result = 1;
00269     }
00270   else
00271     printf ("initial thread stack %p-%p (0x%zx)\n", stackaddr,
00272            stackaddr + stacksize, stacksize);
00273 
00274   size_t guardsize;
00275   err = pthread_attr_getguardsize (&a, &guardsize);
00276   if (err)
00277     {
00278       error (0, err, "pthread_attr_getguardsize failed");
00279       result = 1;
00280     }
00281   else if (guardsize != 0)
00282     {
00283       error (0, 0, "pthread_attr_getguardsize returned %zd != 0",
00284             guardsize);
00285       result = 1;
00286     }
00287 
00288   int scope;
00289   err = pthread_attr_getscope (&a, &scope);
00290   if (err)
00291     {
00292       error (0, err, "pthread_attr_getscope failed");
00293       result = 1;
00294     }
00295   else if (scope != PTHREAD_SCOPE_SYSTEM)
00296     {
00297       error (0, 0, "pthread_attr_getscope returned %d != PTHREAD_SCOPE_SYSTEM",
00298             scope);
00299       result = 1;
00300     }
00301 
00302   int inheritsched;
00303   err = pthread_attr_getinheritsched (&a, &inheritsched);
00304   if (err)
00305     {
00306       error (0, err, "pthread_attr_getinheritsched failed");
00307       result = 1;
00308     }
00309   else if (inheritsched != PTHREAD_INHERIT_SCHED)
00310     {
00311       error (0, 0, "pthread_attr_getinheritsched returned %d != PTHREAD_INHERIT_SCHED",
00312             inheritsched);
00313       result = 1;
00314     }
00315 
00316   err = pthread_getaffinity_np (pthread_self (), sizeof (c1), &c1);
00317   if (err == 0)
00318     {
00319       err = pthread_attr_getaffinity_np (&a, sizeof (c2), &c2);
00320       if (err)
00321        {
00322          error (0, err, "pthread_attr_getaffinity_np failed");
00323          result = 1;
00324        }
00325       else if (memcmp (&c1, &c2, sizeof (c1)))
00326        {
00327          error (0, 0, "pthread_attr_getaffinity_np returned different CPU mask than pthread_getattr_np");
00328          result = 1;
00329        }
00330     }
00331 
00332   err = pthread_attr_destroy (&a);
00333   if (err)
00334     {
00335       error (0, err, "pthread_attr_destroy failed");
00336       result = 1;
00337     }
00338 
00339   pthread_t th;
00340   err = pthread_create (&th, NULL, tf, NULL);
00341   if (err)
00342     {
00343       error (0, err, "pthread_create #1 failed");
00344       result = 1;
00345     }
00346   else
00347     {
00348       void *ret;
00349       err = pthread_join (th, &ret);
00350       if (err)
00351        {
00352          error (0, err, "pthread_join #1 failed");
00353          result = 1;
00354        }
00355       else if (ret != NULL)
00356         result = 1;
00357     }
00358 
00359   err = pthread_attr_init (&a);
00360   if (err)
00361     {
00362       error (0, err, "pthread_attr_init failed");
00363       result = 1;
00364     }
00365 
00366   err = pthread_create (&th, &a, tf, &a);
00367   if (err)
00368     {
00369       error (0, err, "pthread_create #2 failed");
00370       result = 1;
00371     }
00372   else
00373     {
00374       void *ret;
00375       err = pthread_join (th, &ret);
00376       if (err)
00377        {
00378          error (0, err, "pthread_join #2 failed");
00379          result = 1;
00380        }
00381       else if (ret != NULL)
00382         result = 1;
00383     }
00384 
00385   err = pthread_attr_setguardsize (&a, 16 * sysconf (_SC_PAGESIZE));
00386   if (err)
00387     {
00388       error (0, err, "pthread_attr_setguardsize failed");
00389       result = 1;
00390     }
00391 
00392   err = pthread_create (&th, &a, tf, &a);
00393   if (err)
00394     {
00395       error (0, err, "pthread_create #3 failed");
00396       result = 1;
00397     }
00398   else
00399     {
00400       void *ret;
00401       err = pthread_join (th, &ret);
00402       if (err)
00403        {
00404          error (0, err, "pthread_join #3 failed");
00405          result = 1;
00406        }
00407       else if (ret != NULL)
00408         result = 1;
00409     }
00410 
00411   err = pthread_attr_destroy (&a);
00412   if (err)
00413     {
00414       error (0, err, "pthread_attr_destroy failed");
00415       result = 1;
00416     }
00417 
00418   return result;
00419 }
00420 
00421 #define TEST_FUNCTION do_test ()
00422 #include "../test-skeleton.c"