• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 #include "test.h"
2 
3 enum {
4   PTW32TEST_THREAD_INIT_PRIO = 0,
5   PTW32TEST_MAXPRIORITIES = 512
6 };
7 
8 int minPrio;
9 int maxPrio;
10 int validPriorities[PTW32TEST_MAXPRIORITIES];
11 
12 
func(void * arg)13 void * func(void * arg)
14 {
15   int policy;
16   struct sched_param param;
17 
18   assert(pthread_getschedparam(pthread_self(), &policy, &param) == 0);
19   return (void *) (size_t) param.sched_priority;
20 }
21 
22 
23 void *
getValidPriorities(void * arg)24 getValidPriorities(void * arg)
25 {
26   int prioSet;
27   pthread_t thread = pthread_self();
28   HANDLE threadH = pthread_getw32threadhandle_np(thread);
29   struct sched_param param;
30 
31   for (prioSet = minPrio;
32        prioSet <= maxPrio;
33        prioSet++)
34     {
35         /*
36        * If prioSet is invalid then the threads priority is unchanged
37        * from the previous value. Make the previous value a known
38        * one so that we can check later.
39        */
40         param.sched_priority = prioSet;
41         assert(pthread_setschedparam(thread, SCHED_OTHER, &param) == 0);
42         validPriorities[prioSet+(PTW32TEST_MAXPRIORITIES/2)] = GetThreadPriority(threadH);
43     }
44 
45   return (void *) 0;
46 }
47 
48 
49 int
main()50 main()
51 {
52   pthread_t t;
53   pthread_t mainThread = pthread_self();
54   pthread_attr_t attr;
55   void * result = NULL;
56   struct sched_param param;
57   struct sched_param mainParam;
58   int prio;
59   int policy;
60   int inheritsched = -1;
61   pthread_t threadID = pthread_self();
62   HANDLE threadH = pthread_getw32threadhandle_np(threadID);
63 
64   assert((maxPrio = sched_get_priority_max(SCHED_OTHER)) != -1);
65   assert((minPrio = sched_get_priority_min(SCHED_OTHER)) != -1);
66 
67   assert(pthread_create(&t, NULL, getValidPriorities, NULL) == 0);
68   assert(pthread_join(t, &result) == 0);
69 
70   assert(pthread_attr_init(&attr) == 0);
71   assert(pthread_attr_setinheritsched(&attr, PTHREAD_INHERIT_SCHED) == 0);
72   assert(pthread_attr_getinheritsched(&attr, &inheritsched) == 0);
73   assert(inheritsched == PTHREAD_INHERIT_SCHED);
74 
75   for (prio = minPrio; prio <= maxPrio; prio++)
76     {
77       mainParam.sched_priority = prio;
78 
79       /* Set the thread's priority to a known initial value. */
80       SetThreadPriority(threadH, PTW32TEST_THREAD_INIT_PRIO);
81 
82       /* Change the main thread priority */
83       assert(pthread_setschedparam(mainThread, SCHED_OTHER, &mainParam) == 0);
84       assert(pthread_getschedparam(mainThread, &policy, &mainParam) == 0);
85       assert(policy == SCHED_OTHER);
86       /* Priority returned below should be the level set by pthread_setschedparam(). */
87       assert(mainParam.sched_priority == prio);
88       assert(GetThreadPriority(threadH) ==
89                validPriorities[prio+(PTW32TEST_MAXPRIORITIES/2)]);
90       if (prio > minPrio && validPriorities[prio+(PTW32TEST_MAXPRIORITIES/2)] ==
91 	validPriorities[prio-1+(PTW32TEST_MAXPRIORITIES/2)])
92 	continue;
93       for (param.sched_priority = prio;
94            param.sched_priority <= maxPrio;
95            param.sched_priority++)
96         {
97           /* The new thread create should ignore this new priority */
98           assert(pthread_attr_setschedparam(&attr, &param) == 0);
99           assert(pthread_create(&t, &attr, func, NULL) == 0);
100           pthread_join(t, &result);
101           assert(((int) (size_t) result) == mainParam.sched_priority);
102         }
103 	fprintf(stderr, "."); fflush(stderr);
104     }
105 
106   return 0;
107 }
108