version 1.90, 2006/07/11 18:39:28
|
version 1.90.2.3, 2006/07/28 20:46:41
|
|
|
| |
#include "Thread.h" | #include "Thread.h" |
#include <exception> | #include <exception> |
#include <Pegasus/Common/IPC.h> |
|
#include <Pegasus/Common/Tracer.h> | #include <Pegasus/Common/Tracer.h> |
|
#include "Time.h" |
| |
#if defined(PEGASUS_OS_TYPE_WINDOWS) | #if defined(PEGASUS_OS_TYPE_WINDOWS) |
# include "ThreadWindows.cpp" | # include "ThreadWindows.cpp" |
|
|
Boolean Thread::_signals_blocked = false; | Boolean Thread::_signals_blocked = false; |
// l10n | // l10n |
#ifndef PEGASUS_OS_ZOS | #ifndef PEGASUS_OS_ZOS |
PEGASUS_THREAD_KEY_TYPE Thread::_platform_thread_key = PEGASUS_THREAD_KEY_TYPE(-1); |
TSDKeyType Thread::_platform_thread_key = TSDKeyType(-1); |
#else | #else |
PEGASUS_THREAD_KEY_TYPE Thread::_platform_thread_key; |
TSDKeyType Thread::_platform_thread_key; |
#endif | #endif |
Boolean Thread::_key_initialized = false; | Boolean Thread::_key_initialized = false; |
Boolean Thread::_key_error = false; | Boolean Thread::_key_error = false; |
|
|
| |
| |
#ifndef PEGASUS_THREAD_EXIT_NATIVE | #ifndef PEGASUS_THREAD_EXIT_NATIVE |
void Thread::exit_self(PEGASUS_THREAD_RETURN exit_code) |
void Thread::exit_self(ThreadReturnType exit_code) |
{ | { |
// execute the cleanup stack and then return | // execute the cleanup stack and then return |
while( _cleanup.size() ) | while( _cleanup.size() ) |
|
|
} | } |
} | } |
_exit_code = exit_code; | _exit_code = exit_code; |
exit_thread(exit_code); |
Threads::exit(exit_code); |
_handle.thid = 0; |
Threads::clear(_handle.thid); |
} | } |
| |
| |
|
|
return -1; | return -1; |
} | } |
| |
if (pegasus_key_create(&Thread::_platform_thread_key) == 0) |
if (TSDKey::create(&Thread::_platform_thread_key) == 0) |
{ | { |
Tracer::trace(TRC_THREAD, Tracer::LEVEL4, | Tracer::trace(TRC_THREAD, Tracer::LEVEL4, |
"Thread: able to create a thread key"); | "Thread: able to create a thread key"); |
|
|
return NULL; | return NULL; |
} | } |
PEG_METHOD_EXIT(); | PEG_METHOD_EXIT(); |
return (Thread *)pegasus_get_thread_specific(_platform_thread_key); |
return (Thread *)TSDKey::get_thread_specific(_platform_thread_key); |
} | } |
| |
void Thread::setCurrent(Thread * thrd) | void Thread::setCurrent(Thread * thrd) |
|
|
PEG_METHOD_ENTER(TRC_THREAD, "Thread::setCurrent"); | PEG_METHOD_ENTER(TRC_THREAD, "Thread::setCurrent"); |
if (Thread::initializeKey() == 0) | if (Thread::initializeKey() == 0) |
{ | { |
if (pegasus_set_thread_specific( |
if (TSDKey::set_thread_specific( |
Thread::_platform_thread_key, (void *) thrd) == 0) | Thread::_platform_thread_key, (void *) thrd) == 0) |
{ | { |
Tracer::trace(TRC_THREAD, Tracer::LEVEL4, | Tracer::trace(TRC_THREAD, Tracer::LEVEL4, |
|
|
} | } |
else | else |
{ | { |
pegasus_yield(); |
Threads::yield(); |
} | } |
} | } |
} | } |
|
|
} | } |
} | } |
| |
PEGASUS_THREAD_RETURN PEGASUS_THREAD_CDECL ThreadPool::_loop(void* parm) |
ThreadReturnType PEGASUS_THREAD_CDECL ThreadPool::_loop(void* parm) |
{ | { |
PEG_METHOD_ENTER(TRC_THREAD, "ThreadPool::_loop"); | PEG_METHOD_ENTER(TRC_THREAD, "ThreadPool::_loop"); |
| |
|
|
pool->_idleThreads.remove(myself); | pool->_idleThreads.remove(myself); |
pool->_currentThreads--; | pool->_currentThreads--; |
PEG_METHOD_EXIT(); | PEG_METHOD_EXIT(); |
return((PEGASUS_THREAD_RETURN)1); |
return((ThreadReturnType)1); |
} | } |
| |
while (1) | while (1) |
|
|
pool->_idleThreads.remove(myself); | pool->_idleThreads.remove(myself); |
pool->_currentThreads--; | pool->_currentThreads--; |
PEG_METHOD_EXIT(); | PEG_METHOD_EXIT(); |
return((PEGASUS_THREAD_RETURN)1); |
return((ThreadReturnType)1); |
} | } |
| |
// When we awaken we reside on the _runningThreads queue, not the | // When we awaken we reside on the _runningThreads queue, not the |
// _idleThreads queue. | // _idleThreads queue. |
| |
PEGASUS_THREAD_RETURN (PEGASUS_THREAD_CDECL* work)(void *) = 0; |
ThreadReturnType (PEGASUS_THREAD_CDECL* work)(void *) = 0; |
void* parm = 0; | void* parm = 0; |
Semaphore* blocking_sem = 0; | Semaphore* blocking_sem = 0; |
| |
try | try |
{ | { |
work = (PEGASUS_THREAD_RETURN (PEGASUS_THREAD_CDECL *)(void *)) |
work = (ThreadReturnType (PEGASUS_THREAD_CDECL *)(void *)) |
myself->reference_tsd("work func"); | myself->reference_tsd("work func"); |
myself->dereference_tsd(); | myself->dereference_tsd(); |
parm = myself->reference_tsd("work parm"); | parm = myself->reference_tsd("work parm"); |
|
|
pool->_idleThreads.remove(myself); | pool->_idleThreads.remove(myself); |
pool->_currentThreads--; | pool->_currentThreads--; |
PEG_METHOD_EXIT(); | PEG_METHOD_EXIT(); |
return((PEGASUS_THREAD_RETURN)1); |
return((ThreadReturnType)1); |
} | } |
| |
if (work == 0) | if (work == 0) |
|
|
break; | break; |
} | } |
| |
gettimeofday(lastActivityTime, NULL); |
Time::gettimeofday(lastActivityTime); |
| |
try | try |
{ | { |
|
|
// put myself back onto the available list | // put myself back onto the available list |
try | try |
{ | { |
gettimeofday(lastActivityTime, NULL); |
Time::gettimeofday(lastActivityTime); |
if (blocking_sem != 0) | if (blocking_sem != 0) |
{ | { |
blocking_sem->signal(); | blocking_sem->signal(); |
|
|
PEGASUS_ASSERT(false); | PEGASUS_ASSERT(false); |
pool->_currentThreads--; | pool->_currentThreads--; |
PEG_METHOD_EXIT(); | PEG_METHOD_EXIT(); |
return((PEGASUS_THREAD_RETURN)1); |
return((ThreadReturnType)1); |
} | } |
} | } |
} | } |
|
|
} | } |
| |
PEG_METHOD_EXIT(); | PEG_METHOD_EXIT(); |
return((PEGASUS_THREAD_RETURN)0); |
return((ThreadReturnType)0); |
} | } |
| |
ThreadStatus ThreadPool::allocate_and_awaken( | ThreadStatus ThreadPool::allocate_and_awaken( |
void* parm, | void* parm, |
PEGASUS_THREAD_RETURN (PEGASUS_THREAD_CDECL* work)(void *), |
ThreadReturnType (PEGASUS_THREAD_CDECL* work)(void *), |
Semaphore* blocking) | Semaphore* blocking) |
{ | { |
PEG_METHOD_ENTER(TRC_THREAD, "ThreadPool::allocate_and_awaken"); | PEG_METHOD_ENTER(TRC_THREAD, "ThreadPool::allocate_and_awaken"); |
|
|
return PEGASUS_THREAD_UNAVAILABLE; | return PEGASUS_THREAD_UNAVAILABLE; |
} | } |
struct timeval start; | struct timeval start; |
gettimeofday(&start, NULL); |
Time::gettimeofday(&start); |
Thread* th = 0; | Thread* th = 0; |
| |
th = _idleThreads.remove_front(); | th = _idleThreads.remove_front(); |
|
|
| |
th->delete_tsd("work func"); | th->delete_tsd("work func"); |
th->put_tsd("work func", NULL, | th->put_tsd("work func", NULL, |
sizeof( PEGASUS_THREAD_RETURN (PEGASUS_THREAD_CDECL *)(void *)), |
sizeof( ThreadReturnType (PEGASUS_THREAD_CDECL *)(void *)), |
(void *)work); | (void *)work); |
th->delete_tsd("work parm"); | th->delete_tsd("work parm"); |
th->put_tsd("work parm", NULL, sizeof(void *), parm); | th->put_tsd("work parm", NULL, sizeof(void *), parm); |
|
|
thread->delete_tsd("work func"); | thread->delete_tsd("work func"); |
thread->put_tsd( | thread->put_tsd( |
"work func", 0, | "work func", 0, |
sizeof(PEGASUS_THREAD_RETURN (PEGASUS_THREAD_CDECL *)(void *)), |
sizeof(ThreadReturnType (PEGASUS_THREAD_CDECL *)(void *)), |
(void *) 0); | (void *) 0); |
thread->delete_tsd("work parm"); | thread->delete_tsd("work parm"); |
thread->put_tsd("work parm", 0, sizeof(void *), 0); | thread->put_tsd("work parm", 0, sizeof(void *), 0); |
|
|
| |
struct timeval now, finish, remaining; | struct timeval now, finish, remaining; |
Uint32 usec; | Uint32 usec; |
pegasus_gettimeofday(&now); |
Time::gettimeofday(&now); |
pegasus_gettimeofday(&remaining); // Avoid valgrind error |
Time::gettimeofday(&remaining); // Avoid valgrind error |
| |
finish.tv_sec = start->tv_sec + interval->tv_sec; | finish.tv_sec = start->tv_sec + interval->tv_sec; |
usec = start->tv_usec + interval->tv_usec; | usec = start->tv_usec + interval->tv_usec; |
|
|
usec %= 1000000; | usec %= 1000000; |
finish.tv_usec = usec; | finish.tv_usec = usec; |
| |
return (timeval_subtract(&remaining, &finish, &now) != 0); |
return (Time::subtract(&remaining, &finish, &now) != 0); |
} | } |
| |
void ThreadPool::_deleteSemaphore(void *p) | void ThreadPool::_deleteSemaphore(void *p) |
|
|
| |
struct timeval* lastActivityTime = | struct timeval* lastActivityTime = |
(struct timeval *) ::operator new(sizeof(struct timeval)); | (struct timeval *) ::operator new(sizeof(struct timeval)); |
pegasus_gettimeofday(lastActivityTime); |
Time::gettimeofday(lastActivityTime); |
| |
th->put_tsd("last activity time", thread_data::default_delete, | th->put_tsd("last activity time", thread_data::default_delete, |
sizeof(struct timeval), (void *)lastActivityTime); | sizeof(struct timeval), (void *)lastActivityTime); |
|
|
return 0; | return 0; |
} | } |
_currentThreads++; | _currentThreads++; |
pegasus_yield(); |
Threads::yield(); |
| |
PEG_METHOD_EXIT(); | PEG_METHOD_EXIT(); |
return th; | return th; |
|
|
} | } |
} | } |
| |
|
// ATTN: not sure where to put this! |
|
#ifdef PEGASUS_ZOS_SECURITY |
|
bool isEnhancedSecurity=99; |
|
#endif |
|
|
PEGASUS_NAMESPACE_END | PEGASUS_NAMESPACE_END |