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

No CVS admin address has been configured
Powered by
ViewCVS 0.9.2