diff options
Diffstat (limited to 'cpp/src/IceUtil/Thread.cpp')
-rw-r--r-- | cpp/src/IceUtil/Thread.cpp | 84 |
1 files changed, 64 insertions, 20 deletions
diff --git a/cpp/src/IceUtil/Thread.cpp b/cpp/src/IceUtil/Thread.cpp index 37af697dbb9..08c0021c1de 100644 --- a/cpp/src/IceUtil/Thread.cpp +++ b/cpp/src/IceUtil/Thread.cpp @@ -22,6 +22,11 @@ #include <climits> #include <exception> +#ifndef _WIN32 + #include <sys/time.h> + #include <sys/resource.h> +#endif + using namespace std; #ifdef _WIN32 @@ -181,6 +186,12 @@ WINAPI startHook(void* arg) IceUtil::ThreadControl IceUtil::Thread::start(size_t stackSize) { + return start(stackSize, THREAD_PRIORITY_NORMAL); +} + +IceUtil::ThreadControl +IceUtil::Thread::start(size_t stackSize, int priority) +{ // // Keep this alive for the duration of start // @@ -205,12 +216,11 @@ IceUtil::Thread::start(size_t stackSize) __incRef(); unsigned int id; - _handle = - reinterpret_cast<HANDLE>( - _beginthreadex(0, - static_cast<unsigned int>(stackSize), - startHook, this, 0, &id)); + reinterpret_cast<HANDLE>( + _beginthreadex(0, + static_cast<unsigned int>(stackSize), + startHook, this, CREATE_SUSPENDED, &id)); _id = id; if(_handle == 0) @@ -218,10 +228,19 @@ IceUtil::Thread::start(size_t stackSize) __decRef(); throw ThreadSyscallException(__FILE__, __LINE__, GetLastError()); } + if(SetThreadPriority(_handle, priority) == 0) + { + throw ThreadSyscallException(__FILE__, __LINE__, GetLastError()); + } + if(ResumeThread(_handle) == -1) + { + __decRef(); + throw ThreadSyscallException(__FILE__, __LINE__, GetLastError()); + } _started = true; _running = true; - + return ThreadControl(_handle, _id); } @@ -334,7 +353,7 @@ IceUtil::ThreadControl::detach() IceUtil::ThreadControl::ID IceUtil::ThreadControl::id() const { - return _thread;; + return _thread; } void @@ -408,9 +427,21 @@ startHook(void* arg) } } + IceUtil::ThreadControl IceUtil::Thread::start(size_t stackSize) { + return start(stackSize, false, 0); +} + +IceUtil::ThreadControl +IceUtil::Thread::start(size_t stackSize, int priority) +{ + return start(stackSize, true, priority); +} +IceUtil::ThreadControl +IceUtil::Thread::start(size_t stackSize, bool realtimeScheduling, int priority) +{ // // Keep this alive for the duration of start // @@ -434,15 +465,16 @@ IceUtil::Thread::start(size_t stackSize) // __incRef(); + pthread_attr_t attr; + int rc = pthread_attr_init(&attr); + if(rc != 0) + { + __decRef(); + pthread_attr_destroy(&attr); + throw ThreadSyscallException(__FILE__, __LINE__, rc); + } if(stackSize > 0) { - pthread_attr_t attr; - int rc = pthread_attr_init(&attr); - if(rc != 0) - { - __decRef(); - throw ThreadSyscallException(__FILE__, __LINE__, rc); - } if(stackSize < PTHREAD_STACK_MIN) { stackSize = PTHREAD_STACK_MIN; @@ -457,28 +489,40 @@ IceUtil::Thread::start(size_t stackSize) if(rc != 0) { __decRef(); + pthread_attr_destroy(&attr); throw ThreadSyscallException(__FILE__, __LINE__, rc); } - rc = pthread_create(&_thread, &attr, startHook, this); + } + + if(realtimeScheduling) + { + rc = pthread_attr_setschedpolicy(&attr, SCHED_RR); if(rc != 0) { __decRef(); throw ThreadSyscallException(__FILE__, __LINE__, rc); } - } - else - { - int rc = pthread_create(&_thread, 0, startHook, this); + sched_param param; + param.sched_priority = priority; + rc = pthread_attr_setschedparam(&attr, ¶m); if(rc != 0) { __decRef(); + pthread_attr_destroy(&attr); throw ThreadSyscallException(__FILE__, __LINE__, rc); } + pthread_attr_setinheritsched(&attr, PTHREAD_EXPLICIT_SCHED); + } + rc = pthread_create(&_thread, &attr, startHook, this); + pthread_attr_destroy(&attr); + if(rc != 0) + { + __decRef(); + throw ThreadSyscallException(__FILE__, __LINE__, rc); } _started = true; _running = true; - return ThreadControl(_thread); } |