Back to index

glibc  2.9
tst-tpp.h
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 <pthread.h>
00022 #include <sched.h>
00023 #include <stdio.h>
00024 #include <stdlib.h>
00025 #include <string.h>
00026 #include <unistd.h>
00027 #include <sys/syscall.h>
00028 
00029 /* This test is Linux specific.  */
00030 #define CHECK_TPP_PRIORITY(normal, boosted) \
00031   do                                                    \
00032     {                                                   \
00033       pid_t tid = syscall (__NR_gettid);                \
00034                                                         \
00035       struct sched_param cep_sp;                        \
00036       int cep_policy;                                          \
00037       if (pthread_getschedparam (pthread_self (), &cep_policy, \
00038                              &cep_sp) != 0)                    \
00039        {                                                \
00040          puts ("getschedparam failed");                 \
00041          ret = 1;                                       \
00042        }                                                \
00043       else if (cep_sp.sched_priority != (normal))              \
00044        {                                                \
00045          printf ("unexpected priority %d != %d\n",             \
00046                 cep_sp.sched_priority, (normal));              \
00047        }                                                \
00048       if (syscall (__NR_sched_getparam, tid, &cep_sp) == 0     \
00049          && cep_sp.sched_priority != (boosted))         \
00050        {                                                \
00051          printf ("unexpected boosted priority %d != %d\n",     \
00052                 cep_sp.sched_priority, (boosted));             \
00053          ret = 1;                                       \
00054        }                                                \
00055     }                                                   \
00056   while (0)
00057 
00058 int fifo_min, fifo_max;
00059 
00060 void
00061 init_tpp_test (void)
00062 {
00063   fifo_min = sched_get_priority_min (SCHED_FIFO);
00064   if (fifo_min < 0)
00065     {
00066       printf ("couldn't get min priority for SCHED_FIFO: %m\n");
00067       exit (1);
00068     }
00069 
00070   fifo_max = sched_get_priority_max (SCHED_FIFO);
00071   if (fifo_max < 0)
00072     {
00073       printf ("couldn't get max priority for SCHED_FIFO: %m\n");
00074       exit (1);
00075     }
00076 
00077   if (fifo_min > 4 || fifo_max < 10)
00078     {
00079       printf ("%d..%d SCHED_FIFO priority interval not suitable for this test\n",
00080              fifo_min, fifo_max);
00081       exit (0);
00082     }
00083 
00084   struct sched_param sp;
00085   memset (&sp, 0, sizeof (sp));
00086   sp.sched_priority = 4;
00087   int e = pthread_setschedparam (pthread_self (), SCHED_FIFO, &sp);
00088   if (e != 0)
00089     {
00090       errno = e;
00091       printf ("cannot set scheduling params: %m\n");
00092       exit (0);
00093     }
00094 }