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

  1 mday  1.19 //%/-*-c++-*-////////////////////////////////////////////////////////////////////////////
  2 mike  1.2  //
  3 kumpf 1.18 // Copyright (c) 2000, 2001, 2002 BMC Software, Hewlett-Packard Company, IBM,
  4            // The Open Group, Tivoli Systems
  5 mike  1.2  //
  6            // Permission is hereby granted, free of charge, to any person obtaining a copy
  7 kumpf 1.18 // of this software and associated documentation files (the "Software"), to
  8            // deal in the Software without restriction, including without limitation the
  9            // rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
 10 mike  1.2  // sell copies of the Software, and to permit persons to whom the Software is
 11            // furnished to do so, subject to the following conditions:
 12            // 
 13 kumpf 1.18 // THE ABOVE COPYRIGHT NOTICE AND THIS PERMISSION NOTICE SHALL BE INCLUDED IN
 14 mike  1.2  // ALL COPIES OR SUBSTANTIAL PORTIONS OF THE SOFTWARE. THE SOFTWARE IS PROVIDED
 15            // "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT
 16 kumpf 1.18 // LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
 17            // PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
 18            // HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
 19 mike  1.2  // ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
 20            // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 21            //
 22            //==============================================================================
 23            //
 24            // Author: Mike Day (mdday@us.ibm.com)
 25            //
 26            // Modified By: Markus Mueller
 27            //
 28            //%/////////////////////////////////////////////////////////////////////////////
 29            
 30 mday  1.26.2.1 
 31 mike  1.2      #ifndef Pegasus_Thread_h
 32                #define Pegasus_Thread_h
 33                #include <Pegasus/Common/Config.h>
 34 mike  1.5      #include <Pegasus/Common/IPC.h>
 35 mday  1.26.2.1 #include <Pegasus/Common/Exception.h>
 36 mike  1.2      #include <Pegasus/Common/DQueue.h>
 37 kumpf 1.22     #include <Pegasus/Common/Linkage.h>
 38 mike  1.2      
 39                PEGASUS_NAMESPACE_BEGIN
 40                
 41                class PEGASUS_COMMON_LINKAGE cleanup_handler
 42                {
 43                
 44                   public:
 45                      cleanup_handler( void (*routine)(void *), void *arg  ) : _routine(routine), _arg(arg)  {}
 46                      ~cleanup_handler()  {; }
 47                      inline Boolean operator==(const void *key) const
 48                      { 
 49                	 if(key == (void *)_routine) 
 50                	    return true; 
 51                	 return false; 
 52                      }
 53                      inline Boolean operator ==(const cleanup_handler & b) const 
 54                      {
 55                	 return(operator==((const void *)b._routine));
 56                      }
 57                   private:
 58                      void execute(void) { _routine(_arg); } 
 59 mike  1.2            cleanup_handler();
 60                      void (*_routine)(void *);
 61                
 62                      void *_arg; 
 63                      PEGASUS_CLEANUP_HANDLE _cleanup_buffer;
 64                      friend class DQueue<class cleanup_handler>;
 65                      friend class Thread;
 66                };
 67                
 68                ///////////////////////////////////////////////////////////////////////////////
 69                
 70                
 71                class  PEGASUS_COMMON_LINKAGE thread_data
 72                {
 73                
 74                   public:
 75                      static void default_delete(void *data);
 76                      
 77 kumpf 1.7            thread_data( const Sint8 *key ) : _delete_func(NULL) , _data(NULL), _size(0)
 78 mike  1.2            {
 79                	 PEGASUS_ASSERT(key != NULL);
 80                	 size_t keysize = strlen(key);
 81                	 _key = new Sint8 [keysize + 1];
 82                	 memcpy(_key, key, keysize);
 83                	 _key[keysize] = 0x00;
 84                	 
 85                      }
 86                  
 87 kumpf 1.7            thread_data(const Sint8 *key, size_t size) : _delete_func(default_delete), _size(size)
 88 mike  1.2            {
 89                	 PEGASUS_ASSERT(key != NULL);
 90                	 size_t keysize = strlen(key);
 91                	 _key = new Sint8 [keysize + 1];
 92                	 memcpy(_key, key, keysize);
 93                	 _key[keysize] = 0x00;
 94                	 _data = ::operator new(_size) ;
 95                
 96                      }
 97                
 98 kumpf 1.7            thread_data(const Sint8 *key, size_t size, void *data) : _delete_func(default_delete), _size(size)
 99 mike  1.2            {
100                	 PEGASUS_ASSERT(key != NULL);
101                	 PEGASUS_ASSERT(data != NULL);
102                	 size_t keysize = strlen(key);
103                
104                	 _key = new Sint8[keysize + 1];
105                	 memcpy(_key, key, keysize);
106                	 _key[keysize] = 0x00;
107                	 _data = ::operator new(_size);
108                	 memcpy(_data, data, size);
109                      }
110                
111                      ~thread_data() 
112                      { 
113                	 if( _data != NULL) 
114                	    if(_delete_func != NULL)
115 sage  1.9                  {
116 mike  1.2      	       _delete_func( _data ); 
117 sage  1.9                  }
118 mike  1.2      	 if( _key != NULL )
119                	    delete [] _key;
120                      }  
121                
122                      void put_data(void (*del)(void *), size_t size, void *data ) throw(NullPointer)
123                      {
124                	 if(_data != NULL)
125                	    if(_delete_func != NULL)
126                	       _delete_func(_data);
127                
128                	 _delete_func = del;
129                	 _data = data;
130                	 _size = size;
131                	 return ;
132                      }
133                
134                      size_t get_size(void) { return _size; }
135                
136                      void get_data(void **data, size_t *size) 
137                      {  
138                	 if(data == NULL || size == NULL)
139 mike  1.2      	    throw NullPointer();
140                	 
141                	 *data = _data;
142                	 *size = _size;
143                	 return;
144                	 
145                      }
146                
147 kumpf 1.17           void copy_data(void **buf, size_t *size) throw(NullPointer)
148 mike  1.2            {
149                	 if((buf == NULL) || (size == NULL)) 
150                	    throw NullPointer() ; 
151                	 *buf = ::operator new(_size);
152                	 *size = _size;
153                	 memcpy(*buf, _data, _size);
154                	 return;
155                      }
156                      
157                      inline Boolean operator==(const void *key) const 
158                      { 
159                	 if ( ! strcmp(_key, (Sint8 *)key)) 
160                	    return(true); 
161                	 return(false);
162                      } 
163 mday  1.8      
164 mike  1.2            inline Boolean operator==(const thread_data& b) const
165                      {
166                	 return(operator==((const void *)b._key));
167                      }
168                
169                   private:
170                      void (*_delete_func) (void *data) ;
171                      thread_data();
172                      void *_data;
173                      size_t _size;
174                      Sint8 *_key;
175                
176                      friend class DQueue<thread_data>;
177                      friend class Thread;
178                };
179                
180                
181                ///////////////////////////////////////////////////////////////////////////
182                
183                class PEGASUS_COMMON_LINKAGE ThreadPool;
184                
185 mike  1.2      class PEGASUS_COMMON_LINKAGE Thread
186                {
187                
188                   public:
189                      Thread( PEGASUS_THREAD_RETURN (PEGASUS_THREAD_CDECL *start )(void *),
190                	      void *parameter, Boolean detached );
191                
192                      ~Thread();
193                
194                      void run(void);
195                
196                      // get the user parameter 
197                      inline void *get_parm(void) { return _thread_parm; }
198                
199                      // send the thread a signal -- may not be appropriate due to Windows 
200                      //  void kill(int signum); 
201                
202                      // cancellation must be deferred (not asynchronous)
203                      // for user-level threads the thread itself can decide
204                      // when it should die. 
205                      void cancel(void);
206 mike  1.2      
207                      // cancel if there is a pending cancellation request
208 kumpf 1.25           void test_cancel(void);
209 mike  1.2      
210 mday  1.3            Boolean is_cancelled(void);
211                      
212 mike  1.2            // for user-level threads  - put the calling thread
213                      // to sleep and jump to the thread scheduler. 
214                      // platforms with preemptive scheduling and native threads 
215                      // can define this to be a no-op. 
216                      // platforms without preemptive scheduling like NetWare 
217                      // or gnu portable threads will have an existing 
218                      // routine that can be mapped to this method 
219                
220                      void thread_switch(void);
221                
222 kumpf 1.11     #if defined(PEGASUS_PLATFORM_LINUX_IX86_GNU) || defined(PEGASUS_PLATFORM_LINUX_GENERIC_GNU)
223 mike  1.2            // suspend this thread 
224                      void suspend(void) ;
225                
226                      // resume this thread
227                      void resume(void) ;
228                #endif
229                
230                      static void sleep(Uint32 msec) ;
231                
232                      // block the calling thread until this thread terminates
233                      void join( void );
234                      void thread_init(void);
235                
236                      // thread routine needs to call this function when
237                      // it is ready to exit
238 kumpf 1.25           void exit_self(PEGASUS_THREAD_RETURN return_code) ;
239 mike  1.2      
240                      // stack of functions to be called when thread terminates
241                      // will be called last in first out (LIFO)
242                      void cleanup_push( void (*routine) (void *), void *parm ) throw(IPCException);
243                      void cleanup_pop(Boolean execute = true) throw(IPCException);
244                
245                      // create and initialize a tsd
246 kumpf 1.7            inline void create_tsd(const Sint8 *key, int size, void *buffer) throw(IPCException)
247 mike  1.2            {
248                	 thread_data *tsd = new thread_data(key, size, buffer);
249                	 try { _tsd.insert_first(tsd); }
250                	 catch(IPCException& e) { e = e; delete tsd; throw; }
251                      }
252                
253                      // get the buffer associated with the key
254                      // NOTE: this call leaves the tsd LOCKED !!!! 
255 kumpf 1.7            inline void *reference_tsd(const Sint8 *key) throw(IPCException)
256 mike  1.2            {
257                	 _tsd.lock(); 
258 kumpf 1.7      	 thread_data *tsd = _tsd.reference((const void *)key);
259 mike  1.2      	 if(tsd != NULL)
260                	    return( (void *)(tsd->_data) );
261                	 else
262                	    return(NULL);
263                      }
264                
265 kumpf 1.7            inline void *try_reference_tsd(const Sint8 *key) throw(IPCException)
266 mike  1.2            {
267                	 _tsd.try_lock();
268 kumpf 1.7      	 thread_data *tsd = _tsd.reference((const void *)key);
269 mike  1.2      	 if(tsd != NULL)
270                	    return((void *)(tsd->_data) );
271                	 else
272                	    return(NULL);
273                      }
274                      
275                
276                      // release the lock held on the tsd
277                      // NOTE: assumes a corresponding and prior call to reference_tsd() !!!
278                      inline void dereference_tsd(void) throw(IPCException)
279                      {
280                	 _tsd.unlock();
281                      }
282                
283                      // delete the tsd associated with the key
284 kumpf 1.7            inline void delete_tsd(const Sint8 *key) throw(IPCException)
285 mike  1.2            {
286 kumpf 1.7      	 thread_data *tsd = _tsd.remove((const void *)key);
287 mike  1.2      	 if(tsd != NULL)
288                	    delete tsd;
289                      }
290                
291 kumpf 1.14           // Note: Caller must delete the thread_data object returned (if not null)
292 kumpf 1.7            inline void *remove_tsd(const Sint8 *key) throw(IPCException)
293 mike  1.2            {
294 kumpf 1.7      	 return(_tsd.remove((const void *)key));
295 mike  1.2            }
296                      
297                      inline void empty_tsd(void) throw(IPCException)
298                      {
299 kumpf 1.15              thread_data* tsd;
300 mday  1.16              while (0 != (tsd = _tsd.remove_first()))
301 kumpf 1.15              {
302                            delete tsd;
303                         }
304                	 //_tsd.empty_list();
305 mike  1.2            }
306                      
307                      // create or re-initialize tsd associated with the key
308 kumpf 1.14           // if the tsd already exists, delete the existing buffer
309                      void put_tsd(const Sint8 *key, void (*delete_func)(void *), Uint32 size, void *value) 
310 mike  1.2      	 throw(IPCException)
311                
312                      {
313                	 PEGASUS_ASSERT(key != NULL);
314                	 thread_data *tsd ;
315 kumpf 1.7      	 tsd = _tsd.remove((const void *)key);  // may throw an IPC exception 
316 kumpf 1.14     	 delete tsd;
317 mike  1.2      	 thread_data *ntsd = new thread_data(key);
318                	 ntsd->put_data(delete_func, size, value);
319                	 try { _tsd.insert_first(ntsd); }
320                	 catch(IPCException& e) { e = e; delete ntsd; throw; }
321                      }
322                      inline PEGASUS_THREAD_RETURN get_exit(void) { return _exit_code; }
323                      inline PEGASUS_THREAD_TYPE self(void) {return pegasus_thread_self(); }
324                
325                      PEGASUS_THREAD_HANDLE getThreadHandle() {return _handle;}
326                
327                      inline Boolean operator==(const void *key) const 
328                      { 
329                	 if ( (void *)this == key) 
330                	    return(true); 
331                	 return(false);
332                      } 
333                      inline Boolean operator==(const Thread & b) const
334                      {
335                	 return(operator==((const void *)&b ));
336                      }
337                
338 mike  1.2            void detach(void);
339                  
340                   private:
341                      Thread();
342 kumpf 1.7            inline void create_tsd(const Sint8 *key ) throw(IPCException)
343 mike  1.2            {
344                	 thread_data *tsd = new thread_data(key);
345                	 try { _tsd.insert_first(tsd); }
346                	 catch(IPCException& e) { e = e; delete tsd; throw; }
347                      }
348                      PEGASUS_THREAD_HANDLE _handle;
349                      Boolean _is_detached;
350                      Boolean _cancel_enabled;
351                      Boolean _cancelled; 
352                  
353                      PEGASUS_SEM_HANDLE _suspend_count;
354                
355                      // always pass this * as the void * parameter to the thread
356                      // store the user parameter in _thread_parm 
357                
358                      PEGASUS_THREAD_RETURN  ( PEGASUS_THREAD_CDECL *_start)(void *) ;
359                      DQueue<class cleanup_handler> _cleanup;
360                      DQueue<class thread_data> _tsd;
361                
362                      void *_thread_parm;
363                      PEGASUS_THREAD_RETURN _exit_code;
364 mike  1.2            static Boolean _signals_blocked;
365                      friend class ThreadPool;
366                } ;
367                
368                
369                class PEGASUS_COMMON_LINKAGE ThreadPool
370                {
371                   public:
372                
373                      ThreadPool(Sint16 initial_size,
374 kumpf 1.7      		 const Sint8 *key,
375 mike  1.2      		 Sint16 min,
376                		 Sint16 max,
377                		 struct timeval & alloc_wait,
378                		 struct timeval & dealloc_wait, 
379                		 struct timeval & deadlock_detect);
380                      
381                      ~ThreadPool(void);
382                
383 mday  1.8            void allocate_and_awaken(void *parm, 
384 mday  1.10     			       PEGASUS_THREAD_RETURN (PEGASUS_THREAD_CDECL *work)(void *), 
385                			       Semaphore *blocking = 0)
386 mday  1.8      	 throw(IPCException);
387                      
388                
389                      Uint32 kill_dead_threads( void ) 
390 mike  1.2      	 throw(IPCException);
391                      
392                      void get_key(Sint8 *buf, int bufsize);
393                
394                      inline Boolean operator==(const void *key) const 
395                      { 
396                	 if ( ! strncmp( reinterpret_cast<Sint8 *>(const_cast<void *>(key)), _key, 16  )) 
397                	    return(true); 
398                	 return(false);
399                      } 
400                      inline Boolean operator==(const ThreadPool & b) const
401                      {
402                	 return(operator==((const void *) b._key ));
403                      }
404                
405                      inline void set_min_threads(Sint16 min)
406                      {
407                	 _min_threads = min;
408                      }
409                      
410                      inline Sint16 get_min_threads(void) const
411 mike  1.2            {
412                	 return _min_threads;
413                      }
414                
415                      inline void set_max_threads(Sint16 max)
416                      {
417                	 _max_threads = max;
418                      }
419                      
420                      inline Sint16 get_max_threads(void) const
421                      {
422                	 return _max_threads;
423                      }
424                      
425                      inline void set_allocate_wait(const struct timeval & alloc_wait)
426                      {
427                	 _allocate_wait.tv_sec = alloc_wait.tv_sec;
428                	 _allocate_wait.tv_usec = alloc_wait.tv_usec;
429                      }
430                      
431                      inline struct timeval *get_allocate_wait(struct timeval *buffer) const
432 mike  1.2            {
433                	 if(buffer == 0)
434                	    throw NullPointer();
435                	 buffer->tv_sec = _allocate_wait.tv_sec;
436                	 buffer->tv_usec = _allocate_wait.tv_usec;
437                	 return buffer;
438                      }
439                
440                      inline void set_deallocate_wait(const struct timeval & dealloc_wait)
441                      {
442                	 _deallocate_wait.tv_sec = dealloc_wait.tv_sec;
443                	 _deallocate_wait.tv_usec = dealloc_wait.tv_usec;
444                      }
445                      
446                      inline struct timeval *get_deallocate_wait(struct timeval *buffer) const
447                      {
448                	 if(buffer == 0)
449                	    throw NullPointer();
450                	 buffer->tv_sec = _deallocate_wait.tv_sec;
451                	 buffer->tv_usec = _deallocate_wait.tv_usec;
452                	 return buffer;
453 mike  1.2            }
454                
455                      inline void set_deadlock_detect(const struct timeval & deadlock)
456                      {
457                	 _deadlock_detect.tv_sec = deadlock.tv_sec;
458                	 _deadlock_detect.tv_usec = deadlock.tv_usec;
459                      }
460                      
461                      inline struct timeval * get_deadlock_detect(struct timeval *buffer) const
462                      {
463                	 if(buffer == 0)
464                	    throw NullPointer();
465                	 buffer->tv_sec = _deadlock_detect.tv_sec;
466                	 buffer->tv_usec = _deadlock_detect.tv_usec;
467                	 return buffer;
468                      }
469                
470                      inline Uint32 running_count(void)
471                      {
472                	 return _running.count();
473                      }
474 mday  1.19           
475                      inline Uint32 pool_count(void)
476                	{
477                	  return _pool.count();
478                	}
479                      inline Uint32 dead_count(void)
480                	{
481                	  return _dead.count();
482                	}
483                      
484 mike  1.2            
485                      static Boolean check_time(struct timeval *start, struct timeval *interval);
486                
487 mday  1.21           Boolean operator ==(const ThreadPool & p)
488                      {
489                	 return operator==((const void *)&p);
490                      }
491                
492                      Boolean operator ==(const void *p)
493                      {
494                	 if((void *)this == p)
495                	    return true;
496                	 return false;
497                      }
498                      
499                      static void kill_idle_threads(void);
500                      
501 mike  1.2         private:
502                      ThreadPool(void);
503                      Sint16 _max_threads;
504                      Sint16 _min_threads;
505                      AtomicInt _current_threads;
506                      struct timeval _allocate_wait;
507                      struct timeval _deallocate_wait;
508                      struct timeval _deadlock_detect;
509                      static PEGASUS_THREAD_RETURN PEGASUS_THREAD_CDECL _loop(void *);
510                      Sint8 _key[17];
511                      DQueue<Thread> _pool;
512                      DQueue<Thread> _running;
513                      DQueue<Thread> _dead;
514                      AtomicInt _dying;
515                      static void _sleep_sem_del(void *p);
516                      
517                      void _check_deadlock(struct timeval *start) throw(Deadlock);
518                      Boolean _check_deadlock_no_throw(struct timeval *start);
519                      Boolean _check_dealloc(struct timeval *start);
520                      Thread *_init_thread(void) throw(IPCException);
521                      void _link_pool(Thread *th) throw(IPCException);
522 mike  1.2            static PEGASUS_THREAD_RETURN  _undertaker(void *);
523 mday  1.21           static DQueue<ThreadPool> _pools;
524 mike  1.2       };
525                
526                
527                
528                
529                #if defined(PEGASUS_OS_TYPE_WINDOWS)
530                # include "ThreadWindows_inline.h"
531 sage  1.6      #elif defined(PEGASUS_PLATFORM_ZOS_ZSERIES_IBM)
532                # include "ThreadzOS_inline.h"
533 mike  1.2      #elif defined(PEGASUS_OS_TYPE_UNIX)
534                # include "ThreadUnix_inline.h"
535                #endif
536                
537                PEGASUS_NAMESPACE_END
538                
539                #endif // Pegasus_Thread_h

No CVS admin address has been configured
Powered by
ViewCVS 0.9.2