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 void Threads::sleep(int msec)
52 {
53 #if defined(PEGASUS_HAVE_NANOSLEEP)
54
|
55 dave.sudlik 1.8 struct timespec wait, remwait;
|
56 mike 1.2 wait.tv_sec = msec / 1000;
57 wait.tv_nsec = (msec % 1000) * 1000000;
|
58 dave.sudlik 1.8
59 while ((nanosleep(&wait, &remwait) == -1) && (errno == EINTR))
60 {
61 wait.tv_sec = remwait.tv_sec;
62 wait.tv_nsec = remwait.tv_nsec;
63 }
|
64 mike 1.2
|
65 a.dunfey 1.7 #elif defined(PEGASUS_OS_TYPE_WINDOWS)
|
66 mike 1.2
67 if (msec == 0)
|
68 kumpf 1.6 {
|
69 mike 1.2 Sleep(0);
70 return;
71 }
72
73 struct _timeb end, now;
74 _ftime( &end );
75 end.time += (msec / 1000);
76 msec -= (msec / 1000);
77 end.millitm += msec;
78
79 do
80 {
81 Sleep(0);
82 _ftime(&now);
|
83 kumpf 1.6 }
84 while (end.millitm > now.millitm && end.time >= now.time);
|
85 mike 1.2
|
86 ouyang.jian 1.12 #else
|
87 mike 1.2 if (msec < 1000)
88 {
89 usleep(msec*1000);
90 }
91 else
92 {
93 // sleep for loop seconds
|
94 mike 1.3 ::sleep(msec / 1000);
|
95 mike 1.2 // Usleep the remaining micro seconds
96 usleep( (msec*1000) % 1000000 );
97 }
|
98 ouyang.jian 1.12
|
99 mike 1.2 #endif
100 }
101
102 //==============================================================================
103 //
104 // _get_stack_multiplier()
105 //
106 //==============================================================================
107
108 static inline int _get_stack_multiplier()
109 {
110 #if defined(PEGASUS_OS_VMS)
111
112 static int _multiplier = 0;
113 static MutexType _multiplier_mutex = PEGASUS_MUTEX_INITIALIZER;
114
|
115 kumpf 1.6 //
116 // This code uses a, 'hidden' (non-documented), VMS only, logical
|
117 mike 1.2 // name (environment variable), PEGASUS_VMS_THREAD_STACK_MULTIPLIER,
118 // to allow in the field adjustment of the thread stack size.
119 //
120 // We only check for the logical name once to not have an
121 // impact on performance.
|
122 kumpf 1.6 //
|
123 mike 1.2 // Note: This code may have problems in a multithreaded environment
124 // with the setting of doneOnce to true.
|
125 kumpf 1.6 //
|
126 mike 1.2 // Current code in Cimserver and the clients do some serial thread
127 // creations first so this is not a problem now.
|
128 kumpf 1.6 //
|
129 mike 1.2
130 if (_multiplier == 0)
131 {
132 mutex_lock(&_multiplier_mutex);
133
134 if (_multiplier == 0)
135 {
136 const char *env = getenv("PEGASUS_VMS_THREAD_STACK_MULTIPLIER");
137
138 if (env)
139 _multiplier = atoi(env);
140
141 if (_multiplier == 0)
142 _multiplier = 2;
143 }
144
145 mutex_unlock(&_multiplier_mutex);
146 }
147
148 return _multiplier;
|
149 kumpf 1.11 #elif defined(PEGASUS_PLATFORM_HPUX_PARISC_ACC)
150 return 2;
|
151 cheng.sp 1.16 #elif defined(PEGASUS_OS_AIX)
152 return 2;
|
153 mike 1.2 #else
|
154 kumpf 1.11 return 1;
|
155 mike 1.2 #endif
156 }
157
158 //==============================================================================
159 //
160 // PEGASUS_HAVE_PTHREADS
161 //
162 //==============================================================================
163
164 #if defined(PEGASUS_HAVE_PTHREADS)
165
166 int Threads::create(
|
167 kumpf 1.6 ThreadType& thread,
|
168 mike 1.2 Type type,
|
169 kumpf 1.6 void* (*start)(void*),
|
170 mike 1.2 void* arg)
171 {
172 // Initialize thread attributes:
173
174 pthread_attr_t attr;
175 pthread_attr_init(&attr);
176
177 // Detached:
178
179 if (type == DETACHED)
180 {
|
181 r.kieninger 1.15 #if defined(PEGASUS_OS_ZOS)
|
182 mike 1.2 int ds = 1;
183 pthread_attr_setdetachstate(&attr, &ds);
184 #else
185 pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
186 #endif
187 }
188
189 // Stack size:
190
|
191 kumpf 1.11 int multiplier = _get_stack_multiplier();
192
193 if (multiplier != 1)
|
194 mike 1.2 {
195 size_t stacksize;
196
197 if (pthread_attr_getstacksize(&attr, &stacksize) == 0)
198 {
|
199 kumpf 1.11 int rc = pthread_attr_setstacksize(&attr, stacksize * multiplier);
|
200 mike 1.2 PEGASUS_ASSERT(rc == 0);
201 }
202 }
203
204 // Scheduling policy:
205
|
206 karl 1.14 #if defined(PEGASUS_OS_SOLARIS)
207
|
208 mike 1.2 pthread_attr_setschedpolicy(&attr, SCHED_OTHER);
|
209 karl 1.14
210 #endif /* defined(PEGASUS_OS_SOLARIS) */
|
211 mike 1.2
212 // Create thread:
213
|
214 mike 1.4 int rc = pthread_create(&thread.thread, &attr, start, arg);
|
215 mike 1.2
216 if (rc != 0)
217 {
218 thread = ThreadType();
219 return rc;
220 }
221
222 // Destroy attributes now.
223
224 pthread_attr_destroy(&attr);
225
226 // Return:
227
228 return 0;
229 }
230
|
231 kumpf 1.6 ThreadType Threads::self()
|
232 mike 1.2 {
|
233 mike 1.4 ThreadType tt;
234 tt.thread = pthread_self();
235 return tt;
|
236 mike 1.2 }
237
238 #endif /* PEGASUS_HAVE_PTHREADS */
239
240 PEGASUS_NAMESPACE_END
|