(file) Return to Threads.h CVS log (file) (dir) Up to [Pegasus] / pegasus / src / Pegasus / Common

  1 mike  1.2 //%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.2 // 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           //%/////////////////////////////////////////////////////////////////////////////
 33           
 34           #ifndef Pegasus_Threads_h
 35           #define Pegasus_Threads_h
 36           
 37           #include <cstring>
 38 mike  1.5 #include <cstdio>
 39           #include <cstring>
 40 mike  1.2 #include <Pegasus/Common/Config.h>
 41           #include <Pegasus/Common/Linkage.h>
 42           
 43           // ATTN: can we consolidate these someplace?
 44           
 45           #if defined(PEGASUS_HAVE_PTHREADS)
 46           # if defined(PEGASUS_PLATFORM_OS400_ISERIES_IBM)
 47           # define _MULTI_THREADED // Is this really necessary?
 48           # endif
 49           # include <pthread.h>
 50           # include <errno.h>
 51           # include <sys/time.h>
 52           #elif defined(PEGASUS_HAVE_WINDOWS_THREADS)
 53           # include <windows.h>
 54           # include <process.h>
 55           #else
 56           # error "<Pegasus/Common/Threads.h>: not implemented"
 57           #endif
 58           
 59           PEGASUS_NAMESPACE_BEGIN
 60           
 61 mike  1.2 //==============================================================================
 62           //
 63           // PEGASUS_THREAD_CDECL
 64           //
 65           //==============================================================================
 66           
 67 a.dunfey 1.12 #if defined(PEGASUS_OS_TYPE_WINDOWS)
 68 mike     1.2  # define PEGASUS_THREAD_CDECL __stdcall
 69               #else
 70               # define PEGASUS_THREAD_CDECL /* empty */
 71               #endif
 72               
 73               //==============================================================================
 74               //
 75 mike     1.4  // ThreadId
 76               //
 77               //==============================================================================
 78               
 79               struct ThreadId
 80               {
 81 kumpf    1.10     // The character representation of a uint64 requires 22 bytes including the
 82 mike     1.4      // null terminator.
 83                   char buffer[22];
 84               };
 85               
 86               //==============================================================================
 87               //
 88 mike     1.2  // ThreadType
 89               //
 90               //==============================================================================
 91               
 92               #if defined(PEGASUS_HAVE_PTHREADS)
 93               struct ThreadType
 94               {
 95                   ThreadType()
 96                   {
 97 mike     1.3          memset(&thread, 0, sizeof(thread));
 98 mike     1.2      }
 99               
100 mike     1.3      ThreadType(pthread_t thread_) : thread(thread_)
101 mike     1.2      {
102                   }
103               
104 mike     1.3      pthread_t thread;
105               };
106 mike     1.2  #endif /* PEGASUS_HAVE_PTHREADS */
107               
108               #if defined(PEGASUS_HAVE_WINDOWS_THREADS)
109               struct ThreadType
110               {
111 kumpf    1.10     ThreadType() : handle(NULL)
112                   {
113 mike     1.3      }
114               
115 mike     1.2      HANDLE handle;
116               };
117               #endif /* PEGASUS_HAVE_WINDOWS_THREADS */
118               
119               //==============================================================================
120               //
121               // ThreadReturnType
122               //
123               //==============================================================================
124               
125               #if defined(PEGASUS_HAVE_PTHREADS)
126               typedef void* ThreadReturnType;
127               #endif
128               
129               #if defined(PEGASUS_HAVE_WINDOWS_THREADS)
130               typedef unsigned ThreadReturnType;
131               #endif
132               
133               //==============================================================================
134               //
135               // ThreadHandle
136 mike     1.2  //
137               //==============================================================================
138               
139               #if defined(PEGASUS_HAVE_PTHREADS)
140               struct ThreadHandle
141               {
142                   ThreadType thid;
143               };
144               #elif defined(PEGASUS_HAVE_WINDOWS_THREADS)
145               struct ThreadHandle
146               {
147                   ThreadType thid;
148               };
149               #endif
150               
151               //==============================================================================
152               //
153               // Threads
154               //
155               //==============================================================================
156               
157 mike     1.2  class PEGASUS_COMMON_LINKAGE Threads
158               {
159               public:
160               
161                   enum Type { DETACHED, JOINABLE };
162               
163                   static int create(
164 kumpf    1.10         ThreadType& thread,
165 mike     1.2          Type type,
166 kumpf    1.10         void* (*start)(void*),
167 mike     1.2          void* arg);
168               
169                   static ThreadType self();
170               
171                   static bool equal(ThreadType x, ThreadType y);
172               
173                   static void exit(ThreadReturnType rc);
174               
175                   static void cancel(ThreadType th, ThreadReturnType rc);
176               
177                   static void yield();
178               
179                   static void sleep(int msec);
180               
181                   static void cleanup_push(void (*start)(void*), void* arg);
182               
183                   static void cleanup_pop(int execute);
184               
185 mike     1.3      static ThreadId id(const ThreadType& x = Threads::self());
186               
187                   static bool null(const ThreadType& x = Threads::self());
188 mike     1.2  
189                   static void clear(ThreadType& x);
190               };
191               
192               //==============================================================================
193               //
194               // POSIX Threads Implementation
195               //
196               //==============================================================================
197               
198               #if defined(PEGASUS_HAVE_PTHREADS)
199               
200 kumpf    1.10 inline bool Threads::equal(ThreadType x, ThreadType y)
201               {
202 mike     1.3      return pthread_equal(x.thread, y.thread);
203 mike     1.2  }
204               
205               inline void Threads::exit(ThreadReturnType rc)
206               {
207 marek    1.9      // NOTE: pthread_exit exhibits unusual behavior on RHEL 3 U2, as
208                   // documented in Bugzilla 3836.  Where feasible, it may be advantageous
209                   // to avoid using this function.
210 mike     1.2      pthread_exit(rc);
211               }
212               
213               inline void Threads::cancel(ThreadType th, ThreadReturnType rc)
214               {
215 mike     1.3      pthread_cancel(th.thread);
216 mike     1.2  }
217               
218               inline void Threads::yield()
219               {
220               #if defined(PEGASUS_PLATFORM_AIX_RS_IBMCXX) || \
221                   defined(PEGASUS_PLATFORM_HPUX_ACC) || \
222                   defined(PEGASUS_PLATFORM_OS400_ISERIES_IBM) || \
223                   defined(PEGASUS_PLATFORM_TRU64_ALPHA_DECCXX) || \
224                   defined(PEGASUS_OS_VMS) || \
225 karl     1.8      defined(PEGASUS_OS_ZOS) || \
226 mike     1.11     defined(PEGASUS_OS_DARWIN)
227 mike     1.2      sched_yield();
228               #else
229                   pthread_yield();
230               #endif
231               }
232               
233               inline void Threads::cleanup_push(void (*func)(void*), void* arg)
234               {
235                   // ATTN: it is doubtful whether cleanup handlers ever really worked.
236                   //       They are only used in two places and not used in many other
237                   //       places where mutexes are obtained. Further, they are only
238                   //       implemented correctly on one or two platforms. For now, we
239                   //       will defer their implementation until we can find a way to
240                   //       implement them on all platforms (using thread local storage).
241               }
242               
243               inline void Threads::cleanup_pop(int execute)
244               {
245                   // ATTN: not implemented.
246               }
247               
248 mike     1.3  inline ThreadId Threads::id(const ThreadType& x)
249 mike     1.2  {
250 mike     1.3      ThreadId tid;
251               
252               #if defined(PEGASUS_PLATFORM_ZOS_ZSERIES_IBM)
253                   const char* s = x.thread.__;
254 kumpf    1.10     sprintf(tid.buffer, "%X%X%X%X%X%X%X%X",
255 mike     1.3          s[0], s[1], s[2], s[3], s[4], s[5], s[6], s[7]);
256               #else
257 kumpf    1.10     sprintf(tid.buffer, "%" PEGASUS_64BIT_CONVERSION_WIDTH "u",
258 mike     1.6          Uint64(x.thread));
259 mike     1.3  #endif
260               
261                   return tid;
262               }
263               
264               inline bool Threads::null(const ThreadType& x)
265               {
266               #if defined(PEGASUS_PLATFORM_ZOS_ZSERIES_IBM)
267                   Uint64 tmp;
268 marek    1.7      memcpy(&tmp, x.thread.__, sizeof(Uint64));
269 mike     1.3      return tmp == 0;
270               #else
271                   return x.thread == 0;
272               #endif
273 mike     1.2  }
274               
275               inline void Threads::clear(ThreadType& x)
276               {
277                   memset(&x, 0, sizeof(x));
278               }
279               
280               #endif /* defined(PEGASUS_HAVE_PTHREADS) */
281               
282               //==============================================================================
283               //
284               // Windows Threads Implementation
285               //
286               //==============================================================================
287               
288               #if defined(PEGASUS_HAVE_WINDOWS_THREADS)
289               
290 kumpf    1.10 inline ThreadType Threads::self()
291 mike     1.2  {
292                   ThreadType tt;
293 mike     1.5      tt.handle = GetCurrentThread();
294 mike     1.2      return tt;
295               }
296               
297 kumpf    1.10 inline bool Threads::equal(ThreadType x, ThreadType y)
298 mike     1.2  {
299                   return x.handle == y.handle;
300               }
301               
302               inline void Threads::exit(ThreadReturnType rc)
303               {
304                   _endthreadex(rc);
305               }
306               
307               inline void Threads::cancel(ThreadType th, ThreadReturnType rc)
308               {
309                   TerminateThread(th.handle, rc);
310               }
311               
312               inline void Threads::yield()
313               {
314                   Sleep(0);
315               }
316               
317               inline void Threads::cleanup_push(void (*func)(void*), void* arg)
318               {
319 mike     1.2      // ATTN: Not implemented on Windows.
320               }
321               
322               inline void Threads::cleanup_pop(int execute)
323               {
324                   // ATTN: Not implemented on Windows.
325               }
326               
327 mike     1.3  inline ThreadId Threads::id(const ThreadType& x)
328               {
329                   ThreadId tmp;
330               
331 kumpf    1.10     sprintf(tmp.buffer, "%" PEGASUS_64BIT_CONVERSION_WIDTH "u",
332 mike     1.5          Uint64(x.handle));
333 mike     1.3  
334                   return tmp;
335               }
336               
337               inline bool Threads::null(const ThreadType& x)
338 mike     1.2  {
339 mike     1.3      return x.handle == NULL;
340 mike     1.2  }
341               
342               inline void Threads::clear(ThreadType& x)
343               {
344                   x.handle = NULL;
345               }
346               
347               #endif /* defined(PEGASUS_HAVE_WINDOWS_THREADS) */
348               
349               PEGASUS_NAMESPACE_END
350               
351               #endif /* Pegasus_Threads_h */

No CVS admin address has been configured
Powered by
ViewCVS 0.9.2