version 1.1.2.1, 2006/07/27 23:11:52
|
version 1.4, 2006/08/21 15:31:03
|
|
|
//%///////////////////////////////////////////////////////////////////////////// | //%///////////////////////////////////////////////////////////////////////////// |
| |
#include "Threads.h" | #include "Threads.h" |
|
#include "IDFactory.h" |
|
#include "TSDKey.h" |
|
#include "Once.h" |
|
|
|
#if defined(PEGASUS_PLATFORM_WIN32_IX86_MSVC) |
|
# include <sys/timeb.h> |
|
#endif |
| |
PEGASUS_NAMESPACE_BEGIN | PEGASUS_NAMESPACE_BEGIN |
| |
|
|
| |
#elif defined(PEGASUS_PLATFORM_WIN32_IX86_MSVC) | #elif defined(PEGASUS_PLATFORM_WIN32_IX86_MSVC) |
| |
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 |
{ | { |
|
|
| |
#elif defined(PEGASUS_PLATFORM_ZOS_ZSERIES_IBM) | #elif defined(PEGASUS_PLATFORM_ZOS_ZSERIES_IBM) |
int seconds; | int seconds; |
int microsecs = msec * 1000; |
if (msec < 1000) |
|
|
if (microsecs < 1000000) |
|
{ | { |
usleep(microsecs/2); |
usleep(msec*1000); |
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) | #elif defined(PEGASUS_OS_VMS) |
| |
sleep(msec / 1000); |
::sleep(msec / 1000); |
| |
#endif | #endif |
} | } |
| |
|
//============================================================================== |
|
// |
|
// Thread id TSD: |
|
// |
|
//============================================================================== |
|
|
|
static Once _once = PEGASUS_ONCE_INITIALIZER; |
|
static TSDKeyType _key; |
|
|
|
static void _create_key() |
|
{ |
|
TSDKey::create(&_key); |
|
} |
|
|
|
static inline void _set_id_tsd(Uint32 id) |
|
{ |
|
once(&_once, _create_key); |
|
TSDKey::set_thread_specific(_key, (void*)(long)id); |
|
} |
|
|
|
static inline Uint32 _get_id_tsd() |
|
{ |
|
once(&_once, _create_key); |
|
void* ptr = TSDKey::get_thread_specific(_key); |
|
|
|
if (!ptr) |
|
{ |
|
// Main thread's id is 1! |
|
return 1; |
|
} |
|
|
|
return (Uint32)(long)ptr; |
|
} |
|
|
|
//============================================================================== |
|
// |
|
// _get_stack_multiplier() |
|
// |
|
//============================================================================== |
|
|
|
static inline int _get_stack_multiplier() |
|
{ |
|
#if defined(PEGASUS_OS_VMS) |
|
|
|
static int _multiplier = 0; |
|
static MutexType _multiplier_mutex = PEGASUS_MUTEX_INITIALIZER; |
|
|
|
// |
|
// 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. |
|
// |
|
|
|
if (_multiplier == 0) |
|
{ |
|
mutex_lock(&_multiplier_mutex); |
|
|
|
if (_multiplier == 0) |
|
{ |
|
const char *env = getenv("PEGASUS_VMS_THREAD_STACK_MULTIPLIER"); |
|
|
|
if (env) |
|
_multiplier = atoi(env); |
|
|
|
if (_multiplier == 0) |
|
_multiplier = 2; |
|
} |
|
|
|
mutex_unlock(&_multiplier_mutex); |
|
} |
|
|
|
return _multiplier; |
|
#else |
|
return 2; |
|
#endif |
|
} |
|
|
|
//============================================================================== |
|
// |
|
// PEGASUS_HAVE_PTHREADS |
|
// |
|
//============================================================================== |
|
|
|
#if defined(PEGASUS_HAVE_PTHREADS) |
|
|
|
int Threads::create( |
|
ThreadType& thread, |
|
Type type, |
|
void* (*start)(void*), |
|
void* arg) |
|
{ |
|
// Initialize thread attributes: |
|
|
|
pthread_attr_t attr; |
|
pthread_attr_init(&attr); |
|
|
|
// Detached: |
|
|
|
if (type == DETACHED) |
|
{ |
|
#if defined(PEGASUS_PLATFORM_ZOS_ZSERIES_IBM) |
|
int ds = 1; |
|
pthread_attr_setdetachstate(&attr, &ds); |
|
#else |
|
pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED); |
|
#endif |
|
} |
|
|
|
// Stack size: |
|
|
|
#if defined(PEGASUS_PLATFORM_HPUX_PARISC_ACC) || defined(PEGASUS_OS_VMS) |
|
{ |
|
size_t stacksize; |
|
|
|
if (pthread_attr_getstacksize(&attr, &stacksize) == 0) |
|
{ |
|
int m = _get_stack_multiplier(); |
|
int rc = pthread_attr_setstacksize(&attr, stacksize * m); |
|
PEGASUS_ASSERT(rc == 0); |
|
} |
|
} |
|
#endif |
|
|
|
// Scheduling policy: |
|
|
|
#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); |
|
# endif |
|
#endif // PEGASUS_PLATFORM_SOLARIS_SPARC_GNU |
|
|
|
// Create thread: |
|
|
|
int rc = pthread_create(&thread.thread, &attr, start, arg); |
|
|
|
if (rc != 0) |
|
{ |
|
thread = ThreadType(); |
|
return rc; |
|
} |
|
|
|
// Destroy attributes now. |
|
|
|
pthread_attr_destroy(&attr); |
|
|
|
// Return: |
|
|
|
return 0; |
|
} |
|
|
|
ThreadType Threads::self() |
|
{ |
|
ThreadType tt; |
|
tt.thread = pthread_self(); |
|
return tt; |
|
} |
|
|
|
#endif /* PEGASUS_HAVE_PTHREADS */ |
|
|
PEGASUS_NAMESPACE_END | PEGASUS_NAMESPACE_END |