version 1.51, 2003/10/16 19:21:28
|
version 1.52, 2003/10/17 14:22:49
|
|
|
} | } |
// l10n end | // l10n end |
| |
|
|
|
|
|
// two special synchronization classes for ThreadPool |
|
// |
|
|
|
class timed_mutex |
|
{ |
|
public: |
|
timed_mutex(Mutex* mut, int msec) |
|
:_mut(mut) |
|
{ |
|
_mut->timed_lock(msec, pegasus_thread_self()); |
|
} |
|
~timed_mutex(void) |
|
{ |
|
_mut->unlock(); |
|
} |
|
Mutex* _mut; |
|
}; |
|
|
|
|
|
class try_mutex |
|
{ |
|
public: |
|
try_mutex(Mutex* mut) |
|
:_mut(mut) |
|
{ |
|
_mut->try_lock(pegasus_thread_self()); |
|
} |
|
~try_mutex(void) |
|
{ |
|
_mut->unlock(); |
|
} |
|
|
|
Mutex* _mut; |
|
}; |
|
|
|
|
DQueue<ThreadPool> ThreadPool::_pools(true); | DQueue<ThreadPool> ThreadPool::_pools(true); |
| |
| |
|
|
} | } |
| |
| |
|
// Note: <<< Fri Oct 17 09:19:03 2003 mdd >>> |
|
// the pegasus_yield() calls that preceed each th->join() are to |
|
// give a thread on the running list a chance to reach a cancellation |
|
// point before the join |
| |
ThreadPool::~ThreadPool(void) | ThreadPool::~ThreadPool(void) |
{ | { |
|
|
try | try |
{ | { |
|
// set the dying flag so all thread know the destructor has been entered |
{ | { |
auto_mutex(&(this->_monitor)); | auto_mutex(&(this->_monitor)); |
_dying++; | _dying++; |
} | } |
|
// remove from the global pools list |
_pools.remove(this); | _pools.remove(this); |
|
|
|
// start with idle threads. |
Thread *th = 0; | Thread *th = 0; |
th = _pool.remove_first(); | th = _pool.remove_first(); |
|
Semaphore* sleep_sem; |
|
|
while(th != 0) | while(th != 0) |
{ | { |
Semaphore *sleep_sem = (Semaphore *)th->reference_tsd("sleep sem"); |
sleep_sem = (Semaphore *)th->reference_tsd("sleep sem"); |
|
|
if(sleep_sem == 0) | if(sleep_sem == 0) |
{ | { |
th->dereference_tsd(); | th->dereference_tsd(); |
|
|
| |
// Signal to get the thread out of the work loop. | // Signal to get the thread out of the work loop. |
sleep_sem->signal(); | sleep_sem->signal(); |
|
|
// Signal to get the thread past the end. See the comment | // Signal to get the thread past the end. See the comment |
// "wait to be awakend by the thread pool destructor" | // "wait to be awakend by the thread pool destructor" |
// Note: the current implementation of Thread for Windows | // Note: the current implementation of Thread for Windows |
// does not implement "pthread" cancelation points so this | // does not implement "pthread" cancelation points so this |
// is needed. | // is needed. |
sleep_sem->signal(); | sleep_sem->signal(); |
|
|
th->dereference_tsd(); | th->dereference_tsd(); |
// signal the thread's sleep semaphore |
|
th->cancel(); | th->cancel(); |
th->join(); | th->join(); |
th->empty_tsd(); |
|
delete th; | delete th; |
th = _pool.remove_first(); | th = _pool.remove_first(); |
} | } |
|
|
th = _dead.remove_first(); | th = _dead.remove_first(); |
while(th != 0) | while(th != 0) |
{ | { |
Semaphore *sleep_sem = (Semaphore *)th->reference_tsd("sleep sem"); |
sleep_sem = (Semaphore *)th->reference_tsd("sleep sem"); |
| |
if(sleep_sem == 0) | if(sleep_sem == 0) |
{ | { |
|
|
throw NullPointer(); | throw NullPointer(); |
} | } |
| |
|
// signal the thread's sleep semaphore |
|
sleep_sem->signal(); |
sleep_sem->signal(); | sleep_sem->signal(); |
th->dereference_tsd(); | th->dereference_tsd(); |
|
|
// signal the thread's sleep semaphore |
|
th->cancel(); | th->cancel(); |
th->join(); | th->join(); |
th->empty_tsd(); |
|
delete th; | delete th; |
th = _dead.remove_first(); | th = _dead.remove_first(); |
} | } |
{ |
|
| |
auto_mutex(&(this->_monitor)); |
{ |
th = _running.remove_first(); | th = _running.remove_first(); |
while(th != 0) | while(th != 0) |
{ | { |
// signal the thread's sleep semaphore | // signal the thread's sleep semaphore |
Semaphore *sleep_sem = (Semaphore *)th->reference_tsd("sleep sem"); |
|
|
sleep_sem = (Semaphore *)th->reference_tsd("sleep sem"); |
if(sleep_sem == 0 ) | if(sleep_sem == 0 ) |
{ | { |
th->dereference_tsd(); | th->dereference_tsd(); |
|
|
} | } |
| |
sleep_sem->signal(); | sleep_sem->signal(); |
|
sleep_sem->signal(); |
th->dereference_tsd(); | th->dereference_tsd(); |
|
|
th->cancel(); | th->cancel(); |
|
pegasus_yield(); |
| |
// ensure that th->run() has a chance to execute so that the join will not |
|
// block |
|
th->join(); | th->join(); |
th->empty_tsd(); |
|
delete th; | delete th; |
th = _running.remove_first(); | th = _running.remove_first(); |
} | } |
|
|
catch(...) | catch(...) |
{ | { |
} | } |
|
|
} | } |
| |
// make this static to the class | // make this static to the class |
|
|
PEG_METHOD_EXIT(); | PEG_METHOD_EXIT(); |
throw NullPointer(); | throw NullPointer(); |
} | } |
|
if(pool->_dying.value()) |
|
{ |
|
PEG_METHOD_EXIT(); |
|
return((PEGASUS_THREAD_RETURN)0); |
|
} |
| |
Semaphore *sleep_sem = 0; | Semaphore *sleep_sem = 0; |
Semaphore *blocking_sem = 0; | Semaphore *blocking_sem = 0; |
|
|
deadlock_timer = (struct timeval *)myself->reference_tsd("deadlock timer"); | deadlock_timer = (struct timeval *)myself->reference_tsd("deadlock timer"); |
myself->dereference_tsd(); | myself->dereference_tsd(); |
} | } |
catch(IPCException &) |
|
{ |
|
PEG_METHOD_EXIT(); |
|
return(0); |
|
} |
|
catch(...) | catch(...) |
{ | { |
PEG_METHOD_EXIT(); | PEG_METHOD_EXIT(); |
return(0); |
return((PEGASUS_THREAD_RETURN)0); |
} | } |
| |
if(sleep_sem == 0 || deadlock_timer == 0) | if(sleep_sem == 0 || deadlock_timer == 0) |
{ | { |
PEG_METHOD_EXIT(); | PEG_METHOD_EXIT(); |
throw NullPointer(); |
return((PEGASUS_THREAD_RETURN)0); |
} | } |
| |
while(pool->_dying.value() < 1) | while(pool->_dying.value() < 1) |
{ | { |
|
try |
|
{ |
sleep_sem->wait(); | sleep_sem->wait(); |
|
} |
|
catch(IPCException& ) |
|
{ |
|
PEG_METHOD_EXIT(); |
|
return((PEGASUS_THREAD_RETURN)0); |
|
} |
| |
// when we awaken we reside on the running queue, not the pool queue | // when we awaken we reside on the running queue, not the pool queue |
|
if(pool->_dying.value()) |
|
{ |
|
PEG_METHOD_EXIT(); |
|
return((PEGASUS_THREAD_RETURN)0); |
|
} |
| |
| |
PEGASUS_THREAD_RETURN (PEGASUS_THREAD_CDECL *_work)(void *) = 0; | PEGASUS_THREAD_RETURN (PEGASUS_THREAD_CDECL *_work)(void *) = 0; |
|
|
catch(IPCException &) | catch(IPCException &) |
{ | { |
PEG_METHOD_EXIT(); | PEG_METHOD_EXIT(); |
return(0); |
return((PEGASUS_THREAD_RETURN)0); |
} | } |
| |
if(_work == 0) | if(_work == 0) |
|
|
try | try |
{ | { |
{ | { |
auto_mutex(&(pool->_monitor)); |
timed_mutex(&(pool->_monitor), 1000); |
if(pool->_dying.value()) | if(pool->_dying.value()) |
{ | { |
break; |
_undertaker(parm); |
} | } |
} | } |
_work(parm); | _work(parm); |
|
|
return((PEGASUS_THREAD_RETURN)0); | return((PEGASUS_THREAD_RETURN)0); |
} | } |
| |
|
|
|
|
// put myself back onto the available list | // put myself back onto the available list |
try | try |
{ | { |
auto_mutex(&(pool->_monitor)); |
timed_mutex(&(pool->_monitor), 1000); |
if(pool->_dying.value() == 0) | if(pool->_dying.value() == 0) |
{ | { |
gettimeofday(deadlock_timer, NULL); | gettimeofday(deadlock_timer, NULL); |
|
|
return((PEGASUS_THREAD_RETURN)0); | return((PEGASUS_THREAD_RETURN)0); |
} | } |
} | } |
catch(IPCException &) |
|
{ |
|
PEG_METHOD_EXIT(); |
|
return((PEGASUS_THREAD_RETURN)0); |
|
} |
|
catch(...) | catch(...) |
{ | { |
|
PEG_METHOD_EXIT(); |
return((PEGASUS_THREAD_RETURN)0); | return((PEGASUS_THREAD_RETURN)0); |
} | } |
| |
|
|
myself->test_cancel(); | myself->test_cancel(); |
| |
PEG_METHOD_EXIT(); | PEG_METHOD_EXIT(); |
myself->exit_self(0); |
|
return((PEGASUS_THREAD_RETURN)0); | return((PEGASUS_THREAD_RETURN)0); |
} | } |
| |
|
|
| |
try | try |
{ | { |
auto_mutex(&(this->_monitor)); |
timed_mutex(&(this->_monitor), 1000); |
if(_dying.value()) | if(_dying.value()) |
{ | { |
return; | return; |
|
|
pegasus_yield(); | pegasus_yield(); |
try | try |
{ | { |
auto_mutex(&(this->_monitor)); |
timed_mutex(&(this->_monitor), 1000); |
if(_dying.value()) | if(_dying.value()) |
{ | { |
return; | return; |
|
|
th->put_tsd("blocking sem", NULL, sizeof(Semaphore *), blocking); | th->put_tsd("blocking sem", NULL, sizeof(Semaphore *), blocking); |
try | try |
{ | { |
auto_mutex(&(this->_monitor)); |
timed_mutex(&(this->_monitor), 1000); |
if(_dying.value()) | if(_dying.value()) |
{ | { |
th->cancel(); | th->cancel(); |
|
|
// first go thread the dead q and clean it up as much as possible | // first go thread the dead q and clean it up as much as possible |
try | try |
{ | { |
auto_mutex(&(this->_monitor)); |
timed_mutex(&(this->_monitor), 1000); |
if(_dying.value() ) | if(_dying.value() ) |
{ | { |
return 0; | return 0; |
|
|
for( ; i < 2; i++) | for( ; i < 2; i++) |
#endif | #endif |
{ | { |
auto_mutex(&(this->_monitor)); |
try |
|
{ |
|
try_mutex(&(this->_monitor)); |
|
} |
|
catch(IPCException&) |
|
{ |
|
return bodies; |
|
} |
|
|
q = map[i]; | q = map[i]; |
if(q->count() > 0 ) | if(q->count() > 0 ) |
{ | { |
|
|
} | } |
} | } |
th = q->next(th); | th = q->next(th); |
pegasus_sleep(1); |
pegasus_yield(); |
} | } |
q->unlock(); | q->unlock(); |
} | } |
|
|
try | try |
{ | { |
| |
auto_mutex(&(this->_monitor)); |
timed_mutex(&(this->_monitor), 1000); |
if(_dying.value()) | if(_dying.value()) |
{ | { |
th->cancel(); | th->cancel(); |