(file) Return to Thread.cpp CVS log (file) (dir) Up to [Pegasus] / pegasus / src / Pegasus / Common

Diff for /pegasus/src/Pegasus/Common/Thread.cpp between version 1.57 and 1.87.4.1

version 1.57, 2003/10/23 05:59:53 version 1.87.4.1, 2006/01/18 17:37:56
Line 1 
Line 1 
 //%2003////////////////////////////////////////////////////////////////////////  //%2005////////////////////////////////////////////////////////////////////////
 // //
 // Copyright (c) 2000, 2001, 2002  BMC Software, Hewlett-Packard Development  // Copyright (c) 2000, 2001, 2002 BMC Software; Hewlett-Packard Development
 // Company, L. P., IBM Corp., The Open Group, Tivoli Systems.  // Company, L.P.; IBM Corp.; The Open Group; Tivoli Systems.
 // Copyright (c) 2003 BMC Software; Hewlett-Packard Development Company, L. P.; // Copyright (c) 2003 BMC Software; Hewlett-Packard Development Company, L. P.;
 // IBM Corp.; EMC Corporation, The Open Group. // IBM Corp.; EMC Corporation, The Open Group.
   // Copyright (c) 2004 BMC Software; Hewlett-Packard Development Company, L.P.;
   // IBM Corp.; EMC Corporation; VERITAS Software Corporation; The Open Group.
   // Copyright (c) 2005 Hewlett-Packard Development Company, L.P.; IBM Corp.;
   // EMC Corporation; VERITAS Software Corporation; The Open Group.
 // //
 // Permission is hereby granted, free of charge, to any person obtaining a copy // Permission is hereby granted, free of charge, to any person obtaining a copy
 // of this software and associated documentation files (the "Software"), to // of this software and associated documentation files (the "Software"), to
Line 27 
Line 31 
 // //
 // Modified By: Rudy Schuet (rudy.schuet@compaq.com) 11/12/01 // Modified By: Rudy Schuet (rudy.schuet@compaq.com) 11/12/01
 //              added nsk platform support //              added nsk platform support
   //              Roger Kumpf, Hewlett-Packard Company (roger_kumpf@hp.com)
   //              Amit K Arora, IBM (amita@in.ibm.com) for PEP#101
   //              Sean Keenan, Hewlett-Packard Company (sean.keenan@hp.com)
   //              David Dillard, VERITAS Software Corp.
   //                  (david.dillard@veritas.com)
 // //
 //%///////////////////////////////////////////////////////////////////////////// //%/////////////////////////////////////////////////////////////////////////////
  
 #include "Thread.h" #include "Thread.h"
   #include <exception>
 #include <Pegasus/Common/IPC.h> #include <Pegasus/Common/IPC.h>
 #include <Pegasus/Common/Tracer.h> #include <Pegasus/Common/Tracer.h>
  
Line 40 
Line 50 
 # include "ThreadUnix.cpp" # include "ThreadUnix.cpp"
 #elif defined(PEGASUS_OS_TYPE_NSK) #elif defined(PEGASUS_OS_TYPE_NSK)
 # include "ThreadNsk.cpp" # include "ThreadNsk.cpp"
   #elif defined(PEGASUS_OS_VMS)
   # include "ThreadVms.cpp"
 #else #else
 # error "Unsupported platform" # error "Unsupported platform"
 #endif #endif
  
   PEGASUS_USING_STD;
 PEGASUS_NAMESPACE_BEGIN PEGASUS_NAMESPACE_BEGIN
  
  
Line 58 
Line 71 
 { {
    if( data != NULL)    if( data != NULL)
    {    {
       AcceptLanguages * al = static_cast<AcceptLanguages *>(data);        AutoPtr<AcceptLanguageList> al(static_cast<AcceptLanguageList *>(data));
       delete al;  
    }    }
 } }
 // l10n end // l10n end
  
 Boolean Thread::_signals_blocked = false; Boolean Thread::_signals_blocked = false;
 // l10n // l10n
 PEGASUS_THREAD_KEY_TYPE Thread::_platform_thread_key = -1;  #ifndef PEGASUS_OS_ZOS
   PEGASUS_THREAD_KEY_TYPE Thread::_platform_thread_key = PEGASUS_THREAD_KEY_TYPE(-1);
   #else
   PEGASUS_THREAD_KEY_TYPE Thread::_platform_thread_key;
   #endif
 Boolean Thread::_key_initialized = false; Boolean Thread::_key_initialized = false;
 Boolean Thread::_key_error = false; Boolean Thread::_key_error = false;
  
  
 // for non-native implementations  void Thread::cleanup_push( void (*routine)(void *), void *parm)
 #ifndef PEGASUS_THREAD_CLEANUP_NATIVE  
 void Thread::cleanup_push( void (*routine)(void *), void *parm) throw(IPCException)  
 {  
     cleanup_handler *cu = new cleanup_handler(routine, parm);  
     try  
     {  
         _cleanup.insert_first(cu);  
     }  
     catch(IPCException&)  
     {     {
         delete cu;      AutoPtr<cleanup_handler> cu(new cleanup_handler(routine, parm));
         throw;      _cleanup.insert_first(cu.get());
     }      cu.release();
     return;     return;
 } }
  
 void Thread::cleanup_pop(Boolean execute) throw(IPCException)  void Thread::cleanup_pop(Boolean execute)
 { {
     cleanup_handler *cu ;      AutoPtr<cleanup_handler> cu;
     try     try
     {     {
         cu = _cleanup.remove_first() ;          cu.reset(_cleanup.remove_first());
     }     }
     catch(IPCException&)     catch(IPCException&)
     {     {
Line 101 
Line 108 
      }      }
     if(execute == true)     if(execute == true)
         cu->execute();         cu->execute();
     delete cu;  
 } }
  
 #endif  
   
  
 //thread_data *Thread::put_tsd(const Sint8 *key, void (*delete_func)(void *), Uint32 size, void *value) throw(IPCException)  //thread_data *Thread::put_tsd(const Sint8 *key, void (*delete_func)(void *), Uint32 size, void *value)
  
  
 #ifndef PEGASUS_THREAD_EXIT_NATIVE #ifndef PEGASUS_THREAD_EXIT_NATIVE
Line 182 
Line 186 
    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(Thread::_platform_thread_key,          if (pegasus_set_thread_specific(
                                                                  (void *) thrd) == 0)                 Thread::_platform_thread_key, (void *) thrd) == 0)
         {         {
                 Tracer::trace(TRC_THREAD, Tracer::LEVEL4,                 Tracer::trace(TRC_THREAD, Tracer::LEVEL4,
                           "Successful set Thread * into thread specific storage");                           "Successful set Thread * into thread specific storage");
Line 191 
Line 195 
         else         else
         {         {
                 Tracer::trace(TRC_THREAD, Tracer::LEVEL4,                 Tracer::trace(TRC_THREAD, Tracer::LEVEL4,
                           "ERROR: got error setting Thread * into thread specific storage");                  "ERROR: error setting Thread * into thread specific storage");
         }         }
    }    }
    PEG_METHOD_EXIT();    PEG_METHOD_EXIT();
 } }
  
 AcceptLanguages * Thread::getLanguages()  AcceptLanguageList * Thread::getLanguages()
 { {
     PEG_METHOD_ENTER(TRC_THREAD, "Thread::getLanguages");     PEG_METHOD_ENTER(TRC_THREAD, "Thread::getLanguages");
  
         Thread * curThrd = Thread::getCurrent();         Thread * curThrd = Thread::getCurrent();
         if (curThrd == NULL)         if (curThrd == NULL)
                 return NULL;                 return NULL;
         AcceptLanguages * acceptLangs =      AcceptLanguageList * acceptLangs =
                  (AcceptLanguages *)curThrd->reference_tsd("acceptLanguages");          (AcceptLanguageList *)curThrd->reference_tsd("acceptLanguages");
         curThrd->dereference_tsd();         curThrd->dereference_tsd();
     PEG_METHOD_EXIT();     PEG_METHOD_EXIT();
         return acceptLangs;         return acceptLangs;
 } }
  
 void Thread::setLanguages(AcceptLanguages *langs) //l10n  void Thread::setLanguages(AcceptLanguageList *langs) //l10n
 { {
    PEG_METHOD_ENTER(TRC_THREAD, "Thread::setLanguages");    PEG_METHOD_ENTER(TRC_THREAD, "Thread::setLanguages");
  
Line 221 
Line 225 
                 // deletes the old tsd and creates a new one                 // deletes the old tsd and creates a new one
                 currentThrd->put_tsd("acceptLanguages",                 currentThrd->put_tsd("acceptLanguages",
                         language_delete,                         language_delete,
                         sizeof(AcceptLanguages *),              sizeof(AcceptLanguageList *),
                         langs);                         langs);
    }    }
  
Line 243 
Line 247 
 } }
 // l10n end // l10n end
  
 #if 0  
 // 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;  
 };  
 #endif  
   
 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);  
  
   ///////////////////////////////////////////////////////////////////////////////
   //
   // ThreadPool
   //
   ///////////////////////////////////////////////////////////////////////////////
  
 void ThreadPool::kill_idle_threads(void)  ThreadPool::ThreadPool(
       Sint16 initialSize,
       const char* key,
       Sint16 minThreads,
       Sint16 maxThreads,
       struct timeval& deallocateWait)
       : _maxThreads(maxThreads),
         _minThreads(minThreads),
         _currentThreads(0),
         _idleThreads(true),
         _runningThreads(true),
         _dying(0)
 { {
    static struct timeval now, last = {0, 0};      _deallocateWait.tv_sec = deallocateWait.tv_sec;
       _deallocateWait.tv_usec = deallocateWait.tv_usec;
  
    pegasus_gettimeofday(&now);  
    if(now.tv_sec - last.tv_sec > 5)  
    {  
       _pools.lock();  
       ThreadPool *p = _pools.next(0);  
       while(p != 0)  
       {  
          try  
          {  
             p->kill_dead_threads();  
          }  
          catch(...)  
          {  
          }  
          p = _pools.next(p);  
       }  
       _pools.unlock();  
       pegasus_gettimeofday(&last);  
    }  
 }  
   
   
 ThreadPool::ThreadPool(Sint16 initial_size,  
                        const Sint8 *key,  
                        Sint16 min,  
                        Sint16 max,  
                        struct timeval & alloc_wait,  
                        struct timeval & dealloc_wait,  
                        struct timeval & deadlock_detect)  
    : _max_threads(max), _min_threads(min),  
      _current_threads(0),  
      _pool(true), _running(true),  
      _dead(true), _dying(0)  
 {  
    _allocate_wait.tv_sec = alloc_wait.tv_sec;  
    _allocate_wait.tv_usec = alloc_wait.tv_usec;  
    _deallocate_wait.tv_sec = dealloc_wait.tv_sec;  
    _deallocate_wait.tv_usec = dealloc_wait.tv_usec;  
    _deadlock_detect.tv_sec = deadlock_detect.tv_sec;  
    _deadlock_detect.tv_usec = deadlock_detect.tv_usec;  
    memset(_key, 0x00, 17);    memset(_key, 0x00, 17);
    if(key != 0)    if(key != 0)
       strncpy(_key, key, 16);  
    if(_max_threads > 0 && _max_threads < initial_size)  
       _max_threads = initial_size;  
    if(_min_threads > initial_size)  
       _min_threads = initial_size;  
   
    int i;  
    for(i = 0; i < initial_size; i++)  
    {    {
       _link_pool(_init_thread());          strncpy(_key, key, 16);
    }  
    _pools.insert_last(this);  
 } }
  
       if ((_maxThreads > 0) && (_maxThreads < initialSize))
 // 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)  
 {  
    PEG_METHOD_ENTER(TRC_THREAD, "Thread::~ThreadPool");  
    try  
    {  
       // Set the dying flag so all thread know the destructor has been entered  
       {       {
          // ThreadPool::~ThreadPool will wait to set the _dying flag until          _maxThreads = initialSize;
          // it can acquire the lock. Once this lock is acquired, the thread  
          // acquiring the lock must be able to assume that the value of  
          // _dying will not change until the lock is released.  
   
          // Once the _dying flag is set, functions (e.g., kill_dead_threads,  
          // and allocate_and_awaken) that manipulate the ThreadPool  
          // queues (_pool, _dead, and _running), should not be allowed  
          // to run.  
   
          Tracer::trace(TRC_THREAD, Tracer::LEVEL4,  
              "Attempting to aquire _monitor lock");  
          auto_mutex dying_lock(&(this->_monitor));  
          Tracer::trace(TRC_THREAD, Tracer::LEVEL4,  
              "Acquired _monitor lock");  
          _dying++;  
       }       }
       // remove from the global pools list  
       _pools.remove(this);  
  
       // start with idle threads.      if (_minThreads > initialSize)
       Thread *th = 0;  
       th = _pool.remove_first();  
       Semaphore* sleep_sem;  
   
       while(th != 0)  
       {  
          sleep_sem = (Semaphore *)th->reference_tsd("sleep sem");  
          PEGASUS_ASSERT(sleep_sem != 0);  
   
          if(sleep_sem == 0)  
          {          {
             th->dereference_tsd();          _minThreads = initialSize;
          }          }
          else  
          {  
             // Signal to get the thread out of the work loop.  
             sleep_sem->signal();  
  
             // Signal to get the thread past the end. See the comment      for (int i = 0; i < initialSize; i++)
             // "wait to be awakend by the thread pool destructor"      {
             // Note: the current implementation of Thread for Windows          _addToIdleThreadsQueue(_initializeThread());
             // does not implement "pthread" cancelation points so this  
             // is needed.  
             sleep_sem->signal();  
             th->dereference_tsd();  
             th->cancel();  
             th->join();  
             delete th;  
          }          }
          th = _pool.remove_first();  
       }       }
  
       th = _dead.remove_first();  ThreadPool::~ThreadPool()
       while(th != 0)  
       {       {
          sleep_sem = (Semaphore *)th->reference_tsd("sleep sem");      PEG_METHOD_ENTER(TRC_THREAD, "ThreadPool::~ThreadPool");
          PEGASUS_ASSERT(sleep_sem != 0);  
   
          if(sleep_sem == 0)  
          {  
             th->dereference_tsd();  
          }  
          else  
          {  
             //ATTN-DME-P3-20030322: _dead queue processing in  
             //ThreadPool::~ThreadPool is inconsistent with the  
             //processing in kill_dead_threads.  Is this correct?  
   
             // signal the thread's sleep semaphore  
             sleep_sem->signal();  
             sleep_sem->signal();  
             th->dereference_tsd();  
             th->cancel();  
             th->join();  
             delete th;  
          }  
          th = _dead.remove_first();  
       }  
  
       try
       {       {
          th = _running.remove_first();          // Set the dying flag so all thread know the destructor has been entered
          while(th != 0)          _dying++;
          Tracer::trace(TRC_THREAD, Tracer::LEVEL2,
                   "Cleaning up %d idle threads. ", _currentThreads.get());
           while (_currentThreads.get() > 0)
          {          {
             // signal the thread's sleep semaphore              Thread* thread = _idleThreads.remove_first();
               if (thread != 0)
             sleep_sem = (Semaphore *)th->reference_tsd("sleep sem");  
             PEGASUS_ASSERT(sleep_sem != 0);  
   
             if(sleep_sem == 0 )  
             {             {
                th->dereference_tsd();                  _cleanupThread(thread);
                   _currentThreads--;
             }             }
             else             else
             {             {
                sleep_sem->signal();  
                sleep_sem->signal();  
                th->dereference_tsd();  
                th->cancel();  
                pegasus_yield();                pegasus_yield();
   
                th->join();  
                delete th;  
             }  
             th = _running.remove_first();  
          }          }
       }       }
    }    }
   
    catch(...)    catch(...)
    {    {
    }    }
 } }
  
 // make this static to the class  
 PEGASUS_THREAD_RETURN PEGASUS_THREAD_CDECL ThreadPool::_loop(void *parm) PEGASUS_THREAD_RETURN PEGASUS_THREAD_CDECL ThreadPool::_loop(void *parm)
 { {
    PEG_METHOD_ENTER(TRC_THREAD, "ThreadPool::_loop");    PEG_METHOD_ENTER(TRC_THREAD, "ThreadPool::_loop");
  
    Thread *myself = (Thread *)parm;      try
    if(myself == 0)  
    {    {
       Tracer::trace(TRC_DISCARDED_DATA, Tracer::LEVEL2,          Thread* myself = (Thread *)parm;
           "ThreadPool::_loop: Thread pointer is null");          PEGASUS_ASSERT(myself != 0);
       PEG_METHOD_EXIT();  
       throw NullPointer();  
    }  
  
 // l10n  
    // Set myself into thread specific storage    // Set myself into thread specific storage
    // This will allow code to get its own Thread    // This will allow code to get its own Thread
    Thread::setCurrent(myself);    Thread::setCurrent(myself);
  
    ThreadPool *pool = (ThreadPool *)myself->get_parm();    ThreadPool *pool = (ThreadPool *)myself->get_parm();
    if(pool == 0 )          PEGASUS_ASSERT(pool != 0);
    {  
       Tracer::trace(TRC_DISCARDED_DATA, Tracer::LEVEL2,  
           "ThreadPool::_loop: ThreadPool pointer is null");  
       PEG_METHOD_EXIT();  
       throw NullPointer();  
    }  
    if(pool->_dying.value())  
    {  
       Tracer::trace(TRC_DISCARDED_DATA, Tracer::LEVEL2,  
           "ThreadPool::_loop: ThreadPool is dying(1)");  
       PEG_METHOD_EXIT();  
       return((PEGASUS_THREAD_RETURN)0);  
    }  
  
    Semaphore *sleep_sem = 0;    Semaphore *sleep_sem = 0;
    Semaphore *blocking_sem = 0;          struct timeval* lastActivityTime = 0;
   
    struct timeval *deadlock_timer = 0;  
  
    try    try
    {    {
       sleep_sem = (Semaphore *)myself->reference_tsd("sleep sem");       sleep_sem = (Semaphore *)myself->reference_tsd("sleep sem");
       myself->dereference_tsd();       myself->dereference_tsd();
       deadlock_timer = (struct timeval *)myself->reference_tsd("deadlock timer");              PEGASUS_ASSERT(sleep_sem != 0);
   
               lastActivityTime =
                   (struct timeval *)myself->reference_tsd("last activity time");
       myself->dereference_tsd();       myself->dereference_tsd();
               PEGASUS_ASSERT(lastActivityTime != 0);
    }    }
   
    catch(...)    catch(...)
    {    {
       Tracer::trace(TRC_DISCARDED_DATA, Tracer::LEVEL2,       Tracer::trace(TRC_DISCARDED_DATA, Tracer::LEVEL2,
           "ThreadPool::_loop: Failure getting sleep_sem or deadlock_timer");                  "ThreadPool::_loop: Failure getting sleep_sem or "
                       "lastActivityTime.");
               PEGASUS_ASSERT(false);
               pool->_idleThreads.remove(myself);
               pool->_currentThreads--;
       PEG_METHOD_EXIT();       PEG_METHOD_EXIT();
       return((PEGASUS_THREAD_RETURN)0);              return((PEGASUS_THREAD_RETURN)1);
    }  
   
    if(sleep_sem == 0 || deadlock_timer == 0)  
    {  
       Tracer::trace(TRC_DISCARDED_DATA, Tracer::LEVEL2,  
           "ThreadPool::_loop: sleep_sem or deadlock_timer are null.");  
       PEG_METHOD_EXIT();  
       return((PEGASUS_THREAD_RETURN)0);  
    }    }
  
    while(1)    while(1)
Line 540 
Line 369 
       {       {
          sleep_sem->wait();          sleep_sem->wait();
       }       }
       catch(IPCException& )              catch (...)
       {       {
          Tracer::trace(TRC_DISCARDED_DATA, Tracer::LEVEL2,          Tracer::trace(TRC_DISCARDED_DATA, Tracer::LEVEL2,
           "ThreadPool::_loop: failure on sleep_sem->wait().");           "ThreadPool::_loop: failure on sleep_sem->wait().");
                   PEGASUS_ASSERT(false);
                   pool->_idleThreads.remove(myself);
                   pool->_currentThreads--;
          PEG_METHOD_EXIT();          PEG_METHOD_EXIT();
          return((PEGASUS_THREAD_RETURN)0);                  return((PEGASUS_THREAD_RETURN)1);
       }       }
  
       // when we awaken we reside on the running queue, not the pool queue              // When we awaken we reside on the _runningThreads queue, not the
               // _idleThreads queue.
  
       PEGASUS_THREAD_RETURN (PEGASUS_THREAD_CDECL *_work)(void *) = 0;              PEGASUS_THREAD_RETURN (PEGASUS_THREAD_CDECL* work)(void *) = 0;
       void *parm = 0;       void *parm = 0;
               Semaphore* blocking_sem = 0;
  
       try       try
       {       {
          _work = (PEGASUS_THREAD_RETURN (PEGASUS_THREAD_CDECL *)(void *)) \                  work = (PEGASUS_THREAD_RETURN (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");
          myself->dereference_tsd();          myself->dereference_tsd();
          blocking_sem = (Semaphore *)myself->reference_tsd("blocking sem");          blocking_sem = (Semaphore *)myself->reference_tsd("blocking sem");
          myself->dereference_tsd();          myself->dereference_tsd();
   
       }       }
       catch(IPCException &)              catch (...)
       {       {
          Tracer::trace(TRC_DISCARDED_DATA, Tracer::LEVEL2,          Tracer::trace(TRC_DISCARDED_DATA, Tracer::LEVEL2,
            "ThreadPool::_loop: Failure accessing work func, work parm, or blocking sem.");                      "ThreadPool::_loop: Failure accessing work func, work parm, "
                           "or blocking sem.");
                   PEGASUS_ASSERT(false);
                   pool->_idleThreads.remove(myself);
                   pool->_currentThreads--;
          PEG_METHOD_EXIT();          PEG_METHOD_EXIT();
          return((PEGASUS_THREAD_RETURN)0);                  return((PEGASUS_THREAD_RETURN)1);
       }       }
  
       if(_work == 0)              if (work == 0)
       {       {
          Tracer::trace(TRC_DISCARDED_DATA, Tracer::LEVEL2,                  Tracer::trace(TRC_THREAD, Tracer::LEVEL4,
            "ThreadPool::_loop: work func is null.");                      "ThreadPool::_loop: work func is 0, meaning we should exit.");
          PEG_METHOD_EXIT();                  break;
          return((PEGASUS_THREAD_RETURN)0);  
       }       }
  
       if(_work ==              gettimeofday(lastActivityTime, NULL);
          (PEGASUS_THREAD_RETURN (PEGASUS_THREAD_CDECL *)(void *)) &_undertaker)  
               try
       {       {
          PEG_METHOD_EXIT();                  PEG_TRACE_STRING(TRC_THREAD, Tracer::LEVEL4, "Work starting.");
          _work(parm);                  work(parm);
                   PEG_TRACE_STRING(TRC_THREAD, Tracer::LEVEL4, "Work finished.");
       }       }
               catch (Exception & e)
       gettimeofday(deadlock_timer, NULL);  
   
       if (pool->_dying.value() == 0)  
       {       {
          try                  PEG_TRACE_STRING(TRC_DISCARDED_DATA, Tracer::LEVEL2,
                       String("Exception from work in ThreadPool::_loop: ") +
                           e.getMessage());
               }
   #if !defined(PEGASUS_OS_LSB)
               catch (const exception& e)
          {          {
             _work(parm);                  PEG_TRACE_STRING(TRC_DISCARDED_DATA, Tracer::LEVEL2,
                       String("Exception from work in ThreadPool::_loop: ") +
                           e.what());
          }          }
   #endif
          catch(...)          catch(...)
          {          {
             Tracer::trace(TRC_DISCARDED_DATA, Tracer::LEVEL2,                  PEG_TRACE_STRING(TRC_DISCARDED_DATA, Tracer::LEVEL2,
               "ThreadPool::_loop: execution of _work failed.");                      "Unknown exception from work in ThreadPool::_loop.");
             PEG_METHOD_EXIT();  
             return((PEGASUS_THREAD_RETURN)0);  
          }  
        }        }
  
       // put myself back onto the available list       // put myself back onto the available list
       try       try
       {       {
          if(pool->_dying.value() == 0)                  gettimeofday(lastActivityTime, NULL);
          {  
             gettimeofday(deadlock_timer, NULL);  
             if( blocking_sem != 0 )             if( blocking_sem != 0 )
                   {
                blocking_sem->signal();                blocking_sem->signal();
                   }
  
             // If we are not on _running then ~ThreadPool has removed                  Boolean removed = pool->_runningThreads.remove((void *)myself);
             // us and now "owns" our pointer.                  PEGASUS_ASSERT(removed);
             if ( pool->_running.remove((void *)myself) != 0 )  
             {                  pool->_idleThreads.insert_first(myself);
                pool->_pool.insert_first(myself);  
             }             }
             else              catch (...)
             {             {
                   Tracer::trace(TRC_DISCARDED_DATA, Tracer::LEVEL2,
                       "ThreadPool::_loop: Adding thread to idle pool failed.");
                   PEGASUS_ASSERT(false);
                   pool->_currentThreads--;
                PEG_METHOD_EXIT();                PEG_METHOD_EXIT();
                return((PEGASUS_THREAD_RETURN)0);                  return((PEGASUS_THREAD_RETURN)1);
             }             }
          }          }
          else  
          {  
             PEG_METHOD_EXIT();  
             return((PEGASUS_THREAD_RETURN)0);  
          }          }
       catch (const Exception& e)
       {
           PEG_TRACE_STRING(TRC_DISCARDED_DATA, Tracer::LEVEL2,
               "Caught exception: \"" + e.getMessage() + "\".  Exiting _loop.");
       }       }
       catch(...)       catch(...)
       {       {
         Tracer::trace(TRC_DISCARDED_DATA, Tracer::LEVEL2,          PEG_TRACE_STRING(TRC_DISCARDED_DATA, Tracer::LEVEL2,
              "ThreadPool::_loop: Adding thread to idle pool failed.");              "Caught unrecognized exception.  Exiting _loop.");
          PEG_METHOD_EXIT();  
          return((PEGASUS_THREAD_RETURN)0);  
       }       }
  
    }  
   
    // TODO: Why is this needed? Why not just continue?  
    // wait to be awakend by the thread pool destructor  
    //sleep_sem->wait();  
   
    myself->test_cancel();  
   
    PEG_METHOD_EXIT();    PEG_METHOD_EXIT();
    return((PEGASUS_THREAD_RETURN)0);    return((PEGASUS_THREAD_RETURN)0);
 } }
  
 void ThreadPool::allocate_and_awaken(void *parm,  ThreadStatus ThreadPool::allocate_and_awaken(
                                      PEGASUS_THREAD_RETURN \      void* parm,
                                      (PEGASUS_THREAD_CDECL *work)(void *),      PEGASUS_THREAD_RETURN (PEGASUS_THREAD_CDECL* work)(void *),
                                      Semaphore *blocking)                                      Semaphore *blocking)
   
    throw(IPCException)  
 { {
    PEG_METHOD_ENTER(TRC_THREAD, "ThreadPool::allocate_and_awaken");    PEG_METHOD_ENTER(TRC_THREAD, "ThreadPool::allocate_and_awaken");
  
Line 666 
Line 497 
  
    try    try
    {    {
       auto_mutex dying_lock(&(this->_monitor));          if (_dying.get())
       if (_dying.value())  
       {       {
          Tracer::trace(TRC_DISCARDED_DATA, Tracer::LEVEL2,          Tracer::trace(TRC_DISCARDED_DATA, Tracer::LEVEL2,
           "ThreadPool::allocate_and_awaken: ThreadPool is dying(1).");           "ThreadPool::allocate_and_awaken: ThreadPool is dying(1).");
          return;              return PEGASUS_THREAD_UNAVAILABLE;
       }       }
       struct timeval now;  
       struct timeval start;       struct timeval start;
       gettimeofday(&start, NULL);       gettimeofday(&start, NULL);
       Thread *th = 0;       Thread *th = 0;
  
       th = _pool.remove_first();          th = _idleThreads.remove_first();
  
       // wait for the right interval and try again          if (th == 0)
       while (th == 0)  
       {       {
          // will throw an IPCException&              if ((_maxThreads == 0) ||
          _check_deadlock(&start) ;                  (_currentThreads.get() < Uint32(_maxThreads)))
   
          if(_max_threads == 0 || _current_threads < _max_threads)  
          {          {
             th = _init_thread();                  th = _initializeThread();
             continue;  
          }          }
          pegasus_yield();          }
          th = _pool.remove_first();  
           if (th == 0)
           {
               // ATTN-DME-P3-20031103: This trace message should not be
               // be labeled TRC_DISCARDED_DATA, because it does not
               // necessarily imply that a failure has occurred.  However,
               // this label is being used temporarily to help isolate
               // the cause of client timeout problems.
               Tracer::trace(TRC_DISCARDED_DATA, Tracer::LEVEL2,
                   "ThreadPool::allocate_and_awaken: Insufficient resources: "
                       " pool = %s, running threads = %d, idle threads = %d",
                   _key, _runningThreads.count(), _idleThreads.count());
               return PEGASUS_THREAD_INSUFFICIENT_RESOURCES;
       }       }
  
       // initialize the thread data with the work function and parameters       // initialize the thread data with the work function and parameters
Line 711 
Line 548 
            th->put_tsd("blocking sem", NULL, sizeof(Semaphore *), blocking);            th->put_tsd("blocking sem", NULL, sizeof(Semaphore *), blocking);
  
       // put the thread on the running list       // put the thread on the running list
       _running.insert_first(th);          _runningThreads.insert_first(th);
  
       // signal the thread's sleep semaphore to awaken it       // signal the thread's sleep semaphore to awaken it
       Semaphore *sleep_sem = (Semaphore *)th->reference_tsd("sleep sem");       Semaphore *sleep_sem = (Semaphore *)th->reference_tsd("sleep sem");
           PEGASUS_ASSERT(sleep_sem != 0);
  
       if(sleep_sem == 0)  
       {  
          th->dereference_tsd();  
          Tracer::trace(TRC_DISCARDED_DATA, Tracer::LEVEL2,  
            "ThreadPool::allocate_and_awaken: thread data is corrupted.");  
          PEG_METHOD_EXIT();  
          throw NullPointer();  
       }  
       Tracer::trace(TRC_THREAD, Tracer::LEVEL4, "Signal thread to awaken");       Tracer::trace(TRC_THREAD, Tracer::LEVEL4, "Signal thread to awaken");
       sleep_sem->signal();       sleep_sem->signal();
       th->dereference_tsd();       th->dereference_tsd();
Line 733 
Line 563 
       Tracer::trace(TRC_DISCARDED_DATA, Tracer::LEVEL2,       Tracer::trace(TRC_DISCARDED_DATA, Tracer::LEVEL2,
           "ThreadPool::allocate_and_awaken: Operation Failed.");           "ThreadPool::allocate_and_awaken: Operation Failed.");
       PEG_METHOD_EXIT();       PEG_METHOD_EXIT();
       return;          // ATTN: Error result has not yet been defined
           return PEGASUS_THREAD_SETUP_FAILURE;
    }    }
    PEG_METHOD_EXIT();    PEG_METHOD_EXIT();
       return PEGASUS_THREAD_OK;
 } }
  
 // caller is responsible for only calling this routine during slack periods // caller is responsible for only calling this routine during slack periods
 // but should call it at least once per _deadlock_detect with the running q  // but should call it at least once per _deallocateWait interval.
 // and at least once per _deallocate_wait for the pool q  
  
 Uint32 ThreadPool::kill_dead_threads(void)  Uint32 ThreadPool::cleanupIdleThreads()
          throw(IPCException)  
 { {
    // Since the kill_dead_threads, ThreadPool or allocate_and_awaken      PEG_METHOD_ENTER(TRC_THREAD, "ThreadPool::cleanupIdleThreads");
    // manipulate the threads on the ThreadPool queues, they should never  
    // be allowed to run at the same time.  
   
    // kill_dead_threads will "hold" the _monitor lock until it has  
    // completed executing. ~ThreadPool will not set the dying flag until  
    // it can get access to the lock.  
  
    try      Uint32 numThreadsCleanedUp = 0;
    {  
       try_mutex dying_lock(&(this->_monitor));  
       if (_dying.value())  
       {  
          return 0;  
       }  
   
       struct timeval now;  
       gettimeofday(&now, NULL);  
       Uint32 bodies = 0;  
  
       // first go thread the dead q and clean it up as much as possible      Uint32 numIdleThreads = _idleThreads.count();
       try      for (Uint32 i = 0; i < numIdleThreads; i++)
       {       {
          while(_dead.count() > 0)          // Do not dip below the minimum thread count
          {          if (_currentThreads.get() <= (Uint32)_minThreads)
             Tracer::trace(TRC_THREAD, Tracer::LEVEL4, "ThreadPool:: removing and joining dead thread");  
             Thread *dead = _dead.remove_first();  
   
             if(dead == 0)  
                throw NullPointer();  
             dead->join();  
             delete dead;  
          }  
       }  
       catch(...)  
       {       {
               break;
       }       }
  
           Thread* thread = _idleThreads.remove_last();
  
       DQueue<Thread> * map[2] =          // If there are no more threads in the _idleThreads queue, we're done.
          {          if (thread == 0)
             &_pool, &_running  
          };  
   
   
       DQueue<Thread> *q = 0;  
       int i = 0;  
       AtomicInt needed(0);  
   
 #ifdef PEGASUS_DISABLE_KILLING_HUNG_THREADS  
       // This change prevents the thread pool from killing "hung" threads.  
       // The definition of a "hung" thread is one that has been on the run queue  
       // for longer than the time interval set when the thread pool was created.  
       // Cancelling "hung" threads has proven to be problematic.  
   
       // With this change the thread pool will not cancel "hung" threads.  This  
       // may prevent a crash depending upon the state of the "hung" thread.  In  
       // the case that the thread is actually hung, this change causes the  
       // thread resources not to be reclaimed.  
   
       // Idle threads, those that have not executed a routine for a time  
       // interval, continue to be destroyed.  This is normal and should not  
       // cause any problems.  
       for( ; i < 1; i++)  
 #else  
       for( ; i < 2; i++)  
 #endif  
       {  
          q = map[i];  
          if(q->count() > 0 )  
          {  
             try  
             {             {
                q->try_lock();              break;
             }  
             catch(...)  
             {  
                return bodies;  
             }             }
  
             struct timeval dt = { 0, 0 };          struct timeval* lastActivityTime;
             struct timeval *dtp;  
             Thread *th = 0;  
             th = q->next(th);  
             while (th != 0 )  
             {  
                try                try
                {                {
                   dtp = (struct timeval *)th->try_reference_tsd("deadlock timer");              lastActivityTime = (struct timeval *)thread->try_reference_tsd(
                   "last activity time");
               PEGASUS_ASSERT(lastActivityTime != 0);
                }                }
                catch(...)                catch(...)
                {                {
                   q->unlock();              PEGASUS_ASSERT(false);
                   return bodies;              _idleThreads.insert_last(thread);
               break;
                }                }
  
                if(dtp != 0)          Boolean cleanupThisThread =
                {              _timeIntervalExpired(lastActivityTime, &_deallocateWait);
                   memcpy(&dt, dtp, sizeof(struct timeval));          thread->dereference_tsd();
                }  
                th->dereference_tsd();  
                struct timeval deadlock_timeout;  
                Boolean too_long;  
                if( i == 0)  
                {  
                   too_long = check_time(&dt, get_deallocate_wait(&deadlock_timeout));  
                }  
                else  
                {  
                   too_long = check_time(&dt, get_deadlock_detect(&deadlock_timeout));  
                }  
  
                if( true == too_long)          if (cleanupThisThread)
                {  
                   // if we are deallocating from the pool, escape if we are  
                   // down to the minimum thread count  
                   _current_threads--;  
                   if( _current_threads.value() < (Uint32)_min_threads )  
                   {                   {
                      if( i == 0)              _cleanupThread(thread);
                      {              _currentThreads--;
                         _current_threads++;              numThreadsCleanedUp++;
                         th = q->next(th);  
                         continue;  
                      }                      }
                      else                      else
                      {                      {
                         // we are killing a hung thread and we will drop below the              _idleThreads.insert_first(thread);
                         // minimum. create another thread to make up for the one  
                         // we are about to kill  
                         needed++;  
                      }                      }
                   }                   }
  
                   th = q->remove_no_lock((void *)th);      PEG_METHOD_EXIT();
       return numThreadsCleanedUp;
   }
  
                   if(th != 0)  void ThreadPool::_cleanupThread(Thread* thread)
                   {  
                      if( i == 0 )  
                      {                      {
                         th->delete_tsd("work func");      PEG_METHOD_ENTER(TRC_THREAD, "ThreadPool::cleanupThread");
                         th->put_tsd("work func", NULL,  
       // Set the "work func" and "work parm" to 0 so _loop() knows to exit.
       thread->delete_tsd("work func");
       thread->put_tsd(
           "work func", 0,
                                  sizeof( PEGASUS_THREAD_RETURN (PEGASUS_THREAD_CDECL *)(void *)),                                  sizeof( PEGASUS_THREAD_RETURN (PEGASUS_THREAD_CDECL *)(void *)),
                                  (void *)&_undertaker);          (void *) 0);
                         th->delete_tsd("work parm");      thread->delete_tsd("work parm");
                         th->put_tsd("work parm", NULL, sizeof(void *), th);      thread->put_tsd("work parm", 0, sizeof(void *), 0);
  
                         // signal the thread's sleep semaphore to awaken it                         // signal the thread's sleep semaphore to awaken it
                         Semaphore *sleep_sem = (Semaphore *)th->reference_tsd("sleep sem");      Semaphore* sleep_sem = (Semaphore *)thread->reference_tsd("sleep sem");
       PEGASUS_ASSERT(sleep_sem != 0);
                         if(sleep_sem == 0)  
                         {  
                            q->unlock();  
                            th->dereference_tsd();  
                            throw NullPointer();  
                         }  
   
                         bodies++;  
                         th->dereference_tsd();  
                         _dead.insert_first(th);  
                         sleep_sem->signal();                         sleep_sem->signal();
                         th = 0;      thread->dereference_tsd();
                      }  
                      else  
                      {  
                         // deadlocked threads  
                         Tracer::trace(TRC_THREAD, Tracer::LEVEL4, "Killing a deadlocked thread");  
                         th->cancel();  
                         delete th;  
                      }  
                   }  
                }  
                th = q->next(th);  
                pegasus_yield();  
             }  
             q->unlock();  
          }  
       }  
  
       while (needed.value() > 0)   {      thread->join();
          _link_pool(_init_thread());      delete thread;
          needed--;  
          pegasus_sleep(0);  
       }  
        return bodies;  
     }  
     catch (AlreadyLocked &)  
     {  
        // kill_dead_threads was not able to obtain the  
        // _monitor lock that controls access to the  
        // _dying flag and the ThreadPool queues.  
        // This means that one of the queue manipulating  
        // functions (e.g., ~ThreadPool or allocate_and_awaken)  
        // is running.  Since cleanup is opportunistic, this  
        // function just returns.  
     }  
     return 0;  
 }  
  
       PEG_METHOD_EXIT();
   }
  
 Boolean ThreadPool::check_time(struct timeval *start, struct timeval *interval)  Boolean ThreadPool::_timeIntervalExpired(
       struct timeval* start,
       struct timeval* interval)
 { {
    // never time out if the interval is zero    // never time out if the interval is zero
    if(interval && interval->tv_sec == 0 && interval->tv_usec == 0)      if (interval && (interval->tv_sec == 0) && (interval->tv_usec == 0))
       {
       return false;       return false;
       }
  
    struct timeval now , finish , remaining ;    struct timeval now , finish , remaining ;
    Uint32 usec;    Uint32 usec;
    pegasus_gettimeofday(&now);    pegasus_gettimeofday(&now);
    /* remove valgrind error */      pegasus_gettimeofday(&remaining);    // Avoid valgrind error
    pegasus_gettimeofday(&remaining);  
   
  
    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;
Line 964 
Line 676 
    usec %= 1000000;    usec %= 1000000;
    finish.tv_usec = usec;    finish.tv_usec = usec;
  
    if ( timeval_subtract(&remaining, &finish, &now) )      return (timeval_subtract(&remaining, &finish, &now) != 0);
       return true;  
    else  
       return false;  
 } }
  
 PEGASUS_THREAD_RETURN ThreadPool::_undertaker( void *parm )  void ThreadPool::_deleteSemaphore(void *p)
 {  
    exit_thread((PEGASUS_THREAD_RETURN)1);  
    return (PEGASUS_THREAD_RETURN)1;  
 }  
   
   
  void ThreadPool::_sleep_sem_del(void *p)  
 {  
    if(p != 0)  
    {    {
       delete (Semaphore *)p;       delete (Semaphore *)p;
    }    }
 }  
  
  void ThreadPool::_check_deadlock(struct timeval *start) throw(Deadlock)  Thread* ThreadPool::_initializeThread()
 { {
    if (true == check_time(start, &_deadlock_detect))      PEG_METHOD_ENTER(TRC_THREAD, "ThreadPool::_initializeThread");
       throw Deadlock(pegasus_thread_self());  
    return;  
 }  
   
   
  Boolean ThreadPool::_check_deadlock_no_throw(struct timeval *start)  
 {  
    return(check_time(start, &_deadlock_detect));  
 }  
  
  Boolean ThreadPool::_check_dealloc(struct timeval *start)  
 {  
    return(check_time(start, &_deallocate_wait));  
 }  
   
  Thread *ThreadPool::_init_thread(void) throw(IPCException)  
 {  
    Thread *th = (Thread *) new Thread(_loop, this, false);    Thread *th = (Thread *) new Thread(_loop, this, false);
   
    // allocate a sleep semaphore and pass it in the thread context    // allocate a sleep semaphore and pass it in the thread context
    // initial count is zero, loop function will sleep until    // initial count is zero, loop function will sleep until
    // we signal the semaphore    // we signal the semaphore
    Semaphore *sleep_sem = (Semaphore *) new Semaphore(0);    Semaphore *sleep_sem = (Semaphore *) new Semaphore(0);
    th->put_tsd("sleep sem", &_sleep_sem_del, sizeof(Semaphore), (void *)sleep_sem);      th->put_tsd(
           "sleep sem", &_deleteSemaphore, sizeof(Semaphore), (void *)sleep_sem);
  
    struct timeval *dldt = (struct timeval *) ::operator new(sizeof(struct timeval));      struct timeval* lastActivityTime =
    pegasus_gettimeofday(dldt);          (struct timeval *) ::operator new(sizeof(struct timeval));
       pegasus_gettimeofday(lastActivityTime);
  
    th->put_tsd("deadlock timer", thread_data::default_delete, sizeof(struct timeval), (void *)dldt);      th->put_tsd("last activity time", thread_data::default_delete,
    // thread will enter _loop(void *) and sleep on sleep_sem until we signal it          sizeof(struct timeval), (void *)lastActivityTime);
       // thread will enter _loop() and sleep on sleep_sem until we signal it
  
    th->run();      if (th->run() != PEGASUS_THREAD_OK)
    _current_threads++;      {
                   Tracer::trace(TRC_THREAD, Tracer::LEVEL2,
                           "Could not create thread. Error code is %d.", errno);
           delete th;
           return 0;
       }
       _currentThreads++;
    pegasus_yield();    pegasus_yield();
  
       PEG_METHOD_EXIT();
    return th;    return th;
 } }
  
  void ThreadPool::_link_pool(Thread *th) throw(IPCException)  void ThreadPool::_addToIdleThreadsQueue(Thread* th)
 { {
    if(th == 0)    if(th == 0)
    {    {
       Tracer::trace(TRC_DISCARDED_DATA, Tracer::LEVEL2,       Tracer::trace(TRC_DISCARDED_DATA, Tracer::LEVEL2,
           "ThreadPool::_link_pool: Thread pointer is null.");              "ThreadPool::_addToIdleThreadsQueue: Thread pointer is null.");
       throw NullPointer();       throw NullPointer();
    }    }
   
    try    try
    {    {
       _pool.insert_first(th);          _idleThreads.insert_first(th);
    }    }
    catch(...)    catch(...)
    {    {
       Tracer::trace(TRC_DISCARDED_DATA, Tracer::LEVEL2,       Tracer::trace(TRC_DISCARDED_DATA, Tracer::LEVEL2,
           "ThreadPool::_link_pool: _pool.insert_first failed.");              "ThreadPool::_addToIdleThreadsQueue: _idleThreads.insert_first "
                   "failed.");
    }    }
 } }
  
   
 PEGASUS_NAMESPACE_END PEGASUS_NAMESPACE_END
   


Legend:
Removed from v.1.57  
changed lines
  Added in v.1.87.4.1

No CVS admin address has been configured
Powered by
ViewCVS 0.9.2