version 1.27, 2003/06/14 19:25:38
|
version 1.38, 2004/06/04 05:54:51
|
|
|
//%/-*-c++-*-//////////////////////////////////////////////////////////////////////////// |
//%2003//////////////////////////////////////////////////////////////////////// |
// | // |
// Copyright (c) 2000, 2001, 2002 BMC Software, Hewlett-Packard Company, IBM, |
// Copyright (c) 2000, 2001, 2002 BMC Software, Hewlett-Packard Development |
// The Open Group, Tivoli Systems |
// Company, L. P., IBM Corp., The Open Group, Tivoli Systems. |
|
// Copyright (c) 2003 BMC Software; Hewlett-Packard Development Company, L. P.; |
|
// IBM Corp.; EMC 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 |
|
|
// Author: Mike Day (mdday@us.ibm.com) | // Author: Mike Day (mdday@us.ibm.com) |
// | // |
// Modified By: Markus Mueller | // Modified By: Markus Mueller |
|
// Roger Kumpf, Hewlett-Packard Company (roger_kumpf@hp.com) |
|
// Amit K Arora, IBM (amita@in.ibm.com) for PEP#101 |
// | // |
//%///////////////////////////////////////////////////////////////////////////// | //%///////////////////////////////////////////////////////////////////////////// |
| |
|
|
#include <Pegasus/Common/IPC.h> | #include <Pegasus/Common/IPC.h> |
#include <Pegasus/Common/InternalException.h> | #include <Pegasus/Common/InternalException.h> |
#include <Pegasus/Common/DQueue.h> | #include <Pegasus/Common/DQueue.h> |
|
#include <Pegasus/Common/AcceptLanguages.h> // l10n |
#include <Pegasus/Common/Linkage.h> | #include <Pegasus/Common/Linkage.h> |
|
#include <Pegasus/Common/AutoPtr.h> |
| |
PEGASUS_NAMESPACE_BEGIN | PEGASUS_NAMESPACE_BEGIN |
| |
|
|
| |
~Thread(); | ~Thread(); |
| |
void run(void); |
/** |
|
Start the thread. |
|
@return true if the thread is started successfully, false if the |
|
resources necessary to start the thread are not currently |
|
available. ATTN: The result is undefined for any other |
|
type of failure. (See Bugzilla 972) |
|
*/ |
|
Boolean run(void); |
| |
// get the user parameter | // get the user parameter |
inline void *get_parm(void) { return _thread_parm; } | inline void *get_parm(void) { return _thread_parm; } |
|
|
// create and initialize a tsd | // create and initialize a tsd |
inline void create_tsd(const Sint8 *key, int size, void *buffer) throw(IPCException) | inline void create_tsd(const Sint8 *key, int size, void *buffer) throw(IPCException) |
{ | { |
thread_data *tsd = new thread_data(key, size, buffer); |
AutoPtr<thread_data> tsd(new thread_data(key, size, buffer)); |
try { _tsd.insert_first(tsd); } |
_tsd.insert_first(tsd.get()); |
catch(IPCException& e) { e = e; delete tsd; throw; } |
tsd.release(); |
} | } |
| |
// get the buffer associated with the key | // get the buffer associated with the key |
|
|
// delete the tsd associated with the key | // delete the tsd associated with the key |
inline void delete_tsd(const Sint8 *key) throw(IPCException) | inline void delete_tsd(const Sint8 *key) throw(IPCException) |
{ | { |
thread_data *tsd = _tsd.remove((const void *)key); |
AutoPtr<thread_data> tsd(_tsd.remove((const void *)key)); |
if(tsd != NULL) |
|
delete tsd; |
|
} | } |
| |
// 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 empty_tsd(void) throw(IPCException) | inline void empty_tsd(void) throw(IPCException) |
{ | { |
thread_data* tsd; |
|
while (0 != (tsd = _tsd.remove_first())) |
try |
{ | { |
delete tsd; |
|
|
_tsd.try_lock(); |
} | } |
//_tsd.empty_list(); |
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 |
|
|
| |
{ | { |
PEGASUS_ASSERT(key != NULL); | PEGASUS_ASSERT(key != NULL); |
thread_data *tsd ; |
AutoPtr<thread_data> tsd ; |
tsd = _tsd.remove((const void *)key); // may throw an IPC exception |
tsd.reset(_tsd.remove((const void *)key)); // may throw an IPC exception |
delete tsd; |
tsd.reset(); |
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); } |
try { _tsd.insert_first(ntsd.get()); } |
catch(IPCException& e) { e = e; delete ntsd; throw; } |
catch(IPCException& e) { e = e; throw; } |
|
ntsd.release(); |
} | } |
inline PEGASUS_THREAD_RETURN get_exit(void) { return _exit_code; } | inline PEGASUS_THREAD_RETURN get_exit(void) { return _exit_code; } |
inline PEGASUS_THREAD_TYPE self(void) {return pegasus_thread_self(); } | inline PEGASUS_THREAD_TYPE self(void) {return pegasus_thread_self(); } |
|
|
| |
void detach(void); | void detach(void); |
| |
|
// |
|
// Gets the Thread object associated with the caller's thread. |
|
// Note: this may return NULL if no Thread object is associated |
|
// with the caller's thread. |
|
// |
|
static Thread * getCurrent(); // l10n |
|
|
|
// |
|
// Sets the Thread object associated with the caller's thread. |
|
// Note: the Thread object must be placed on the heap. |
|
// |
|
static void setCurrent(Thread * thrd); // l10n |
|
|
|
// |
|
// Gets the AcceptLanguages object associated with the caller's |
|
// Thread. |
|
// Note: this may return NULL if no Thread object, or no |
|
// AcceptLanguages object, is associated with the caller's thread. |
|
// |
|
static AcceptLanguages * getLanguages(); //l10n |
|
|
|
// |
|
// Sets the AcceptLanguages object associated with the caller's |
|
// Thread. |
|
// Note: a Thread object must have been previously associated with |
|
// the caller's thread. |
|
// Note: the AcceptLanguages object must be placed on the heap. |
|
// |
|
static void setLanguages(AcceptLanguages *langs); //l10n |
|
|
|
// |
|
// Removes the AcceptLanguages object associated with the caller's |
|
// Thread. |
|
// |
|
static void clearLanguages(); //l10n |
|
|
private: | private: |
Thread(); | Thread(); |
|
|
|
static Sint8 initializeKey(); // l10n |
|
|
inline void create_tsd(const Sint8 *key ) throw(IPCException) | inline void create_tsd(const Sint8 *key ) throw(IPCException) |
{ | { |
thread_data *tsd = new thread_data(key); |
AutoPtr<thread_data> tsd(new thread_data(key)); |
try { _tsd.insert_first(tsd); } |
_tsd.insert_first(tsd.get()); |
catch(IPCException& e) { e = e; delete tsd; throw; } |
tsd.release(); |
} | } |
PEGASUS_THREAD_HANDLE _handle; | PEGASUS_THREAD_HANDLE _handle; |
Boolean _is_detached; | Boolean _is_detached; |
|
|
void *_thread_parm; | void *_thread_parm; |
PEGASUS_THREAD_RETURN _exit_code; | PEGASUS_THREAD_RETURN _exit_code; |
static Boolean _signals_blocked; | static Boolean _signals_blocked; |
|
static PEGASUS_THREAD_KEY_TYPE _platform_thread_key; //l10n |
|
static Boolean _key_initialized; // l10n |
|
static Boolean _key_error; // l10n |
friend class ThreadPool; | friend class ThreadPool; |
} ; | } ; |
| |
|
|
| |
~ThreadPool(void); | ~ThreadPool(void); |
| |
void allocate_and_awaken(void *parm, |
/** |
|
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 true if the thread is started successfully, false if the |
|
resources necessary to start the thread are not currently |
|
available. ATTN: The result is undefined for any other |
|
type of thread creation failure. |
|
*/ |
|
Boolean allocate_and_awaken(void *parm, |
PEGASUS_THREAD_RETURN (PEGASUS_THREAD_CDECL *work)(void *), | PEGASUS_THREAD_RETURN (PEGASUS_THREAD_CDECL *work)(void *), |
Semaphore *blocking = 0) | Semaphore *blocking = 0) |
throw(IPCException); | throw(IPCException); |
|
|
DQueue<Thread> _running; | DQueue<Thread> _running; |
DQueue<Thread> _dead; | DQueue<Thread> _dead; |
AtomicInt _dying; | AtomicInt _dying; |
|
|
static void _sleep_sem_del(void *p); | static void _sleep_sem_del(void *p); |
| |
void _check_deadlock(struct timeval *start) throw(Deadlock); | void _check_deadlock(struct timeval *start) throw(Deadlock); |