version 1.100.2.6, 2008/10/23 18:59:32
|
version 1.113, 2013/04/25 13:00:21
|
|
|
//%2006//////////////////////////////////////////////////////////////////////// |
//%LICENSE//////////////////////////////////////////////////////////////// |
// | // |
// Copyright (c) 2000, 2001, 2002 BMC Software; Hewlett-Packard Development |
// Licensed to The Open Group (TOG) under one or more contributor license |
// Company, L.P.; IBM Corp.; The Open Group; Tivoli Systems. |
// agreements. Refer to the OpenPegasusNOTICE.txt file distributed with |
// Copyright (c) 2003 BMC Software; Hewlett-Packard Development Company, L.P.; |
// this work for additional information regarding copyright ownership. |
// IBM Corp.; EMC Corporation, The Open Group. |
// Each contributor licenses this file to you under the OpenPegasus Open |
// Copyright (c) 2004 BMC Software; Hewlett-Packard Development Company, L.P.; |
// Source License; you may not use this file except in compliance with the |
// IBM Corp.; EMC Corporation; VERITAS Software Corporation; The Open Group. |
// License. |
// Copyright (c) 2005 Hewlett-Packard Development Company, L.P.; IBM Corp.; |
// |
// EMC Corporation; VERITAS Software Corporation; The Open Group. |
// Permission is hereby granted, free of charge, to any person obtaining a |
// Copyright (c) 2006 Hewlett-Packard Development Company, L.P.; IBM Corp.; |
// copy of this software and associated documentation files (the "Software"), |
// EMC Corporation; Symantec Corporation; The Open Group. |
// to deal in the Software without restriction, including without limitation |
// |
// the rights to use, copy, modify, merge, publish, distribute, sublicense, |
// Permission is hereby granted, free of charge, to any person obtaining a copy |
// and/or sell copies of the Software, and to permit persons to whom the |
// of this software and associated documentation files (the "Software"), to |
// Software is furnished to do so, subject to the following conditions: |
// deal in the Software without restriction, including without limitation the |
// |
// rights to use, copy, modify, merge, publish, distribute, sublicense, and/or |
// The above copyright notice and this permission notice shall be included |
// sell copies of the Software, and to permit persons to whom the Software is |
// in all copies or substantial portions of the Software. |
// furnished to do so, subject to the following conditions: |
// |
// |
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS |
// THE ABOVE COPYRIGHT NOTICE AND THIS PERMISSION NOTICE SHALL BE INCLUDED IN |
// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF |
// ALL COPIES OR SUBSTANTIAL PORTIONS OF THE SOFTWARE. THE SOFTWARE IS PROVIDED |
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. |
// "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT |
// IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY |
// LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR |
// CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, |
// PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT |
// TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE |
// HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN |
// SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
// ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION |
|
// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
|
// | // |
//============================================================================== |
////////////////////////////////////////////////////////////////////////// |
// | // |
//%///////////////////////////////////////////////////////////////////////////// | //%///////////////////////////////////////////////////////////////////////////// |
| |
|
|
#include <errno.h> | #include <errno.h> |
#include <exception> | #include <exception> |
#include <Pegasus/Common/Tracer.h> | #include <Pegasus/Common/Tracer.h> |
|
#include <Pegasus/Common/AutoPtr.h> |
#include "Time.h" | #include "Time.h" |
| |
PEGASUS_USING_STD; | PEGASUS_USING_STD; |
|
|
| |
void Thread::cancel() | void Thread::cancel() |
{ | { |
_cancelled = true; |
|
pthread_cancel(_handle.thid.thread); | pthread_cancel(_handle.thid.thread); |
} | } |
| |
void Thread::test_cancel() |
|
{ |
|
#if defined(PEGASUS_PLATFORM_ZOS_ZSERIES_IBM) |
|
pthread_testintr(); |
|
#else |
|
pthread_testcancel(); |
|
#endif |
|
} |
|
|
|
Boolean Thread::is_cancelled() |
|
{ |
|
return _cancelled; |
|
} |
|
|
|
void Thread::thread_switch() | void Thread::thread_switch() |
{ | { |
#if defined(PEGASUS_PLATFORM_ZOS_ZSERIES_IBM) |
#if defined(PEGASUS_OS_ZOS) |
pthread_yield(NULL); | pthread_yield(NULL); |
#else | #else |
sched_yield(); | sched_yield(); |
#endif | #endif |
} | } |
| |
/* |
|
ATTN: why are these missing on other platforms? |
|
*/ |
|
#if defined(PEGASUS_PLATFORM_LINUX_GENERIC_GNU) |
|
void Thread::suspend() |
|
{ |
|
pthread_kill(_handle.thid.thread, SIGSTOP); |
|
} |
|
|
|
void Thread::resume() |
|
{ |
|
pthread_kill(_handle.thid.thread, SIGCONT); |
|
} |
|
#endif |
|
|
|
void Thread::sleep(Uint32 msec) | void Thread::sleep(Uint32 msec) |
{ | { |
Threads::sleep(msec); | Threads::sleep(msec); |
|
|
Threads::clear(_handle.thid); | Threads::clear(_handle.thid); |
} | } |
| |
void Thread::thread_init() |
|
{ |
|
#if defined(PEGASUS_PLATFORM_ZOS_ZSERIES_IBM) |
|
pthread_setintr(PTHREAD_INTR_ENABLE); |
|
pthread_setintrtype(PTHREAD_INTR_ASYNCHRONOUS); |
|
#else |
|
pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, NULL); |
|
pthread_setcanceltype(PTHREAD_CANCEL_DEFERRED, NULL); |
|
#endif |
|
_cancel_enabled = true; |
|
} |
|
|
|
void Thread::detach() | void Thread::detach() |
{ | { |
_is_detached = true; | _is_detached = true; |
#if defined(PEGASUS_PLATFORM_ZOS_ZSERIES_IBM) |
#if defined(PEGASUS_OS_ZOS) |
pthread_t thread_id=_handle.thid.thread; | pthread_t thread_id=_handle.thid.thread; |
pthread_detach(&thread_id); | pthread_detach(&thread_id); |
#else | #else |
|
|
arg->start = _start; | arg->start = _start; |
arg->arg = this; | arg->arg = this; |
| |
Threads::Type type = |
Threads::Type type = _is_detached ? Threads::DETACHED : Threads::JOINABLE; |
_is_detached ? Threads::THREADS_DETACHED : Threads::THREADS_JOINABLE; |
|
int rc = Threads::create(_handle.thid, type, _start_wrapper, arg); | int rc = Threads::create(_handle.thid, type, _start_wrapper, arg); |
| |
// On Linux distributions released prior 2005, the implementation of | // On Linux distributions released prior 2005, the implementation of |
|
|
return PEGASUS_THREAD_OK; | return PEGASUS_THREAD_OK; |
} | } |
| |
static sigset_t *block_signal_mask(sigset_t * sig) |
|
{ |
|
sigemptyset(sig); |
|
// should not be used for main() |
|
sigaddset(sig, SIGHUP); |
|
sigaddset(sig, SIGINT); |
|
// maybe useless, since KILL can't be blocked according to POSIX |
|
sigaddset(sig, SIGKILL); |
|
|
|
sigaddset(sig, SIGABRT); |
|
sigaddset(sig, SIGALRM); |
|
sigaddset(sig, SIGPIPE); |
|
|
|
|
|
// Note: older versions of the linux pthreads library use SIGUSR1 and SIGUSR2 |
|
// internally to stop and start threads that are blocking, the newer ones |
|
// implement this through the kernel's real time signals |
|
// since SIGSTOP/CONT can handle suspend()/resume() on Linux |
|
// block them |
|
// #if defined(PEGASUS_PLATFORM_LINUX_IX86_GNU) |
|
// sigaddset(sig, SIGUSR1); |
|
// sigaddset(sig, SIGUSR2); |
|
// #endif |
|
#if defined (PEGASUS_PLATFORM_ZOS_ZSERIES_IBM) || defined (PEGASUS_OS_VMS) |
|
sigprocmask(SIG_BLOCK, sig, NULL); |
|
#else |
|
pthread_sigmask(SIG_BLOCK, sig, NULL); |
|
#endif |
|
return sig; |
|
} |
|
|
|
/* |
|
ATTN: remove this! |
|
*/ |
|
#if 1 |
|
static Uint32 _num_threads = 0; |
|
static Mutex _num_threads_mutex; |
|
#endif |
|
|
|
Thread::Thread( | Thread::Thread( |
ThreadReturnType(PEGASUS_THREAD_CDECL* start) (void*), | ThreadReturnType(PEGASUS_THREAD_CDECL* start) (void*), |
void* parameter, | void* parameter, |
Boolean detached) | Boolean detached) |
: _is_detached(detached), | : _is_detached(detached), |
_cancel_enabled(true), |
|
_cancelled(false), |
|
_start(start), | _start(start), |
_cleanup(), | _cleanup(), |
_tsd(), |
|
_thread_parm(parameter), | _thread_parm(parameter), |
_exit_code(0) | _exit_code(0) |
{ | { |
/* |
|
ATTN: remove this! |
|
*/ |
|
#if 1 |
|
Uint32 num_threads; |
|
_num_threads_mutex.lock(); |
|
_num_threads++; |
|
num_threads = _num_threads; |
|
printf("Thread::Thread(): num_threads=%u\n", num_threads); |
|
_num_threads_mutex.unlock(); |
|
#endif |
|
|
|
Threads::clear(_handle.thid); | Threads::clear(_handle.thid); |
|
memset(_tsd, 0, sizeof(_tsd)); |
} | } |
| |
Thread::~Thread() | Thread::~Thread() |
{ | { |
/* |
|
ATTN: remove this! |
|
*/ |
|
#if 1 |
|
Uint32 num_threads; |
|
_num_threads_mutex.lock(); |
|
_num_threads--; |
|
num_threads = _num_threads; |
|
printf("Thread::~Thread(): num_threads=%u\n", num_threads); |
|
_num_threads_mutex.unlock(); |
|
#endif |
|
|
|
try | try |
{ | { |
join(); | join(); |
|
|
_cancelled = true; | _cancelled = true; |
} | } |
| |
void Thread::test_cancel() |
|
{ |
|
if (_cancel_enabled && _cancelled) |
|
{ |
|
exit_self(0); |
|
} |
|
} |
|
|
|
Boolean Thread::is_cancelled() |
|
{ |
|
return _cancelled; |
|
} |
|
|
|
void Thread::thread_switch() | void Thread::thread_switch() |
{ | { |
Sleep(0); | Sleep(0); |
|
|
} | } |
} | } |
| |
void Thread::thread_init() |
|
{ |
|
_cancel_enabled = true; |
|
} |
|
|
|
void Thread::detach() | void Thread::detach() |
{ | { |
_is_detached = true; | _is_detached = true; |
} | } |
| |
Thread::Thread(ThreadReturnType(PEGASUS_THREAD_CDECL * start) (void *), |
Thread::Thread( |
|
ThreadReturnType(PEGASUS_THREAD_CDECL* start)(void*), |
void *parameter, | void *parameter, |
Boolean detached):_is_detached(detached), | Boolean detached):_is_detached(detached), |
_cancel_enabled(true), |
|
_cancelled(false), | _cancelled(false), |
_start(start), _cleanup(), _tsd(), _thread_parm(parameter), _exit_code(0) |
_start(start), |
|
_cleanup(), |
|
_thread_parm(parameter), |
|
_exit_code(0) |
{ | { |
Threads::clear(_handle.thid); | Threads::clear(_handle.thid); |
|
memset(_tsd, 0, sizeof(_tsd)); |
} | } |
| |
Thread::~Thread() | Thread::~Thread() |
|
|
// | // |
//============================================================================== | //============================================================================== |
| |
void thread_data::default_delete(void *data) |
|
{ |
|
if (data != NULL) |
|
::operator delete(data); |
|
} |
|
|
|
void language_delete(void *data) | void language_delete(void *data) |
{ | { |
if (data != NULL) | if (data != NULL) |
|
|
{ | { |
cu.reset(_cleanup.remove_front()); | cu.reset(_cleanup.remove_front()); |
} | } |
catch (IPCException &) |
catch (...) |
{ | { |
PEGASUS_ASSERT(0); | PEGASUS_ASSERT(0); |
} | } |
if (execute == true) | if (execute == true) |
|
{ |
cu->execute(); | cu->execute(); |
} | } |
|
} |
| |
| |
void Thread::exit_self(ThreadReturnType exit_code) | void Thread::exit_self(ThreadReturnType exit_code) |
|
|
{ | { |
cleanup_pop(true); | cleanup_pop(true); |
} | } |
catch (IPCException &) |
catch (...) |
{ | { |
PEGASUS_ASSERT(0); | PEGASUS_ASSERT(0); |
break; | break; |
|
|
{ | { |
if (Thread::_key_error) | if (Thread::_key_error) |
{ | { |
PEG_TRACE_CSTRING(TRC_THREAD, Tracer::LEVEL4, |
PEG_TRACE_CSTRING(TRC_THREAD, Tracer::LEVEL1, |
"Thread: ERROR - thread key error"); | "Thread: ERROR - thread key error"); |
return -1; | return -1; |
} | } |
|
|
} | } |
else | else |
{ | { |
PEG_TRACE_CSTRING(TRC_THREAD, Tracer::LEVEL4, |
PEG_TRACE_CSTRING(TRC_THREAD, Tracer::LEVEL1, |
"Thread: ERROR - unable to create a thread key"); | "Thread: ERROR - unable to create a thread key"); |
Thread::_key_error = true; | Thread::_key_error = true; |
return -1; | return -1; |
|
|
} | } |
else | else |
{ | { |
PEG_TRACE_CSTRING(TRC_THREAD, Tracer::LEVEL4, |
PEG_TRACE_CSTRING(TRC_THREAD, Tracer::LEVEL1, |
"ERROR: error setting Thread * into thread specific storage"); | "ERROR: error setting Thread * into thread specific storage"); |
} | } |
} | } |
|
|
if (curThrd == NULL) | if (curThrd == NULL) |
return NULL; | return NULL; |
AcceptLanguageList *acceptLangs = | AcceptLanguageList *acceptLangs = |
(AcceptLanguageList *) curThrd->reference_tsd("acceptLanguages"); |
(AcceptLanguageList *) curThrd->reference_tsd(TSD_ACCEPT_LANGUAGES); |
curThrd->dereference_tsd(); | curThrd->dereference_tsd(); |
PEG_METHOD_EXIT(); | PEG_METHOD_EXIT(); |
return acceptLangs; | return acceptLangs; |
} | } |
| |
void Thread::setLanguages(AcceptLanguageList * langs) |
void Thread::setLanguages(const AcceptLanguageList& langs) |
{ | { |
PEG_METHOD_ENTER(TRC_THREAD, "Thread::setLanguages"); | PEG_METHOD_ENTER(TRC_THREAD, "Thread::setLanguages"); |
| |
Thread *currentThrd = Thread::getCurrent(); | Thread *currentThrd = Thread::getCurrent(); |
if (currentThrd != NULL) | if (currentThrd != NULL) |
{ | { |
|
AutoPtr<AcceptLanguageList> langsCopy(new AcceptLanguageList(langs)); |
|
|
// deletes the old tsd and creates a new one | // deletes the old tsd and creates a new one |
currentThrd->put_tsd("acceptLanguages", |
currentThrd->put_tsd( |
|
TSD_ACCEPT_LANGUAGES, |
language_delete, | language_delete, |
sizeof (AcceptLanguageList *), langs); |
sizeof (AcceptLanguageList *), |
|
langsCopy.get()); |
|
|
|
langsCopy.release(); |
} | } |
| |
PEG_METHOD_EXIT(); | PEG_METHOD_EXIT(); |
|
|
if (currentThrd != NULL) | if (currentThrd != NULL) |
{ | { |
// deletes the old tsd | // deletes the old tsd |
currentThrd->delete_tsd("acceptLanguages"); |
currentThrd->delete_tsd(TSD_ACCEPT_LANGUAGES); |
} | } |
| |
PEG_METHOD_EXIT(); | PEG_METHOD_EXIT(); |