1 | /* $Id: thread-posix.cpp 29250 2010-05-09 17:53:58Z 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_POKE_SIG 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_POKE_SIG
|
---|
70 | /** Set if we can poke a thread, clear if we cannot. */
|
---|
71 | static bool g_fCanPokeThread;
|
---|
72 | #endif
|
---|
73 |
|
---|
74 |
|
---|
75 | /*******************************************************************************
|
---|
76 | * Internal Functions *
|
---|
77 | *******************************************************************************/
|
---|
78 | static void *rtThreadNativeMain(void *pvArgs);
|
---|
79 | static void rtThreadKeyDestruct(void *pvValue);
|
---|
80 | static void rtThreadPosixPokeSignal(int iSignal);
|
---|
81 |
|
---|
82 |
|
---|
83 | int rtThreadNativeInit(void)
|
---|
84 | {
|
---|
85 | /*
|
---|
86 | * Allocate the TLS (key in posix terms) where we store the pointer to
|
---|
87 | * a threads RTTHREADINT structure.
|
---|
88 | */
|
---|
89 | int rc = pthread_key_create(&g_SelfKey, rtThreadKeyDestruct);
|
---|
90 | if (rc)
|
---|
91 | return VERR_NO_TLS_FOR_SELF;
|
---|
92 |
|
---|
93 | #ifdef RTTHREAD_POSIX_POKE_SIG
|
---|
94 | /*
|
---|
95 | * Try register the dummy signal handler for RTThreadPoke.
|
---|
96 | */
|
---|
97 | g_fCanPokeThread = false;
|
---|
98 | struct sigaction SigActOld;
|
---|
99 | if (!sigaction(RTTHREAD_POSIX_POKE_SIG, NULL, &SigActOld))
|
---|
100 | {
|
---|
101 | if ( SigActOld.sa_handler == SIG_DFL
|
---|
102 | || SigActOld.sa_handler == rtThreadPosixPokeSignal)
|
---|
103 | {
|
---|
104 | struct sigaction SigAct;
|
---|
105 | memset(&SigAct, '\0', sizeof(SigAct));
|
---|
106 | SigAct.sa_handler = rtThreadPosixPokeSignal;
|
---|
107 | sigfillset(&SigAct.sa_mask);
|
---|
108 | SigAct.sa_flags = 0;
|
---|
109 |
|
---|
110 | /* ASSUMES no sigaction race... (lazy bird) */
|
---|
111 | if (!sigaction(RTTHREAD_POSIX_POKE_SIG, &SigAct, NULL))
|
---|
112 | g_fCanPokeThread = true;
|
---|
113 | else
|
---|
114 | {
|
---|
115 | AssertMsgFailed(("rc=%Rrc errno=%d\n", RTErrConvertFromErrno(errno), errno));
|
---|
116 | pthread_key_delete(g_SelfKey);
|
---|
117 | g_SelfKey = 0;
|
---|
118 | }
|
---|
119 | }
|
---|
120 | }
|
---|
121 | else
|
---|
122 | AssertMsgFailed(("rc=%Rrc errno=%d\n", RTErrConvertFromErrno(errno), errno));
|
---|
123 | #endif /* RTTHREAD_POSIX_POKE_SIG */
|
---|
124 | return rc;
|
---|
125 | }
|
---|
126 |
|
---|
127 |
|
---|
128 | /**
|
---|
129 | * Destructor called when a thread terminates.
|
---|
130 | * @param pvValue The key value. PRTTHREAD in our case.
|
---|
131 | */
|
---|
132 | static void rtThreadKeyDestruct(void *pvValue)
|
---|
133 | {
|
---|
134 | /*
|
---|
135 | * Deal with alien threads.
|
---|
136 | */
|
---|
137 | PRTTHREADINT pThread = (PRTTHREADINT)pvValue;
|
---|
138 | if (pThread->fIntFlags & RTTHREADINT_FLAGS_ALIEN)
|
---|
139 | {
|
---|
140 | pthread_setspecific(g_SelfKey, pThread);
|
---|
141 | rtThreadTerminate(pThread, 0);
|
---|
142 | pthread_setspecific(g_SelfKey, NULL);
|
---|
143 | }
|
---|
144 | }
|
---|
145 |
|
---|
146 |
|
---|
147 | #ifdef RTTHREAD_POSIX_POKE_SIG
|
---|
148 | /**
|
---|
149 | * Dummy signal handler for the poke signal.
|
---|
150 | *
|
---|
151 | * @param iSignal The signal number.
|
---|
152 | */
|
---|
153 | static void rtThreadPosixPokeSignal(int iSignal)
|
---|
154 | {
|
---|
155 | Assert(iSignal == RTTHREAD_POSIX_POKE_SIG);
|
---|
156 | NOREF(iSignal);
|
---|
157 | }
|
---|
158 | #endif
|
---|
159 |
|
---|
160 |
|
---|
161 | /**
|
---|
162 | * Adopts a thread, this is called immediately after allocating the
|
---|
163 | * thread structure.
|
---|
164 | *
|
---|
165 | * @param pThread Pointer to the thread structure.
|
---|
166 | */
|
---|
167 | int rtThreadNativeAdopt(PRTTHREADINT pThread)
|
---|
168 | {
|
---|
169 | /*
|
---|
170 | * Block SIGALRM - required for timer-posix.cpp.
|
---|
171 | * This is done to limit harm done by OSes which doesn't do special SIGALRM scheduling.
|
---|
172 | * It will not help much if someone creates threads directly using pthread_create. :/
|
---|
173 | */
|
---|
174 | sigset_t SigSet;
|
---|
175 | sigemptyset(&SigSet);
|
---|
176 | sigaddset(&SigSet, SIGALRM);
|
---|
177 | sigprocmask(SIG_BLOCK, &SigSet, NULL);
|
---|
178 | #ifdef RTTHREAD_POSIX_POKE_SIG
|
---|
179 | if (g_fCanPokeThread)
|
---|
180 | siginterrupt(RTTHREAD_POSIX_POKE_SIG, 1);
|
---|
181 | #endif
|
---|
182 |
|
---|
183 | int rc = pthread_setspecific(g_SelfKey, pThread);
|
---|
184 | if (!rc)
|
---|
185 | return VINF_SUCCESS;
|
---|
186 | return VERR_FAILED_TO_SET_SELF_TLS;
|
---|
187 | }
|
---|
188 |
|
---|
189 |
|
---|
190 | /**
|
---|
191 | * Wrapper which unpacks the params and calls thread function.
|
---|
192 | */
|
---|
193 | static void *rtThreadNativeMain(void *pvArgs)
|
---|
194 | {
|
---|
195 | PRTTHREADINT pThread = (PRTTHREADINT)pvArgs;
|
---|
196 |
|
---|
197 | #if defined(RT_OS_LINUX)
|
---|
198 | /*
|
---|
199 | * Set the TID.
|
---|
200 | */
|
---|
201 | pThread->tid = syscall(__NR_gettid);
|
---|
202 | ASMMemoryFence();
|
---|
203 | #endif
|
---|
204 |
|
---|
205 | /*
|
---|
206 | * Block SIGALRM - required for timer-posix.cpp.
|
---|
207 | * This is done to limit harm done by OSes which doesn't do special SIGALRM scheduling.
|
---|
208 | * It will not help much if someone creates threads directly using pthread_create. :/
|
---|
209 | */
|
---|
210 | sigset_t SigSet;
|
---|
211 | sigemptyset(&SigSet);
|
---|
212 | sigaddset(&SigSet, SIGALRM);
|
---|
213 | sigprocmask(SIG_BLOCK, &SigSet, NULL);
|
---|
214 | #ifdef RTTHREAD_POSIX_POKE_SIG
|
---|
215 | if (g_fCanPokeThread)
|
---|
216 | siginterrupt(RTTHREAD_POSIX_POKE_SIG, 1);
|
---|
217 | #endif
|
---|
218 |
|
---|
219 | int rc = pthread_setspecific(g_SelfKey, pThread);
|
---|
220 | AssertReleaseMsg(!rc, ("failed to set self TLS. rc=%d thread '%s'\n", rc, pThread->szName));
|
---|
221 |
|
---|
222 | /*
|
---|
223 | * Call common main.
|
---|
224 | */
|
---|
225 | pthread_t Self = pthread_self();
|
---|
226 | Assert((uintptr_t)Self == (RTNATIVETHREAD)Self && (uintptr_t)Self != NIL_RTNATIVETHREAD);
|
---|
227 | rc = rtThreadMain(pThread, (uintptr_t)Self, &pThread->szName[0]);
|
---|
228 |
|
---|
229 | pthread_setspecific(g_SelfKey, NULL);
|
---|
230 | pthread_exit((void *)rc);
|
---|
231 | return (void *)rc;
|
---|
232 | }
|
---|
233 |
|
---|
234 |
|
---|
235 | int rtThreadNativeCreate(PRTTHREADINT pThread, PRTNATIVETHREAD pNativeThread)
|
---|
236 | {
|
---|
237 | /*
|
---|
238 | * Set the default stack size.
|
---|
239 | */
|
---|
240 | if (!pThread->cbStack)
|
---|
241 | pThread->cbStack = 512*1024;
|
---|
242 |
|
---|
243 | #ifdef RT_OS_LINUX
|
---|
244 | pThread->tid = -1;
|
---|
245 | #endif
|
---|
246 |
|
---|
247 | /*
|
---|
248 | * Setup thread attributes.
|
---|
249 | */
|
---|
250 | pthread_attr_t ThreadAttr;
|
---|
251 | int rc = pthread_attr_init(&ThreadAttr);
|
---|
252 | if (!rc)
|
---|
253 | {
|
---|
254 | rc = pthread_attr_setdetachstate(&ThreadAttr, PTHREAD_CREATE_DETACHED);
|
---|
255 | if (!rc)
|
---|
256 | {
|
---|
257 | rc = pthread_attr_setstacksize(&ThreadAttr, pThread->cbStack);
|
---|
258 | if (!rc)
|
---|
259 | {
|
---|
260 | /*
|
---|
261 | * Create the thread.
|
---|
262 | */
|
---|
263 | pthread_t ThreadId;
|
---|
264 | rc = pthread_create(&ThreadId, &ThreadAttr, rtThreadNativeMain, pThread);
|
---|
265 | if (!rc)
|
---|
266 | {
|
---|
267 | *pNativeThread = (uintptr_t)ThreadId;
|
---|
268 | return VINF_SUCCESS;
|
---|
269 | }
|
---|
270 | }
|
---|
271 | }
|
---|
272 | pthread_attr_destroy(&ThreadAttr);
|
---|
273 | }
|
---|
274 | return RTErrConvertFromErrno(rc);
|
---|
275 | }
|
---|
276 |
|
---|
277 |
|
---|
278 | RTDECL(RTTHREAD) RTThreadSelf(void)
|
---|
279 | {
|
---|
280 | PRTTHREADINT pThread = (PRTTHREADINT)pthread_getspecific(g_SelfKey);
|
---|
281 | /** @todo import alien threads? */
|
---|
282 | return pThread;
|
---|
283 | }
|
---|
284 |
|
---|
285 |
|
---|
286 | RTDECL(RTNATIVETHREAD) RTThreadNativeSelf(void)
|
---|
287 | {
|
---|
288 | return (RTNATIVETHREAD)pthread_self();
|
---|
289 | }
|
---|
290 |
|
---|
291 |
|
---|
292 | RTDECL(int) RTThreadSleep(RTMSINTERVAL cMillies)
|
---|
293 | {
|
---|
294 | LogFlow(("RTThreadSleep: cMillies=%d\n", cMillies));
|
---|
295 | if (!cMillies)
|
---|
296 | {
|
---|
297 | /* pthread_yield() isn't part of SuS, thus this fun. */
|
---|
298 | #ifdef RT_OS_DARWIN
|
---|
299 | pthread_yield_np();
|
---|
300 | #elif defined(RT_OS_FREEBSD) /* void pthread_yield */
|
---|
301 | pthread_yield();
|
---|
302 | #elif defined(RT_OS_SOLARIS)
|
---|
303 | sched_yield();
|
---|
304 | #else
|
---|
305 | if (!pthread_yield())
|
---|
306 | #endif
|
---|
307 | {
|
---|
308 | LogFlow(("RTThreadSleep: returning %Rrc (cMillies=%d)\n", VINF_SUCCESS, cMillies));
|
---|
309 | return VINF_SUCCESS;
|
---|
310 | }
|
---|
311 | }
|
---|
312 | else
|
---|
313 | {
|
---|
314 | struct timespec ts;
|
---|
315 | struct timespec tsrem = {0,0};
|
---|
316 |
|
---|
317 | ts.tv_nsec = (cMillies % 1000) * 1000000;
|
---|
318 | ts.tv_sec = cMillies / 1000;
|
---|
319 | if (!nanosleep(&ts, &tsrem))
|
---|
320 | {
|
---|
321 | LogFlow(("RTThreadSleep: returning %Rrc (cMillies=%d)\n", VINF_SUCCESS, cMillies));
|
---|
322 | return VINF_SUCCESS;
|
---|
323 | }
|
---|
324 | }
|
---|
325 |
|
---|
326 | int rc = RTErrConvertFromErrno(errno);
|
---|
327 | LogFlow(("RTThreadSleep: returning %Rrc (cMillies=%d)\n", rc, cMillies));
|
---|
328 | return rc;
|
---|
329 | }
|
---|
330 |
|
---|
331 |
|
---|
332 | RTDECL(bool) RTThreadYield(void)
|
---|
333 | {
|
---|
334 | #if defined(RT_ARCH_AMD64) || defined(RT_ARCH_X86)
|
---|
335 | uint64_t u64TS = ASMReadTSC();
|
---|
336 | #endif
|
---|
337 | #ifdef RT_OS_DARWIN
|
---|
338 | pthread_yield_np();
|
---|
339 | #elif defined(RT_OS_SOLARIS)
|
---|
340 | sched_yield();
|
---|
341 | #else
|
---|
342 | pthread_yield();
|
---|
343 | #endif
|
---|
344 | #if defined(RT_ARCH_AMD64) || defined(RT_ARCH_X86)
|
---|
345 | u64TS = ASMReadTSC() - u64TS;
|
---|
346 | bool fRc = u64TS > 1500;
|
---|
347 | LogFlow(("RTThreadYield: returning %d (%llu ticks)\n", fRc, u64TS));
|
---|
348 | #else
|
---|
349 | bool fRc = true; /* PORTME: Add heuristics for determining whether the cpus was yielded. */
|
---|
350 | #endif
|
---|
351 | return fRc;
|
---|
352 | }
|
---|
353 |
|
---|
354 |
|
---|
355 | RTR3DECL(uint64_t) RTThreadGetAffinity(void)
|
---|
356 | {
|
---|
357 | return 1;
|
---|
358 | }
|
---|
359 |
|
---|
360 |
|
---|
361 | RTR3DECL(int) RTThreadSetAffinity(uint64_t u64Mask)
|
---|
362 | {
|
---|
363 | if (u64Mask != 1)
|
---|
364 | return VERR_INVALID_PARAMETER;
|
---|
365 | return VINF_SUCCESS;
|
---|
366 | }
|
---|
367 |
|
---|
368 |
|
---|
369 | #ifdef RTTHREAD_POSIX_POKE_SIG
|
---|
370 | RTDECL(int) RTThreadPoke(RTTHREAD hThread)
|
---|
371 | {
|
---|
372 | AssertReturn(hThread != RTThreadSelf(), VERR_INVALID_PARAMETER);
|
---|
373 | PRTTHREADINT pThread = rtThreadGet(hThread);
|
---|
374 | AssertReturn(pThread, VERR_INVALID_HANDLE);
|
---|
375 | int rc;
|
---|
376 | if (g_fCanPokeThread)
|
---|
377 | {
|
---|
378 | rc = pthread_kill((pthread_t)(uintptr_t)pThread->Core.Key, RTTHREAD_POSIX_POKE_SIG);
|
---|
379 | rc = RTErrConvertFromErrno(rc);
|
---|
380 | }
|
---|
381 | else
|
---|
382 | rc = VERR_NOT_SUPPORTED;
|
---|
383 |
|
---|
384 | rtThreadRelease(pThread);
|
---|
385 | return rc;
|
---|
386 | }
|
---|
387 | #endif
|
---|
388 |
|
---|