version 1.1.2.2, 2006/07/28 17:41:27
|
version 1.18, 2008/12/02 09:00:53
|
|
|
//%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. |
|
// | // |
//============================================================================== |
////////////////////////////////////////////////////////////////////////// |
// |
|
// Author: Mike Brasher (m.brasher@inovadevelopment.com) |
|
// | // |
//%///////////////////////////////////////////////////////////////////////////// | //%///////////////////////////////////////////////////////////////////////////// |
| |
|
#include <errno.h> |
#include "Threads.h" | #include "Threads.h" |
#include "IDFactory.h" | #include "IDFactory.h" |
#include "TSDKey.h" | #include "TSDKey.h" |
|
#include "Once.h" |
|
|
|
#if defined(PEGASUS_OS_TYPE_WINDOWS) |
|
# include <sys/timeb.h> |
|
#endif |
|
#if defined(PEGASUS_OS_ZOS) |
|
# include <unistd.h> |
|
#endif |
|
|
|
#if defined(PEGASUS_OS_SOLARIS) |
|
# include <unistd.h> |
|
#endif |
| |
PEGASUS_NAMESPACE_BEGIN | PEGASUS_NAMESPACE_BEGIN |
| |
|
|
{ | { |
#if defined(PEGASUS_HAVE_NANOSLEEP) | #if defined(PEGASUS_HAVE_NANOSLEEP) |
| |
struct timespec wait; |
struct timespec wait, remwait; |
wait.tv_sec = msec / 1000; | wait.tv_sec = msec / 1000; |
wait.tv_nsec = (msec % 1000) * 1000000; | wait.tv_nsec = (msec % 1000) * 1000000; |
nanosleep(&wait, NULL); |
|
| |
#elif defined(PEGASUS_PLATFORM_OS400_ISERIES_IBM) |
while ((nanosleep(&wait, &remwait) == -1) && (errno == EINTR)) |
|
|
int loop; |
|
int microsecs = msec * 1000; /* convert from milliseconds to microseconds */ |
|
|
|
if (microsecs < 1000000) |
|
usleep(microsecs); |
|
else |
|
{ | { |
loop = microsecs / 1000000; |
wait.tv_sec = remwait.tv_sec; |
for(int i = 0; i < loop; i++) |
wait.tv_nsec = remwait.tv_nsec; |
usleep(1000000); |
|
if ((loop*1000000) < microsecs) |
|
usleep(microsecs - (loop*1000000)); |
|
} | } |
| |
#elif defined(PEGASUS_PLATFORM_WIN32_IX86_MSVC) |
#elif defined(PEGASUS_OS_TYPE_WINDOWS) |
| |
if (ms == 0) |
if (msec == 0) |
{ | { |
Sleep(0); | Sleep(0); |
return; | return; |
|
|
| |
struct _timeb end, now; | struct _timeb end, now; |
_ftime( &end ); | _ftime( &end ); |
end.time += (ms / 1000); |
end.time += (msec / 1000); |
ms -= (ms / 1000); |
msec -= (msec / 1000); |
end.millitm += ms; |
end.millitm += msec; |
| |
do | do |
{ | { |
|
|
} | } |
while( end.millitm > now.millitm && end.time >= now.time); | while( end.millitm > now.millitm && end.time >= now.time); |
| |
#elif defined(PEGASUS_PLATFORM_ZOS_ZSERIES_IBM) |
#else |
int seconds; |
if (msec < 1000) |
int microsecs = msec * 1000; |
{ |
|
usleep(msec*1000); |
if (microsecs < 1000000) |
|
{ |
|
usleep(microsecs/2); |
|
pthread_testintr(); |
|
usleep(microsecs/2); |
|
} | } |
else | else |
{ | { |
// sleep for loop seconds | // sleep for loop seconds |
seconds = microsecs / 1000000; |
::sleep(msec / 1000); |
sleep(seconds); |
// Usleep the remaining micro seconds |
|
usleep( (msec*1000) % 1000000 ); |
// Usleep the remaining time |
|
if ((seconds*1000000) < microsecs) |
|
usleep(microsecs - (seconds*1000000)); |
|
} | } |
#elif defined(PEGASUS_OS_VMS) |
|
|
|
sleep(msec / 1000); |
|
| |
#endif | #endif |
} | } |
| |
//============================================================================== | //============================================================================== |
// | // |
// Thread id TSD: |
// _get_stack_multiplier() |
// | // |
//============================================================================== | //============================================================================== |
| |
static MutexType _mutex = PEGASUS_MUTEX_INITIALIZER; |
static inline int _get_stack_multiplier() |
static int _initialized; |
|
static TSDKeyType _key; |
|
|
|
static void _init_id_tsd() |
|
{ | { |
mutex_lock(&_mutex); |
#if defined(PEGASUS_OS_VMS) |
| |
if (_initialized == 0) |
static int _multiplier = 0; |
{ |
static MutexType _multiplier_mutex = PEGASUS_MUTEX_INITIALIZER; |
TSDKey::create(&_key); |
|
_initialized = 1; |
|
} |
|
| |
mutex_unlock(&_mutex); |
// |
} |
// This code uses a, 'hidden' (non-documented), VMS only, logical |
|
// name (environment variable), PEGASUS_VMS_THREAD_STACK_MULTIPLIER, |
|
// to allow in the field adjustment of the thread stack size. |
|
// |
|
// We only check for the logical name once to not have an |
|
// impact on performance. |
|
// |
|
// Note: This code may have problems in a multithreaded environment |
|
// with the setting of doneOnce to true. |
|
// |
|
// Current code in Cimserver and the clients do some serial thread |
|
// creations first so this is not a problem now. |
|
// |
| |
static inline void _set_id_tsd(Uint32 id) |
if (_multiplier == 0) |
{ | { |
if (_initialized == 0) |
mutex_lock(&_multiplier_mutex); |
_init_id_tsd(); |
|
| |
TSDKey::set_thread_specific(_key, (void*)(long)id); |
if (_multiplier == 0) |
} |
|
|
|
static inline Uint32 _get_id_tsd() |
|
{ | { |
if (_initialized == 0) |
const char *env = getenv("PEGASUS_VMS_THREAD_STACK_MULTIPLIER"); |
_init_id_tsd(); |
|
| |
void* ptr = TSDKey::get_thread_specific(_key); |
if (env) |
|
_multiplier = atoi(env); |
| |
if (!ptr) |
if (_multiplier == 0) |
{ |
_multiplier = 2; |
// Main thread's id is 1! |
|
return 1; |
|
} | } |
| |
return (Uint32)(long)ptr; |
mutex_unlock(&_multiplier_mutex); |
|
} |
|
|
|
return _multiplier; |
|
#elif defined(PEGASUS_PLATFORM_HPUX_PARISC_ACC) |
|
return 2; |
|
#elif defined(PEGASUS_OS_AIX) |
|
return 2; |
|
#else |
|
return 1; |
|
#endif |
} | } |
| |
//============================================================================== | //============================================================================== |
|
|
// | // |
//============================================================================== | //============================================================================== |
| |
static IDFactory _thread_ids(2); /* 1 reserved for main thread */ |
|
|
|
#if defined(PEGASUS_HAVE_PTHREADS) | #if defined(PEGASUS_HAVE_PTHREADS) |
| |
int Threads::create( | int Threads::create( |
|
|
pthread_attr_t attr; | pthread_attr_t attr; |
pthread_attr_init(&attr); | pthread_attr_init(&attr); |
| |
|
// Detached: |
|
|
if (type == DETACHED) | if (type == DETACHED) |
|
{ |
|
#if defined(PEGASUS_OS_ZOS) |
|
int ds = 1; |
|
pthread_attr_setdetachstate(&attr, &ds); |
|
#else |
pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED); | pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED); |
|
#endif |
|
} |
|
|
|
// Stack size: |
|
|
|
int multiplier = _get_stack_multiplier(); |
|
|
|
if (multiplier != 1) |
|
{ |
|
size_t stacksize; |
|
|
|
if (pthread_attr_getstacksize(&attr, &stacksize) == 0) |
|
{ |
|
int rc = pthread_attr_setstacksize(&attr, stacksize * multiplier); |
|
PEGASUS_ASSERT(rc == 0); |
|
} |
|
} |
|
|
|
// Scheduling policy: |
|
|
|
#if defined(PEGASUS_OS_SOLARIS) |
| |
#if defined(PEGASUS_PLATFORM_SOLARIS_SPARC_GNU) || \ |
|
defined(PEGASUS_PLATFORM_SOLARIS_SPARC_CC) |
|
# if defined SUNOS_5_7 |
|
pthread_attr_setschedpolicy(&attr, SCHED_RR); |
|
# else |
|
pthread_attr_setschedpolicy(&attr, SCHED_OTHER); | pthread_attr_setschedpolicy(&attr, SCHED_OTHER); |
# endif |
|
#endif // PEGASUS_PLATFORM_SOLARIS_SPARC_GNU |
#endif /* defined(PEGASUS_OS_SOLARIS) */ |
| |
// Create thread: | // Create thread: |
| |
pthread_t thr; |
int rc = pthread_create(&thread.thread, &attr, start, arg); |
int rc = pthread_create(&thr, &attr, start, arg); |
|
| |
if (rc != 0) | if (rc != 0) |
{ | { |
|
|
return rc; | return rc; |
} | } |
| |
// Assign thread id (and put into thread specific storage). |
// Destroy attributes now. |
| |
Uint32 id = _thread_ids.getID(); |
pthread_attr_destroy(&attr); |
_set_id_tsd(id); |
|
| |
// Return: | // Return: |
| |
thread = ThreadType(thr, id); |
|
return 0; | return 0; |
} | } |
| |
ThreadType Threads::self() | ThreadType Threads::self() |
{ | { |
return ThreadType(pthread_self(), _get_id_tsd()); |
ThreadType tt; |
|
tt.thread = pthread_self(); |
|
return tt; |
} | } |
| |
#endif /* PEGASUS_HAVE_PTHREADS */ | #endif /* PEGASUS_HAVE_PTHREADS */ |