1 | /* $Id: thread-posix.cpp 34175 2010-11-18 14:53:50Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * IPRT - Threads, POSIX.
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2006-2007 Oracle Corporation
|
---|
8 | *
|
---|
9 | * This file is part of VirtualBox Open Source Edition (OSE), as
|
---|
10 | * available from http://www.virtualbox.org. This file is free software;
|
---|
11 | * you can redistribute it and/or modify it under the terms of the GNU
|
---|
12 | * General Public License (GPL) as published by the Free Software
|
---|
13 | * Foundation, in version 2 as it comes in the "COPYING" file of the
|
---|
14 | * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
|
---|
15 | * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
|
---|
16 | *
|
---|
17 | * The contents of this file may alternatively be used under the terms
|
---|
18 | * of the Common Development and Distribution License Version 1.0
|
---|
19 | * (CDDL) only, as it comes in the "COPYING.CDDL" file of the
|
---|
20 | * VirtualBox OSE distribution, in which case the provisions of the
|
---|
21 | * CDDL are applicable instead of those of the GPL.
|
---|
22 | *
|
---|
23 | * You may elect to license modified versions of this file under the
|
---|
24 | * terms and conditions of either the GPL or the CDDL or both.
|
---|
25 | */
|
---|
26 |
|
---|
27 |
|
---|
28 | /*******************************************************************************
|
---|
29 | * Header Files *
|
---|
30 | *******************************************************************************/
|
---|
31 | #define LOG_GROUP RTLOGGROUP_THREAD
|
---|
32 | #include <errno.h>
|
---|
33 | #include <pthread.h>
|
---|
34 | #include <signal.h>
|
---|
35 | #if defined(RT_OS_LINUX)
|
---|
36 | # include <unistd.h>
|
---|
37 | # include <sys/syscall.h>
|
---|
38 | #endif
|
---|
39 | #if defined(RT_OS_SOLARIS)
|
---|
40 | # include <sched.h>
|
---|
41 | #endif
|
---|
42 |
|
---|
43 | #include <iprt/thread.h>
|
---|
44 | #include <iprt/log.h>
|
---|
45 | #include <iprt/assert.h>
|
---|
46 | #include <iprt/asm.h>
|
---|
47 | #if defined(RT_ARCH_AMD64) || defined(RT_ARCH_X86)
|
---|
48 | # include <iprt/asm-amd64-x86.h>
|
---|
49 | #endif
|
---|
50 | #include <iprt/err.h>
|
---|
51 | #include <iprt/string.h>
|
---|
52 | #include "internal/thread.h"
|
---|
53 |
|
---|
54 |
|
---|
55 | /*******************************************************************************
|
---|
56 | * Defined Constants And Macros *
|
---|
57 | *******************************************************************************/
|
---|
58 | #ifndef IN_GUEST
|
---|
59 | /** The signal we're using for RTThreadPoke. */
|
---|
60 | # define RTTHREAD_POSIX_WITH_POKE SIGUSR2
|
---|
61 | #endif
|
---|
62 |
|
---|
63 |
|
---|
64 | /*******************************************************************************
|
---|
65 | * Global Variables *
|
---|
66 | *******************************************************************************/
|
---|
67 | /** The pthread key in which we store the pointer to our own PRTTHREAD structure. */
|
---|
68 | static pthread_key_t g_SelfKey;
|
---|
69 | #ifdef RTTHREAD_POSIX_WITH_POKE
|
---|
70 | /** The signal we use for poking threads.
|
---|
71 | * This is set to -1 if no available signal was found. */
|
---|
72 | static int g_iSigPokeThread = -1;
|
---|
73 | #endif
|
---|
74 |
|
---|
75 |
|
---|
76 | /*******************************************************************************
|
---|
77 | * Internal Functions *
|
---|
78 | *******************************************************************************/
|
---|
79 | static void *rtThreadNativeMain(void *pvArgs);
|
---|
80 | static void rtThreadKeyDestruct(void *pvValue);
|
---|
81 | static void rtThreadPosixPokeSignal(int iSignal);
|
---|
82 |
|
---|
83 |
|
---|
84 | int rtThreadNativeInit(void)
|
---|
85 | {
|
---|
86 | /*
|
---|
87 | * Allocate the TLS (key in posix terms) where we store the pointer to
|
---|
88 | * a threads RTTHREADINT structure.
|
---|
89 | */
|
---|
90 | int rc = pthread_key_create(&g_SelfKey, rtThreadKeyDestruct);
|
---|
91 | if (rc)
|
---|
92 | return VERR_NO_TLS_FOR_SELF;
|
---|
93 |
|
---|
94 | #ifdef RTTHREAD_POSIX_WITH_POKE
|
---|
95 | /*
|
---|
96 | * Try register the dummy signal handler for RTThreadPoke.
|
---|
97 | * Avoid SIGRTMIN thru SIGRTMIN+2 because of LinuxThreads.
|
---|
98 | */
|
---|
99 | static const int s_aiSigCandidates[] =
|
---|
100 | {
|
---|
101 | # ifdef SIGRTMAX
|
---|
102 | SIGRTMAX-3,
|
---|
103 | SIGRTMAX-2,
|
---|
104 | SIGRTMAX-1,
|
---|
105 | # endif
|
---|
106 | # ifndef RT_OS_SOLARIS
|
---|
107 | SIGUSR2,
|
---|
108 | # endif
|
---|
109 | SIGWINCH
|
---|
110 | };
|
---|
111 |
|
---|
112 | g_iSigPokeThread = -1;
|
---|
113 | for (unsigned iSig = 0; iSig < RT_ELEMENTS(s_aiSigCandidates); iSig++)
|
---|
114 | {
|
---|
115 | struct sigaction SigActOld;
|
---|
116 | if (!sigaction(s_aiSigCandidates[iSig], NULL, &SigActOld))
|
---|
117 | {
|
---|
118 | if ( SigActOld.sa_handler == SIG_DFL
|
---|
119 | || SigActOld.sa_handler == rtThreadPosixPokeSignal)
|
---|
120 | {
|
---|
121 | struct sigaction SigAct;
|
---|
122 | RT_ZERO(SigAct);
|
---|
123 | SigAct.sa_handler = rtThreadPosixPokeSignal;
|
---|
124 | SigAct.sa_flags = 0;
|
---|
125 | sigfillset(&SigAct.sa_mask);
|
---|
126 |
|
---|
127 | /* ASSUMES no sigaction race... (lazy bird) */
|
---|
128 | if (!sigaction(s_aiSigCandidates[iSig], &SigAct, NULL))
|
---|
129 | {
|
---|
130 | g_iSigPokeThread = s_aiSigCandidates[iSig];
|
---|
131 | break;
|
---|
132 | }
|
---|
133 | AssertMsgFailed(("rc=%Rrc errno=%d\n", RTErrConvertFromErrno(errno), errno));
|
---|
134 | }
|
---|
135 | }
|
---|
136 | else
|
---|
137 | AssertMsgFailed(("rc=%Rrc errno=%d\n", RTErrConvertFromErrno(errno), errno));
|
---|
138 | }
|
---|
139 | #endif /* RTTHREAD_POSIX_WITH_POKE */
|
---|
140 | return rc;
|
---|
141 | }
|
---|
142 |
|
---|
143 |
|
---|
144 | /**
|
---|
145 | * Destructor called when a thread terminates.
|
---|
146 | * @param pvValue The key value. PRTTHREAD in our case.
|
---|
147 | */
|
---|
148 | static void rtThreadKeyDestruct(void *pvValue)
|
---|
149 | {
|
---|
150 | /*
|
---|
151 | * Deal with alien threads.
|
---|
152 | */
|
---|
153 | PRTTHREADINT pThread = (PRTTHREADINT)pvValue;
|
---|
154 | if (pThread->fIntFlags & RTTHREADINT_FLAGS_ALIEN)
|
---|
155 | {
|
---|
156 | pthread_setspecific(g_SelfKey, pThread);
|
---|
157 | rtThreadTerminate(pThread, 0);
|
---|
158 | pthread_setspecific(g_SelfKey, NULL);
|
---|
159 | }
|
---|
160 | }
|
---|
161 |
|
---|
162 |
|
---|
163 | #ifdef RTTHREAD_POSIX_WITH_POKE
|
---|
164 | /**
|
---|
165 | * Dummy signal handler for the poke signal.
|
---|
166 | *
|
---|
167 | * @param iSignal The signal number.
|
---|
168 | */
|
---|
169 | static void rtThreadPosixPokeSignal(int iSignal)
|
---|
170 | {
|
---|
171 | Assert(iSignal == g_iSigPokeThread);
|
---|
172 | NOREF(iSignal);
|
---|
173 | }
|
---|
174 | #endif
|
---|
175 |
|
---|
176 |
|
---|
177 | /**
|
---|
178 | * Adopts a thread, this is called immediately after allocating the
|
---|
179 | * thread structure.
|
---|
180 | *
|
---|
181 | * @param pThread Pointer to the thread structure.
|
---|
182 | */
|
---|
183 | int rtThreadNativeAdopt(PRTTHREADINT pThread)
|
---|
184 | {
|
---|
185 | /*
|
---|
186 | * Block SIGALRM - required for timer-posix.cpp.
|
---|
187 | * This is done to limit harm done by OSes which doesn't do special SIGALRM scheduling.
|
---|
188 | * It will not help much if someone creates threads directly using pthread_create. :/
|
---|
189 | */
|
---|
190 | sigset_t SigSet;
|
---|
191 | sigemptyset(&SigSet);
|
---|
192 | sigaddset(&SigSet, SIGALRM);
|
---|
193 | sigprocmask(SIG_BLOCK, &SigSet, NULL);
|
---|
194 | #ifdef RTTHREAD_POSIX_WITH_POKE
|
---|
195 | if (g_iSigPokeThread != -1)
|
---|
196 | siginterrupt(g_iSigPokeThread, 1);
|
---|
197 | #endif
|
---|
198 |
|
---|
199 | int rc = pthread_setspecific(g_SelfKey, pThread);
|
---|
200 | if (!rc)
|
---|
201 | return VINF_SUCCESS;
|
---|
202 | return VERR_FAILED_TO_SET_SELF_TLS;
|
---|
203 | }
|
---|
204 |
|
---|
205 |
|
---|
206 | /**
|
---|
207 | * Wrapper which unpacks the params and calls thread function.
|
---|
208 | */
|
---|
209 | static void *rtThreadNativeMain(void *pvArgs)
|
---|
210 | {
|
---|
211 | PRTTHREADINT pThread = (PRTTHREADINT)pvArgs;
|
---|
212 |
|
---|
213 | #if defined(RT_OS_LINUX)
|
---|
214 | /*
|
---|
215 | * Set the TID.
|
---|
216 | */
|
---|
217 | pThread->tid = syscall(__NR_gettid);
|
---|
218 | ASMMemoryFence();
|
---|
219 | #endif
|
---|
220 |
|
---|
221 | /*
|
---|
222 | * Block SIGALRM - required for timer-posix.cpp.
|
---|
223 | * This is done to limit harm done by OSes which doesn't do special SIGALRM scheduling.
|
---|
224 | * It will not help much if someone creates threads directly using pthread_create. :/
|
---|
225 | */
|
---|
226 | sigset_t SigSet;
|
---|
227 | sigemptyset(&SigSet);
|
---|
228 | sigaddset(&SigSet, SIGALRM);
|
---|
229 | sigprocmask(SIG_BLOCK, &SigSet, NULL);
|
---|
230 | #ifdef RTTHREAD_POSIX_WITH_POKE
|
---|
231 | if (g_iSigPokeThread != -1)
|
---|
232 | siginterrupt(g_iSigPokeThread, 1);
|
---|
233 | #endif
|
---|
234 |
|
---|
235 | int rc = pthread_setspecific(g_SelfKey, pThread);
|
---|
236 | AssertReleaseMsg(!rc, ("failed to set self TLS. rc=%d thread '%s'\n", rc, pThread->szName));
|
---|
237 |
|
---|
238 | /*
|
---|
239 | * Call common main.
|
---|
240 | */
|
---|
241 | pthread_t Self = pthread_self();
|
---|
242 | Assert((uintptr_t)Self == (RTNATIVETHREAD)Self && (uintptr_t)Self != NIL_RTNATIVETHREAD);
|
---|
243 | rc = rtThreadMain(pThread, (uintptr_t)Self, &pThread->szName[0]);
|
---|
244 |
|
---|
245 | pthread_setspecific(g_SelfKey, NULL);
|
---|
246 | pthread_exit((void *)rc);
|
---|
247 | return (void *)rc;
|
---|
248 | }
|
---|
249 |
|
---|
250 |
|
---|
251 | int rtThreadNativeCreate(PRTTHREADINT pThread, PRTNATIVETHREAD pNativeThread)
|
---|
252 | {
|
---|
253 | /*
|
---|
254 | * Set the default stack size.
|
---|
255 | */
|
---|
256 | if (!pThread->cbStack)
|
---|
257 | pThread->cbStack = 512*1024;
|
---|
258 |
|
---|
259 | #ifdef RT_OS_LINUX
|
---|
260 | pThread->tid = -1;
|
---|
261 | #endif
|
---|
262 |
|
---|
263 | /*
|
---|
264 | * Setup thread attributes.
|
---|
265 | */
|
---|
266 | pthread_attr_t ThreadAttr;
|
---|
267 | int rc = pthread_attr_init(&ThreadAttr);
|
---|
268 | if (!rc)
|
---|
269 | {
|
---|
270 | rc = pthread_attr_setdetachstate(&ThreadAttr, PTHREAD_CREATE_DETACHED);
|
---|
271 | if (!rc)
|
---|
272 | {
|
---|
273 | rc = pthread_attr_setstacksize(&ThreadAttr, pThread->cbStack);
|
---|
274 | if (!rc)
|
---|
275 | {
|
---|
276 | /*
|
---|
277 | * Create the thread.
|
---|
278 | */
|
---|
279 | pthread_t ThreadId;
|
---|
280 | rc = pthread_create(&ThreadId, &ThreadAttr, rtThreadNativeMain, pThread);
|
---|
281 | if (!rc)
|
---|
282 | {
|
---|
283 | *pNativeThread = (uintptr_t)ThreadId;
|
---|
284 | return VINF_SUCCESS;
|
---|
285 | }
|
---|
286 | }
|
---|
287 | }
|
---|
288 | pthread_attr_destroy(&ThreadAttr);
|
---|
289 | }
|
---|
290 | return RTErrConvertFromErrno(rc);
|
---|
291 | }
|
---|
292 |
|
---|
293 |
|
---|
294 | RTDECL(RTTHREAD) RTThreadSelf(void)
|
---|
295 | {
|
---|
296 | PRTTHREADINT pThread = (PRTTHREADINT)pthread_getspecific(g_SelfKey);
|
---|
297 | /** @todo import alien threads? */
|
---|
298 | return pThread;
|
---|
299 | }
|
---|
300 |
|
---|
301 |
|
---|
302 | RTDECL(RTNATIVETHREAD) RTThreadNativeSelf(void)
|
---|
303 | {
|
---|
304 | return (RTNATIVETHREAD)pthread_self();
|
---|
305 | }
|
---|
306 |
|
---|
307 |
|
---|
308 | RTDECL(int) RTThreadSleep(RTMSINTERVAL cMillies)
|
---|
309 | {
|
---|
310 | LogFlow(("RTThreadSleep: cMillies=%d\n", cMillies));
|
---|
311 | if (!cMillies)
|
---|
312 | {
|
---|
313 | /* pthread_yield() isn't part of SuS, thus this fun. */
|
---|
314 | #ifdef RT_OS_DARWIN
|
---|
315 | pthread_yield_np();
|
---|
316 | #elif defined(RT_OS_FREEBSD) /* void pthread_yield */
|
---|
317 | pthread_yield();
|
---|
318 | #elif defined(RT_OS_SOLARIS)
|
---|
319 | sched_yield();
|
---|
320 | #else
|
---|
321 | if (!pthread_yield())
|
---|
322 | #endif
|
---|
323 | {
|
---|
324 | LogFlow(("RTThreadSleep: returning %Rrc (cMillies=%d)\n", VINF_SUCCESS, cMillies));
|
---|
325 | return VINF_SUCCESS;
|
---|
326 | }
|
---|
327 | }
|
---|
328 | else
|
---|
329 | {
|
---|
330 | struct timespec ts;
|
---|
331 | struct timespec tsrem = {0,0};
|
---|
332 |
|
---|
333 | ts.tv_nsec = (cMillies % 1000) * 1000000;
|
---|
334 | ts.tv_sec = cMillies / 1000;
|
---|
335 | if (!nanosleep(&ts, &tsrem))
|
---|
336 | {
|
---|
337 | LogFlow(("RTThreadSleep: returning %Rrc (cMillies=%d)\n", VINF_SUCCESS, cMillies));
|
---|
338 | return VINF_SUCCESS;
|
---|
339 | }
|
---|
340 | }
|
---|
341 |
|
---|
342 | int rc = RTErrConvertFromErrno(errno);
|
---|
343 | LogFlow(("RTThreadSleep: returning %Rrc (cMillies=%d)\n", rc, cMillies));
|
---|
344 | return rc;
|
---|
345 | }
|
---|
346 |
|
---|
347 |
|
---|
348 | RTDECL(bool) RTThreadYield(void)
|
---|
349 | {
|
---|
350 | #if defined(RT_ARCH_AMD64) || defined(RT_ARCH_X86)
|
---|
351 | uint64_t u64TS = ASMReadTSC();
|
---|
352 | #endif
|
---|
353 | #ifdef RT_OS_DARWIN
|
---|
354 | pthread_yield_np();
|
---|
355 | #elif defined(RT_OS_SOLARIS)
|
---|
356 | sched_yield();
|
---|
357 | #else
|
---|
358 | pthread_yield();
|
---|
359 | #endif
|
---|
360 | #if defined(RT_ARCH_AMD64) || defined(RT_ARCH_X86)
|
---|
361 | u64TS = ASMReadTSC() - u64TS;
|
---|
362 | bool fRc = u64TS > 1500;
|
---|
363 | LogFlow(("RTThreadYield: returning %d (%llu ticks)\n", fRc, u64TS));
|
---|
364 | #else
|
---|
365 | bool fRc = true; /* PORTME: Add heuristics for determining whether the cpus was yielded. */
|
---|
366 | #endif
|
---|
367 | return fRc;
|
---|
368 | }
|
---|
369 |
|
---|
370 |
|
---|
371 | RTR3DECL(uint64_t) RTThreadGetAffinity(void)
|
---|
372 | {
|
---|
373 | return 1;
|
---|
374 | }
|
---|
375 |
|
---|
376 |
|
---|
377 | RTR3DECL(int) RTThreadSetAffinity(uint64_t u64Mask)
|
---|
378 | {
|
---|
379 | if (u64Mask != 1)
|
---|
380 | return VERR_INVALID_PARAMETER;
|
---|
381 | return VINF_SUCCESS;
|
---|
382 | }
|
---|
383 |
|
---|
384 |
|
---|
385 | #ifdef RTTHREAD_POSIX_WITH_POKE
|
---|
386 | RTDECL(int) RTThreadPoke(RTTHREAD hThread)
|
---|
387 | {
|
---|
388 | AssertReturn(hThread != RTThreadSelf(), VERR_INVALID_PARAMETER);
|
---|
389 | PRTTHREADINT pThread = rtThreadGet(hThread);
|
---|
390 | AssertReturn(pThread, VERR_INVALID_HANDLE);
|
---|
391 |
|
---|
392 | int rc;
|
---|
393 | if (g_iSigPokeThread != -1)
|
---|
394 | {
|
---|
395 | rc = pthread_kill((pthread_t)(uintptr_t)pThread->Core.Key, g_iSigPokeThread);
|
---|
396 | rc = RTErrConvertFromErrno(rc);
|
---|
397 | }
|
---|
398 | else
|
---|
399 | rc = VERR_NOT_SUPPORTED;
|
---|
400 |
|
---|
401 | rtThreadRelease(pThread);
|
---|
402 | return rc;
|
---|
403 | }
|
---|
404 | #endif
|
---|
405 |
|
---|
406 | RTR3DECL(int) RTThreadGetExecutionTimeMilli(uint64_t *pKernelTime, uint64_t *pUserTime)
|
---|
407 | {
|
---|
408 | return VERR_NOT_IMPLEMENTED;
|
---|
409 | }
|
---|