Back to index

glibc  2.9
tst-attr1.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   int i;
00031   pthread_attr_t a;
00032 
00033   if (pthread_attr_init (&a) != 0)
00034     {
00035       puts ("attr_init failed");
00036       exit (1);
00037     }
00038 
00039   pthread_mutexattr_t ma;
00040 
00041   if (pthread_mutexattr_init (&ma) != 0)
00042     {
00043       puts ("mutexattr_init failed");
00044       exit (1);
00045     }
00046 
00047   pthread_rwlockattr_t rwa;
00048 
00049   if (pthread_rwlockattr_init (&rwa) != 0)
00050     {
00051       puts ("rwlockattr_init failed");
00052       exit (1);
00053     }
00054 
00055   /* XXX Remove if default value is clear.  */
00056   pthread_attr_setinheritsched (&a, PTHREAD_INHERIT_SCHED);
00057   pthread_attr_setschedpolicy (&a, SCHED_OTHER);
00058   pthread_attr_setscope (&a, PTHREAD_SCOPE_SYSTEM);
00059 
00060   for (i = 0; i < 10000; ++i)
00061     {
00062       long int r = random ();
00063 
00064       if (r != PTHREAD_CREATE_DETACHED && r != PTHREAD_CREATE_JOINABLE)
00065        {
00066          int e = pthread_attr_setdetachstate (&a, r);
00067 
00068          if (e == 0)
00069            {
00070              printf ("attr_setdetachstate with value %ld succeeded\n", r);
00071              exit (1);
00072            }
00073          if (e != EINVAL)
00074            {
00075              puts ("attr_setdetachstate didn't return EINVAL");
00076              exit (1);
00077            }
00078 
00079          int s;
00080          if (pthread_attr_getdetachstate (&a, &s) != 0)
00081            {
00082              puts ("attr_getdetachstate failed");
00083              exit (1);
00084            }
00085 
00086          if (s != PTHREAD_CREATE_JOINABLE)
00087            {
00088              printf ("\
00089 detach state changed to %d by invalid setdetachstate call\n", s);
00090              exit (1);
00091            }
00092        }
00093 
00094       if (r != PTHREAD_INHERIT_SCHED && r != PTHREAD_EXPLICIT_SCHED)
00095        {
00096          int e = pthread_attr_setinheritsched (&a, r);
00097 
00098          if (e == 0)
00099            {
00100              printf ("attr_setinheritsched with value %ld succeeded\n", r);
00101              exit (1);
00102            }
00103          if (e != EINVAL)
00104            {
00105              puts ("attr_setinheritsched didn't return EINVAL");
00106              exit (1);
00107            }
00108 
00109          int s;
00110          if (pthread_attr_getinheritsched (&a, &s) != 0)
00111            {
00112              puts ("attr_getinheritsched failed");
00113              exit (1);
00114            }
00115 
00116          if (s != PTHREAD_INHERIT_SCHED)
00117            {
00118              printf ("\
00119 inheritsched changed to %d by invalid setinheritsched call\n", s);
00120              exit (1);
00121            }
00122        }
00123 
00124       if (r != SCHED_OTHER && r != SCHED_RR && r != SCHED_FIFO)
00125        {
00126          int e = pthread_attr_setschedpolicy (&a, r);
00127 
00128          if (e == 0)
00129            {
00130              printf ("attr_setschedpolicy with value %ld succeeded\n", r);
00131              exit (1);
00132            }
00133          if (e != EINVAL)
00134            {
00135              puts ("attr_setschedpolicy didn't return EINVAL");
00136              exit (1);
00137            }
00138 
00139          int s;
00140          if (pthread_attr_getschedpolicy (&a, &s) != 0)
00141            {
00142              puts ("attr_getschedpolicy failed");
00143              exit (1);
00144            }
00145 
00146          if (s != SCHED_OTHER)
00147            {
00148              printf ("\
00149 schedpolicy changed to %d by invalid setschedpolicy call\n", s);
00150              exit (1);
00151            }
00152        }
00153 
00154       if (r != PTHREAD_SCOPE_SYSTEM && r != PTHREAD_SCOPE_PROCESS)
00155        {
00156          int e = pthread_attr_setscope (&a, r);
00157 
00158          if (e == 0)
00159            {
00160              printf ("attr_setscope with value %ld succeeded\n", r);
00161              exit (1);
00162            }
00163          if (e != EINVAL)
00164            {
00165              puts ("attr_setscope didn't return EINVAL");
00166              exit (1);
00167            }
00168 
00169          int s;
00170          if (pthread_attr_getscope (&a, &s) != 0)
00171            {
00172              puts ("attr_getscope failed");
00173              exit (1);
00174            }
00175 
00176          if (s != PTHREAD_SCOPE_SYSTEM)
00177            {
00178              printf ("\
00179 contentionscope changed to %d by invalid setscope call\n", s);
00180              exit (1);
00181            }
00182        }
00183 
00184       if (r != PTHREAD_PROCESS_PRIVATE && r != PTHREAD_PROCESS_SHARED)
00185        {
00186          int e = pthread_mutexattr_setpshared (&ma, r);
00187 
00188          if (e == 0)
00189            {
00190              printf ("mutexattr_setpshared with value %ld succeeded\n", r);
00191              exit (1);
00192            }
00193          if (e != EINVAL)
00194            {
00195              puts ("mutexattr_setpshared didn't return EINVAL");
00196              exit (1);
00197            }
00198 
00199          int s;
00200          if (pthread_mutexattr_getpshared (&ma, &s) != 0)
00201            {
00202              puts ("mutexattr_getpshared failed");
00203              exit (1);
00204            }
00205 
00206          if (s != PTHREAD_PROCESS_PRIVATE)
00207            {
00208              printf ("\
00209 pshared changed to %d by invalid mutexattr_setpshared call\n", s);
00210              exit (1);
00211            }
00212 
00213          e = pthread_rwlockattr_setpshared (&rwa, r);
00214 
00215          if (e == 0)
00216            {
00217              printf ("rwlockattr_setpshared with value %ld succeeded\n", r);
00218              exit (1);
00219            }
00220          if (e != EINVAL)
00221            {
00222              puts ("rwlockattr_setpshared didn't return EINVAL");
00223              exit (1);
00224            }
00225 
00226          if (pthread_rwlockattr_getpshared (&rwa, &s) != 0)
00227            {
00228              puts ("rwlockattr_getpshared failed");
00229              exit (1);
00230            }
00231 
00232          if (s != PTHREAD_PROCESS_PRIVATE)
00233            {
00234              printf ("\
00235 pshared changed to %d by invalid rwlockattr_setpshared call\n", s);
00236              exit (1);
00237            }
00238        }
00239 
00240       if (r != PTHREAD_CANCEL_ENABLE && r != PTHREAD_CANCEL_DISABLE)
00241        {
00242          int e = pthread_setcancelstate (r, NULL);
00243 
00244          if (e == 0)
00245            {
00246              printf ("setcancelstate with value %ld succeeded\n", r);
00247              exit (1);
00248            }
00249 
00250          if (e != EINVAL)
00251            {
00252              puts ("setcancelstate didn't return EINVAL");
00253              exit (1);
00254            }
00255 
00256          int s;
00257          if (pthread_setcancelstate (PTHREAD_CANCEL_ENABLE, &s) != 0)
00258            {
00259              puts ("setcancelstate failed for PTHREAD_CANCEL_ENABLE");
00260              exit (1);
00261            }
00262 
00263          if (s != PTHREAD_CANCEL_ENABLE)
00264            {
00265              puts ("invalid setcancelstate changed state");
00266              exit (1);
00267            }
00268        }
00269 
00270       if (r != PTHREAD_CANCEL_DEFERRED && r != PTHREAD_CANCEL_ASYNCHRONOUS)
00271        {
00272          int e = pthread_setcanceltype (r, NULL);
00273 
00274          if (e == 0)
00275            {
00276              printf ("setcanceltype with value %ld succeeded\n", r);
00277              exit (1);
00278            }
00279 
00280          if (e != EINVAL)
00281            {
00282              puts ("setcanceltype didn't return EINVAL");
00283              exit (1);
00284            }
00285 
00286          int s;
00287          if (pthread_setcanceltype (PTHREAD_CANCEL_DEFERRED, &s) != 0)
00288            {
00289              puts ("setcanceltype failed for PTHREAD_CANCEL_DEFERRED");
00290              exit (1);
00291            }
00292 
00293          if (s != PTHREAD_CANCEL_DEFERRED)
00294            {
00295              puts ("invalid setcanceltype changed state");
00296              exit (1);
00297            }
00298        }
00299     }
00300 
00301   return 0;
00302 }
00303 
00304 
00305 #define TEST_FUNCTION do_test ()
00306 #include "../test-skeleton.c"