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