Back to index

glibc  2.9
tst-mutexpp10.c
Go to the documentation of this file.
00001 /* Copyright (C) 2006 Free Software Foundation, Inc.
00002    This file is part of the GNU C Library.
00003    Contributed by Jakub Jelinek <jakub@redhat.com>, 2006.
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 <limits.h>
00022 #include <pthread.h>
00023 #include <sched.h>
00024 #include <stdio.h>
00025 #include <stdlib.h>
00026 #include <string.h>
00027 #include <unistd.h>
00028 
00029 #include "tst-tpp.h"
00030 
00031 static int
00032 do_test (void)
00033 {
00034   int ret = 0;
00035 
00036   init_tpp_test ();
00037 
00038   pthread_mutexattr_t ma;
00039   if (pthread_mutexattr_init (&ma))
00040     {
00041       puts ("mutexattr_init failed");
00042       return 1;
00043     }
00044   if (pthread_mutexattr_setprotocol (&ma, PTHREAD_PRIO_PROTECT))
00045     {
00046       puts ("mutexattr_setprotocol failed");
00047       return 1;
00048     }
00049 
00050   int prioceiling;
00051   if (pthread_mutexattr_getprioceiling (&ma, &prioceiling))
00052     {
00053       puts ("mutexattr_getprioceiling failed");
00054       return 1;
00055     }
00056 
00057   if (prioceiling < fifo_min || prioceiling > fifo_max)
00058     {
00059       printf ("prioceiling %d not in %d..%d range\n",
00060              prioceiling, fifo_min, fifo_max);
00061       return 1;
00062     }
00063 
00064   if (fifo_max < INT_MAX
00065       && pthread_mutexattr_setprioceiling (&ma, fifo_max + 1) != EINVAL)
00066     {
00067       printf ("mutexattr_setprioceiling %d did not fail with EINVAL\n",
00068              fifo_max + 1);
00069       return 1;
00070     }
00071   
00072   if (fifo_min > 0
00073       && pthread_mutexattr_setprioceiling (&ma, fifo_min - 1) != EINVAL)
00074     {
00075       printf ("mutexattr_setprioceiling %d did not fail with EINVAL\n",
00076              fifo_min - 1);
00077       return 1;
00078     }
00079 
00080   if (pthread_mutexattr_setprioceiling (&ma, fifo_min))
00081     {
00082       puts ("mutexattr_setprioceiling failed");
00083       return 1;
00084     }
00085 
00086   if (pthread_mutexattr_setprioceiling (&ma, fifo_max))
00087     {
00088       puts ("mutexattr_setprioceiling failed");
00089       return 1;
00090     }
00091 
00092   if (pthread_mutexattr_setprioceiling (&ma, 6))
00093     {
00094       puts ("mutexattr_setprioceiling failed");
00095       return 1;
00096     }
00097 
00098   if (pthread_mutexattr_getprioceiling (&ma, &prioceiling))
00099     {
00100       puts ("mutexattr_getprioceiling failed");
00101       return 1;
00102     }
00103 
00104   if (prioceiling != 6)
00105     {
00106       printf ("mutexattr_getprioceiling returned %d != 6\n",
00107              prioceiling);
00108       return 1;
00109     }
00110 
00111   pthread_mutex_t m1, m2, m3;
00112   int e = pthread_mutex_init (&m1, &ma);
00113   if (e == ENOTSUP)
00114     {
00115       puts ("cannot support selected type of mutexes");
00116       return 0;
00117     }
00118   else if (e != 0)
00119     {
00120       puts ("mutex_init failed");
00121       return 1;
00122     }
00123 
00124   if (pthread_mutexattr_setprioceiling (&ma, 8))
00125     {
00126       puts ("mutexattr_setprioceiling failed");
00127       return 1;
00128     }
00129 
00130   if (pthread_mutex_init (&m2, &ma))
00131     {
00132       puts ("mutex_init failed");
00133       return 1;
00134     }
00135 
00136   if (pthread_mutexattr_setprioceiling (&ma, 5))
00137     {
00138       puts ("mutexattr_setprioceiling failed");
00139       return 1;
00140     }
00141 
00142   if (pthread_mutex_init (&m3, &ma))
00143     {
00144       puts ("mutex_init failed");
00145       return 1;
00146     }
00147 
00148   CHECK_TPP_PRIORITY (4, 4);
00149 
00150   if (pthread_mutex_lock (&m1) != 0)
00151     {
00152       puts ("mutex_lock failed");
00153       return 1;
00154     }
00155 
00156   CHECK_TPP_PRIORITY (4, 6);
00157 
00158   if (pthread_mutex_trylock (&m2) != 0)
00159     {
00160       puts ("mutex_lock failed");
00161       return 1;
00162     }
00163 
00164   CHECK_TPP_PRIORITY (4, 8);
00165 
00166   if (pthread_mutex_lock (&m3) != 0)
00167     {
00168       puts ("mutex_lock failed");
00169       return 1;
00170     }
00171 
00172   CHECK_TPP_PRIORITY (4, 8);
00173 
00174   if (pthread_mutex_unlock (&m2) != 0)
00175     {
00176       puts ("mutex_unlock failed");
00177       return 1;
00178     }
00179 
00180   CHECK_TPP_PRIORITY (4, 6);
00181 
00182   if (pthread_mutex_unlock (&m1) != 0)
00183     {
00184       puts ("mutex_unlock failed");
00185       return 1;
00186     }
00187 
00188   CHECK_TPP_PRIORITY (4, 5);
00189 
00190   if (pthread_mutex_lock (&m2) != 0)
00191     {
00192       puts ("mutex_lock failed");
00193       return 1;
00194     }
00195 
00196   CHECK_TPP_PRIORITY (4, 8);
00197 
00198   if (pthread_mutex_unlock (&m2) != 0)
00199     {
00200       puts ("mutex_unlock failed");
00201       return 1;
00202     }
00203 
00204   CHECK_TPP_PRIORITY (4, 5);
00205 
00206   if (pthread_mutex_getprioceiling (&m1, &prioceiling))
00207     {
00208       puts ("mutex_getprioceiling m1 failed");
00209       return 1;
00210     }
00211   else if (prioceiling != 6)
00212     {
00213       printf ("unexpected m1 prioceiling %d != 6\n", prioceiling);
00214       return 1;
00215     }
00216 
00217   if (pthread_mutex_getprioceiling (&m2, &prioceiling))
00218     {
00219       puts ("mutex_getprioceiling m2 failed");
00220       return 1;
00221     }
00222   else if (prioceiling != 8)
00223     {
00224       printf ("unexpected m2 prioceiling %d != 8\n", prioceiling);
00225       return 1;
00226     }
00227 
00228   if (pthread_mutex_getprioceiling (&m3, &prioceiling))
00229     {
00230       puts ("mutex_getprioceiling m3 failed");
00231       return 1;
00232     }
00233   else if (prioceiling != 5)
00234     {
00235       printf ("unexpected m3 prioceiling %d != 5\n", prioceiling);
00236       return 1;
00237     }
00238 
00239   if (pthread_mutex_setprioceiling (&m1, 7, &prioceiling))
00240     {
00241       printf ("mutex_setprioceiling failed");
00242       return 1;
00243     }
00244   else if (prioceiling != 6)
00245     {
00246       printf ("unexpected m1 old prioceiling %d != 6\n", prioceiling);
00247       return 1;
00248     }
00249     
00250   if (pthread_mutex_getprioceiling (&m1, &prioceiling))
00251     {
00252       puts ("mutex_getprioceiling m1 failed");
00253       return 1;
00254     }
00255   else if (prioceiling != 7)
00256     {
00257       printf ("unexpected m1 prioceiling %d != 7\n", prioceiling);
00258       return 1;
00259     }
00260 
00261   CHECK_TPP_PRIORITY (4, 5);
00262 
00263   if (pthread_mutex_unlock (&m3) != 0)
00264     {
00265       puts ("mutex_unlock failed");
00266       return 1;
00267     }
00268 
00269   CHECK_TPP_PRIORITY (4, 4);
00270 
00271   if (pthread_mutex_trylock (&m1) != 0)
00272     {
00273       puts ("mutex_lock failed");
00274       return 1;
00275     }
00276 
00277   CHECK_TPP_PRIORITY (4, 7);
00278 
00279   struct sched_param sp;
00280   memset (&sp, 0, sizeof (sp));
00281   sp.sched_priority = 8;
00282   if (pthread_setschedparam (pthread_self (), SCHED_FIFO, &sp))
00283     {
00284       puts ("cannot set scheduling params");
00285       return 1;
00286     }
00287 
00288   CHECK_TPP_PRIORITY (8, 8);
00289 
00290   if (pthread_mutex_unlock (&m1) != 0)
00291     {
00292       puts ("mutex_unlock failed");
00293       return 1;
00294     }
00295 
00296   CHECK_TPP_PRIORITY (8, 8);
00297 
00298   if (pthread_mutex_lock (&m3) != EINVAL)
00299     {
00300       puts ("pthread_mutex_lock didn't fail with EINVAL");
00301       return 1;
00302     }
00303 
00304   CHECK_TPP_PRIORITY (8, 8);
00305 
00306   if (pthread_mutex_destroy (&m1) != 0)
00307     {
00308       puts ("mutex_destroy failed");
00309       return 1;
00310     }
00311 
00312   if (pthread_mutex_destroy (&m2) != 0)
00313     {
00314       puts ("mutex_destroy failed");
00315       return 1;
00316     }
00317 
00318   if (pthread_mutex_destroy (&m3) != 0)
00319     {
00320       puts ("mutex_destroy failed");
00321       return 1;
00322     }
00323 
00324   if (pthread_mutexattr_destroy (&ma) != 0)
00325     {
00326       puts ("mutexattr_destroy failed");
00327       return 1;
00328     }
00329 
00330   return ret;
00331 }
00332 
00333 #define TEST_FUNCTION do_test ()
00334 #include "../test-skeleton.c"