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 // PEGASUS_HAVE_PTHREADS
105 //
106 //==============================================================================
107
108 #if defined(PEGASUS_HAVE_PTHREADS)
109
110 int Threads::create(
|
111 kumpf 1.6 ThreadType& thread,
|
112 mike 1.2 Type type,
|
113 kumpf 1.6 void* (*start)(void*),
|
114 mike 1.2 void* arg)
115 {
116 // Initialize thread attributes:
117
118 pthread_attr_t attr;
|
119 sahana.prabhakar 1.19 int rc = pthread_attr_init(&attr);
120 if(rc != 0)
121 {
122 return rc;
123 }
|
124 mike 1.2
125 // Detached:
126
127 if (type == DETACHED)
128 {
|
129 r.kieninger 1.15 #if defined(PEGASUS_OS_ZOS)
|
130 mike 1.2 int ds = 1;
131 pthread_attr_setdetachstate(&attr, &ds);
132 #else
133 pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
134 #endif
135 }
136
137 // Stack size:
|
138 marek 1.21 rc = pthread_attr_setstacksize(&attr, PEGASUS_INITIAL_THREADSTACK_SIZE);
139 PEGASUS_ASSERT(rc == 0);
|
140 mike 1.2
141 // Scheduling policy:
142
|
143 karl 1.14 #if defined(PEGASUS_OS_SOLARIS)
144
|
145 mike 1.2 pthread_attr_setschedpolicy(&attr, SCHED_OTHER);
|
146 karl 1.14
147 #endif /* defined(PEGASUS_OS_SOLARIS) */
|
148 mike 1.2
149 // Create thread:
150
|
151 sahana.prabhakar 1.19 rc = pthread_create(&thread.thread, &attr, start, arg);
|
152 mike 1.2
153 if (rc != 0)
154 {
155 thread = ThreadType();
156 }
157
158 // Destroy attributes now.
159
160 pthread_attr_destroy(&attr);
161
162 // Return:
163
|
164 sahana.prabhakar 1.19 return rc;
|
165 mike 1.2 }
166
|
167 kumpf 1.6 ThreadType Threads::self()
|
168 mike 1.2 {
|
169 mike 1.4 ThreadType tt;
170 tt.thread = pthread_self();
171 return tt;
|
172 mike 1.2 }
173
174 #endif /* PEGASUS_HAVE_PTHREADS */
175
176 PEGASUS_NAMESPACE_END
|