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 #ifndef Pegasus_Thread_h
31 #define Pegasus_Thread_h
|
32 kumpf 1.26
33 #include <cstring>
|
34 mike 1.2 #include <Pegasus/Common/Config.h>
|
35 mike 1.5 #include <Pegasus/Common/IPC.h>
|
36 kumpf 1.23 #include <Pegasus/Common/InternalException.h>
|
37 mike 1.2 #include <Pegasus/Common/DQueue.h>
|
38 chuck 1.28 #include <Pegasus/Common/AcceptLanguages.h> // l10n
|
39 kumpf 1.22 #include <Pegasus/Common/Linkage.h>
|
40 mike 1.2
41 PEGASUS_NAMESPACE_BEGIN
42
43 class PEGASUS_COMMON_LINKAGE cleanup_handler
44 {
45
46 public:
47 cleanup_handler( void (*routine)(void *), void *arg ) : _routine(routine), _arg(arg) {}
48 ~cleanup_handler() {; }
49 inline Boolean operator==(const void *key) const
50 {
51 if(key == (void *)_routine)
52 return true;
53 return false;
54 }
55 inline Boolean operator ==(const cleanup_handler & b) const
56 {
57 return(operator==((const void *)b._routine));
58 }
59 private:
60 void execute(void) { _routine(_arg); }
61 mike 1.2 cleanup_handler();
62 void (*_routine)(void *);
63
64 void *_arg;
65 PEGASUS_CLEANUP_HANDLE _cleanup_buffer;
66 friend class DQueue<class cleanup_handler>;
67 friend class Thread;
68 };
69
70 ///////////////////////////////////////////////////////////////////////////////
71
72
73 class PEGASUS_COMMON_LINKAGE thread_data
74 {
75
76 public:
77 static void default_delete(void *data);
78
|
79 kumpf 1.7 thread_data( const Sint8 *key ) : _delete_func(NULL) , _data(NULL), _size(0)
|
80 mike 1.2 {
81 PEGASUS_ASSERT(key != NULL);
82 size_t keysize = strlen(key);
83 _key = new Sint8 [keysize + 1];
84 memcpy(_key, key, keysize);
85 _key[keysize] = 0x00;
86
87 }
88
|
89 kumpf 1.7 thread_data(const Sint8 *key, size_t size) : _delete_func(default_delete), _size(size)
|
90 mike 1.2 {
91 PEGASUS_ASSERT(key != NULL);
92 size_t keysize = strlen(key);
93 _key = new Sint8 [keysize + 1];
94 memcpy(_key, key, keysize);
95 _key[keysize] = 0x00;
96 _data = ::operator new(_size) ;
97
98 }
99
|
100 kumpf 1.7 thread_data(const Sint8 *key, size_t size, void *data) : _delete_func(default_delete), _size(size)
|
101 mike 1.2 {
102 PEGASUS_ASSERT(key != NULL);
103 PEGASUS_ASSERT(data != NULL);
104 size_t keysize = strlen(key);
105
106 _key = new Sint8[keysize + 1];
107 memcpy(_key, key, keysize);
108 _key[keysize] = 0x00;
109 _data = ::operator new(_size);
110 memcpy(_data, data, size);
111 }
112
113 ~thread_data()
114 {
115 if( _data != NULL)
116 if(_delete_func != NULL)
|
117 sage 1.9 {
|
118 mike 1.2 _delete_func( _data );
|
119 sage 1.9 }
|
120 mike 1.2 if( _key != NULL )
121 delete [] _key;
122 }
123
124 void put_data(void (*del)(void *), size_t size, void *data ) throw(NullPointer)
125 {
126 if(_data != NULL)
127 if(_delete_func != NULL)
128 _delete_func(_data);
129
130 _delete_func = del;
131 _data = data;
132 _size = size;
133 return ;
134 }
135
136 size_t get_size(void) { return _size; }
137
138 void get_data(void **data, size_t *size)
139 {
140 if(data == NULL || size == NULL)
141 mike 1.2 throw NullPointer();
142
143 *data = _data;
144 *size = _size;
145 return;
146
147 }
148
|
149 kumpf 1.17 void copy_data(void **buf, size_t *size) throw(NullPointer)
|
150 mike 1.2 {
151 if((buf == NULL) || (size == NULL))
152 throw NullPointer() ;
153 *buf = ::operator new(_size);
154 *size = _size;
155 memcpy(*buf, _data, _size);
156 return;
157 }
158
159 inline Boolean operator==(const void *key) const
160 {
161 if ( ! strcmp(_key, (Sint8 *)key))
162 return(true);
163 return(false);
164 }
|
165 mday 1.8
|
166 mike 1.2 inline Boolean operator==(const thread_data& b) const
167 {
168 return(operator==((const void *)b._key));
169 }
170
171 private:
172 void (*_delete_func) (void *data) ;
173 thread_data();
174 void *_data;
175 size_t _size;
176 Sint8 *_key;
177
178 friend class DQueue<thread_data>;
179 friend class Thread;
180 };
181
182
183 ///////////////////////////////////////////////////////////////////////////
184
185 class PEGASUS_COMMON_LINKAGE ThreadPool;
186
187 mike 1.2 class PEGASUS_COMMON_LINKAGE Thread
188 {
189
190 public:
191 Thread( PEGASUS_THREAD_RETURN (PEGASUS_THREAD_CDECL *start )(void *),
192 void *parameter, Boolean detached );
193
194 ~Thread();
195
196 void run(void);
197
198 // get the user parameter
199 inline void *get_parm(void) { return _thread_parm; }
200
201 // send the thread a signal -- may not be appropriate due to Windows
202 // void kill(int signum);
203
204 // cancellation must be deferred (not asynchronous)
205 // for user-level threads the thread itself can decide
206 // when it should die.
207 void cancel(void);
208 mike 1.2
209 // cancel if there is a pending cancellation request
|
210 kumpf 1.25 void test_cancel(void);
|
211 mike 1.2
|
212 mday 1.3 Boolean is_cancelled(void);
213
|
214 mike 1.2 // for user-level threads - put the calling thread
215 // to sleep and jump to the thread scheduler.
216 // platforms with preemptive scheduling and native threads
217 // can define this to be a no-op.
218 // platforms without preemptive scheduling like NetWare
219 // or gnu portable threads will have an existing
220 // routine that can be mapped to this method
221
222 void thread_switch(void);
223
|
224 david.eger 1.27 #if defined(PEGASUS_PLATFORM_LINUX_GENERIC_GNU)
|
225 mike 1.2 // suspend this thread
226 void suspend(void) ;
227
228 // resume this thread
229 void resume(void) ;
230 #endif
231
232 static void sleep(Uint32 msec) ;
233
234 // block the calling thread until this thread terminates
235 void join( void );
236 void thread_init(void);
237
238 // thread routine needs to call this function when
239 // it is ready to exit
|
240 kumpf 1.25 void exit_self(PEGASUS_THREAD_RETURN return_code) ;
|
241 mike 1.2
242 // stack of functions to be called when thread terminates
243 // will be called last in first out (LIFO)
244 void cleanup_push( void (*routine) (void *), void *parm ) throw(IPCException);
245 void cleanup_pop(Boolean execute = true) throw(IPCException);
246
247 // create and initialize a tsd
|
248 kumpf 1.7 inline void create_tsd(const Sint8 *key, int size, void *buffer) throw(IPCException)
|
249 mike 1.2 {
250 thread_data *tsd = new thread_data(key, size, buffer);
251 try { _tsd.insert_first(tsd); }
252 catch(IPCException& e) { e = e; delete tsd; throw; }
253 }
254
255 // get the buffer associated with the key
256 // NOTE: this call leaves the tsd LOCKED !!!!
|
257 kumpf 1.7 inline void *reference_tsd(const Sint8 *key) throw(IPCException)
|
258 mike 1.2 {
259 _tsd.lock();
|
260 kumpf 1.7 thread_data *tsd = _tsd.reference((const void *)key);
|
261 mike 1.2 if(tsd != NULL)
262 return( (void *)(tsd->_data) );
263 else
264 return(NULL);
265 }
266
|
267 kumpf 1.7 inline void *try_reference_tsd(const Sint8 *key) throw(IPCException)
|
268 mike 1.2 {
269 _tsd.try_lock();
|
270 kumpf 1.7 thread_data *tsd = _tsd.reference((const void *)key);
|
271 mike 1.2 if(tsd != NULL)
272 return((void *)(tsd->_data) );
273 else
274 return(NULL);
275 }
276
277
278 // release the lock held on the tsd
279 // NOTE: assumes a corresponding and prior call to reference_tsd() !!!
280 inline void dereference_tsd(void) throw(IPCException)
281 {
282 _tsd.unlock();
283 }
284
285 // delete the tsd associated with the key
|
286 kumpf 1.7 inline void delete_tsd(const Sint8 *key) throw(IPCException)
|
287 mike 1.2 {
|
288 kumpf 1.7 thread_data *tsd = _tsd.remove((const void *)key);
|
289 mike 1.2 if(tsd != NULL)
290 delete tsd;
291 }
292
|
293 kumpf 1.14 // Note: Caller must delete the thread_data object returned (if not null)
|
294 kumpf 1.7 inline void *remove_tsd(const Sint8 *key) throw(IPCException)
|
295 mike 1.2 {
|
296 kumpf 1.7 return(_tsd.remove((const void *)key));
|
297 mike 1.2 }
298
299 inline void empty_tsd(void) throw(IPCException)
300 {
|
301 kumpf 1.15 thread_data* tsd;
|
302 mday 1.16 while (0 != (tsd = _tsd.remove_first()))
|
303 kumpf 1.15 {
304 delete tsd;
305 }
306 //_tsd.empty_list();
|
307 mike 1.2 }
308
309 // create or re-initialize tsd associated with the key
|
310 kumpf 1.14 // if the tsd already exists, delete the existing buffer
311 void put_tsd(const Sint8 *key, void (*delete_func)(void *), Uint32 size, void *value)
|
312 mike 1.2 throw(IPCException)
313
314 {
315 PEGASUS_ASSERT(key != NULL);
316 thread_data *tsd ;
|
317 kumpf 1.7 tsd = _tsd.remove((const void *)key); // may throw an IPC exception
|
318 kumpf 1.14 delete tsd;
|
319 mike 1.2 thread_data *ntsd = new thread_data(key);
320 ntsd->put_data(delete_func, size, value);
321 try { _tsd.insert_first(ntsd); }
322 catch(IPCException& e) { e = e; delete ntsd; throw; }
323 }
324 inline PEGASUS_THREAD_RETURN get_exit(void) { return _exit_code; }
325 inline PEGASUS_THREAD_TYPE self(void) {return pegasus_thread_self(); }
326
327 PEGASUS_THREAD_HANDLE getThreadHandle() {return _handle;}
328
329 inline Boolean operator==(const void *key) const
330 {
331 if ( (void *)this == key)
332 return(true);
333 return(false);
334 }
335 inline Boolean operator==(const Thread & b) const
336 {
337 return(operator==((const void *)&b ));
338 }
339
340 mike 1.2 void detach(void);
|
341 chuck 1.28
|
342 chuck 1.29 //
343 // Gets the Thread object associated with the caller's thread.
|
344 chuck 1.31 // Note: this may return NULL if no Thread object is associated
345 // with the caller's thread.
|
346 chuck 1.29 //
|
347 chuck 1.28 static Thread * getCurrent(); // l10n
|
348 chuck 1.29
349 //
|
350 chuck 1.31 // Sets the Thread object associated with the caller's thread.
351 // Note: the Thread object must be placed on the heap.
|
352 chuck 1.29 //
353 static void setCurrent(Thread * thrd); // l10n
354
355 //
|
356 chuck 1.31 // Gets the AcceptLanguages object associated with the caller's
|
357 chuck 1.29 // Thread.
|
358 chuck 1.31 // Note: this may return NULL if no Thread object, or no
359 // AcceptLanguages object, is associated with the caller's thread.
|
360 chuck 1.29 //
|
361 chuck 1.28 static AcceptLanguages * getLanguages(); //l10n
362
|
363 chuck 1.29 //
|
364 chuck 1.31 // Sets the AcceptLanguages object associated with the caller's
|
365 chuck 1.29 // Thread.
|
366 chuck 1.31 // Note: a Thread object must have been previously associated with
367 // the caller's thread.
368 // Note: the AcceptLanguages object must be placed on the heap.
|
369 chuck 1.29 //
|
370 chuck 1.28 static void setLanguages(AcceptLanguages *langs); //l10n
371
|
372 chuck 1.29 //
|
373 chuck 1.31 // Removes the AcceptLanguages object associated with the caller's
|
374 chuck 1.29 // Thread.
375 //
|
376 chuck 1.28 static void clearLanguages(); //l10n
|
377 mike 1.2
378 private:
379 Thread();
|
380 chuck 1.29
381 static Sint8 initializeKey(); // l10n
382
|
383 kumpf 1.7 inline void create_tsd(const Sint8 *key ) throw(IPCException)
|
384 mike 1.2 {
385 thread_data *tsd = new thread_data(key);
386 try { _tsd.insert_first(tsd); }
387 catch(IPCException& e) { e = e; delete tsd; throw; }
388 }
389 PEGASUS_THREAD_HANDLE _handle;
390 Boolean _is_detached;
391 Boolean _cancel_enabled;
392 Boolean _cancelled;
393
394 PEGASUS_SEM_HANDLE _suspend_count;
395
396 // always pass this * as the void * parameter to the thread
397 // store the user parameter in _thread_parm
398
399 PEGASUS_THREAD_RETURN ( PEGASUS_THREAD_CDECL *_start)(void *) ;
400 DQueue<class cleanup_handler> _cleanup;
401 DQueue<class thread_data> _tsd;
402
403 void *_thread_parm;
404 PEGASUS_THREAD_RETURN _exit_code;
405 mike 1.2 static Boolean _signals_blocked;
|
406 chuck 1.28 static PEGASUS_THREAD_KEY_TYPE _platform_thread_key; //l10n
|
407 chuck 1.30 static Boolean _key_initialized; // l10n
408 static Boolean _key_error; // l10n
|
409 mike 1.2 friend class ThreadPool;
410 } ;
411
412
413 class PEGASUS_COMMON_LINKAGE ThreadPool
414 {
415 public:
416
417 ThreadPool(Sint16 initial_size,
|
418 kumpf 1.7 const Sint8 *key,
|
419 mike 1.2 Sint16 min,
420 Sint16 max,
421 struct timeval & alloc_wait,
422 struct timeval & dealloc_wait,
423 struct timeval & deadlock_detect);
424
425 ~ThreadPool(void);
426
|
427 mday 1.8 void allocate_and_awaken(void *parm,
|
428 mday 1.10 PEGASUS_THREAD_RETURN (PEGASUS_THREAD_CDECL *work)(void *),
429 Semaphore *blocking = 0)
|
430 mday 1.8 throw(IPCException);
431
432
433 Uint32 kill_dead_threads( void )
|
434 mike 1.2 throw(IPCException);
435
436 void get_key(Sint8 *buf, int bufsize);
437
438 inline Boolean operator==(const void *key) const
439 {
440 if ( ! strncmp( reinterpret_cast<Sint8 *>(const_cast<void *>(key)), _key, 16 ))
441 return(true);
442 return(false);
443 }
444 inline Boolean operator==(const ThreadPool & b) const
445 {
446 return(operator==((const void *) b._key ));
447 }
448
449 inline void set_min_threads(Sint16 min)
450 {
451 _min_threads = min;
452 }
453
454 inline Sint16 get_min_threads(void) const
455 mike 1.2 {
456 return _min_threads;
457 }
458
459 inline void set_max_threads(Sint16 max)
460 {
461 _max_threads = max;
462 }
463
464 inline Sint16 get_max_threads(void) const
465 {
466 return _max_threads;
467 }
468
469 inline void set_allocate_wait(const struct timeval & alloc_wait)
470 {
471 _allocate_wait.tv_sec = alloc_wait.tv_sec;
472 _allocate_wait.tv_usec = alloc_wait.tv_usec;
473 }
474
475 inline struct timeval *get_allocate_wait(struct timeval *buffer) const
476 mike 1.2 {
477 if(buffer == 0)
478 throw NullPointer();
479 buffer->tv_sec = _allocate_wait.tv_sec;
480 buffer->tv_usec = _allocate_wait.tv_usec;
481 return buffer;
482 }
483
484 inline void set_deallocate_wait(const struct timeval & dealloc_wait)
485 {
486 _deallocate_wait.tv_sec = dealloc_wait.tv_sec;
487 _deallocate_wait.tv_usec = dealloc_wait.tv_usec;
488 }
489
490 inline struct timeval *get_deallocate_wait(struct timeval *buffer) const
491 {
492 if(buffer == 0)
493 throw NullPointer();
494 buffer->tv_sec = _deallocate_wait.tv_sec;
495 buffer->tv_usec = _deallocate_wait.tv_usec;
496 return buffer;
497 mike 1.2 }
498
499 inline void set_deadlock_detect(const struct timeval & deadlock)
500 {
501 _deadlock_detect.tv_sec = deadlock.tv_sec;
502 _deadlock_detect.tv_usec = deadlock.tv_usec;
503 }
504
505 inline struct timeval * get_deadlock_detect(struct timeval *buffer) const
506 {
507 if(buffer == 0)
508 throw NullPointer();
509 buffer->tv_sec = _deadlock_detect.tv_sec;
510 buffer->tv_usec = _deadlock_detect.tv_usec;
511 return buffer;
512 }
513
514 inline Uint32 running_count(void)
515 {
516 return _running.count();
517 }
|
518 mday 1.19
519 inline Uint32 pool_count(void)
520 {
521 return _pool.count();
522 }
523 inline Uint32 dead_count(void)
524 {
525 return _dead.count();
526 }
527
|
528 mike 1.2
529 static Boolean check_time(struct timeval *start, struct timeval *interval);
530
|
531 mday 1.21 Boolean operator ==(const ThreadPool & p)
532 {
533 return operator==((const void *)&p);
534 }
535
536 Boolean operator ==(const void *p)
537 {
538 if((void *)this == p)
539 return true;
540 return false;
541 }
542
543 static void kill_idle_threads(void);
544
|
545 mike 1.2 private:
546 ThreadPool(void);
547 Sint16 _max_threads;
548 Sint16 _min_threads;
549 AtomicInt _current_threads;
550 struct timeval _allocate_wait;
551 struct timeval _deallocate_wait;
552 struct timeval _deadlock_detect;
553 static PEGASUS_THREAD_RETURN PEGASUS_THREAD_CDECL _loop(void *);
554 Sint8 _key[17];
555 DQueue<Thread> _pool;
556 DQueue<Thread> _running;
557 DQueue<Thread> _dead;
558 AtomicInt _dying;
559 static void _sleep_sem_del(void *p);
560
561 void _check_deadlock(struct timeval *start) throw(Deadlock);
562 Boolean _check_deadlock_no_throw(struct timeval *start);
563 Boolean _check_dealloc(struct timeval *start);
564 Thread *_init_thread(void) throw(IPCException);
565 void _link_pool(Thread *th) throw(IPCException);
566 mike 1.2 static PEGASUS_THREAD_RETURN _undertaker(void *);
|
567 mday 1.21 static DQueue<ThreadPool> _pools;
|
568 mike 1.2 };
569
570
571
572
573 #if defined(PEGASUS_OS_TYPE_WINDOWS)
574 # include "ThreadWindows_inline.h"
|
575 sage 1.6 #elif defined(PEGASUS_PLATFORM_ZOS_ZSERIES_IBM)
576 # include "ThreadzOS_inline.h"
|
577 mike 1.2 #elif defined(PEGASUS_OS_TYPE_UNIX)
578 # include "ThreadUnix_inline.h"
579 #endif
580
581 PEGASUS_NAMESPACE_END
582
583 #endif // Pegasus_Thread_h
|