1 mike 1.1.2.1 //%2006////////////////////////////////////////////////////////////////////////
2 //
3 // Copyright (c) 2000, 2001, 2002 BMC Software; Hewlett-Packard Development
4 // Company, L.P.; IBM Corp.; The Open Group; Tivoli Systems.
5 // Copyright (c) 2003 BMC Software; Hewlett-Packard Development Company, L.P.;
6 // IBM Corp.; EMC Corporation, The Open Group.
7 // Copyright (c) 2004 BMC Software; Hewlett-Packard Development Company, L.P.;
8 // IBM Corp.; EMC Corporation; VERITAS Software Corporation; The Open Group.
9 // Copyright (c) 2005 Hewlett-Packard Development Company, L.P.; IBM Corp.;
10 // EMC Corporation; VERITAS Software Corporation; The Open Group.
11 // Copyright (c) 2006 Hewlett-Packard Development Company, L.P.; IBM Corp.;
12 // EMC Corporation; Symantec Corporation; The Open Group.
13 //
14 // Permission is hereby granted, free of charge, to any person obtaining a copy
15 // of this software and associated documentation files (the "Software"), to
16 // deal in the Software without restriction, including without limitation the
17 // rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
18 // sell copies of the Software, and to permit persons to whom the Software is
19 // furnished to do so, subject to the following conditions:
20 //
21 // THE ABOVE COPYRIGHT NOTICE AND THIS PERMISSION NOTICE SHALL BE INCLUDED IN
22 mike 1.1.2.1 // ALL COPIES OR SUBSTANTIAL PORTIONS OF THE SOFTWARE. THE SOFTWARE IS PROVIDED
23 // "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT
24 // LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
25 // PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
26 // HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
27 // ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
28 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
29 //
30 //==============================================================================
31 //
32 // Author: Mike Brasher (m.brasher@inovadevelopment.com)
33 //
34 //%/////////////////////////////////////////////////////////////////////////////
35
36 #ifndef Pegasus_Threads_h
37 #define Pegasus_Threads_h
38
39 #include <Pegasus/Common/Config.h>
40 #include <Pegasus/Common/Linkage.h>
41
42 #if defined(PEGASUS_HAVE_PTHREADS)
43 mike 1.1.2.1 # if defined(PEGASUS_PLATFORM_OS400_ISERIES_IBM)
44 # define _MULTI_THREADED // Is this really necessary?
45 # endif
46 # include <pthread.h>
47 # include <errno.h>
48 # include <sys/time.h>
49 #elif defined(PEGASUS_HAVE_WINDOWS_THREADS)
50 # include <windows.h>
51 #else
52 # error "<Pegasus/Common/Threads.h>: not implemented"
53 #endif
54
55 PEGASUS_NAMESPACE_BEGIN
56
57 //==============================================================================
58 //
59 // PEGASUS_THREAD_CDECL
60 //
61 //==============================================================================
62
63 #if defined(PEGASUS_PLATFORM_WIN32_IX86_MSVC)
64 mike 1.1.2.1 # define PEGASUS_THREAD_CDECL __stdcall
65 #else
66 # define PEGASUS_THREAD_CDECL /* empty */
67 #endif
68
69 //==============================================================================
70 //
71 // ThreadCleanupType
72 //
73 //==============================================================================
74
75 struct ThreadCleanupBuffer
76 {
77 void (*__routine)(void*);
78 void* __arg;
79 int __canceltype;
80 struct ThreadCleanupBuffer* __prev;
81 };
82
83 #if defined(PEGASUS_PLATFORM_AIX_RS_IBMCXX)
84 typedef void* ThreadCleanupType;
85 mike 1.1.2.1 #elif defined(PEGASUS_PLATFORM_HPUX_ACC)
86 typedef __pthread_cleanup_handler_t ThreadCleanupType;
87 #elif defined(PEGASUS_PLATFORM_OS400_ISERIES_IBM)
88 typedef ThreadCleanupBuffer ThreadCleanupType;
89 #elif defined(PEGASUS_PLATFORM_ZOS_ZSERIES_IBM)
90 typedef ThreadCleanupBuffer ThreadCleanupType;
91 #elif defined(PEGASUS_PLATFORM_SOLARIS_SPARC_GNU)
92 typedef _cleanup_t ThreadCleanupType;
93 #elif defined(PEGASUS_PLATFORM_TRU64_ALPHA_DECCXX)
94 typedef void* ThreadCleanupType;
95 #elif defined(PEGASUS_DARWIN_PPC_GNU)
96 typedef void* ThreadCleanupType;
97 #elif defined(PEGASUS_OS_VMS)
98 typedef void* ThreadCleanupType;
99 #elif defined(PEGASUS_PLATFORM_WIN32_IX86_MSVC)
100 typedef void* ThreadCleanupType;
101 #elif defined(PEGASUS_PLATFORM_LINUX_GENERIC_GNU)
102 typedef struct _pthread_cleanup_buffer ThreadCleanupType;
103 #else
104 # error "<Pegasus/Common/Threads.h>: unsupported platform"
105 #endif
106 mike 1.1.2.1
107 //==============================================================================
108 //
109 // Thread-related type definitions
110 //
111 //==============================================================================
112
113 #if defined(PEGASUS_HAVE_PTHREADS)
114 typedef pthread_t ThreadType;
115 typedef void* ThreadReturnType;
116 #endif
117
118 #if defined(PEGASUS_HAVE_WINDOWS_THREADS)
119 typedef HANDLE ThreadType;
120 typedef unsigned ThreadReturnType;
121 #endif
122
123 //==============================================================================
124 //
125 // ThreadHandle
126 //
127 mike 1.1.2.1 //==============================================================================
128
129 #if defined(PEGASUS_HAVE_PTHREADS)
130 struct ThreadHandle
131 {
132 ThreadType thid;
133 pthread_attr_t thatt;
134 };
135 #elif defined(PEGASUS_HAVE_WINDOWS_THREADS)
136 struct ThreadHandle
137 {
138 ThreadType thid;
139 void * thatt;
140 };
141 #endif
142
143 //==============================================================================
144 //
145 // Threads
146 //
147 //==============================================================================
148 mike 1.1.2.1
149 class PEGASUS_COMMON_LINKAGE Threads
150 {
151 public:
152
153 static ThreadType self();
154
155 static bool equal(ThreadType x, ThreadType y);
156
157 static void exit(ThreadReturnType rc);
158
159 static void cancel(ThreadType th, ThreadReturnType rc);
160
161 static void yield();
162
163 static void sleep(int msec);
164
165 static void cleanup_push(void (*func)(void*), void* arg);
166
167 static void cleanup_pop(int execute);
168 };
169 mike 1.1.2.1
170 //==============================================================================
171 //
172 // POSIX Threads Implementation
173 //
174 //==============================================================================
175
176 #if defined(PEGASUS_HAVE_PTHREADS)
177
178 inline ThreadType Threads::self()
179 {
180 return pthread_self();
181 }
182
183 inline bool Threads::equal(ThreadType x, ThreadType y)
184 {
185 return pthread_equal(x, y);
186 }
187
188 inline void Threads::exit(ThreadReturnType rc)
189 {
190 mike 1.1.2.1 pthread_exit(rc);
191 }
192
193 inline void Threads::cancel(ThreadType th, ThreadReturnType rc)
194 {
195 pthread_cancel(th);
196 }
197
198 inline void Threads::yield()
199 {
200 #if defined(PEGASUS_PLATFORM_AIX_RS_IBMCXX) || \
201 defined(PEGASUS_PLATFORM_HPUX_ACC) || \
202 defined(PEGASUS_PLATFORM_OS400_ISERIES_IBM) || \
203 defined(PEGASUS_PLATFORM_TRU64_ALPHA_DECCXX) || \
204 defined(PEGASUS_OS_VMS)
205 sched_yield();
206 #else
207 pthread_yield();
208 #endif
209 }
210
211 mike 1.1.2.1 inline void Threads::cleanup_push(void (*func)(void*), void* arg)
212 {
213 // ATTN: it is doubtful whether cleanup handlers ever really worked.
214 // They are only used in two places and not used in many other
215 // places where mutexes are obtained. Further, they are only
216 // implemented correctly on one or two platforms. For now, we
217 // will defer their implementation until we can find a way to
218 // implement them on all platforms (using thread local storage).
219 }
220
221 inline void Threads::cleanup_pop(int execute)
222 {
223 // ATTN: not implemented.
224 }
225
226 #endif /* defined(PEGASUS_HAVE_PTHREADS) */
227
228 //==============================================================================
229 //
230 // Windows Threads Implementation
231 //
232 mike 1.1.2.1 //==============================================================================
233
234 #if defined(PEGASUS_HAVE_WINDOWS_THREADS)
235
236 inline ThreadType Threads::self()
237 {
238 return ThreadType(GetCurrentThreadId());
239 }
240
241 inline bool Threads::equal(ThreadType x, ThreadType y)
242 {
243 return x == y;
244 }
245
246 inline void Threads::exit(ThreadReturnType rc)
247 {
248 _endthreadex(rc);
249 }
250
251 inline void Threads::cancel(ThreadType th, ThreadReturnType rc)
252 {
253 mike 1.1.2.1 TerminateThread(th, rc);
254 }
255
256 inline void Threads::yield()
257 {
258 Sleep(0);
259 }
260
261 inline void Threads::cleanup_push(void (*func)(void*), void* arg)
262 {
263 // ATTN: Not implemented on Windows.
264 }
265
266 inline void Threads::cleanup_pop(int execute)
267 {
268 // ATTN: Not implemented on Windows.
269 }
270 #endif /* defined(PEGASUS_HAVE_WINDOWS_THREADS) */
271
272 PEGASUS_NAMESPACE_END
273
274 mike 1.1.2.1 #endif /* Pegasus_Threads_h */
|