(file) Return to Threads.cpp 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           // Author: Mike Brasher (m.brasher@inovadevelopment.com)
 33           //
 34           //%/////////////////////////////////////////////////////////////////////////////
 35           
 36 mike  1.5 #include <errno.h>
 37 mike  1.2 #include "Threads.h"
 38           #include "IDFactory.h"
 39           #include "TSDKey.h"
 40           #include "Once.h"
 41           
 42           #if defined(PEGASUS_PLATFORM_WIN32_IX86_MSVC)
 43           # include <sys/timeb.h>
 44           #endif
 45           
 46           PEGASUS_NAMESPACE_BEGIN
 47           
 48           void Threads::sleep(int msec)
 49           {
 50           #if defined(PEGASUS_HAVE_NANOSLEEP)
 51           
 52               struct timespec wait;
 53               wait.tv_sec = msec / 1000;
 54               wait.tv_nsec = (msec % 1000) * 1000000;
 55               nanosleep(&wait, NULL);
 56           
 57           #elif defined(PEGASUS_PLATFORM_OS400_ISERIES_IBM)
 58 mike  1.2 
 59              int loop;
 60              int microsecs = msec * 1000; /* convert from milliseconds to microseconds */
 61           
 62              if (microsecs < 1000000)
 63                  usleep(microsecs);
 64              else
 65              {
 66                  loop = microsecs / 1000000;
 67                  for(int i = 0; i < loop; i++)
 68                      usleep(1000000);
 69                  if ((loop*1000000) < microsecs)
 70                      usleep(microsecs - (loop*1000000));
 71              }
 72           
 73           #elif defined(PEGASUS_PLATFORM_WIN32_IX86_MSVC)
 74           
 75               if (msec == 0)
 76               {         
 77                   Sleep(0);
 78                   return;
 79 mike  1.2     }
 80           
 81               struct _timeb end, now;
 82               _ftime( &end );
 83               end.time += (msec / 1000);
 84               msec -= (msec / 1000);
 85               end.millitm += msec;
 86           
 87               do
 88               {
 89                   Sleep(0);
 90                   _ftime(&now);
 91               } 
 92               while( end.millitm > now.millitm && end.time >= now.time);
 93           
 94           #elif defined(PEGASUS_PLATFORM_ZOS_ZSERIES_IBM)
 95               int seconds;
 96               if (msec < 1000)
 97               {
 98                   usleep(msec*1000);
 99               }
100 mike  1.2     else
101               {
102                   // sleep for loop seconds
103 mike  1.3         ::sleep(msec / 1000);
104 mike  1.2         // Usleep the remaining micro seconds
105                   usleep( (msec*1000) % 1000000 );
106               }
107           #elif defined(PEGASUS_OS_VMS)
108           
109 mike  1.3     ::sleep(msec / 1000);
110 mike  1.2 
111           #endif
112           }
113           
114           //==============================================================================
115           //
116           // Thread id TSD:
117           //
118           //==============================================================================
119           
120           static Once _once = PEGASUS_ONCE_INITIALIZER;
121           static TSDKeyType _key;
122           
123           static void _create_key()
124           {
125               TSDKey::create(&_key);
126           }
127           
128           static inline void _set_id_tsd(Uint32 id)
129           {
130               once(&_once, _create_key);
131 mike  1.2     TSDKey::set_thread_specific(_key, (void*)(long)id);
132           }
133           
134           static inline Uint32 _get_id_tsd()
135           {
136               once(&_once, _create_key);
137               void* ptr = TSDKey::get_thread_specific(_key);
138           
139               if (!ptr)
140               {
141                   // Main thread's id is 1!
142                   return 1;
143               }
144           
145               return (Uint32)(long)ptr;
146           }
147           
148           //==============================================================================
149           //
150           // _get_stack_multiplier()
151           //
152 mike  1.2 //==============================================================================
153           
154           static inline int _get_stack_multiplier()
155           {
156           #if defined(PEGASUS_OS_VMS)
157           
158               static int _multiplier = 0;
159               static MutexType _multiplier_mutex = PEGASUS_MUTEX_INITIALIZER;
160           
161               // 
162               // This code uses a, 'hidden' (non-documented), VMS only, logical 
163               //  name (environment variable), PEGASUS_VMS_THREAD_STACK_MULTIPLIER,
164               //  to allow in the field adjustment of the thread stack size.
165               //
166               // We only check for the logical name once to not have an
167               //  impact on performance.
168               // 
169               // Note:  This code may have problems in a multithreaded environment
170               //  with the setting of doneOnce to true.
171               // 
172               // Current code in Cimserver and the clients do some serial thread
173 mike  1.2     //  creations first so this is not a problem now.
174               // 
175           
176               if (_multiplier == 0)
177               {
178                   mutex_lock(&_multiplier_mutex);
179           
180                   if (_multiplier == 0)
181                   {
182                       const char *env = getenv("PEGASUS_VMS_THREAD_STACK_MULTIPLIER");
183           
184                       if (env)
185                           _multiplier = atoi(env);
186           
187                       if (_multiplier == 0)
188                           _multiplier = 2;
189                   }
190           
191                   mutex_unlock(&_multiplier_mutex);
192               }
193           
194 mike  1.2     return _multiplier;
195           #else
196               return 2;
197           #endif
198           }
199           
200           //==============================================================================
201           //
202           // PEGASUS_HAVE_PTHREADS
203           //
204           //==============================================================================
205           
206           #if defined(PEGASUS_HAVE_PTHREADS)
207           
208           int Threads::create(
209               ThreadType& thread, 
210               Type type,
211               void* (*start)(void*), 
212               void* arg)
213           {
214               // Initialize thread attributes:
215 mike  1.2 
216               pthread_attr_t attr;
217               pthread_attr_init(&attr);
218           
219               // Detached:
220           
221               if (type == DETACHED)
222               {
223           #if defined(PEGASUS_PLATFORM_ZOS_ZSERIES_IBM)
224                   int ds = 1;
225                   pthread_attr_setdetachstate(&attr, &ds);
226           #else
227                   pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
228           #endif
229               }
230           
231               // Stack size:
232           
233           #if defined(PEGASUS_PLATFORM_HPUX_PARISC_ACC) || defined(PEGASUS_OS_VMS)
234               {
235                   size_t stacksize;
236 mike  1.2 
237                   if (pthread_attr_getstacksize(&attr, &stacksize) == 0)
238                   {
239                       int m = _get_stack_multiplier();
240                       int rc = pthread_attr_setstacksize(&attr, stacksize * m);
241                       PEGASUS_ASSERT(rc == 0);
242                   }
243               }
244           #endif
245           
246               // Scheduling policy:
247           
248           #if defined(PEGASUS_PLATFORM_SOLARIS_SPARC_GNU) || \
249               defined(PEGASUS_PLATFORM_SOLARIS_SPARC_CC)
250           # if defined SUNOS_5_7
251               pthread_attr_setschedpolicy(&attr, SCHED_RR);
252           # else
253               pthread_attr_setschedpolicy(&attr, SCHED_OTHER);
254           # endif
255           #endif // PEGASUS_PLATFORM_SOLARIS_SPARC_GNU
256           
257 mike  1.2     // Create thread:
258           
259 mike  1.4     int rc = pthread_create(&thread.thread, &attr, start, arg);
260 mike  1.2 
261               if (rc != 0)
262               {
263                   thread = ThreadType();
264                   return rc;
265               }
266           
267               // Destroy attributes now.
268           
269               pthread_attr_destroy(&attr);
270           
271               // Return:
272           
273               return 0;
274           }
275           
276           ThreadType Threads::self() 
277           {
278 mike  1.4     ThreadType tt;
279               tt.thread = pthread_self();
280               return tt;
281 mike  1.2 }
282           
283           #endif /* PEGASUS_HAVE_PTHREADS */
284           
285           PEGASUS_NAMESPACE_END

No CVS admin address has been configured
Powered by
ViewCVS 0.9.2