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

  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 mike  1.1.2.2 #include <cstring>
 40 mike  1.1.2.1 #include <Pegasus/Common/Config.h>
 41               #include <Pegasus/Common/Linkage.h>
 42               
 43               #if defined(PEGASUS_HAVE_PTHREADS)
 44               # if defined(PEGASUS_PLATFORM_OS400_ISERIES_IBM)
 45               # define _MULTI_THREADED // Is this really necessary?
 46               # endif
 47               # include <pthread.h>
 48               # include <errno.h>
 49               # include <sys/time.h>
 50               #elif defined(PEGASUS_HAVE_WINDOWS_THREADS)
 51               # include <windows.h>
 52               #else
 53               # error "<Pegasus/Common/Threads.h>: not implemented"
 54               #endif
 55               
 56               PEGASUS_NAMESPACE_BEGIN
 57               
 58               //==============================================================================
 59               //
 60               // PEGASUS_THREAD_CDECL
 61 mike  1.1.2.1 //
 62               //==============================================================================
 63               
 64               #if defined(PEGASUS_PLATFORM_WIN32_IX86_MSVC)
 65               # define PEGASUS_THREAD_CDECL __stdcall
 66               #else
 67               # define PEGASUS_THREAD_CDECL /* empty */
 68               #endif
 69               
 70               //==============================================================================
 71               //
 72 mike  1.1.2.2 // Thread-related type definitions
 73 mike  1.1.2.1 //
 74               //==============================================================================
 75               
 76 mike  1.1.2.2 #if defined(PEGASUS_HAVE_PTHREADS)
 77               
 78               struct ThreadType
 79 mike  1.1.2.1 {
 80 mike  1.1.2.2 public:
 81 mike  1.1.2.1 
 82 mike  1.1.2.2     ThreadType()
 83                   {
 84                       clear();
 85                   }
 86               
 87                   ThreadType(const ThreadType& x) : _thread(x._thread), _id(x._id) 
 88                   {
 89                   }
 90               
 91                   ThreadType(pthread_t thread, Uint32 id) : _thread(thread), _id(id) 
 92                   {
 93                   }
 94               
 95                   ThreadType& operator=(const ThreadType& x)
 96                   {
 97                       if (&x != this)
 98                       {
 99                           _id = x._id;
100                           _thread = x._thread;
101                       }
102                       return *this;
103 mike  1.1.2.2     }
104               
105                   void clear()
106                   {
107                       memset(this, 0, sizeof(*this));
108                   }
109               
110                   pthread_t thread() const 
111                   { 
112                       return _thread; 
113                   }
114               
115                   Uint32 id() const 
116                   { 
117                       return _id; 
118                   }
119               
120                   void print() const
121                   {
122                       printf("ThreadType(%lu, %lu)\n", _thread, (unsigned long)_id);
123                   }
124 mike  1.1.2.1 
125 mike  1.1.2.2 private:
126                   pthread_t _thread;
127               
128                   // And id of zero indicates a null object. 1 indicates the main thread.
129                   Uint32 _id;
130               };
131 mike  1.1.2.1 
132               typedef void* ThreadReturnType;
133               #endif
134               
135               #if defined(PEGASUS_HAVE_WINDOWS_THREADS)
136               typedef HANDLE ThreadType;
137               typedef unsigned ThreadReturnType;
138               #endif
139               
140               //==============================================================================
141               //
142               // ThreadHandle
143               //
144               //==============================================================================
145               
146               #if defined(PEGASUS_HAVE_PTHREADS)
147               struct ThreadHandle
148               {
149                   ThreadType thid;
150               };
151               #elif defined(PEGASUS_HAVE_WINDOWS_THREADS)
152 mike  1.1.2.1 struct ThreadHandle
153               {
154                   ThreadType thid;
155               };
156               #endif
157               
158               //==============================================================================
159               //
160               // Threads
161               //
162               //==============================================================================
163               
164               class PEGASUS_COMMON_LINKAGE Threads
165               {
166               public:
167               
168 mike  1.1.2.2     enum Type { DETACHED, JOINABLE };
169               
170                   static int create(
171                       ThreadType& thread, 
172                       Type type,
173                       void* (*start)(void*), 
174                       void* arg);
175               
176 mike  1.1.2.1     static ThreadType self();
177               
178                   static bool equal(ThreadType x, ThreadType y);
179               
180                   static void exit(ThreadReturnType rc);
181               
182                   static void cancel(ThreadType th, ThreadReturnType rc);
183               
184                   static void yield();
185               
186                   static void sleep(int msec);
187               
188 mike  1.1.2.2     static void cleanup_push(void (*start)(void*), void* arg);
189 mike  1.1.2.1 
190                   static void cleanup_pop(int execute);
191               };
192               
193               //==============================================================================
194               //
195               // POSIX Threads Implementation
196               //
197               //==============================================================================
198               
199               #if defined(PEGASUS_HAVE_PTHREADS)
200               
201               inline bool Threads::equal(ThreadType x, ThreadType y) 
202               { 
203 mike  1.1.2.2     return pthread_equal(x.thread(), y.thread());
204 mike  1.1.2.1 }
205               
206               inline void Threads::exit(ThreadReturnType rc)
207               {
208                   pthread_exit(rc);
209               }
210               
211               inline void Threads::cancel(ThreadType th, ThreadReturnType rc)
212               {
213 mike  1.1.2.2     pthread_cancel(th.thread());
214 mike  1.1.2.1 }
215               
216               inline void Threads::yield()
217               {
218               #if defined(PEGASUS_PLATFORM_AIX_RS_IBMCXX) || \
219                   defined(PEGASUS_PLATFORM_HPUX_ACC) || \
220                   defined(PEGASUS_PLATFORM_OS400_ISERIES_IBM) || \
221                   defined(PEGASUS_PLATFORM_TRU64_ALPHA_DECCXX) || \
222                   defined(PEGASUS_OS_VMS)
223                   sched_yield();
224               #else
225                   pthread_yield();
226               #endif
227               }
228               
229               inline void Threads::cleanup_push(void (*func)(void*), void* arg)
230               {
231                   // ATTN: it is doubtful whether cleanup handlers ever really worked.
232                   //       They are only used in two places and not used in many other
233                   //       places where mutexes are obtained. Further, they are only
234                   //       implemented correctly on one or two platforms. For now, we
235 mike  1.1.2.1     //       will defer their implementation until we can find a way to
236                   //       implement them on all platforms (using thread local storage).
237               }
238               
239               inline void Threads::cleanup_pop(int execute)
240               {
241                   // ATTN: not implemented.
242               }
243               
244               #endif /* defined(PEGASUS_HAVE_PTHREADS) */
245               
246               //==============================================================================
247               //
248               // Windows Threads Implementation
249               //
250               //==============================================================================
251               
252               #if defined(PEGASUS_HAVE_WINDOWS_THREADS)
253               
254               inline ThreadType Threads::self() 
255               {
256 mike  1.1.2.1     return ThreadType(GetCurrentThreadId()); 
257               }
258               
259               inline bool Threads::equal(ThreadType x, ThreadType y) 
260               {
261                   return x == y;
262               }
263               
264               inline void Threads::exit(ThreadReturnType rc)
265               {
266                   _endthreadex(rc);
267               }
268               
269               inline void Threads::cancel(ThreadType th, ThreadReturnType rc)
270               {
271                   TerminateThread(th, rc);
272               }
273               
274               inline void Threads::yield()
275               {
276                   Sleep(0);
277 mike  1.1.2.1 }
278               
279               inline void Threads::cleanup_push(void (*func)(void*), void* arg)
280               {
281                   // ATTN: Not implemented on Windows.
282               }
283               
284               inline void Threads::cleanup_pop(int execute)
285               {
286                   // ATTN: Not implemented on Windows.
287               }
288 mike  1.1.2.2 
289 mike  1.1.2.1 #endif /* defined(PEGASUS_HAVE_WINDOWS_THREADS) */
290               
291               PEGASUS_NAMESPACE_END
292               
293               #endif /* Pegasus_Threads_h */

No CVS admin address has been configured
Powered by
ViewCVS 0.9.2