#include <ThreadPosix.h>
Inheritance diagram for vpr::ThreadPosix:


Public Methods | |
| ThreadPosix (VPRThreadPriority priority=VPR_PRIORITY_NORMAL, VPRThreadScope scope=VPR_GLOBAL_THREAD, VPRThreadState state=VPR_JOINABLE_THREAD, size_t stack_size=0) | |
| Non-spawning constructor. More... | |
| ThreadPosix (thread_func_t func, void *arg=NULL, VPRThreadPriority priority=VPR_PRIORITY_NORMAL, VPRThreadScope scope=VPR_GLOBAL_THREAD, VPRThreadState state=VPR_JOINABLE_THREAD, size_t stack_size=0) | |
| Spawning constructor with argument. More... | |
| ThreadPosix (BaseThreadFunctor *functorPtr, VPRThreadPriority priority=VPR_PRIORITY_NORMAL, VPRThreadScope scope=VPR_GLOBAL_THREAD, VPRThreadState state=VPR_JOINABLE_THREAD, size_t stack_size=0) | |
| Spawning constructor (functor version). More... | |
| virtual | ~ThreadPosix () |
| Destructor. More... | |
| virtual void | setFunctor (BaseThreadFunctor *functorPtr) |
| Sets the functor that this thread will execute. More... | |
| virtual vpr::ReturnStatus | start () |
| Starts this thread's execution. More... | |
| virtual int | join (void **status=0) |
| Makes the calling thread wait for the termination of this thread. More... | |
| virtual int | resume (void) |
| Resumes the execution of a thread that was previously suspended using suspend(). More... | |
| virtual int | suspend (void) |
| Suspends the execution of this thread. More... | |
| virtual int | getPrio (VPRThreadPriority *prio) |
| Gets this thread's priority. More... | |
| virtual int | setPrio (VPRThreadPriority prio) |
| Sets this thread's priority. More... | |
| virtual int | setRunOn (int cpu) |
| Sets the CPU affinity for this thread (the CPU on which this thread will exclusively run). More... | |
| virtual int | getRunOn (int *cur_cpu) |
| Gets the CPU affinity for this thread (the CPU on which this thread exclusively runs). More... | |
| virtual int | kill (int signum) |
| Sends the specified signal to this thread (not necessarily SIGKILL). More... | |
| virtual void | kill () |
| Kills (cancels) this thread. More... | |
| std::ostream & | outStream (std::ostream &out) |
| Provides a way of printing the process ID neatly. More... | |
Static Public Methods | |
| void | yield () |
| Yields execution of the calling thread to allow a different blocked thread to execute. More... | |
| BaseThread * | self () |
| Get a ptr to the thread we are in. More... | |
Protected Methods | |
| vpr::ReturnStatus | spawn (BaseThreadFunctor *functorPtr) |
| Creates a new thread that will execute functorPtr. More... | |
| void | startThread (void *nullParam) |
| Called by the spawn routine to start the user thread function. More... | |
Functions by recieving a function in the constructor that is the function to call when the new thread is created. This function is stored internally to the class, then the class is "boot-strapped" by spawning a call to the startThread function with in turn will call the previously set thread function
Definition at line 80 of file ThreadPosix.h.
|
||||||||||||||||||||
|
Non-spawning constructor. This will not start a thread. Definition at line 75 of file ThreadPosix.cpp.
00077 : mUserThreadFunctor(NULL)
00078 , mDeleteThreadFunctor(false)
00079 , mRunning(false)
00080 , mPriority(priority)
00081 , mScope(scope)
00082 , mState(state)
00083 , mStackSize(stackSize)
00084 , mThreadStartCompleted(false)
00085 , mStartFunctor(NULL)
00086 {
00087 /* Do nothing. */ ;
00088 }
|
|
||||||||||||||||||||||||||||
|
Spawning constructor with argument. This will start a new thread that will execute the specified function. Definition at line 92 of file ThreadPosix.cpp. References setFunctor, start, and vpr::thread_func_t.
00095 : mUserThreadFunctor(NULL)
00096 , mDeleteThreadFunctor(false)
00097 , mRunning(false)
00098 , mPriority(priority)
00099 , mScope(scope)
00100 , mState(state)
00101 , mStackSize(stackSize)
00102 , mThreadStartCompleted(false)
00103 , mStartFunctor(NULL)
00104 {
00105 // Create the thread functor to start. This will be deleted in the
00106 // destructor.
00107 setFunctor(new ThreadNonMemberFunctor(func, arg));
00108 mDeleteThreadFunctor = true;
00109 start();
00110 }
|
|
||||||||||||||||||||||||
|
Spawning constructor (functor version). This will start a new thread that will execute the function encapsulated by the functor. Definition at line 114 of file ThreadPosix.cpp. References setFunctor, and start.
00117 : mUserThreadFunctor(NULL)
00118 , mDeleteThreadFunctor(false)
00119 , mRunning(false)
00120 , mPriority(priority)
00121 , mScope(scope)
00122 , mState(state)
00123 , mStackSize(stackSize)
00124 , mThreadStartCompleted(false)
00125 , mStartFunctor(NULL)
00126 {
00127 setFunctor(functorPtr);
00128 start();
00129 }
|
|
|
Destructor.
Definition at line 132 of file ThreadPosix.cpp.
00133 {
00134 if ( mDeleteThreadFunctor )
00135 {
00136 delete mUserThreadFunctor;
00137 }
00138
00139 if ( NULL != mStartFunctor )
00140 {
00141 delete mStartFunctor;
00142 }
00143 }
|
|
|
Sets the functor that this thread will execute.
Implements vpr::BaseThread. Definition at line 145 of file ThreadPosix.cpp. References vprASSERT. Referenced by ThreadPosix.
|
|
|
Starts this thread's execution.
Implements vpr::BaseThread. Definition at line 153 of file ThreadPosix.cpp. References vpr::CondVarPosix::acquire, vpr::ReturnStatus::Fail, vpr::BaseThread::registerThread, vpr::CondVarPosix::release, vpr::ReturnStatus::setCode, spawn, vpr::ReturnStatus::success, vprASSERT, and vpr::CondVarPosix::wait. Referenced by ThreadPosix.
00154 {
00155 vpr::ReturnStatus status;
00156
00157 if ( mRunning )
00158 {
00159 vprASSERT(false && "Thread already started");
00160 status.setCode(vpr::ReturnStatus::Fail);
00161 }
00162 else if ( NULL == mUserThreadFunctor )
00163 {
00164 vprASSERT(false && "No functor set");
00165 status.setCode(vpr::ReturnStatus::Fail);
00166 }
00167 else
00168 {
00169 mStartFunctor =
00170 new ThreadMemberFunctor<ThreadPosix>(this, &ThreadPosix::startThread,
00171 NULL);
00172
00173 // Spawn the thread. If the thread is spawned successfully, the method
00174 // startThread() will register the actual thread info.
00175 mThreadStartCompleted = false; // Make sure this is set correctly
00176 status = spawn(mStartFunctor);
00177
00178 // Thread spawned successfully.
00179 if ( status.success() )
00180 {
00181 // startThread() will register the thread, so we wait for
00182 // registration to complete here.
00183 mThreadStartCondVar.acquire();
00184 {
00185 while ( ! mThreadStartCompleted )
00186 {
00187 mThreadStartCondVar.wait();
00188 }
00189 }
00190 mThreadStartCondVar.release();
00191
00192 mRunning = true;
00193 }
00194 // Thread spawning failed. Yikes!
00195 else
00196 {
00197 ThreadManager::instance()->lock();
00198 {
00199 registerThread(false);
00200 }
00201 ThreadManager::instance()->unlock();
00202 }
00203 }
00204
00205 return status;
00206 }
|
|
|
Creates a new thread that will execute functorPtr.
Definition at line 209 of file ThreadPosix.cpp. References vpr::ReturnStatus::Fail, sched_param_t, vpr::ReturnStatus::setCode, and vpr::vprThreadFunctorFunction. Referenced by start.
00210 {
00211 vpr::ReturnStatus status;
00212 int ret_val;
00213 pthread_attr_t thread_attrs;
00214
00215 int pthread_prio = vprThreadPriorityToPOSIX(mPriority);
00216
00217 // Initialize thread_attrs and set the priority of the thread if it is
00218 // supported.
00219 sched_param_t prio_param;
00220
00221 pthread_attr_init(&thread_attrs);
00222 pthread_attr_setdetachstate(&thread_attrs, vprThreadStateToPOSIX(mState));
00223
00224 // If thread priority scheduling is available, set the thread's priority
00225 // if it is set to be higher than 0.
00226 # ifdef _POSIX_THREAD_PRIORITY_SCHEDULING
00227 int thread_scope = vprThreadScopeToPOSIX(mScope);
00228
00229 # if defined(HAVE_SYS_CAPABILITY_H) && ! defined(VPR_OS_FreeBSD) && \
00230 ! defined(VPR_OS_Linux)
00231 cap_t capabilities = cap_get_proc();
00232
00233 // If we have the capability to do so, set the scope of the threads
00234 // to system scope.
00235 if ( capabilities->cap_effective & CAP_SCHED_MGT )
00236 {
00237 thread_scope = PTHREAD_SCOPE_SYSTEM;
00238 }
00239 # endif /* HAVE_SYS_CAPABILITY_H */
00240
00241 pthread_attr_setscope(&thread_attrs, thread_scope);
00242
00243 if ( pthread_prio > 0 )
00244 {
00245 prio_param.sched_priority = pthread_prio;
00246 pthread_attr_setschedparam(&thread_attrs, &prio_param);
00247 }
00248 # endif /* _POSIX_THREAD_PRIORITY_SCHEDULING */
00249
00250 // Set the stack size if a value greater than 0 is specified and this
00251 // pthreads implementation supports it. Ensure that
00252 // _POSIX_THREAD_ATTR_STACKSIZE is defined before trying to test its
00253 // value.
00254 #ifdef _POSIX_THREAD_ATTR_STACKSIZE
00255 if ( mStackSize > 0 )
00256 {
00257 // XXX: ** STACK SIZE CHECK NEEDED **
00258
00259 pthread_attr_setstacksize(&thread_attrs, mStackSize);
00260 }
00261 #endif
00262
00263 // Finally create the thread.
00264 ret_val = pthread_create(&(mThread), &thread_attrs,
00265 vprThreadFunctorFunction, (void *) functorPtr);
00266
00267 // Inform the caller if the thread was not created successfully.
00268 if ( ret_val != 0 )
00269 {
00270 status.setCode(vpr::ReturnStatus::Fail);
00271 std::cerr << "vpr::ThreadPosix::spawn() - Cannot create thread:"
00272 << strerror(ret_val) << std::endl;
00273 }
00274
00275 return status;
00276 }
|
|
|
Called by the spawn routine to start the user thread function.
Definition at line 279 of file ThreadPosix.cpp. References vpr::CondVarPosix::acquire, vpr::BaseThread::registerThread, vpr::CondVarPosix::release, and vpr::CondVarPosix::signal.
00280 {
00281 boost::ignore_unused_variable_warning(nullParam);
00282
00283 // WE are a new thread... yeah!!!!
00284 // TELL EVERYONE THAT WE LIVE!!!!
00285 ThreadManager::instance()->lock(); // Lock manager
00286 {
00287 threadIdKey().setspecific((void*)this); // Store the pointer to me
00288 registerThread(true); // Finish thread initialization
00289 }
00290 ThreadManager::instance()->unlock();
00291
00292 // Signal that thread registration is complete.
00293 mThreadStartCondVar.acquire();
00294 {
00295 mThreadStartCompleted = true;
00296 mThreadStartCondVar.signal();
00297 }
00298 mThreadStartCondVar.release();
00299
00300 // --- CALL USER FUNCTOR --- //
00301 (*mUserThreadFunctor)();
00302 }
|
|
|
Makes the calling thread wait for the termination of this thread.
Reimplemented from vpr::BaseThread. Definition at line 192 of file ThreadPosix.h.
00193 {
00194 return pthread_join(mThread, status);
00195 }
|
|
|
Resumes the execution of a thread that was previously suspended using suspend().
Reimplemented from vpr::BaseThread. Definition at line 211 of file ThreadPosix.h. References kill.
00212 {
00213 return kill(SIGCONT);
00214 }
|
|
|
Suspends the execution of this thread.
Reimplemented from vpr::BaseThread. Definition at line 228 of file ThreadPosix.h. References kill.
00229 {
00230 return kill(SIGSTOP);
00231 }
|
|
|
Gets this thread's priority.
Reimplemented from vpr::BaseThread. Definition at line 305 of file ThreadPosix.cpp. References sched_param_t.
00306 {
00307 #ifdef _POSIX_THREAD_PRIORITY_SCHEDULING
00308 int policy, ret_val;
00309 sched_param_t fifo_sched_param;
00310
00311 ret_val = pthread_getschedparam(mThread, &policy, &fifo_sched_param);
00312 *prio = posixThreadPriorityToVPR(fifo_sched_param.sched_priority);
00313
00314 return ret_val;
00315 #else
00316 std::cerr << "vpr::ThreadPosix::getPrio(): Not supported\n";
00317
00318 return -1;
00319 #endif
00320 }
|
|
|
Sets this thread's priority.
Reimplemented from vpr::BaseThread. Definition at line 323 of file ThreadPosix.cpp. References sched_param_t.
00324 {
00325 #ifdef _POSIX_THREAD_PRIORITY_SCHEDULING
00326 sched_param_t sched_param;
00327 sched_param.sched_priority = prio;
00328
00329 return pthread_setschedparam(mThread, SCHED_RR, &sched_param);
00330 #else
00331 boost::ignore_unused_variable_warning(prio);
00332 std::cerr << "vpr::ThreadPosix::setPrio(): Not supported\n";
00333
00334 return -1;
00335 #endif
00336 }
|
|
|
Sets the CPU affinity for this thread (the CPU on which this thread will exclusively run).
Definition at line 338 of file ThreadPosix.cpp.
00339 {
00340 #ifdef VPR_OS_IRIX
00341 int ret_val;
00342
00343 if ( mScope == PTHREAD_SCOPE_SYSTEM )
00344 {
00345 ret_val = pthread_setrunon_np(cpu);
00346 }
00347 else
00348 {
00349 std::cerr << "This thread is not a system-scope thread!\n";
00350 ret_val = -1;
00351 }
00352
00353 return ret_val;
00354 #else
00355 boost::ignore_unused_variable_warning(cpu);
00356 std::cerr << "vpr::ThreadPosix::setRunOn(): Not available on this system.\n";
00357
00358 return -1;
00359 #endif
00360 }
|
|
|
Gets the CPU affinity for this thread (the CPU on which this thread exclusively runs).
Definition at line 362 of file ThreadPosix.cpp.
00363 {
00364 #ifdef VPR_OS_IRIX
00365 int ret_val;
00366
00367 if ( mScope == PTHREAD_SCOPE_SYSTEM )
00368 {
00369 ret_val = pthread_getrunon_np(cur_cpu);
00370 }
00371 else
00372 {
00373 std::cerr << "This thread is not a system-scope thread!\n";
00374 ret_val = -1;
00375 }
00376
00377 return ret_val;
00378 #else
00379 boost::ignore_unused_variable_warning(cur_cpu);
00380 std::cerr << "vpr::ThreadPosix::getRunOn(): Not available on this system.\n";
00381
00382 return -1;
00383 #endif
00384 }
|
|
|
Yields execution of the calling thread to allow a different blocked thread to execute.
Definition at line 311 of file ThreadPosix.h.
00312 {
00313 sched_yield();
00314 }
|
|
|
Sends the specified signal to this thread (not necessarily SIGKILL).
Reimplemented from vpr::BaseThread. Definition at line 386 of file ThreadPosix.cpp.
00387 {
00388 return pthread_kill(mThread, signum);
00389 }
|
|
|
Kills (cancels) this thread.
Reimplemented from vpr::BaseThread. Definition at line 346 of file ThreadPosix.h. Referenced by resume, and suspend.
00347 {
00348 pthread_cancel(mThread);
00349 }
|
|
|
Get a ptr to the thread we are in.
Definition at line 391 of file ThreadPosix.cpp. References vpr::BaseThread::BaseThread, and vprASSERT.
00392 {
00393 vprASSERT((statics.mStaticsInitialized==1221) && "Trying to call vpr::ThreadPosix::self before statics are initialized. Don't do that");
00394
00395 BaseThread* my_thread;
00396 threadIdKey().getspecific((void**)&my_thread);
00397
00398 return my_thread;
00399 }
|
|
|
Provides a way of printing the process ID neatly.
Reimplemented from vpr::BaseThread. Definition at line 402 of file ThreadPosix.cpp.
00403 {
00404 out.setf(std::ios::right);
00405 out << std::setw(7) << std::setfill('0') << getpid() << "/";
00406 out.unsetf(std::ios::right);
00407 BaseThread::outStream(out);
00408 out << std::setfill(' ');
00409 return out;
00410 }
|
1.2.14 written by Dimitri van Heesch,
© 1997-2002