Back to index

glibc  2.9
tst-attr2.c
Go to the documentation of this file.
00001 /* Copyright (C) 2003 Free Software Foundation, Inc.
00002    This file is part of the GNU C Library.
00003    Contributed by Ulrich Drepper <drepper@redhat.com>, 2003.
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 <errno.h>
00021 #include <pthread.h>
00022 #include <stdio.h>
00023 #include <stdlib.h>
00024 #include <unistd.h>
00025 
00026 
00027 int
00028 do_test (void)
00029 {
00030   pthread_attr_t a;
00031 
00032   if (pthread_attr_init (&a) != 0)
00033     {
00034       puts ("attr_init failed");
00035       exit (1);
00036     }
00037 
00038   /* Check default value of detach state.  */
00039   int s;
00040   if (pthread_attr_getdetachstate (&a, &s) != 0)
00041     {
00042       puts ("1st attr_getdestachstate failed");
00043       exit (1);
00044     }
00045   if (s != PTHREAD_CREATE_JOINABLE)
00046     {
00047       printf ("\
00048 default detach state wrong: %d, expected %d (PTHREAD_CREATE_JOINABLE)\n",
00049              s, PTHREAD_CREATE_JOINABLE);
00050       exit (1);
00051     }
00052 
00053   int e = pthread_attr_setdetachstate (&a, PTHREAD_CREATE_DETACHED);
00054   if (e != 0)
00055     {
00056       puts ("1st attr_setdetachstate failed");
00057       exit (1);
00058     }
00059   if (pthread_attr_getdetachstate (&a, &s) != 0)
00060     {
00061       puts ("2nd attr_getdestachstate failed");
00062       exit (1);
00063     }
00064   if (s != PTHREAD_CREATE_DETACHED)
00065     {
00066       puts ("PTHREAD_CREATE_DETACHED set, but not given back");
00067       exit (1);
00068     }
00069 
00070   e = pthread_attr_setdetachstate (&a, PTHREAD_CREATE_JOINABLE);
00071   if (e != 0)
00072     {
00073       puts ("2nd attr_setdetachstate failed");
00074       exit (1);
00075     }
00076   if (pthread_attr_getdetachstate (&a, &s) != 0)
00077     {
00078       puts ("3rd attr_getdestachstate failed");
00079       exit (1);
00080     }
00081   if (s != PTHREAD_CREATE_JOINABLE)
00082     {
00083       puts ("PTHREAD_CREATE_JOINABLE set, but not given back");
00084       exit (1);
00085     }
00086 
00087 
00088   size_t g;
00089   if (pthread_attr_getguardsize (&a, &g) != 0)
00090     {
00091       puts ("1st attr_getguardsize failed");
00092       exit (1);
00093     }
00094   if (g != (size_t) sysconf (_SC_PAGESIZE))
00095     {
00096       printf ("default guardsize %zu, expected %ld (PAGESIZE)\n",
00097              g, sysconf (_SC_PAGESIZE));
00098       exit (1);
00099     }
00100 
00101   e = pthread_attr_setguardsize (&a, 0);
00102   if (e != 0)
00103     {
00104       puts ("1st attr_setguardsize failed");
00105       exit (1);
00106     }
00107   if (pthread_attr_getguardsize (&a, &g) != 0)
00108     {
00109       puts ("2nd attr_getguardsize failed");
00110       exit (1);
00111     }
00112   if (g != 0)
00113     {
00114       printf ("guardsize set to zero but %zu returned\n", g);
00115       exit (1);
00116     }
00117 
00118   e = pthread_attr_setguardsize (&a, 1);
00119   if (e != 0)
00120     {
00121       puts ("2nd attr_setguardsize failed");
00122       exit (1);
00123     }
00124   if (pthread_attr_getguardsize (&a, &g) != 0)
00125     {
00126       puts ("3rd attr_getguardsize failed");
00127       exit (1);
00128     }
00129   if (g != 1)
00130     {
00131       printf ("guardsize set to 1 but %zu returned\n", g);
00132       exit (1);
00133     }
00134 
00135 
00136   if (pthread_attr_getinheritsched (&a, &s) != 0)
00137     {
00138       puts ("1st attr_getinheritsched failed");
00139       exit (1);
00140     }
00141   /* XXX What is the correct default value.  */
00142   if (s != PTHREAD_INHERIT_SCHED && s != PTHREAD_EXPLICIT_SCHED)
00143     {
00144       puts ("incorrect default value for inheritsched");
00145       exit (1);
00146     }
00147 
00148   e = pthread_attr_setinheritsched (&a, PTHREAD_EXPLICIT_SCHED);
00149   if (e != 0)
00150     {
00151       puts ("1st attr_setinheritsched failed");
00152       exit (1);
00153     }
00154   if (pthread_attr_getinheritsched (&a, &s) != 0)
00155     {
00156       puts ("2nd attr_getinheritsched failed");
00157       exit (1);
00158     }
00159   if (s != PTHREAD_EXPLICIT_SCHED)
00160     {
00161       printf ("inheritsched set to PTHREAD_EXPLICIT_SCHED, but got %d\n", s);
00162       exit (1);
00163     }
00164 
00165   e = pthread_attr_setinheritsched (&a, PTHREAD_INHERIT_SCHED);
00166   if (e != 0)
00167     {
00168       puts ("2nd attr_setinheritsched failed");
00169       exit (1);
00170     }
00171   if (pthread_attr_getinheritsched (&a, &s) != 0)
00172     {
00173       puts ("3rd attr_getinheritsched failed");
00174       exit (1);
00175     }
00176   if (s != PTHREAD_INHERIT_SCHED)
00177     {
00178       printf ("inheritsched set to PTHREAD_INHERIT_SCHED, but got %d\n", s);
00179       exit (1);
00180     }
00181 
00182 
00183   if (pthread_attr_getschedpolicy (&a, &s) != 0)
00184     {
00185       puts ("1st attr_getschedpolicy failed");
00186       exit (1);
00187     }
00188   /* XXX What is the correct default value.  */
00189   if (s != SCHED_OTHER && s != SCHED_FIFO && s != SCHED_RR)
00190     {
00191       puts ("incorrect default value for schedpolicy");
00192       exit (1);
00193     }
00194 
00195   e = pthread_attr_setschedpolicy (&a, SCHED_RR);
00196   if (e != 0)
00197     {
00198       puts ("1st attr_setschedpolicy failed");
00199       exit (1);
00200     }
00201   if (pthread_attr_getschedpolicy (&a, &s) != 0)
00202     {
00203       puts ("2nd attr_getschedpolicy failed");
00204       exit (1);
00205     }
00206   if (s != SCHED_RR)
00207     {
00208       printf ("schedpolicy set to SCHED_RR, but got %d\n", s);
00209       exit (1);
00210     }
00211 
00212   e = pthread_attr_setschedpolicy (&a, SCHED_FIFO);
00213   if (e != 0)
00214     {
00215       puts ("2nd attr_setschedpolicy failed");
00216       exit (1);
00217     }
00218   if (pthread_attr_getschedpolicy (&a, &s) != 0)
00219     {
00220       puts ("3rd attr_getschedpolicy failed");
00221       exit (1);
00222     }
00223   if (s != SCHED_FIFO)
00224     {
00225       printf ("schedpolicy set to SCHED_FIFO, but got %d\n", s);
00226       exit (1);
00227     }
00228 
00229   e = pthread_attr_setschedpolicy (&a, SCHED_OTHER);
00230   if (e != 0)
00231     {
00232       puts ("3rd attr_setschedpolicy failed");
00233       exit (1);
00234     }
00235   if (pthread_attr_getschedpolicy (&a, &s) != 0)
00236     {
00237       puts ("4th attr_getschedpolicy failed");
00238       exit (1);
00239     }
00240   if (s != SCHED_OTHER)
00241     {
00242       printf ("schedpolicy set to SCHED_OTHER, but got %d\n", s);
00243       exit (1);
00244     }
00245 
00246 
00247   if (pthread_attr_getscope (&a, &s) != 0)
00248     {
00249       puts ("1st attr_getscope failed");
00250       exit (1);
00251     }
00252   /* XXX What is the correct default value.  */
00253   if (s != PTHREAD_SCOPE_SYSTEM && s != PTHREAD_SCOPE_PROCESS)
00254     {
00255       puts ("incorrect default value for contentionscope");
00256       exit (1);
00257     }
00258 
00259   e = pthread_attr_setscope (&a, PTHREAD_SCOPE_PROCESS);
00260   if (e != ENOTSUP)
00261     {
00262       if (e != 0)
00263        {
00264          puts ("1st attr_setscope failed");
00265          exit (1);
00266        }
00267       if (pthread_attr_getscope (&a, &s) != 0)
00268        {
00269          puts ("2nd attr_getscope failed");
00270          exit (1);
00271        }
00272       if (s != PTHREAD_SCOPE_PROCESS)
00273        {
00274          printf ("\
00275 contentionscope set to PTHREAD_SCOPE_PROCESS, but got %d\n", s);
00276          exit (1);
00277        }
00278     }
00279 
00280   e = pthread_attr_setscope (&a, PTHREAD_SCOPE_SYSTEM);
00281   if (e != 0)
00282     {
00283       puts ("2nd attr_setscope failed");
00284       exit (1);
00285     }
00286   if (pthread_attr_getscope (&a, &s) != 0)
00287     {
00288       puts ("3rd attr_getscope failed");
00289       exit (1);
00290     }
00291   if (s != PTHREAD_SCOPE_SYSTEM)
00292     {
00293       printf ("contentionscope set to PTHREAD_SCOPE_SYSTEM, but got %d\n", s);
00294       exit (1);
00295     }
00296 
00297   char buf[1];
00298   e = pthread_attr_setstack (&a, buf, 1);
00299   if (e != EINVAL)
00300     {
00301       puts ("setstack with size 1 did not produce EINVAL");
00302       exit (1);
00303     }
00304 
00305   e = pthread_attr_setstacksize (&a, 1);
00306   if (e != EINVAL)
00307     {
00308       puts ("setstacksize with size 1 did not produce EINVAL");
00309       exit (1);
00310     }
00311 
00312   return 0;
00313 }
00314 
00315 
00316 #define TEST_FUNCTION do_test ()
00317 #include "../test-skeleton.c"