VirtualBox

source: vbox/trunk/src/VBox/Runtime/r3/posix/thread-posix.cpp@ 34174

Last change on this file since 34174 was 34174, checked in by vboxsync, 15 years ago

IPRT: Use prefer real-time signal over SIGUSR2 for RTThreadPoke. Try several ones before giving up.

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

© 2025 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette