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

  1 martin 1.17 //%LICENSE////////////////////////////////////////////////////////////////
  2 martin 1.18 //
  3 martin 1.17 // Licensed to The Open Group (TOG) under one or more contributor license
  4             // agreements.  Refer to the OpenPegasusNOTICE.txt file distributed with
  5             // this work for additional information regarding copyright ownership.
  6             // Each contributor licenses this file to you under the OpenPegasus Open
  7             // Source License; you may not use this file except in compliance with the
  8             // License.
  9 martin 1.18 //
 10 martin 1.17 // Permission is hereby granted, free of charge, to any person obtaining a
 11             // copy of this software and associated documentation files (the "Software"),
 12             // to deal in the Software without restriction, including without limitation
 13             // the rights to use, copy, modify, merge, publish, distribute, sublicense,
 14             // and/or sell copies of the Software, and to permit persons to whom the
 15             // Software is furnished to do so, subject to the following conditions:
 16 martin 1.18 //
 17 martin 1.17 // The above copyright notice and this permission notice shall be included
 18             // in all copies or substantial portions of the Software.
 19 martin 1.18 //
 20 martin 1.17 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
 21 martin 1.18 // OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
 22 martin 1.17 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
 23             // IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
 24             // CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
 25             // TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
 26             // SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 27 martin 1.18 //
 28 martin 1.17 //////////////////////////////////////////////////////////////////////////
 29 mike   1.2  //
 30             //%/////////////////////////////////////////////////////////////////////////////
 31             
 32 mike   1.5  #include <errno.h>
 33 mike   1.2  #include "Threads.h"
 34             #include "IDFactory.h"
 35             #include "TSDKey.h"
 36             #include "Once.h"
 37             
 38 a.dunfey 1.7  #if defined(PEGASUS_OS_TYPE_WINDOWS)
 39 mike     1.2  # include <sys/timeb.h>
 40               #endif
 41 marek    1.13 #if defined(PEGASUS_OS_ZOS)
 42               # include <unistd.h>
 43               #endif
 44 mike     1.2  
 45 karl     1.14 #if defined(PEGASUS_OS_SOLARIS)
 46               # include <unistd.h>
 47               #endif
 48               
 49 mike     1.2  PEGASUS_NAMESPACE_BEGIN
 50               
 51 sahana.prabhakar 1.20 namespace 
 52                       {
 53                           //mutex used for synchronising threads calling _get_stack_multiplier
 54                           MutexType _multiplier_mutex = PEGASUS_MUTEX_INITIALIZER;
 55                       }
 56                       
 57 mike             1.2  void Threads::sleep(int msec)
 58                       {
 59                       #if defined(PEGASUS_HAVE_NANOSLEEP)
 60                       
 61 dave.sudlik      1.8      struct timespec wait, remwait;
 62 mike             1.2      wait.tv_sec = msec / 1000;
 63                           wait.tv_nsec = (msec % 1000) * 1000000;
 64 dave.sudlik      1.8  
 65                           while ((nanosleep(&wait, &remwait) == -1) && (errno == EINTR))
 66                           {
 67                               wait.tv_sec = remwait.tv_sec;
 68                               wait.tv_nsec = remwait.tv_nsec;
 69                           }
 70 mike             1.2  
 71 a.dunfey         1.7  #elif defined(PEGASUS_OS_TYPE_WINDOWS)
 72 mike             1.2  
 73                           if (msec == 0)
 74 kumpf            1.6      {
 75 mike             1.2          Sleep(0);
 76                               return;
 77                           }
 78                       
 79                           struct _timeb end, now;
 80                           _ftime( &end );
 81                           end.time += (msec / 1000);
 82                           msec -= (msec / 1000);
 83                           end.millitm += msec;
 84                       
 85                           do
 86                           {
 87                               Sleep(0);
 88                               _ftime(&now);
 89 kumpf            1.6      }
 90                           while (end.millitm > now.millitm && end.time >= now.time);
 91 mike             1.2  
 92 ouyang.jian      1.12 #else
 93 mike             1.2      if (msec < 1000)
 94                           {
 95                               usleep(msec*1000);
 96                           }
 97                           else
 98                           {
 99                               // sleep for loop seconds
100 mike             1.3          ::sleep(msec / 1000);
101 mike             1.2          // Usleep the remaining micro seconds
102                               usleep( (msec*1000) % 1000000 );
103                           }
104 ouyang.jian      1.12 
105 mike             1.2  #endif
106                       }
107                       
108                       //==============================================================================
109                       //
110                       // PEGASUS_HAVE_PTHREADS
111                       //
112                       //==============================================================================
113                       
114                       #if defined(PEGASUS_HAVE_PTHREADS)
115                       
116                       int Threads::create(
117 kumpf            1.6      ThreadType& thread,
118 mike             1.2      Type type,
119 kumpf            1.6      void* (*start)(void*),
120 mike             1.2      void* arg)
121                       {
122                           // Initialize thread attributes:
123                       
124                           pthread_attr_t attr;
125 sahana.prabhakar 1.19     int rc = pthread_attr_init(&attr);
126                           if(rc != 0)
127                           {
128                               return rc;
129                           }
130 mike             1.2  
131                           // Detached:
132                       
133                           if (type == DETACHED)
134                           {
135 r.kieninger      1.15 #if defined(PEGASUS_OS_ZOS)
136 mike             1.2          int ds = 1;
137                               pthread_attr_setdetachstate(&attr, &ds);
138                       #else
139                               pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
140                       #endif
141                           }
142                       
143                           // Stack size:
144 marek            1.21     rc = pthread_attr_setstacksize(&attr, PEGASUS_INITIAL_THREADSTACK_SIZE);
145                           PEGASUS_ASSERT(rc == 0);
146 mike             1.2  
147                           // Scheduling policy:
148                       
149 karl             1.14 #if defined(PEGASUS_OS_SOLARIS)
150                       
151 mike             1.2      pthread_attr_setschedpolicy(&attr, SCHED_OTHER);
152 karl             1.14 
153                       #endif /* defined(PEGASUS_OS_SOLARIS) */
154 mike             1.2  
155                           // Create thread:
156                       
157 sahana.prabhakar 1.19     rc = pthread_create(&thread.thread, &attr, start, arg);
158 mike             1.2  
159                           if (rc != 0)
160                           {
161                               thread = ThreadType();
162                           }
163                       
164                           // Destroy attributes now.
165                       
166                           pthread_attr_destroy(&attr);
167                       
168                           // Return:
169                       
170 sahana.prabhakar 1.19     return rc;
171 mike             1.2  }
172                       
173 kumpf            1.6  ThreadType Threads::self()
174 mike             1.2  {
175 mike             1.4      ThreadType tt;
176                           tt.thread = pthread_self();
177                           return tt;
178 mike             1.2  }
179                       
180                       #endif /* PEGASUS_HAVE_PTHREADS */
181                       
182                       PEGASUS_NAMESPACE_END

No CVS admin address has been configured
Powered by
ViewCVS 0.9.2