(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.42.2.1 and 1.53

version 1.42.2.1, 2005/08/12 22:52:42 version 1.53, 2005/06/24 19:34:23
Line 1 
Line 1 
 //%2004////////////////////////////////////////////////////////////////////////  //%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.
Line 6 
Line 6 
 // 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.; // Copyright (c) 2004 BMC Software; Hewlett-Packard Development Company, L.P.;
 // IBM Corp.; EMC Corporation; VERITAS Software Corporation; The Open Group. // 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 30 
Line 32 
 // Modified By: Markus Mueller // Modified By: Markus Mueller
 //              Roger Kumpf, Hewlett-Packard Company (roger_kumpf@hp.com) //              Roger Kumpf, Hewlett-Packard Company (roger_kumpf@hp.com)
 //              Amit K Arora, IBM (amita@in.ibm.com) for PEP#101 //              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)
   //              Josephine Eskaline Joyce, IBM (jojustin@in.ibm.com) for Bug#2393
 // //
 //%///////////////////////////////////////////////////////////////////////////// //%/////////////////////////////////////////////////////////////////////////////
  
Line 64 
Line 70 
          return(operator==((const void *)b._routine));          return(operator==((const void *)b._routine));
       }       }
    private:    private:
       void execute(void) { _routine(_arg); }        void execute() { _routine(_arg); }
       cleanup_handler();       cleanup_handler();
       void (*_routine)(void *);       void (*_routine)(void *);
  
Line 83 
Line 89 
    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.reset(new char[keysize + 1]);
          memcpy(_key, key, keysize);           memcpy(_key.get(), key, keysize);
          _key[keysize] = 0x00;           _key.get()[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.reset(new char[keysize + 1]);
          memcpy(_key, key, keysize);           memcpy(_key.get(), key, keysize);
          _key[keysize] = 0x00;           _key.get()[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.reset(new char[keysize + 1]);
          memcpy(_key, key, keysize);           memcpy(_key.get(), key, keysize);
          _key[keysize] = 0x00;           _key.get()[keysize] = 0x00;
          _data = ::operator new(_size);          _data = ::operator new(_size);
          memcpy(_data, data, size);          memcpy(_data, data, size);
       }       }
Line 124 
Line 130 
             {             {
                _delete_func( _data );                _delete_func( _data );
             }             }
          if( _key != NULL )  
             delete [] _key;  
       }       }
  
       /**       /**
Line 138 
Line 142 
        * You, the developer has to make sure that there are        * You, the developer has to make sure that there are
        * no situations in which this can arise (ie, have a        * no situations in which this can arise (ie, have a
        * lock for the function which manipulates the TSD.        * lock for the function which manipulates the TSD.
          *
          * @exception NullPointer
        */        */
       void put_data(void (*del)(void *), size_t size, void *data ) throw(NullPointer)        void put_data(void (*del)(void *), size_t size, void *data )
       {       {
          if(_data != NULL)          if(_data != NULL)
             if(_delete_func != NULL)             if(_delete_func != NULL)
Line 151 
Line 157 
          return ;          return ;
       }       }
  
       size_t get_size(void) { return _size; }        size_t get_size() { return _size; }
  
       /**       /**
        * This function is used to retrieve data from the        * This function is used to retrieve data from the
Line 176 
Line 182 
  
       }       }
  
       void copy_data(void **buf, size_t *size) throw(NullPointer)        // @exception NullPointer
         void copy_data(void **buf, size_t *size)
       {       {
          if((buf == NULL) || (size == NULL))          if((buf == NULL) || (size == NULL))
             throw NullPointer() ;             throw NullPointer() ;
Line 188 
Line 195 
  
       inline Boolean operator==(const void *key) const       inline Boolean operator==(const void *key) const
       {       {
          if ( ! strcmp(_key, (Sint8 *)key))           if ( ! strcmp(_key.get(), 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.get()));
       }       }
  
    private:    private:
Line 203 
Line 210 
       thread_data();       thread_data();
       void *_data;       void *_data;
       size_t _size;       size_t _size;
       Sint8 *_key;        AutoArrayPtr<char> _key;
  
       friend class DQueue<thread_data>;       friend class DQueue<thread_data>;
       friend class Thread;       friend class Thread;
 }; };
  
   
 enum ThreadStatus { enum ThreadStatus {
         PEGASUS_THREAD_OK = 1, /* No problems */         PEGASUS_THREAD_OK = 1, /* No problems */
         PEGASUS_THREAD_INSUFFICIENT_RESOURCES, /* Can't allocate a thread. Not enough         PEGASUS_THREAD_INSUFFICIENT_RESOURCES, /* Can't allocate a thread. Not enough
Line 221 
Line 229 
  
 /////////////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////////////
  
 class PEGASUS_COMMON_LINKAGE ThreadPool;  
   
 class PEGASUS_COMMON_LINKAGE Thread class PEGASUS_COMMON_LINKAGE Thread
 { {
   
    public:    public:
   
       Thread( PEGASUS_THREAD_RETURN (PEGASUS_THREAD_CDECL *start )(void *),       Thread( PEGASUS_THREAD_RETURN (PEGASUS_THREAD_CDECL *start )(void *),
               void *parameter, Boolean detached );               void *parameter, Boolean detached );
  
Line 244 
Line 250 
       ThreadStatus run();       ThreadStatus run();
  
       // get the user parameter       // get the user parameter
       inline void *get_parm(void) { return _thread_parm; }        inline void *get_parm() { return _thread_parm; }
   
       // send the thread a signal -- may not be appropriate due to Windows  
       //  void kill(int signum);  
  
       // cancellation must be deferred (not asynchronous)       // cancellation must be deferred (not asynchronous)
       // for user-level threads the thread itself can decide       // for user-level threads the thread itself can decide
       // when it should die.       // when it should die.
       void cancel(void);        void cancel();
  
       // cancel if there is a pending cancellation request       // cancel if there is a pending cancellation request
       void test_cancel(void);        void test_cancel();
  
       Boolean is_cancelled(void);        Boolean is_cancelled();
  
       // for user-level threads  - put the calling thread       // for user-level threads  - put the calling thread
       // to sleep and jump to the thread scheduler.       // to sleep and jump to the thread scheduler.
Line 267 
Line 270 
       // or gnu portable threads will have an existing       // or gnu portable threads will have an existing
       // routine that can be mapped to this method       // routine that can be mapped to this method
  
       void thread_switch(void);        void thread_switch();
  
 #if defined(PEGASUS_PLATFORM_LINUX_GENERIC_GNU) #if defined(PEGASUS_PLATFORM_LINUX_GENERIC_GNU)
       // suspend this thread       // suspend this thread
       void suspend(void) ;        void suspend();
  
       // resume this thread       // resume this thread
       void resume(void) ;        void resume();
 #endif #endif
  
       static void sleep(Uint32 msec) ;       static void sleep(Uint32 msec) ;
  
       // block the calling thread until this thread terminates       // block the calling thread until this thread terminates
       void join( void );        void join();
       void thread_init(void);        void thread_init();
  
       // thread routine needs to call this function when       // thread routine needs to call this function when
       // it is ready to exit       // it is ready to exit
Line 289 
Line 292 
  
       // stack of functions to be called when thread terminates       // stack of functions to be called when thread terminates
       // will be called last in first out (LIFO)       // will be called last in first out (LIFO)
       void cleanup_push( void (*routine) (void *), void *parm ) throw(IPCException);        // @exception IPCException
       void cleanup_pop(Boolean execute = true) throw(IPCException);        void cleanup_push(void (*routine) (void *), void *parm);
   
         // @exception IPCException
         void cleanup_pop(Boolean execute = true);
  
       // create and initialize a tsd       // create and initialize a tsd
       inline void create_tsd(const Sint8 *key, int size, void *buffer) throw(IPCException)        // @exception IPCException
         inline void create_tsd(const char *key, int size, void *buffer)
       {       {
         AutoPtr<thread_data> tsd(new thread_data(key, size, buffer));         AutoPtr<thread_data> tsd(new thread_data(key, size, buffer));
         _tsd.insert_first(tsd.get());         _tsd.insert_first(tsd.get());
Line 302 
Line 309 
  
       // 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)        // @exception IPCException
         inline void *reference_tsd(const char *key)
       {       {
          _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)        // @exception IPCException
         inline void *try_reference_tsd(const char *key)
       {       {
          _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 325 
Line 334 
  
       // release the lock held on the tsd       // release the lock held on the tsd
       // NOTE: assumes a corresponding and prior call to reference_tsd() !!!       // NOTE: assumes a corresponding and prior call to reference_tsd() !!!
       inline void dereference_tsd(void) throw(IPCException)        // @exception IPCException
         inline void dereference_tsd()
       {       {
          _tsd.unlock();          _tsd.unlock();
       }       }
  
       // delete the tsd associated with the key       // delete the tsd associated with the key
       inline void delete_tsd(const Sint8 *key) throw(IPCException)        // @exception IPCException
         inline void delete_tsd(const char *key)
       {       {
          AutoPtr<thread_data> tsd(_tsd.remove((const void *)key));           AutoPtr<thread_data> tsd(_tsd.remove(key));
       }       }
  
       // Note: Caller must delete the thread_data object returned (if not null)       // Note: Caller must delete the thread_data object returned (if not null)
       inline void *remove_tsd(const Sint8 *key) throw(IPCException)        // @exception IPCException
         inline void *remove_tsd(const char *key)
       {       {
          return(_tsd.remove((const void *)key));          return(_tsd.remove((const void *)key));
       }       }
  
       inline void empty_tsd(void) throw(IPCException)        // @exception IPCException
         inline void empty_tsd()
       {       {
   
          try          try
          {          {
   
             _tsd.try_lock();             _tsd.try_lock();
          }          }
          catch(IPCException&)          catch(IPCException&)
Line 366 
Line 377 
  
       // create or re-initialize tsd associated with the key       // create or re-initialize tsd associated with the key
       // if the tsd already exists, delete the existing buffer       // if the tsd already exists, delete the existing buffer
       void put_tsd(const Sint8 *key, void (*delete_func)(void *), Uint32 size, void *value)        // @exception IPCException
          throw(IPCException)        void put_tsd(const char *key, void (*delete_func)(void *), Uint32 size, void *value)
   
       {       {
          PEGASUS_ASSERT(key != NULL);          PEGASUS_ASSERT(key != NULL);
          AutoPtr<thread_data> tsd ;          AutoPtr<thread_data> tsd ;
Line 380 
Line 390 
          catch(IPCException& e) { e = e; throw; }          catch(IPCException& e) { e = e; throw; }
      ntsd.release();      ntsd.release();
       }       }
       inline PEGASUS_THREAD_RETURN get_exit(void) { return _exit_code; }        inline PEGASUS_THREAD_RETURN get_exit() { return _exit_code; }
       inline PEGASUS_THREAD_TYPE self(void) {return pegasus_thread_self(); }        inline PEGASUS_THREAD_TYPE self() {return pegasus_thread_self(); }
  
       PEGASUS_THREAD_HANDLE getThreadHandle() {return _handle;}       PEGASUS_THREAD_HANDLE getThreadHandle() {return _handle;}
  
Line 396 
Line 406 
          return(operator==((const void *)&b ));          return(operator==((const void *)&b ));
       }       }
  
       void detach(void);        void detach();
  
       //       //
       //  Gets the Thread object associated with the caller's thread.       //  Gets the Thread object associated with the caller's thread.
Line 439 
Line 449 
  
       static Sint8 initializeKey();  // l10n       static Sint8 initializeKey();  // l10n
  
       inline void create_tsd(const Sint8 *key ) throw(IPCException)        // @exception IPCException
         inline void create_tsd(const char *key )
       {       {
          AutoPtr<thread_data> tsd(new thread_data(key));          AutoPtr<thread_data> tsd(new thread_data(key));
          _tsd.insert_first(tsd.get());          _tsd.insert_first(tsd.get());
Line 465 
Line 476 
       static PEGASUS_THREAD_KEY_TYPE _platform_thread_key;  //l10n       static PEGASUS_THREAD_KEY_TYPE _platform_thread_key;  //l10n
       static Boolean _key_initialized; // l10n       static Boolean _key_initialized; // l10n
       static Boolean _key_error; // l10n       static Boolean _key_error; // l10n
       friend class ThreadPool;  
 } ; } ;
  
  
Line 473 
Line 483 
 { {
    public:    public:
  
       ThreadPool(Sint16 initial_size,      /**
                  const Sint8 *key,          Constructs a new ThreadPool object.
                  Sint16 min,          @param initialSize The number of threads that are initially added to
                  Sint16 max,              the thread pool.
                  struct timeval & alloc_wait,          @param key A name for this thread pool that can be used to determine
                  struct timeval & dealloc_wait,              equality of two thread pool objects.  Only the first 16 characters
                  struct timeval & deadlock_detect);              of this value are used.
           @param minThreads The minimum number of threads that should be
               contained in this thread pool at any given time.
           @param maxThreads The maximum number of threads that should be
               contained in this thread pool at any given time.
           @param deallocateWait The minimum time that a thread should be idle
               before it is removed from the pool and cleaned up.
        */
       ThreadPool(
           Sint16 initialSize,
           const char* key,
           Sint16 minThreads,
           Sint16 maxThreads,
           struct timeval& deallocateWait);
  
       ~ThreadPool(void);      /**
           Destructs the ThreadPool object.
        */
       ~ThreadPool();
  
       /**       /**
           Allocate and start a thread to do a unit of work.           Allocate and start a thread to do a unit of work.
Line 505 
Line 531 
           PEGASUS_THREAD_RETURN (PEGASUS_THREAD_CDECL* work)(void *),           PEGASUS_THREAD_RETURN (PEGASUS_THREAD_CDECL* work)(void *),
           Semaphore* blocking = 0);           Semaphore* blocking = 0);
  
       Uint32 kill_dead_threads( void )      /**
          throw(IPCException);          Cleans up idle threads if they have been running longer than the
           deallocate_wait configuration and more than the configured
           minimum number of threads is running.
           @return The number of threads that were cleaned up.
           @exception IPCException
        */
       Uint32 cleanupIdleThreads();
  
       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  ))          return (!strncmp(key, _key, 16));
             return(true);  
          return(false);  
       }  
       inline Boolean operator==(const ThreadPool & b) const  
       {  
          return(operator==((const void *) b._key ));  
       }  
   
       inline void set_min_threads(Sint16 min)  
       {  
          _min_threads = min;  
       }       }
  
       inline Sint16 get_min_threads(void) const      Boolean operator==(const void* p) const
       {       {
          return _min_threads;          return ((void *)this == p);
       }       }
  
       inline void set_max_threads(Sint16 max)      Boolean operator==(const ThreadPool & p) const
       {       {
          _max_threads = max;          return operator==((const void *)&p);
       }  
   
       inline Sint16 get_max_threads(void) const  
       {  
          return _max_threads;  
       }  
   
       inline void set_allocate_wait(const struct timeval & alloc_wait)  
       {  
          _allocate_wait.tv_sec = alloc_wait.tv_sec;  
          _allocate_wait.tv_usec = alloc_wait.tv_usec;  
       }  
   
       inline struct timeval *get_allocate_wait(struct timeval *buffer) const  
       {  
          if(buffer == 0)  
             throw NullPointer();  
          buffer->tv_sec = _allocate_wait.tv_sec;  
          buffer->tv_usec = _allocate_wait.tv_usec;  
          return buffer;  
       }       }
  
       inline void set_deallocate_wait(const struct timeval & dealloc_wait)      inline void setMinThreads(Sint16 min)
       {       {
          _deallocate_wait.tv_sec = dealloc_wait.tv_sec;          _minThreads = min;
          _deallocate_wait.tv_usec = dealloc_wait.tv_usec;  
       }       }
  
       inline struct timeval *get_deallocate_wait(struct timeval *buffer) const      inline Sint16 getMinThreads() const
       {       {
          if(buffer == 0)          return _minThreads;
             throw NullPointer();  
          buffer->tv_sec = _deallocate_wait.tv_sec;  
          buffer->tv_usec = _deallocate_wait.tv_usec;  
          return buffer;  
       }       }
  
       inline void set_deadlock_detect(const struct timeval & deadlock)      inline void setMaxThreads(Sint16 max)
       {       {
          _deadlock_detect.tv_sec = deadlock.tv_sec;          _maxThreads = max;
          _deadlock_detect.tv_usec = deadlock.tv_usec;  
       }       }
  
       inline Uint32 running_count(void)      inline Sint16 getMaxThreads() const
       {       {
          return _running.count();          return _maxThreads;
       }       }
  
       inline Uint32 pool_count(void)      inline Uint32 runningCount()
         {         {
           return _pool.count();          return _runningThreads.count();
         }         }
       inline Uint32 dead_count(void)  
         {  
           return _dead.count();  
         }  
   
  
       static Boolean check_time(struct timeval *start, struct timeval *interval);      inline Uint32 idleCount()
   
       Boolean operator ==(const ThreadPool & p)  
       {       {
          return operator==((const void *)&p);          return _idleThreads.count();
       }       }
  
       Boolean operator ==(const void *p)  private:
       {  
          if((void *)this == p)  
             return true;  
          return false;  
       }  
  
       static void kill_idle_threads(void);      ThreadPool();    // Unimplemented
       ThreadPool(const ThreadPool&);    // Unimplemented
       ThreadPool& operator=(const ThreadPool&);    // Unimplemented
  
    private:  
       ThreadPool(void);  
       Sint16 _max_threads;  
       Sint16 _min_threads;  
       AtomicInt _current_threads;  
       struct timeval _allocate_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];  
       DQueue<Thread> _pool;  
       DQueue<Thread> _running;  
       DQueue<Thread> _dead;  
       AtomicInt _dying;  
   
       static void _sleep_sem_del(void *p);  
  
       void _check_deadlock(struct timeval *start) throw(Deadlock);      static Boolean _timeIntervalExpired(
       Boolean _check_deadlock_no_throw(struct timeval *start);          struct timeval* start,
       Boolean _check_dealloc(struct timeval *start);          struct timeval* interval);
       Thread *_init_thread(void) throw(IPCException);  
       void _link_pool(Thread *th) throw(IPCException);      static void _deleteSemaphore(void* p);
       static PEGASUS_THREAD_RETURN  _undertaker(void *);  
       static PEGASUS_THREAD_RETURN  _graveyard(Thread *);      void _cleanupThread(Thread* thread);
       static DQueue<ThreadPool> _pools;      Thread* _initializeThread();
       void _addToIdleThreadsQueue(Thread* th);
   
       Sint16 _maxThreads;
       Sint16 _minThreads;
       AtomicInt _currentThreads;
       struct timeval _deallocateWait;
       char _key[17];
       DQueue<Thread> _idleThreads;
       DQueue<Thread> _runningThreads;
       AtomicInt _dying;
  };  };
  
  
   
   
 #if defined(PEGASUS_OS_TYPE_WINDOWS) #if defined(PEGASUS_OS_TYPE_WINDOWS)
 # include "ThreadWindows_inline.h" # include "ThreadWindows_inline.h"
 #elif defined(PEGASUS_PLATFORM_ZOS_ZSERIES_IBM) #elif defined(PEGASUS_PLATFORM_ZOS_ZSERIES_IBM)
 # 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.42.2.1  
changed lines
  Added in v.1.53

No CVS admin address has been configured
Powered by
ViewCVS 0.9.2