version 1.1.2.2, 2006/07/28 17:41:27
|
version 1.1.2.9, 2006/07/28 21:51:00
|
|
|
#include <Pegasus/Common/Config.h> | #include <Pegasus/Common/Config.h> |
#include <Pegasus/Common/Linkage.h> | #include <Pegasus/Common/Linkage.h> |
| |
|
// ATTN: can we consolidate these someplace? |
|
|
#if defined(PEGASUS_HAVE_PTHREADS) | #if defined(PEGASUS_HAVE_PTHREADS) |
# if defined(PEGASUS_PLATFORM_OS400_ISERIES_IBM) | # if defined(PEGASUS_PLATFORM_OS400_ISERIES_IBM) |
# define _MULTI_THREADED // Is this really necessary? | # define _MULTI_THREADED // Is this really necessary? |
|
|
# 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 |
|
|
| |
//============================================================================== | //============================================================================== |
// | // |
// Thread-related type definitions |
// ThreadType |
// | // |
//============================================================================== | //============================================================================== |
| |
#if defined(PEGASUS_HAVE_PTHREADS) | #if defined(PEGASUS_HAVE_PTHREADS) |
|
|
struct ThreadType | struct ThreadType |
{ | { |
public: | public: |
| |
ThreadType() | ThreadType() |
{ | { |
clear(); |
memset(this, 0, sizeof(*this)); |
} | } |
| |
ThreadType(const ThreadType& x) : _thread(x._thread), _id(x._id) | ThreadType(const ThreadType& x) : _thread(x._thread), _id(x._id) |
|
|
return *this; | return *this; |
} | } |
| |
void clear() |
pthread_t tt_handle() const |
{ |
|
memset(this, 0, sizeof(*this)); |
|
} |
|
|
|
pthread_t thread() const |
|
{ | { |
return _thread; | return _thread; |
} | } |
| |
Uint32 id() const |
|
{ |
|
return _id; |
|
} |
|
|
|
void print() const | void print() const |
{ | { |
printf("ThreadType(%lu, %lu)\n", _thread, (unsigned long)_id); | printf("ThreadType(%lu, %lu)\n", _thread, (unsigned long)_id); |
|
|
| |
private: | private: |
pthread_t _thread; | pthread_t _thread; |
|
// An id of zero indicates a null object. 1 indicates the main thread. |
// And id of zero indicates a null object. 1 indicates the main thread. |
|
Uint32 _id; | Uint32 _id; |
|
friend class Threads; |
}; | }; |
|
#endif /* PEGASUS_HAVE_PTHREADS */ |
|
|
|
#if defined(PEGASUS_HAVE_WINDOWS_THREADS) |
|
struct ThreadType |
|
{ |
|
ThreadType() : handle(NULL) { } |
|
HANDLE handle; |
|
}; |
|
#endif /* PEGASUS_HAVE_WINDOWS_THREADS */ |
|
|
|
//============================================================================== |
|
// |
|
// ThreadReturnType |
|
// |
|
//============================================================================== |
| |
|
#if defined(PEGASUS_HAVE_PTHREADS) |
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 |
| |
|
|
static void cleanup_push(void (*start)(void*), void* arg); | static void cleanup_push(void (*start)(void*), void* arg); |
| |
static void cleanup_pop(int execute); | static void cleanup_pop(int execute); |
|
|
|
static Uint32 id(const ThreadType& x = Threads::self()); |
|
|
|
static void clear(ThreadType& x); |
}; | }; |
| |
//============================================================================== | //============================================================================== |
|
|
| |
inline bool Threads::equal(ThreadType x, ThreadType y) | inline bool Threads::equal(ThreadType x, ThreadType y) |
{ | { |
return pthread_equal(x.thread(), y.thread()); |
return pthread_equal(x.tt_handle(), y.tt_handle()); |
} | } |
| |
inline void Threads::exit(ThreadReturnType rc) | inline void Threads::exit(ThreadReturnType rc) |
|
|
| |
inline void Threads::cancel(ThreadType th, ThreadReturnType rc) | inline void Threads::cancel(ThreadType th, ThreadReturnType rc) |
{ | { |
pthread_cancel(th.thread()); |
pthread_cancel(th.tt_handle()); |
} | } |
| |
inline void Threads::yield() | inline void Threads::yield() |
|
|
// ATTN: not implemented. | // ATTN: not implemented. |
} | } |
| |
|
inline Uint32 Threads::id(const ThreadType& x) |
|
{ |
|
return x._id; |
|
} |
|
|
|
inline void Threads::clear(ThreadType& x) |
|
{ |
|
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 = HANDLE(GetCurrentThreadId()); |
|
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) |
|
|
| |
inline void Threads::cancel(ThreadType th, ThreadReturnType rc) | inline void Threads::cancel(ThreadType th, ThreadReturnType rc) |
{ | { |
TerminateThread(th, rc); |
TerminateThread(th.handle, rc); |
} | } |
| |
inline void Threads::yield() | inline void Threads::yield() |
|
|
// ATTN: Not implemented on Windows. | // ATTN: Not implemented on Windows. |
} | } |
| |
|
inline Uint32 Threads::id(const ThreadType& x) |
|
{ |
|
return (Uint32)(long)x.handle; |
|
} |
|
|
|
inline void Threads::clear(ThreadType& x) |
|
{ |
|
x.handle = NULL; |
|
} |
|
|
#endif /* defined(PEGASUS_HAVE_WINDOWS_THREADS) */ | #endif /* defined(PEGASUS_HAVE_WINDOWS_THREADS) */ |
| |
PEGASUS_NAMESPACE_END | PEGASUS_NAMESPACE_END |