(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.50 and 1.57.2.1

version 1.50, 2005/05/13 20:53:39 version 1.57.2.1, 2006/07/27 23:11:52
Line 1 
Line 1 
 //%2005////////////////////////////////////////////////////////////////////////  //%2006////////////////////////////////////////////////////////////////////////
 // //
 // 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 8 
Line 8 
 // 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.; // Copyright (c) 2005 Hewlett-Packard Development Company, L.P.; IBM Corp.;
 // EMC Corporation; VERITAS Software Corporation; The Open Group. // EMC Corporation; VERITAS Software Corporation; The Open Group.
   // Copyright (c) 2006 Hewlett-Packard Development Company, L.P.; IBM Corp.;
   // EMC Corporation; Symantec 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 35 
Line 37 
 //              David Dillard, VERITAS Software Corp. //              David Dillard, VERITAS Software Corp.
 //                  (david.dillard@veritas.com) //                  (david.dillard@veritas.com)
 //              Sean Keenan, Hewlett-Packard Company (sean.keenan@hp.com) //              Sean Keenan, Hewlett-Packard Company (sean.keenan@hp.com)
   //              Josephine Eskaline Joyce, IBM (jojustin@in.ibm.com) for Bug#2393
 // //
 //%///////////////////////////////////////////////////////////////////////////// //%/////////////////////////////////////////////////////////////////////////////
  
Line 43 
Line 46 
  
 #include <cstring> #include <cstring>
 #include <Pegasus/Common/Config.h> #include <Pegasus/Common/Config.h>
 #include <Pegasus/Common/IPC.h>  #include <Pegasus/Common/AtomicInt.h>
 #include <Pegasus/Common/InternalException.h> #include <Pegasus/Common/InternalException.h>
 #include <Pegasus/Common/DQueue.h>  #include <Pegasus/Common/AcceptLanguageList.h>
 #include <Pegasus/Common/AcceptLanguages.h>  // l10n  
 #include <Pegasus/Common/Linkage.h> #include <Pegasus/Common/Linkage.h>
 #include <Pegasus/Common/AutoPtr.h> #include <Pegasus/Common/AutoPtr.h>
   #include <Pegasus/Common/List.h>
   #include <Pegasus/Common/Mutex.h>
   #include <Pegasus/Common/Semaphore.h>
   #include <Pegasus/Common/TSDKey.h>
  
 PEGASUS_NAMESPACE_BEGIN PEGASUS_NAMESPACE_BEGIN
  
 class PEGASUS_COMMON_LINKAGE cleanup_handler  class PEGASUS_COMMON_LINKAGE cleanup_handler : public Linkable
 { {
  
    public:    public:
       cleanup_handler( void (*routine)(void *), void *arg  ) : _routine(routine), _arg(arg)  {}       cleanup_handler( void (*routine)(void *), void *arg  ) : _routine(routine), _arg(arg)  {}
       ~cleanup_handler()  {; }       ~cleanup_handler()  {; }
       inline Boolean operator==(const void *key) const  
       {  
          if(key == (void *)_routine)  
             return true;  
          return false;  
       }  
       inline Boolean operator ==(const cleanup_handler & b) const  
       {  
          return(operator==((const void *)b._routine));  
       }  
    private:    private:
       void execute() { _routine(_arg); }       void execute() { _routine(_arg); }
       cleanup_handler();       cleanup_handler();
       void (*_routine)(void *);       void (*_routine)(void *);
  
       void *_arg;       void *_arg;
       PEGASUS_CLEANUP_HANDLE _cleanup_buffer;        ThreadCleanupType _cleanup_buffer;
       friend class DQueue<class cleanup_handler>;  
       friend class Thread;       friend class Thread;
 }; };
  
 /////////////////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////////////////
  
  
 class  PEGASUS_COMMON_LINKAGE thread_data  class  PEGASUS_COMMON_LINKAGE thread_data : public Linkable
 { {
  
    public:    public:
Line 92 
Line 88 
       {       {
          PEGASUS_ASSERT(key != NULL);          PEGASUS_ASSERT(key != NULL);
          size_t keysize = strlen(key);          size_t keysize = strlen(key);
          _key = new char[keysize + 1];           _key.reset(new char[keysize + 1]);
          memcpy(_key, key, keysize);           memcpy(_key.get(), key, keysize);
          _key[keysize] = 0x00;           _key.get()[keysize] = 0x00;
  
       }       }
  
Line 102 
Line 98 
       {       {
          PEGASUS_ASSERT(key != NULL);          PEGASUS_ASSERT(key != NULL);
          size_t keysize = strlen(key);          size_t keysize = strlen(key);
          _key = new char[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);
  
       }       }
Line 115 
Line 111 
          PEGASUS_ASSERT(data != NULL);          PEGASUS_ASSERT(data != NULL);
          size_t keysize = strlen(key);          size_t keysize = strlen(key);
  
          _key = new char[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 129 
Line 125 
             {             {
                _delete_func( _data );                _delete_func( _data );
             }             }
          if( _key != NULL )  
             delete [] _key;  
       }       }
  
       /**       /**
Line 196 
Line 190 
  
       inline Boolean operator==(const void *key) const       inline Boolean operator==(const void *key) const
       {       {
          if ( ! strcmp(_key, reinterpret_cast<const char *>(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==(b._key));           return(operator==(b._key.get()));
         }
   
         static bool equal(const thread_data* node, const void* key)
         {
            return ((thread_data*)node)->operator==(key);
       }       }
  
    private:    private:
Line 211 
Line 210 
       thread_data();       thread_data();
       void *_data;       void *_data;
       size_t _size;       size_t _size;
       char *_key;        AutoArrayPtr<char> _key;
  
       friend class DQueue<thread_data>;  
       friend class Thread;       friend class Thread;
 }; };
  
  
   enum ThreadStatus {
           PEGASUS_THREAD_OK = 1, /* No problems */
           PEGASUS_THREAD_INSUFFICIENT_RESOURCES, /* Can't allocate a thread. Not enough
                                           memory. Try again later */
           PEGASUS_THREAD_SETUP_FAILURE, /* Could not allocate into the thread specific
                                    data storage. */
           PEGASUS_THREAD_UNAVAILABLE  /* Service is being destroyed and no new threads can
                                  be provided. */
   };
   
 /////////////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////////////
  
 class PEGASUS_COMMON_LINKAGE Thread  class PEGASUS_COMMON_LINKAGE Thread : public Linkable
 { {
   
    public:    public:
       Thread( PEGASUS_THREAD_RETURN (PEGASUS_THREAD_CDECL *start )(void *),  
         Thread( ThreadReturnType (PEGASUS_THREAD_CDECL *start )(void *),
               void *parameter, Boolean detached );               void *parameter, Boolean detached );
  
       ~Thread();       ~Thread();
  
       /**       /**
           Start the thread.           Start the thread.
           @return true if the thread is started successfully, false if the            @return PEGASUS_THREAD_OK if the thread is started successfully,
                   resources necessary to start the thread are not currently                    PEGASUS_THREAD_INSUFFICIENT_RESOURCES if the resources necessary
                   available.  ATTN: The result is undefined for any other                    to start the thread are not currently available.
                   type of failure.  (See Bugzilla 972)                    PEGASUS_THREAD_SETUP_FAILURE if the thread could not
                     be create properly - check the 'errno' value for specific operating
                     system return code.
        */        */
       Boolean run();        ThreadStatus run();
  
       // get the user parameter       // get the user parameter
       inline void *get_parm() { return _thread_parm; }       inline void *get_parm() { return _thread_parm; }
Line 277 
Line 287 
  
       // 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
       void exit_self(PEGASUS_THREAD_RETURN return_code);        void exit_self(ThreadReturnType return_code);
  
       // 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)
Line 292 
Line 302 
       inline void create_tsd(const char *key, int size, void *buffer)       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_front(tsd.get());
         tsd.release();         tsd.release();
       }       }
  
Line 302 
Line 312 
       inline void *reference_tsd(const char *key)       inline void *reference_tsd(const char *key)
       {       {
          _tsd.lock();          _tsd.lock();
          thread_data *tsd = _tsd.reference(key);           thread_data *tsd = _tsd.find(thread_data::equal, key);
          if(tsd != NULL)          if(tsd != NULL)
             return( (void *)(tsd->_data) );             return( (void *)(tsd->_data) );
          else          else
Line 313 
Line 323 
       inline void *try_reference_tsd(const char *key)       inline void *try_reference_tsd(const char *key)
       {       {
          _tsd.try_lock();          _tsd.try_lock();
          thread_data *tsd = _tsd.reference(key);           thread_data *tsd = _tsd.find(thread_data::equal, key);
          if(tsd != NULL)          if(tsd != NULL)
             return((void *)(tsd->_data) );             return((void *)(tsd->_data) );
          else          else
Line 333 
Line 343 
       // @exception IPCException       // @exception IPCException
       inline void delete_tsd(const char *key)       inline void delete_tsd(const char *key)
       {       {
          AutoPtr<thread_data> tsd(_tsd.remove(key));           AutoPtr<thread_data> tsd(_tsd.remove(thread_data::equal, key));
       }  
   
       // Note: Caller must delete the thread_data object returned (if not null)  
       // @exception IPCException  
       inline void *remove_tsd(const char *key)  
       {  
          return(_tsd.remove((const void *)key));  
       }       }
  
       // @exception IPCException       // @exception IPCException
       inline void empty_tsd()       inline void empty_tsd()
       {       {
          try           _tsd.clear();
          {  
             _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
Line 371 
Line 359 
       {       {
          PEGASUS_ASSERT(key != NULL);          PEGASUS_ASSERT(key != NULL);
          AutoPtr<thread_data> tsd;          AutoPtr<thread_data> tsd;
          tsd.reset(_tsd.remove((const void *)key));  // may throw an IPC exception           tsd.reset(_tsd.remove(thread_data::equal, key));  // may throw an IPC exception
          tsd.reset();          tsd.reset();
          AutoPtr<thread_data> ntsd(new thread_data(key));          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.get()); }           try { _tsd.insert_front(ntsd.get()); }
          catch(IPCException& e) { e = e; throw; }          catch(IPCException& e) { e = e; throw; }
          ntsd.release();          ntsd.release();
       }       }
       inline PEGASUS_THREAD_RETURN get_exit() { return _exit_code; }        inline ThreadReturnType get_exit() { return _exit_code; }
       inline PEGASUS_THREAD_TYPE self() {return pegasus_thread_self(); }        inline ThreadType self() {return Threads::self(); }
  
       PEGASUS_THREAD_HANDLE getThreadHandle() {return _handle;}        ThreadHandle getThreadHandle() {return _handle;}
   
       inline Boolean operator==(const void *key) const  
       {  
          if ( (void *)this == key)  
             return(true);  
          return(false);  
       }  
       inline Boolean operator==(const Thread & b) const  
       {  
          return(operator==((const void *)&b ));  
       }  
  
       void detach();       void detach();
  
Line 411 
Line 388 
       static void setCurrent(Thread * thrd); // l10n       static void setCurrent(Thread * thrd); // l10n
  
       //       //
       //  Gets the AcceptLanguages object associated with the caller's        //  Gets the AcceptLanguageList object associated with the caller's
       //  Thread.       //  Thread.
       //  Note: this may return NULL if no Thread object, or no       //  Note: this may return NULL if no Thread object, or no
       //  AcceptLanguages object, is associated with the caller's thread.        //  AcceptLanguageList object, is associated with the caller's thread.
       //       //
       static AcceptLanguages * getLanguages(); //l10n        static AcceptLanguageList * getLanguages(); //l10n
  
       //       //
       //  Sets the AcceptLanguages object associated with the caller's        //  Sets the AcceptLanguageList object associated with the caller's
       //  Thread.       //  Thread.
       //  Note: a Thread object must have been previously associated with       //  Note: a Thread object must have been previously associated with
       //  the caller's thread.       //  the caller's thread.
       //  Note: the AcceptLanguages object must be placed on the heap.        //  Note: the AcceptLanguageList object must be placed on the heap.
       //       //
       static void setLanguages(AcceptLanguages *langs); //l10n        static void setLanguages(AcceptLanguageList *langs); //l10n
  
       //       //
       //  Removes the AcceptLanguages object associated with the caller's        //  Removes the AcceptLanguageList object associated with the caller's
       //  Thread.       //  Thread.
       //       //
       static void clearLanguages(); //l10n       static void clearLanguages(); //l10n
Line 442 
Line 419 
       inline void create_tsd(const char *key )       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_front(tsd.get());
          tsd.release();          tsd.release();
       }       }
       PEGASUS_THREAD_HANDLE _handle;        ThreadHandle _handle;
       Boolean _is_detached;       Boolean _is_detached;
       Boolean _cancel_enabled;       Boolean _cancel_enabled;
       Boolean _cancelled;       Boolean _cancelled;
  
       PEGASUS_SEM_HANDLE _suspend_count;  
   
       // always pass this * as the void * parameter to the thread       // always pass this * as the void * parameter to the thread
       // store the user parameter in _thread_parm       // store the user parameter in _thread_parm
  
       PEGASUS_THREAD_RETURN  ( PEGASUS_THREAD_CDECL *_start)(void *);        ThreadReturnType  ( PEGASUS_THREAD_CDECL *_start)(void *);
       DQueue<class cleanup_handler> _cleanup;        List<cleanup_handler, Mutex> _cleanup;
       DQueue<class thread_data> _tsd;        List<thread_data, Mutex> _tsd;
  
       void *_thread_parm;       void *_thread_parm;
       PEGASUS_THREAD_RETURN _exit_code;        ThreadReturnType _exit_code;
       static Boolean _signals_blocked;       static Boolean _signals_blocked;
       static PEGASUS_THREAD_KEY_TYPE _platform_thread_key;  //l10n        static TSDKeyType _platform_thread_key;  //l10n
       static Boolean _key_initialized; // l10n       static Boolean _key_initialized; // l10n
       static Boolean _key_error; // l10n       static Boolean _key_error; // l10n
 }; };
Line 506 
Line 481 
         @param blocking A pointer to an optional semaphore which, if         @param blocking A pointer to an optional semaphore which, if
                         specified, is signaled after the thread finishes                         specified, is signaled after the thread finishes
                         executing the work function                         executing the work function
         @return true if the thread is started successfully, false if the          @return PEGASUS_THREAD_OK if the thread is started successfully,
                   PEGASUS_THREAD_INSUFFICIENT_RESOURCES  if the
                 resources necessary to start the thread are not currently                 resources necessary to start the thread are not currently
                 available.  ATTN: The result is undefined for any other                  available.  PEGASUS_THREAD_SETUP_FAILURE if the thread
                 type of thread creation failure.                  could not be setup properly. PEGASUS_THREAD_UNAVAILABLE
                   if this service is shutting down and no more threads can
                   be allocated.
         @exception IPCException         @exception IPCException
      */      */
     Boolean allocate_and_awaken(      ThreadStatus allocate_and_awaken(
         void* parm,         void* parm,
         PEGASUS_THREAD_RETURN (PEGASUS_THREAD_CDECL* work)(void *),          ThreadReturnType (PEGASUS_THREAD_CDECL* work)(void *),
         Semaphore* blocking = 0);         Semaphore* blocking = 0);
  
     /**     /**
Line 528 
Line 506 
  
     void get_key(Sint8* buf, int bufsize);     void get_key(Sint8* buf, int bufsize);
  
     inline Boolean operator==(const char* key) const  
     {  
         return (!strncmp(key, _key, 16));  
     }  
   
     Boolean operator==(const void* p) const  
     {  
         return ((void *)this == p);  
     }  
   
     Boolean operator==(const ThreadPool & p) const  
     {  
         return operator==((const void *)&p);  
     }  
   
     inline void setMinThreads(Sint16 min)     inline void setMinThreads(Sint16 min)
     {     {
         _minThreads = min;         _minThreads = min;
Line 565 
Line 528 
  
     inline Uint32 runningCount()     inline Uint32 runningCount()
     {     {
         return _runningThreads.count();          return _runningThreads.size();
     }     }
  
     inline Uint32 idleCount()     inline Uint32 idleCount()
     {     {
         return _idleThreads.count();          return _idleThreads.size();
     }     }
  
 private: private:
Line 579 
Line 542 
     ThreadPool(const ThreadPool&);    // Unimplemented     ThreadPool(const ThreadPool&);    // Unimplemented
     ThreadPool& operator=(const ThreadPool&);    // Unimplemented     ThreadPool& operator=(const ThreadPool&);    // Unimplemented
  
     static PEGASUS_THREAD_RETURN PEGASUS_THREAD_CDECL _loop(void *);      static ThreadReturnType PEGASUS_THREAD_CDECL _loop(void *);
  
     static Boolean _timeIntervalExpired(     static Boolean _timeIntervalExpired(
         struct timeval* start,         struct timeval* start,
Line 596 
Line 559 
     AtomicInt _currentThreads;     AtomicInt _currentThreads;
     struct timeval _deallocateWait;     struct timeval _deallocateWait;
     char _key[17];     char _key[17];
     DQueue<Thread> _idleThreads;      List<Thread, Mutex> _idleThreads;
     DQueue<Thread> _runningThreads;      List<Thread, Mutex> _runningThreads;
     AtomicInt _dying;     AtomicInt _dying;
 }; };
  


Legend:
Removed from v.1.50  
changed lines
  Added in v.1.57.2.1

No CVS admin address has been configured
Powered by
ViewCVS 0.9.2