1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198
|
//
// Copyright (c) ZeroC, Inc. All rights reserved.
//
#include <IceUtil/IceUtil.h>
#include <ThreadPriority.h>
#include <TestHelper.h>
using namespace std;
using namespace IceUtil;
class PriorityTestThread : public Thread
{
public:
PriorityTestThread() :
_priority(-1024) //Initialize to some strange value, so by default is not a valid priority
{
}
virtual void run()
{
#ifdef _WIN32
_priority = GetThreadPriority(GetCurrentThread());
#else
sched_param param;
int sched_policy;
pthread_t thread = pthread_self();
pthread_getschedparam(thread, &sched_policy, ¶m);
_priority = param.sched_priority;
#endif
}
int getPriority()
{
return _priority;
}
private:
int _priority;
};
typedef Handle<PriorityTestThread> PriorityTestThreadPtr;
static const string priorityTestName("priority");
ThreadPriorityTest::ThreadPriorityTest() :
TestBase(priorityTestName)
{
#ifdef _WIN32
ThreadControl c;
try
{
PriorityTestThreadPtr t1 = new PriorityTestThread();
c = t1->start(128, THREAD_PRIORITY_IDLE);
c.join();
test(t1->getPriority() == THREAD_PRIORITY_IDLE);
t1 = new PriorityTestThread();
c = t1->start(128, THREAD_PRIORITY_LOWEST);
c.join();
test(t1->getPriority() == THREAD_PRIORITY_LOWEST);
t1 = new PriorityTestThread();
c = t1->start(128, THREAD_PRIORITY_BELOW_NORMAL);
c.join();
test(t1->getPriority() == THREAD_PRIORITY_BELOW_NORMAL);
t1 = new PriorityTestThread();
c = t1->start(128, THREAD_PRIORITY_NORMAL);
c.join();
test(t1->getPriority() == THREAD_PRIORITY_NORMAL);
t1 = new PriorityTestThread();
c = t1->start(128, THREAD_PRIORITY_ABOVE_NORMAL);
c.join();
test(t1->getPriority() == THREAD_PRIORITY_ABOVE_NORMAL);
t1 = new PriorityTestThread();
c = t1->start(128, THREAD_PRIORITY_HIGHEST);
c.join();
test(t1->getPriority() == THREAD_PRIORITY_HIGHEST);
t1 = new PriorityTestThread();
c = t1->start(128, THREAD_PRIORITY_TIME_CRITICAL);
c.join();
test(t1->getPriority() == THREAD_PRIORITY_TIME_CRITICAL);
}
catch(...)
{
test(false);
}
//
// Test to set invalid priorities too high
//
try
{
PriorityTestThreadPtr t1 = new PriorityTestThread();
c = t1->start(128, THREAD_PRIORITY_TIME_CRITICAL + 10);
test(false);
}
catch(const ThreadSyscallException&)
{
//Expected
}
catch(...)
{
test(false);
}
//
// Test to set invalid priorities too low
//
try
{
PriorityTestThreadPtr t1 = new PriorityTestThread();
c = t1->start(128, THREAD_PRIORITY_IDLE - 10);
test(false);
}
catch(const ThreadSyscallException&)
{
//Expected
}
catch(...)
{
test(false);
}
#else
ThreadControl c;
try
{
for(int cont = 1; cont < 10; ++cont)
{
PriorityTestThreadPtr t1 = new PriorityTestThread();
c = t1->start(128, cont);
c.join();
test(t1->getPriority() == cont);
}
}
catch(...)
{
test(false);
}
//
// Test to set invalid priorities too high
//
for(int cont = 1; cont < 10; ++cont)
{
try
{
PriorityTestThreadPtr t1 = new PriorityTestThread();
c = t1->start(128, 300 * cont);
test(false);
}
catch(const ThreadSyscallException& e)
{
//Expected
}
catch(...)
{
test(false);
}
}
//
// Test to set invalid priorities too low
//
for(int cont = 1; cont < 10; ++cont)
{
try
{
PriorityTestThreadPtr t1 = new PriorityTestThread();
c = t1->start(128, -10 * cont);
test(false);
}
catch(const ThreadSyscallException& e)
{
//Expected
}
catch(...)
{
test(false);
}
}
#endif
}
void
ThreadPriorityTest::run()
{
}
|