version 1.1.2.1, 2006/07/27 23:11:52
|
version 1.22, 2013/02/13 18:21:14
|
|
|
//%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) |
|
// | // |
//%///////////////////////////////////////////////////////////////////////////// | //%///////////////////////////////////////////////////////////////////////////// |
| |
#ifndef Pegasus_Threads_h | #ifndef Pegasus_Threads_h |
#define Pegasus_Threads_h | #define Pegasus_Threads_h |
| |
|
#include <cstring> |
|
#include <cstdio> |
|
#include <cstring> |
#include <Pegasus/Common/Config.h> | #include <Pegasus/Common/Config.h> |
#include <Pegasus/Common/Linkage.h> | #include <Pegasus/Common/Linkage.h> |
| |
#if defined(PEGASUS_HAVE_PTHREADS) |
// ATTN: can we consolidate these someplace? |
# if defined(PEGASUS_PLATFORM_OS400_ISERIES_IBM) |
|
# define _MULTI_THREADED // Is this really necessary? |
#ifdef PEGASUS_OS_ZOS |
|
# include <sched.h> |
# endif | # endif |
|
#if defined(PEGASUS_HAVE_PTHREADS) |
# include <pthread.h> | # include <pthread.h> |
# include <errno.h> | # include <errno.h> |
# include <sys/time.h> | # include <sys/time.h> |
#elif defined(PEGASUS_HAVE_WINDOWS_THREADS) | #elif defined(PEGASUS_HAVE_WINDOWS_THREADS) |
# include <windows.h> | # include <windows.h> |
|
# include <process.h> |
#else | #else |
# error "<Pegasus/Common/Threads.h>: not implemented" | # error "<Pegasus/Common/Threads.h>: not implemented" |
#endif | #endif |
| |
|
#if defined(PEGASUS_OS_SOLARIS) |
|
# include <string.h> |
|
# include <stdio.h> |
|
#endif |
|
|
PEGASUS_NAMESPACE_BEGIN | PEGASUS_NAMESPACE_BEGIN |
| |
//============================================================================== | //============================================================================== |
|
|
// | // |
//============================================================================== | //============================================================================== |
| |
#if defined(PEGASUS_PLATFORM_WIN32_IX86_MSVC) |
#if defined(PEGASUS_OS_TYPE_WINDOWS) |
# define PEGASUS_THREAD_CDECL __stdcall | # define PEGASUS_THREAD_CDECL __stdcall |
#else | #else |
# define PEGASUS_THREAD_CDECL /* empty */ | # define PEGASUS_THREAD_CDECL /* empty */ |
|
|
| |
//============================================================================== | //============================================================================== |
// | // |
// ThreadCleanupType |
// ThreadId |
// | // |
//============================================================================== | //============================================================================== |
| |
struct ThreadCleanupBuffer |
struct ThreadId |
{ | { |
void (*__routine)(void*); |
// The character representation of a uint64 requires 22 bytes including the |
void* __arg; |
// null terminator. |
int __canceltype; |
char buffer[22]; |
struct ThreadCleanupBuffer* __prev; |
|
}; | }; |
| |
#if defined(PEGASUS_PLATFORM_AIX_RS_IBMCXX) |
//============================================================================== |
typedef void* ThreadCleanupType; |
// |
#elif defined(PEGASUS_PLATFORM_HPUX_ACC) |
// ThreadType |
typedef __pthread_cleanup_handler_t ThreadCleanupType; |
// |
#elif defined(PEGASUS_PLATFORM_OS400_ISERIES_IBM) |
//============================================================================== |
typedef ThreadCleanupBuffer ThreadCleanupType; |
|
#elif defined(PEGASUS_PLATFORM_ZOS_ZSERIES_IBM) |
#if defined(PEGASUS_HAVE_PTHREADS) |
typedef ThreadCleanupBuffer ThreadCleanupType; |
struct ThreadType |
#elif defined(PEGASUS_PLATFORM_SOLARIS_SPARC_GNU) |
{ |
typedef _cleanup_t ThreadCleanupType; |
ThreadType() |
#elif defined(PEGASUS_PLATFORM_TRU64_ALPHA_DECCXX) |
{ |
typedef void* ThreadCleanupType; |
memset(&thread, 0, sizeof(thread)); |
#elif defined(PEGASUS_DARWIN_PPC_GNU) |
} |
typedef void* ThreadCleanupType; |
|
#elif defined(PEGASUS_OS_VMS) |
ThreadType(pthread_t thread_) : thread(thread_) |
typedef void* ThreadCleanupType; |
{ |
#elif defined(PEGASUS_PLATFORM_WIN32_IX86_MSVC) |
} |
typedef void* ThreadCleanupType; |
|
#elif defined(PEGASUS_PLATFORM_LINUX_GENERIC_GNU) |
pthread_t thread; |
typedef struct _pthread_cleanup_buffer ThreadCleanupType; |
}; |
#else |
#endif /* PEGASUS_HAVE_PTHREADS */ |
# error "<Pegasus/Common/Threads.h>: unsupported platform" |
|
#endif |
#if defined(PEGASUS_HAVE_WINDOWS_THREADS) |
|
struct ThreadType |
|
{ |
|
ThreadType() : handle(NULL) |
|
{ |
|
} |
|
|
|
HANDLE handle; |
|
}; |
|
#endif /* PEGASUS_HAVE_WINDOWS_THREADS */ |
| |
//============================================================================== | //============================================================================== |
// | // |
// Thread-related type definitions |
// ThreadReturnType |
// | // |
//============================================================================== | //============================================================================== |
| |
#if defined(PEGASUS_HAVE_PTHREADS) | #if defined(PEGASUS_HAVE_PTHREADS) |
typedef pthread_t ThreadType; |
|
typedef void* ThreadReturnType; | typedef void* ThreadReturnType; |
#endif | #endif |
| |
#if defined(PEGASUS_HAVE_WINDOWS_THREADS) | #if defined(PEGASUS_HAVE_WINDOWS_THREADS) |
typedef HANDLE ThreadType; |
|
typedef unsigned ThreadReturnType; | typedef unsigned ThreadReturnType; |
#endif | #endif |
| |
|
|
struct ThreadHandle | struct ThreadHandle |
{ | { |
ThreadType thid; | ThreadType thid; |
pthread_attr_t thatt; |
|
}; | }; |
#elif defined(PEGASUS_HAVE_WINDOWS_THREADS) | #elif defined(PEGASUS_HAVE_WINDOWS_THREADS) |
struct ThreadHandle | struct ThreadHandle |
{ | { |
ThreadType thid; | ThreadType thid; |
void * thatt; |
|
}; | }; |
#endif | #endif |
| |
|
|
{ | { |
public: | public: |
| |
|
enum Type { DETACHED, JOINABLE }; |
|
|
|
static int create( |
|
ThreadType& thread, |
|
Type type, |
|
void* (*start)(void*), |
|
void* arg); |
|
|
static ThreadType self(); | static ThreadType self(); |
| |
static bool equal(ThreadType x, ThreadType y); | static bool equal(ThreadType x, ThreadType y); |
| |
static void exit(ThreadReturnType rc); | static void exit(ThreadReturnType rc); |
| |
static void cancel(ThreadType th, ThreadReturnType rc); |
|
|
|
static void yield(); | static void yield(); |
| |
static void sleep(int msec); | static void sleep(int msec); |
| |
static void cleanup_push(void (*func)(void*), void* arg); |
static ThreadId id(const ThreadType& x = Threads::self()); |
| |
static void cleanup_pop(int execute); |
static bool null(const ThreadType& x = Threads::self()); |
|
|
|
static void clear(ThreadType& x); |
}; | }; |
| |
//============================================================================== | //============================================================================== |
|
|
| |
#if defined(PEGASUS_HAVE_PTHREADS) | #if defined(PEGASUS_HAVE_PTHREADS) |
| |
inline ThreadType Threads::self() |
|
{ |
|
return pthread_self(); |
|
} |
|
|
|
inline bool Threads::equal(ThreadType x, ThreadType y) | inline bool Threads::equal(ThreadType x, ThreadType y) |
{ | { |
return pthread_equal(x, y); |
return pthread_equal(x.thread, y.thread); |
} | } |
| |
inline void Threads::exit(ThreadReturnType rc) | inline void Threads::exit(ThreadReturnType rc) |
{ | { |
|
// NOTE: pthread_exit exhibits unusual behavior on RHEL 3 U2, as |
|
// documented in Bugzilla 3836. Where feasible, it may be advantageous |
|
// to avoid using this function. |
pthread_exit(rc); | pthread_exit(rc); |
} | } |
| |
inline void Threads::cancel(ThreadType th, ThreadReturnType rc) |
inline void Threads::yield() |
{ | { |
pthread_cancel(th); |
#ifdef PEGASUS_OS_LINUX |
|
pthread_yield(); |
|
#else |
|
sched_yield(); |
|
#endif |
} | } |
| |
inline void Threads::yield() |
inline ThreadId Threads::id(const ThreadType& x) |
{ | { |
#if defined(PEGASUS_PLATFORM_AIX_RS_IBMCXX) || \ |
ThreadId tid = { { 0 } }; |
defined(PEGASUS_PLATFORM_HPUX_ACC) || \ |
|
defined(PEGASUS_PLATFORM_OS400_ISERIES_IBM) || \ |
#if defined(PEGASUS_OS_ZOS) |
defined(PEGASUS_PLATFORM_TRU64_ALPHA_DECCXX) || \ |
const char* s = x.thread.__; |
defined(PEGASUS_OS_VMS) |
sprintf(tid.buffer, "%X%X%X%X%X%X%X%X", |
sched_yield(); |
s[0], s[1], s[2], s[3], s[4], s[5], s[6], s[7]); |
#else | #else |
pthread_yield(); |
sprintf(tid.buffer, "%" PEGASUS_64BIT_CONVERSION_WIDTH "u", |
|
Uint64(x.thread)); |
#endif | #endif |
|
|
|
return tid; |
} | } |
| |
inline void Threads::cleanup_push(void (*func)(void*), void* arg) |
inline bool Threads::null(const ThreadType& x) |
{ | { |
// ATTN: it is doubtful whether cleanup handlers ever really worked. |
#if defined(PEGASUS_OS_ZOS) |
// They are only used in two places and not used in many other |
Uint64 tmp; |
// places where mutexes are obtained. Further, they are only |
memcpy(&tmp, x.thread.__, sizeof(Uint64)); |
// implemented correctly on one or two platforms. For now, we |
return tmp == 0; |
// will defer their implementation until we can find a way to |
#else |
// implement them on all platforms (using thread local storage). |
return x.thread == 0; |
|
#endif |
} | } |
| |
inline void Threads::cleanup_pop(int execute) |
inline void Threads::clear(ThreadType& x) |
{ | { |
// ATTN: not implemented. |
memset(&x, 0, sizeof(x)); |
} | } |
| |
#endif /* defined(PEGASUS_HAVE_PTHREADS) */ | #endif /* defined(PEGASUS_HAVE_PTHREADS) */ |
|
|
| |
inline ThreadType Threads::self() | inline ThreadType Threads::self() |
{ | { |
return ThreadType(GetCurrentThreadId()); |
ThreadType tt; |
|
tt.handle = GetCurrentThread(); |
|
return tt; |
} | } |
| |
inline bool Threads::equal(ThreadType x, ThreadType y) | inline bool Threads::equal(ThreadType x, ThreadType y) |
{ | { |
return x == y; |
return x.handle == y.handle; |
} | } |
| |
inline void Threads::exit(ThreadReturnType rc) | inline void Threads::exit(ThreadReturnType rc) |
|
|
_endthreadex(rc); | _endthreadex(rc); |
} | } |
| |
inline void Threads::cancel(ThreadType th, ThreadReturnType rc) |
inline void Threads::yield() |
{ | { |
TerminateThread(th, rc); |
Sleep(0); |
} | } |
| |
inline void Threads::yield() |
inline ThreadId Threads::id(const ThreadType& x) |
{ | { |
Sleep(0); |
ThreadId tmp; |
|
|
|
sprintf(tmp.buffer, "%" PEGASUS_64BIT_CONVERSION_WIDTH "u", |
|
Uint64(x.handle)); |
|
|
|
return tmp; |
} | } |
| |
inline void Threads::cleanup_push(void (*func)(void*), void* arg) |
inline bool Threads::null(const ThreadType& x) |
{ | { |
// ATTN: Not implemented on Windows. |
return x.handle == NULL; |
} | } |
| |
inline void Threads::cleanup_pop(int execute) |
inline void Threads::clear(ThreadType& x) |
{ | { |
// ATTN: Not implemented on Windows. |
x.handle = NULL; |
} | } |
|
|
#endif /* defined(PEGASUS_HAVE_WINDOWS_THREADS) */ | #endif /* defined(PEGASUS_HAVE_WINDOWS_THREADS) */ |
| |
PEGASUS_NAMESPACE_END | PEGASUS_NAMESPACE_END |