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

Diff for /pegasus/src/Pegasus/Common/Thread.h between version 1.11 and 1.46

version 1.11, 2002/03/31 00:37:56 version 1.46, 2005/03/10 00:38:17
Line 1 
Line 1 
 //%///////////-*-c++-*-//////////////////////////////////////////////////////  //%2005////////////////////////////////////////////////////////////////////////
 // //
 // Copyright (c) 2000, 2001 The Open group, BMC Software, Tivoli Systems, IBM  // Copyright (c) 2000, 2001, 2002 BMC Software; Hewlett-Packard Development
   // Company, L.P.; IBM Corp.; The Open Group; Tivoli Systems.
   // Copyright (c) 2003 BMC Software; Hewlett-Packard Development Company, L.P.;
   // 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 23 
Line 30 
 // Author: Mike Day (mdday@us.ibm.com) // Author: Mike Day (mdday@us.ibm.com)
 // //
 // Modified By: Markus Mueller // Modified By: Markus Mueller
   //              Roger Kumpf, Hewlett-Packard Company (roger_kumpf@hp.com)
   //              Amit K Arora, IBM (amita@in.ibm.com) for PEP#101
   //              David Dillard, VERITAS Software Corp.
   //                  (david.dillard@veritas.com)
   //              Sean Keenan, Hewlett-Packard Company (sean.keenan@hp.com)
 // //
 //%///////////////////////////////////////////////////////////////////////////// //%/////////////////////////////////////////////////////////////////////////////
  
   
 #ifndef Pegasus_Thread_h #ifndef Pegasus_Thread_h
 #define Pegasus_Thread_h #define Pegasus_Thread_h
   
   #include <cstring>
 #include <Pegasus/Common/Config.h> #include <Pegasus/Common/Config.h>
 #include <Pegasus/Common/IPC.h> #include <Pegasus/Common/IPC.h>
 #include <Pegasus/Common/Exception.h>  #include <Pegasus/Common/InternalException.h>
 #include <Pegasus/Common/DQueue.h> #include <Pegasus/Common/DQueue.h>
   #include <Pegasus/Common/AcceptLanguages.h>  // l10n
 // REVIEW: Spend time getting to know this.  #include <Pegasus/Common/Linkage.h>
   #include <Pegasus/Common/AutoPtr.h>
  
 PEGASUS_NAMESPACE_BEGIN PEGASUS_NAMESPACE_BEGIN
 PEGASUS_USING_STD;  
  
 class PEGASUS_COMMON_LINKAGE cleanup_handler class PEGASUS_COMMON_LINKAGE cleanup_handler
 { {
Line 75 
Line 88 
    public:    public:
       static void default_delete(void *data);       static void default_delete(void *data);
  
       thread_data( const Sint8 *key ) : _delete_func(NULL) , _data(NULL), _size(0)        thread_data( const char *key ) : _delete_func(NULL) , _data(NULL), _size(0)
       {       {
          PEGASUS_ASSERT(key != NULL);          PEGASUS_ASSERT(key != NULL);
          size_t keysize = strlen(key);          size_t keysize = strlen(key);
          _key = new Sint8 [keysize + 1];           _key = new char[keysize + 1];
          memcpy(_key, key, keysize);          memcpy(_key, key, keysize);
          _key[keysize] = 0x00;          _key[keysize] = 0x00;
  
       }       }
  
       thread_data(const Sint8 *key, size_t size) : _delete_func(default_delete), _size(size)        thread_data(const char *key, size_t size) : _delete_func(default_delete), _size(size)
       {       {
          PEGASUS_ASSERT(key != NULL);          PEGASUS_ASSERT(key != NULL);
          size_t keysize = strlen(key);          size_t keysize = strlen(key);
          _key = new Sint8 [keysize + 1];           _key = new char[keysize + 1];
          memcpy(_key, key, keysize);          memcpy(_key, key, keysize);
          _key[keysize] = 0x00;          _key[keysize] = 0x00;
          _data = ::operator new(_size) ;          _data = ::operator new(_size) ;
  
       }       }
  
       thread_data(const Sint8 *key, size_t size, void *data) : _delete_func(default_delete), _size(size)        thread_data(const char *key, size_t size, void *data) : _delete_func(default_delete), _size(size)
       {       {
          PEGASUS_ASSERT(key != NULL);          PEGASUS_ASSERT(key != NULL);
          PEGASUS_ASSERT(data != NULL);          PEGASUS_ASSERT(data != NULL);
          size_t keysize = strlen(key);          size_t keysize = strlen(key);
  
          _key = new Sint8[keysize + 1];           _key = new char[keysize + 1];
          memcpy(_key, key, keysize);          memcpy(_key, key, keysize);
          _key[keysize] = 0x00;          _key[keysize] = 0x00;
          _data = ::operator new(_size);          _data = ::operator new(_size);
Line 120 
Line 133 
             delete [] _key;             delete [] _key;
       }       }
  
         /**
          * This function is used to put data in thread space.
          *
          * Be aware that there is NOTHING in place to stop
          * other users of the thread to remove this data.
          * Or change the data.
          *
          * You, the developer has to make sure that there are
          * no situations in which this can arise (ie, have a
          * lock for the function which manipulates the TSD.
          */
       void put_data(void (*del)(void *), size_t size, void *data ) throw(NullPointer)       void put_data(void (*del)(void *), size_t size, void *data ) throw(NullPointer)
       {       {
          if(_data != NULL)          if(_data != NULL)
Line 134 
Line 158 
  
       size_t get_size(void) { return _size; }       size_t get_size(void) { return _size; }
  
         /**
          * This function is used to retrieve data from the
          * TSD, the thread specific data.
          *
          * Be aware that there is NOTHING in place to stop
          * other users of the thread to change the data you
          * get from this function.
          *
          * You, the developer has to make sure that there are
          * no situations in which this can arise (ie, have a
          * lock for the function which manipulates the TSD.
          */
       void get_data(void **data, size_t *size)       void get_data(void **data, size_t *size)
       {       {
          if(data == NULL || size == NULL)          if(data == NULL || size == NULL)
Line 145 
Line 181 
  
       }       }
  
       void copy_data(void **buf, size_t *size) throw(BufferTooSmall, NullPointer)        void copy_data(void **buf, size_t *size) throw(NullPointer)
       {       {
          if((buf == NULL) || (size == NULL))          if((buf == NULL) || (size == NULL))
             throw NullPointer() ;             throw NullPointer() ;
Line 157 
Line 193 
  
       inline Boolean operator==(const void *key) const       inline Boolean operator==(const void *key) const
       {       {
          if ( ! strcmp(_key, (Sint8 *)key))           if ( ! strcmp(_key, reinterpret_cast<const char *>(key)))
             return(true);             return(true);
          return(false);          return(false);
       }       }
  
       inline Boolean operator==(const thread_data& b) const       inline Boolean operator==(const thread_data& b) const
       {       {
          return(operator==((const void *)b._key));           return(operator==(b._key));
       }       }
  
    private:    private:
Line 172 
Line 208 
       thread_data();       thread_data();
       void *_data;       void *_data;
       size_t _size;       size_t _size;
       Sint8 *_key;        char *_key;
  
       friend class DQueue<thread_data>;       friend class DQueue<thread_data>;
       friend class Thread;       friend class Thread;
Line 192 
Line 228 
  
       ~Thread();       ~Thread();
  
       void run(void);        /**
             Start the thread.
             @return true if the thread is started successfully, false if the
                     resources necessary to start the thread are not currently
                     available.  ATTN: The result is undefined for any other
                     type of failure.  (See Bugzilla 972)
          */
         Boolean run(void);
  
       // get the user parameter       // get the user parameter
       inline void *get_parm(void) { return _thread_parm; }       inline void *get_parm(void) { return _thread_parm; }
Line 220 
Line 263 
  
       void thread_switch(void);       void thread_switch(void);
  
 #if defined(PEGASUS_PLATFORM_LINUX_IX86_GNU) || defined(PEGASUS_PLATFORM_LINUX_GENERIC_GNU)  #if defined(PEGASUS_PLATFORM_LINUX_GENERIC_GNU)
       // suspend this thread       // suspend this thread
       void suspend(void) ;       void suspend(void) ;
  
Line 244 
Line 287 
       void cleanup_pop(Boolean execute = true) throw(IPCException);       void cleanup_pop(Boolean execute = true) throw(IPCException);
  
       // create and initialize a tsd       // create and initialize a tsd
       inline void create_tsd(const Sint8 *key, int size, void *buffer) throw(IPCException)        inline void create_tsd(const char *key, int size, void *buffer) throw(IPCException)
       {       {
          thread_data *tsd = new thread_data(key, size, buffer);          AutoPtr<thread_data> tsd(new thread_data(key, size, buffer));
          try { _tsd.insert_first(tsd); }          _tsd.insert_first(tsd.get());
          catch(IPCException& e) { e = e; delete tsd; throw; }          tsd.release();
       }       }
  
       // get the buffer associated with the key       // get the buffer associated with the key
       // NOTE: this call leaves the tsd LOCKED !!!!       // NOTE: this call leaves the tsd LOCKED !!!!
       inline void *reference_tsd(const Sint8 *key) throw(IPCException)        inline void *reference_tsd(const char *key) throw(IPCException)
       {       {
          _tsd.lock();          _tsd.lock();
          thread_data *tsd = _tsd.reference((const void *)key);           thread_data *tsd = _tsd.reference(key);
          if(tsd != NULL)          if(tsd != NULL)
             return( (void *)(tsd->_data) );             return( (void *)(tsd->_data) );
          else          else
             return(NULL);             return(NULL);
       }       }
  
       inline void *try_reference_tsd(const Sint8 *key) throw(IPCException)        inline void *try_reference_tsd(const char *key) throw(IPCException)
       {       {
          _tsd.try_lock();          _tsd.try_lock();
          thread_data *tsd = _tsd.reference((const void *)key);           thread_data *tsd = _tsd.reference(key);
          if(tsd != NULL)          if(tsd != NULL)
             return((void *)(tsd->_data) );             return((void *)(tsd->_data) );
          else          else
Line 282 
Line 325 
       }       }
  
       // delete the tsd associated with the key       // delete the tsd associated with the key
       inline void delete_tsd(const Sint8 *key) throw(IPCException)        inline void delete_tsd(const char *key) throw(IPCException)
       {       {
          thread_data *tsd = _tsd.remove((const void *)key);           AutoPtr<thread_data> tsd(_tsd.remove(key));
          if(tsd != NULL)  
             delete tsd;  
       }       }
  
       inline void *remove_tsd(const Sint8 *key) throw(IPCException)        // Note: Caller must delete the thread_data object returned (if not null)
         inline void *remove_tsd(const char *key) throw(IPCException)
       {       {
          return(_tsd.remove((const void *)key));          return(_tsd.remove((const void *)key));
       }       }
  
       inline void empty_tsd(void) throw(IPCException)       inline void empty_tsd(void) throw(IPCException)
       {       {
          _tsd.empty_list();  
            try
            {
   
               _tsd.try_lock();
            }
            catch(IPCException&)
            {
               return;
            }
   
            AutoPtr<thread_data> tsd(_tsd.next(0));
            while(tsd.get())
            {
               _tsd.remove_no_lock(tsd.get());
               tsd.reset(_tsd.next(0));
            }
            _tsd.unlock();
       }       }
  
       // create or re-initialize tsd associated with the key       // create or re-initialize tsd associated with the key
       // if the tsd already exists, return the existing buffer        // if the tsd already exists, delete the existing buffer
       thread_data *put_tsd(const Sint8 *key, void (*delete_func)(void *), Uint32 size, void *value)        void put_tsd(const char *key, void (*delete_func)(void *), Uint32 size, void *value)
          throw(IPCException)          throw(IPCException)
  
       {       {
          PEGASUS_ASSERT(key != NULL);          PEGASUS_ASSERT(key != NULL);
          thread_data *tsd ;           AutoPtr<thread_data> tsd ;
          tsd = _tsd.remove((const void *)key);  // may throw an IPC exception           tsd.reset(_tsd.remove((const void *)key));  // may throw an IPC exception
          thread_data *ntsd = new thread_data(key);           tsd.reset();
            AutoPtr<thread_data> ntsd(new thread_data(key));
          ntsd->put_data(delete_func, size, value);          ntsd->put_data(delete_func, size, value);
          try { _tsd.insert_first(ntsd); }           try { _tsd.insert_first(ntsd.get()); }
          catch(IPCException& e) { e = e; delete ntsd; throw; }           catch(IPCException& e) { e = e; throw; }
          return(tsd);       ntsd.release();
       }       }
       inline PEGASUS_THREAD_RETURN get_exit(void) { return _exit_code; }       inline PEGASUS_THREAD_RETURN get_exit(void) { return _exit_code; }
       inline PEGASUS_THREAD_TYPE self(void) {return pegasus_thread_self(); }       inline PEGASUS_THREAD_TYPE self(void) {return pegasus_thread_self(); }
Line 332 
Line 392 
  
       void detach(void);       void detach(void);
  
         //
         //  Gets the Thread object associated with the caller's thread.
         //  Note: this may return NULL if no Thread object is associated
         //  with the caller's thread.
         //
         static Thread * getCurrent();  // l10n
   
         //
         //  Sets the Thread object associated with the caller's thread.
         //  Note: the Thread object must be placed on the heap.
         //
         static void setCurrent(Thread * thrd); // l10n
   
         //
         //  Gets the AcceptLanguages object associated with the caller's
         //  Thread.
         //  Note: this may return NULL if no Thread object, or no
         //  AcceptLanguages object, is associated with the caller's thread.
         //
         static AcceptLanguages * getLanguages(); //l10n
   
         //
         //  Sets the AcceptLanguages object associated with the caller's
         //  Thread.
         //  Note: a Thread object must have been previously associated with
         //  the caller's thread.
         //  Note: the AcceptLanguages object must be placed on the heap.
         //
         static void setLanguages(AcceptLanguages *langs); //l10n
   
         //
         //  Removes the AcceptLanguages object associated with the caller's
         //  Thread.
         //
         static void clearLanguages(); //l10n
   
    private:    private:
       Thread();       Thread();
       inline void create_tsd(const Sint8 *key ) throw(IPCException)  
         static Sint8 initializeKey();  // l10n
   
         inline void create_tsd(const char *key ) throw(IPCException)
       {       {
          thread_data *tsd = new thread_data(key);           AutoPtr<thread_data> tsd(new thread_data(key));
          try { _tsd.insert_first(tsd); }           _tsd.insert_first(tsd.get());
          catch(IPCException& e) { e = e; delete tsd; throw; }           tsd.release();
       }       }
       PEGASUS_THREAD_HANDLE _handle;       PEGASUS_THREAD_HANDLE _handle;
       Boolean _is_detached;       Boolean _is_detached;
Line 357 
Line 456 
       void *_thread_parm;       void *_thread_parm;
       PEGASUS_THREAD_RETURN _exit_code;       PEGASUS_THREAD_RETURN _exit_code;
       static Boolean _signals_blocked;       static Boolean _signals_blocked;
         static PEGASUS_THREAD_KEY_TYPE _platform_thread_key;  //l10n
         static Boolean _key_initialized; // l10n
         static Boolean _key_error; // l10n
       friend class ThreadPool;       friend class ThreadPool;
 } ; } ;
  
Line 366 
Line 468 
    public:    public:
  
       ThreadPool(Sint16 initial_size,       ThreadPool(Sint16 initial_size,
                  const Sint8 *key,                   const char *key,
                  Sint16 min,                  Sint16 min,
                  Sint16 max,                  Sint16 max,
                  struct timeval & alloc_wait,                  struct timeval & alloc_wait,
                  struct timeval & dealloc_wait,                   struct timeval & dealloc_wait);
                  struct timeval & deadlock_detect);  
  
       ~ThreadPool(void);       ~ThreadPool(void);
  
       void allocate_and_awaken(void *parm,        /**
             Allocate and start a thread to do a unit of work.
             @param parm A generic parameter to pass to the thread
             @param work A pointer to the function that is to be executed by
                         the thread
             @param blocking A pointer to an optional semaphore which, if
                             specified, is signaled after the thread finishes
                             executing the work function
             @return true if the thread is started successfully, false if the
                     resources necessary to start the thread are not currently
                     available.  ATTN: The result is undefined for any other
                     type of thread creation failure.
          */
         Boolean allocate_and_awaken(void *parm,
                                PEGASUS_THREAD_RETURN (PEGASUS_THREAD_CDECL *work)(void *),                                PEGASUS_THREAD_RETURN (PEGASUS_THREAD_CDECL *work)(void *),
                                Semaphore *blocking = 0)                                Semaphore *blocking = 0)
          throw(IPCException);          throw(IPCException);
Line 386 
Line 500 
  
       void get_key(Sint8 *buf, int bufsize);       void get_key(Sint8 *buf, int bufsize);
  
       inline Boolean operator==(const void *key) const        inline Boolean operator==(const char *key) const
       {       {
          if ( ! strncmp( reinterpret_cast<Sint8 *>(const_cast<void *>(key)), _key, 16  ))           if ( ! strncmp(key, _key, 16))
             return(true);             return(true);
          return(false);          return(false);
       }       }
       inline Boolean operator==(const ThreadPool & b) const       inline Boolean operator==(const ThreadPool & b) const
       {       {
          return(operator==((const void *) b._key ));           return(operator==(b._key));
       }       }
  
       inline void set_min_threads(Sint16 min)       inline void set_min_threads(Sint16 min)
Line 447 
Line 561 
          return buffer;          return buffer;
       }       }
  
       inline void set_deadlock_detect(const struct timeval & deadlock)        inline Uint32 running_count(void)
       {       {
          _deadlock_detect.tv_sec = deadlock.tv_sec;           return _running.count();
          _deadlock_detect.tv_usec = deadlock.tv_usec;  
       }       }
  
       inline struct timeval * get_deadlock_detect(struct timeval *buffer) const        inline Uint32 pool_count(void)
       {       {
          if(buffer == 0)            return _pool.count();
             throw NullPointer();  
          buffer->tv_sec = _deadlock_detect.tv_sec;  
          buffer->tv_usec = _deadlock_detect.tv_usec;  
          return buffer;  
       }       }
         inline Uint32 dead_count(void)
       inline Uint32 running_count(void)  
       {       {
          return _running.count();            return _dead.count();
       }       }
  
   
       static Boolean check_time(struct timeval *start, struct timeval *interval);       static Boolean check_time(struct timeval *start, struct timeval *interval);
  
         Boolean operator ==(const ThreadPool & p)
         {
            return operator==((const void *)&p);
         }
   
         Boolean operator ==(const void *p)
         {
            if((void *)this == p)
               return true;
            return false;
         }
   
         static void kill_idle_threads(void);
   
    private:    private:
       ThreadPool(void);       ThreadPool(void);
       Sint16 _max_threads;       Sint16 _max_threads;
Line 476 
Line 599 
       AtomicInt _current_threads;       AtomicInt _current_threads;
       struct timeval _allocate_wait;       struct timeval _allocate_wait;
       struct timeval _deallocate_wait;       struct timeval _deallocate_wait;
       struct timeval _deadlock_detect;  
       static PEGASUS_THREAD_RETURN PEGASUS_THREAD_CDECL _loop(void *);       static PEGASUS_THREAD_RETURN PEGASUS_THREAD_CDECL _loop(void *);
       Sint8 _key[17];        char _key[17];
       DQueue<Thread> _pool;       DQueue<Thread> _pool;
       DQueue<Thread> _running;       DQueue<Thread> _running;
       DQueue<Thread> _dead;       DQueue<Thread> _dead;
       AtomicInt _dying;       AtomicInt _dying;
  
   
       static void _sleep_sem_del(void *p);       static void _sleep_sem_del(void *p);
  
       void _check_deadlock(struct timeval *start) throw(Deadlock);  
       Boolean _check_deadlock_no_throw(struct timeval *start);  
       Boolean _check_dealloc(struct timeval *start);       Boolean _check_dealloc(struct timeval *start);
       Thread *_init_thread(void) throw(IPCException);       Thread *_init_thread(void) throw(IPCException);
       void _link_pool(Thread *th) throw(IPCException);       void _link_pool(Thread *th) throw(IPCException);
       static PEGASUS_THREAD_RETURN  _undertaker(void *);       static PEGASUS_THREAD_RETURN  _undertaker(void *);
         static PEGASUS_THREAD_RETURN  _graveyard(Thread *);
         static DQueue<ThreadPool> _pools;
  };  };
  
  
 inline void ThreadPool::_sleep_sem_del(void *p)  
 {  
    if(p != 0)  
    {  
       delete (Semaphore *)p;  
    }  
 }  
   
 inline void ThreadPool::_check_deadlock(struct timeval *start) throw(Deadlock)  
 {  
    if (true == check_time(start, &_deadlock_detect))  
       throw Deadlock(pegasus_thread_self());  
    return;  
 }  
   
   
 inline Boolean ThreadPool::_check_deadlock_no_throw(struct timeval *start)  
 {  
    return(check_time(start, &_deadlock_detect));  
 }  
   
 inline Boolean ThreadPool::_check_dealloc(struct timeval *start)  
 {  
    return(check_time(start, &_deallocate_wait));  
 }  
   
 inline Thread *ThreadPool::_init_thread(void) throw(IPCException)  
 {  
    Thread *th = (Thread *) new Thread(_loop, this, false);  
    // allocate a sleep semaphore and pass it in the thread context  
    // initial count is zero, loop function will sleep until  
    // we signal the semaphore  
    Semaphore *sleep_sem = (Semaphore *) new Semaphore(0);  
    th->put_tsd("sleep sem", &_sleep_sem_del, sizeof(Semaphore), (void *)sleep_sem);  
   
    struct timeval *dldt = (struct timeval *) ::operator new(sizeof(struct timeval));  
    th->put_tsd("deadlock timer", thread_data::default_delete, sizeof(struct timeval), (void *)dldt);  
    // thread will enter _loop(void *) and sleep on sleep_sem until we signal it  
    th->run();  
    _current_threads++;  
    pegasus_yield();  
   
    return th;  
 }  
   
 inline void ThreadPool::_link_pool(Thread *th) throw(IPCException)  
 {  
    if(th == 0)  
       throw NullPointer();  
    _pool.insert_first(th);  
 }  
  
  
 #if defined(PEGASUS_OS_TYPE_WINDOWS) #if defined(PEGASUS_OS_TYPE_WINDOWS)
Line 556 
Line 625 
 # include "ThreadzOS_inline.h" # include "ThreadzOS_inline.h"
 #elif defined(PEGASUS_OS_TYPE_UNIX) #elif defined(PEGASUS_OS_TYPE_UNIX)
 # include "ThreadUnix_inline.h" # include "ThreadUnix_inline.h"
   #elif defined(PEGASUS_OS_VMS)
   # include "ThreadVms_inline.h"
 #endif #endif
  
 PEGASUS_NAMESPACE_END PEGASUS_NAMESPACE_END


Legend:
Removed from v.1.11  
changed lines
  Added in v.1.46

No CVS admin address has been configured
Powered by
ViewCVS 0.9.2