version 1.3, 2006/08/15 17:17:30
|
version 1.15, 2008/10/17 11:07:33
|
|
|
// | // |
//============================================================================== | //============================================================================== |
// | // |
// 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" | #include "Once.h" |
| |
#if defined(PEGASUS_PLATFORM_WIN32_IX86_MSVC) |
#if defined(PEGASUS_OS_TYPE_WINDOWS) |
# include <sys/timeb.h> | # include <sys/timeb.h> |
#endif | #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) |
|
|
|
int loop; |
|
int microsecs = msec * 1000; /* convert from milliseconds to microseconds */ |
|
| |
if (microsecs < 1000000) |
while ((nanosleep(&wait, &remwait) == -1) && (errno == EINTR)) |
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 (msec == 0) | if (msec == 0) |
{ | { |
|
|
} | } |
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) | if (msec < 1000) |
{ | { |
usleep(msec*1000); | usleep(msec*1000); |
|
|
// Usleep the remaining micro seconds | // Usleep the remaining micro seconds |
usleep( (msec*1000) % 1000000 ); | usleep( (msec*1000) % 1000000 ); |
} | } |
#elif defined(PEGASUS_OS_VMS) |
|
|
|
::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() | // _get_stack_multiplier() |
// | // |
//============================================================================== | //============================================================================== |
|
|
} | } |
| |
return _multiplier; | return _multiplier; |
#else |
#elif defined(PEGASUS_PLATFORM_HPUX_PARISC_ACC) |
return 2; | return 2; |
|
#else |
|
return 1; |
#endif | #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( |
|
|
| |
if (type == DETACHED) | if (type == DETACHED) |
{ | { |
#if defined(PEGASUS_PLATFORM_ZOS_ZSERIES_IBM) |
#if defined(PEGASUS_OS_ZOS) |
int ds = 1; | int ds = 1; |
pthread_attr_setdetachstate(&attr, &ds); | pthread_attr_setdetachstate(&attr, &ds); |
#else | #else |
|
|
| |
// Stack size: | // Stack size: |
| |
#if defined(PEGASUS_PLATFORM_HPUX_PARISC_ACC) || defined(PEGASUS_OS_VMS) |
int multiplier = _get_stack_multiplier(); |
|
|
|
if (multiplier != 1) |
{ | { |
size_t stacksize; | size_t stacksize; |
| |
if (pthread_attr_getstacksize(&attr, &stacksize) == 0) | if (pthread_attr_getstacksize(&attr, &stacksize) == 0) |
{ | { |
int m = _get_stack_multiplier(); |
int rc = pthread_attr_setstacksize(&attr, stacksize * multiplier); |
int rc = pthread_attr_setstacksize(&attr, stacksize * m); |
|
PEGASUS_ASSERT(rc == 0); | PEGASUS_ASSERT(rc == 0); |
} | } |
} | } |
#endif |
|
| |
// Scheduling policy: | // Scheduling policy: |
| |
#if defined(PEGASUS_PLATFORM_SOLARIS_SPARC_GNU) || \ |
#if defined(PEGASUS_OS_SOLARIS) |
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). |
|
|
|
Uint32 id = _thread_ids.getID(); |
|
_set_id_tsd(id); |
|
|
|
// Destroy attributes now. | // Destroy attributes now. |
| |
pthread_attr_destroy(&attr); | pthread_attr_destroy(&attr); |
| |
// 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 */ |