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