(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.57.2.1 and 1.61.14.1

version 1.57.2.1, 2006/07/27 23:11:52 version 1.61.14.1, 2008/08/20 23:05:50
Line 29 
Line 29 
 // //
 //============================================================================== //==============================================================================
 // //
 // Author: Mike Day (mdday@us.ibm.com)  
 //  
 // 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)  
 //              Josephine Eskaline Joyce, IBM (jojustin@in.ibm.com) for Bug#2393  
 //  
 //%///////////////////////////////////////////////////////////////////////////// //%/////////////////////////////////////////////////////////////////////////////
  
 #ifndef Pegasus_Thread_h #ifndef Pegasus_Thread_h
Line 55 
Line 45 
 #include <Pegasus/Common/Mutex.h> #include <Pegasus/Common/Mutex.h>
 #include <Pegasus/Common/Semaphore.h> #include <Pegasus/Common/Semaphore.h>
 #include <Pegasus/Common/TSDKey.h> #include <Pegasus/Common/TSDKey.h>
   #include <Pegasus/Common/Threads.h>
   
   #if defined(PEGASUS_HAVE_PTHREADS)
   # include <signal.h>
   #endif
  
 PEGASUS_NAMESPACE_BEGIN PEGASUS_NAMESPACE_BEGIN
  
 class PEGASUS_COMMON_LINKAGE cleanup_handler : public Linkable 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),
       ~cleanup_handler()  {; }          _arg(arg)
       {
       }
       ~cleanup_handler()
       {;
       }
  
    private:    private:
       void execute() { _routine(_arg); }  
       void execute()
       {
           _routine(_arg);
       }
   
       cleanup_handler();       cleanup_handler();
       void (*_routine)(void *);  
  
       void (*_routine)(void*);
       void *_arg;       void *_arg;
       ThreadCleanupType _cleanup_buffer;  
       friend class Thread;       friend class Thread;
 }; };
  
 /////////////////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////////////////
  
   
 class  PEGASUS_COMMON_LINKAGE thread_data : public Linkable class  PEGASUS_COMMON_LINKAGE thread_data : public Linkable
 { {
   
    public:    public:
   
       static void default_delete(void *data);       static void default_delete(void *data);
  
       thread_data( const char *key ) : _delete_func(NULL) , _data(NULL), _size(0)       thread_data( const char *key ) : _delete_func(NULL) , _data(NULL), _size(0)
Line 91 
Line 94 
          _key.reset(new char[keysize + 1]);          _key.reset(new char[keysize + 1]);
          memcpy(_key.get(), key, keysize);          memcpy(_key.get(), key, keysize);
          _key.get()[keysize] = 0x00;          _key.get()[keysize] = 0x00;
   
       }       }
  
       thread_data(const char *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);
Line 102 
Line 105 
          memcpy(_key.get(), key, keysize);          memcpy(_key.get(), key, keysize);
          _key.get()[keysize] = 0x00;          _key.get()[keysize] = 0x00;
          _data = ::operator new(_size);          _data = ::operator new(_size);
   
       }       }
  
       thread_data(const char *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);
Line 152 
Line 155 
          return;          return;
       }       }
  
       size_t get_size() { 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
        * TSD, the thread specific data.          TSD, the thread specific data.
        *  
        * Be aware that there is NOTHING in place to stop          Be aware that there is NOTHING in place to stop
        * other users of the thread to change the data you          other users of the thread to change the data you
        * get from this function.          get from this function.
        *  
        * 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.
        */        */
       void get_data(void **data, size_t *size)       void get_data(void **data, size_t *size)
       {       {
Line 173 
Line 179 
  
          *data = _data;          *data = _data;
          *size = _size;          *size = _size;
          return;  
   
       }       }
  
       // @exception NullPointer       // @exception NullPointer
Line 185 
Line 189 
          *buf = ::operator new(_size);          *buf = ::operator new(_size);
          *size = _size;          *size = _size;
          memcpy(*buf, _data, _size);          memcpy(*buf, _data, _size);
          return;  
       }       }
  
       inline Boolean operator==(const void *key) const       inline Boolean operator==(const void *key) const
       {       {
          if ( ! strcmp(_key.get(), 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.get()));          return operator==(b._key.get());
       }       }
  
       static bool equal(const thread_data* node, const void* key)       static bool equal(const thread_data* node, const void* key)
Line 216 
Line 219 
 }; };
  
  
 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.
                                         memory. Try again later */                                                     Not enough memory. Try
         PEGASUS_THREAD_SETUP_FAILURE, /* Could not allocate into the thread specific                                                     again later */
                                  data storage. */      PEGASUS_THREAD_SETUP_FAILURE,       /* Could not allocate into the thread
         PEGASUS_THREAD_UNAVAILABLE  /* Service is being destroyed and no new threads can                                             specific data storage. */
                                be provided. */      PEGASUS_THREAD_UNAVAILABLE  /* Service is being destroyed and no new
                                      threads can be provided. */
 }; };
  
 /////////////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////////////
Line 232 
Line 237 
 { {
    public:    public:
  
       Thread( ThreadReturnType (PEGASUS_THREAD_CDECL *start )(void *),      Thread(
               void *parameter, Boolean detached );          ThreadReturnType(PEGASUS_THREAD_CDECL* start) (void*),
           void* parameter,
           Boolean detached);
  
       ~Thread();       ~Thread();
  
       /**        /** Start the thread.
           Start the thread.  
           @return PEGASUS_THREAD_OK if the thread is started successfully,           @return PEGASUS_THREAD_OK if the thread is started successfully,
                   PEGASUS_THREAD_INSUFFICIENT_RESOURCES if the resources necessary                   PEGASUS_THREAD_INSUFFICIENT_RESOURCES if the resources necessary
                   to start the thread are not currently available.                   to start the thread are not currently available.
Line 249 
Line 255 
       ThreadStatus 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;
       }
  
       // 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 cancel();
  
       // cancel if there is a pending cancellation request  
       void test_cancel();  
   
       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.
       // platforms with preemptive scheduling and native threads       // platforms with preemptive scheduling and native threads
Line 271 
Line 275 
  
       void thread_switch();       void thread_switch();
  
 #if defined(PEGASUS_PLATFORM_LINUX_GENERIC_GNU)  
       // suspend this thread  
       void suspend();  
   
       // resume this thread  
       void resume();  
 #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 join();
       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 314 
Line 309 
          _tsd.lock();          _tsd.lock();
          thread_data *tsd = _tsd.find(thread_data::equal, 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
             return(NULL);              return NULL;
       }       }
  
       // @exception IPCException       // @exception IPCException
       inline void *try_reference_tsd(const char *key)      inline Boolean try_reference_tsd(const char *key, void** data)
       {
           if (!_tsd.try_lock())
       {       {
          _tsd.try_lock();              return false;
           }
          thread_data *tsd = _tsd.find(thread_data::equal, key);          thread_data *tsd = _tsd.find(thread_data::equal, key);
          if(tsd != NULL)          if(tsd != NULL)
             return((void *)(tsd->_data) );              *data = (void*) (tsd->_data);
          else          else
             return(NULL);              *data = NULL;
           return true;
       }       }
  
  
Line 355 
Line 354 
       // 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
       // @exception IPCException       // @exception IPCException
       void put_tsd(const char *key, void (*delete_func)(void *), Uint32 size, void *value)      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;
          tsd.reset(_tsd.remove(thread_data::equal, key));  // may throw an IPC exception          // This may throw an IPCException
           tsd.reset(_tsd.remove(thread_data::equal, key));
          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_front(ntsd.get()); }          // This may throw an IPCException
          catch(IPCException& e) { e = e; throw; }          _tsd.insert_front(ntsd.get());
          ntsd.release();          ntsd.release();
       }       }
       inline ThreadReturnType get_exit() { return _exit_code; }  
       inline ThreadType self() {return Threads::self(); }  
  
       ThreadHandle getThreadHandle() {return _handle;}      inline ThreadReturnType get_exit()
       {
           return _exit_code;
       }
       inline ThreadType self()
       {
           return Threads::self();
       }
   
       ThreadHandle getThreadHandle()
       {
           return _handle;
       }
  
       void detach();       void detach();
  
Line 379 
Line 393 
       //  Note: this may return NULL if no Thread object is associated       //  Note: this may return NULL if no Thread object is associated
       //  with the caller's thread.       //  with the caller's thread.
       //       //
       static Thread * getCurrent();  // l10n      static Thread *getCurrent();
  
       //       //
       //  Sets the Thread object associated with the caller's thread.       //  Sets the Thread object associated with the caller's thread.
       //  Note: the Thread object must be placed on the heap.       //  Note: the Thread object must be placed on the heap.
       //       //
       static void setCurrent(Thread * thrd); // l10n      static void setCurrent(Thread* thrd);
  
       //       //
       //  Gets the AcceptLanguageList object associated with the caller's       //  Gets the AcceptLanguageList object associated with the caller's
Line 393 
Line 407 
       //  Note: this may return NULL if no Thread object, or no       //  Note: this may return NULL if no Thread object, or no
       //  AcceptLanguageList object, is associated with the caller's thread.       //  AcceptLanguageList object, is associated with the caller's thread.
       //       //
       static AcceptLanguageList * getLanguages(); //l10n      static AcceptLanguageList* getLanguages();
  
       //       //
       //  Sets the AcceptLanguageList 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 AcceptLanguageList object must be placed on the heap.  
       //       //
       static void setLanguages(AcceptLanguageList *langs); //l10n      static void setLanguages(const AcceptLanguageList& langs);
  
       //       //
       //  Removes the AcceptLanguageList object associated with the caller's       //  Removes the AcceptLanguageList object associated with the caller's
       //  Thread.       //  Thread.
       //       //
       static void clearLanguages(); //l10n      static void clearLanguages();
  
    private:    private:
       Thread();       Thread();
  
       static Sint8 initializeKey();  // l10n      static Sint8 initializeKey();
  
       // @exception IPCException       // @exception IPCException
       inline void create_tsd(const char *key )       inline void create_tsd(const char *key )
Line 422 
Line 435 
          _tsd.insert_front(tsd.get());          _tsd.insert_front(tsd.get());
          tsd.release();          tsd.release();
       }       }
   
       ThreadHandle _handle;       ThreadHandle _handle;
       Boolean _is_detached;       Boolean _is_detached;
       Boolean _cancel_enabled;  
       Boolean _cancelled;       Boolean _cancelled;
  
       // always pass this * as the void * parameter to the thread       // always pass this * as the void * parameter to the thread
Line 437 
Line 450 
       void *_thread_parm;       void *_thread_parm;
       ThreadReturnType _exit_code;       ThreadReturnType _exit_code;
       static Boolean _signals_blocked;       static Boolean _signals_blocked;
       static TSDKeyType _platform_thread_key;  //l10n      static TSDKeyType _platform_thread_key;
       static Boolean _key_initialized; // l10n      static Boolean _key_initialized;
       static Boolean _key_error; // l10n      static Boolean _key_error;
 };  
   
   
 class PEGASUS_COMMON_LINKAGE ThreadPool  
 {  
 public:  
   
     /**  
         Constructs a new ThreadPool object.  
         @param initialSize The number of threads that are initially added to  
             the thread pool.  
         @param key A name for this thread pool that can be used to determine  
             equality of two thread pool objects.  Only the first 16 characters  
             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);  
   
     /**  
         Destructs the ThreadPool object.  
      */  
     ~ThreadPool();  
   
     /**  
         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 PEGASUS_THREAD_OK if the thread is started successfully,  
                 PEGASUS_THREAD_INSUFFICIENT_RESOURCES  if the  
                 resources necessary to start the thread are not currently  
                 available.  PEGASUS_THREAD_SETUP_FAILURE if the thread  
                 could not be setup properly. PEGASUS_THREAD_UNAVAILABLE  
                 if this service is shutting down and no more threads can  
                 be allocated.  
         @exception IPCException  
      */  
     ThreadStatus allocate_and_awaken(  
         void* parm,  
         ThreadReturnType (PEGASUS_THREAD_CDECL* work)(void *),  
         Semaphore* blocking = 0);  
   
     /**  
         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);  
   
     inline void setMinThreads(Sint16 min)  
     {  
         _minThreads = min;  
     }  
   
     inline Sint16 getMinThreads() const  
     {  
         return _minThreads;  
     }  
   
     inline void setMaxThreads(Sint16 max)  
     {  
         _maxThreads = max;  
     }  
   
     inline Sint16 getMaxThreads() const  
     {  
         return _maxThreads;  
     }  
   
     inline Uint32 runningCount()  
     {  
         return _runningThreads.size();  
     }  
   
     inline Uint32 idleCount()  
     {  
         return _idleThreads.size();  
     }  
   
 private:  
   
     ThreadPool();    // Unimplemented  
     ThreadPool(const ThreadPool&);    // Unimplemented  
     ThreadPool& operator=(const ThreadPool&);    // Unimplemented  
   
     static ThreadReturnType PEGASUS_THREAD_CDECL _loop(void *);  
   
     static Boolean _timeIntervalExpired(  
         struct timeval* start,  
         struct timeval* interval);  
   
     static void _deleteSemaphore(void* p);  
   
     void _cleanupThread(Thread* thread);  
     Thread* _initializeThread();  
     void _addToIdleThreadsQueue(Thread* th);  
   
     Sint16 _maxThreads;  
     Sint16 _minThreads;  
     AtomicInt _currentThreads;  
     struct timeval _deallocateWait;  
     char _key[17];  
     List<Thread, Mutex> _idleThreads;  
     List<Thread, Mutex> _runningThreads;  
     AtomicInt _dying;  
 }; };
  
   
 #if defined(PEGASUS_OS_TYPE_WINDOWS)  
 # include "ThreadWindows_inline.h"  
 #elif defined(PEGASUS_PLATFORM_ZOS_ZSERIES_IBM)  
 # include "ThreadzOS_inline.h"  
 #elif defined(PEGASUS_OS_TYPE_UNIX)  
 # include "ThreadUnix_inline.h"  
 #elif defined(PEGASUS_OS_VMS)  
 # include "ThreadVms_inline.h"  
 #endif  
   
 PEGASUS_NAMESPACE_END PEGASUS_NAMESPACE_END
  
 #endif // Pegasus_Thread_h #endif // Pegasus_Thread_h


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

No CVS admin address has been configured
Powered by
ViewCVS 0.9.2