(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                 // _get_stack_multiplier()
100                 //
101                 //==============================================================================
102                 
103                 static inline int _get_stack_multiplier()
104                 {
105                 #if defined(PEGASUS_OS_VMS)
106                 
107                     static int _multiplier = 0;
108                     static MutexType _multiplier_mutex = PEGASUS_MUTEX_INITIALIZER;
109                 
110 kumpf       1.6     //
111                     // This code uses a, 'hidden' (non-documented), VMS only, logical
112 mike        1.2     //  name (environment variable), PEGASUS_VMS_THREAD_STACK_MULTIPLIER,
113                     //  to allow in the field adjustment of the thread stack size.
114                     //
115                     // We only check for the logical name once to not have an
116                     //  impact on performance.
117 kumpf       1.6     //
118 mike        1.2     // Note:  This code may have problems in a multithreaded environment
119                     //  with the setting of doneOnce to true.
120 kumpf       1.6     //
121 mike        1.2     // Current code in Cimserver and the clients do some serial thread
122                     //  creations first so this is not a problem now.
123 kumpf       1.6     //
124 mike        1.2 
125                     if (_multiplier == 0)
126                     {
127                         mutex_lock(&_multiplier_mutex);
128                 
129                         if (_multiplier == 0)
130                         {
131                             const char *env = getenv("PEGASUS_VMS_THREAD_STACK_MULTIPLIER");
132                 
133                             if (env)
134                                 _multiplier = atoi(env);
135                 
136                             if (_multiplier == 0)
137                                 _multiplier = 2;
138                         }
139                 
140                         mutex_unlock(&_multiplier_mutex);
141                     }
142                 
143                     return _multiplier;
144 kumpf       1.10.8.1 #elif defined(PEGASUS_PLATFORM_HPUX_PARISC_ACC)
145 mike        1.2          return 2;
146 kumpf       1.10.8.1 #else
147                          return 1;
148 mike        1.2      #endif
149                      }
150                      
151                      //==============================================================================
152                      //
153                      // PEGASUS_HAVE_PTHREADS
154                      //
155                      //==============================================================================
156                      
157                      #if defined(PEGASUS_HAVE_PTHREADS)
158                      
159                      int Threads::create(
160 kumpf       1.6          ThreadType& thread,
161 mike        1.2          Type type,
162 kumpf       1.6          void* (*start)(void*),
163 mike        1.2          void* arg)
164                      {
165                          // Initialize thread attributes:
166                      
167                          pthread_attr_t attr;
168                          pthread_attr_init(&attr);
169                      
170                          // Detached:
171                      
172                          if (type == DETACHED)
173                          {
174                      #if defined(PEGASUS_PLATFORM_ZOS_ZSERIES_IBM)
175                              int ds = 1;
176                              pthread_attr_setdetachstate(&attr, &ds);
177                      #else
178                              pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
179                      #endif
180                          }
181                      
182                          // Stack size:
183                      
184 kumpf       1.10.8.1     int multiplier = _get_stack_multiplier();
185                      
186                          if (multiplier != 1)
187 mike        1.2          {
188                              size_t stacksize;
189                      
190                              if (pthread_attr_getstacksize(&attr, &stacksize) == 0)
191                              {
192 kumpf       1.10.8.1             int rc = pthread_attr_setstacksize(&attr, stacksize * multiplier);
193 mike        1.2                  PEGASUS_ASSERT(rc == 0);
194                              }
195                          }
196                      
197                          // Scheduling policy:
198                      
199                      #if defined(PEGASUS_PLATFORM_SOLARIS_SPARC_GNU) || \
200                          defined(PEGASUS_PLATFORM_SOLARIS_SPARC_CC)
201                      # if defined SUNOS_5_7
202                          pthread_attr_setschedpolicy(&attr, SCHED_RR);
203                      # else
204                          pthread_attr_setschedpolicy(&attr, SCHED_OTHER);
205                      # endif
206                      #endif // PEGASUS_PLATFORM_SOLARIS_SPARC_GNU
207                      
208                          // Create thread:
209                      
210 mike        1.4          int rc = pthread_create(&thread.thread, &attr, start, arg);
211 mike        1.2      
212                          if (rc != 0)
213                          {
214                              thread = ThreadType();
215                              return rc;
216                          }
217                      
218                          // Destroy attributes now.
219                      
220                          pthread_attr_destroy(&attr);
221                      
222                          // Return:
223                      
224                          return 0;
225                      }
226                      
227 kumpf       1.6      ThreadType Threads::self()
228 mike        1.2      {
229 mike        1.4          ThreadType tt;
230                          tt.thread = pthread_self();
231                          return tt;
232 mike        1.2      }
233                      
234                      #endif /* PEGASUS_HAVE_PTHREADS */
235                      
236                      PEGASUS_NAMESPACE_END

No CVS admin address has been configured
Powered by
ViewCVS 0.9.2