VirtualBox

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

Last change on this file since 44020 was 44020, checked in by vboxsync, 12 years ago

IPRT/thread-posix.cpp: avoid the pthread_setname_np dlsym trickery in the static variant of the runtime, causes crashes

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 12.9 KB
Line 
1/* $Id: thread-posix.cpp 44020 2012-12-03 17:43:17Z vboxsync $ */
2/** @file
3 * IPRT - Threads, POSIX.
4 */
5
6/*
7 * Copyright (C) 2006-2012 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# include <sys/resource.h>
42#endif
43#if defined(RT_OS_DARWIN)
44# include <mach/thread_act.h>
45# include <mach/thread_info.h>
46# include <mach/host_info.h>
47# include <mach/mach_init.h>
48# include <mach/mach_host.h>
49#endif
50#ifdef IN_RT_STATIC
51/* The pthread_setname_np trickery below assumes a working dl env which is
52 * not guaranteed in the context of a fully static executable. */
53# undef IPRT_MAY_HAVE_PTHREAD_SET_NAME_NP
54#else
55# if defined(RT_OS_DARWIN) /*|| defined(RT_OS_FREEBSD) - later */ || defined(RT_OS_LINUX) \
56 || defined(IPRT_MAY_HAVE_PTHREAD_SET_NAME_NP)
57# define IPRT_MAY_HAVE_PTHREAD_SET_NAME_NP
58# include <dlfcn.h>
59# endif
60#endif
61#if defined(RT_OS_HAIKU)
62# include <OS.h>
63#endif
64
65#include <iprt/thread.h>
66#include <iprt/log.h>
67#include <iprt/assert.h>
68#include <iprt/asm.h>
69#include <iprt/err.h>
70#include <iprt/string.h>
71#include "internal/thread.h"
72
73
74/*******************************************************************************
75* Defined Constants And Macros *
76*******************************************************************************/
77#ifndef IN_GUEST
78/** Includes RTThreadPoke. */
79# define RTTHREAD_POSIX_WITH_POKE
80#endif
81
82
83/*******************************************************************************
84* Global Variables *
85*******************************************************************************/
86/** The pthread key in which we store the pointer to our own PRTTHREAD structure. */
87static pthread_key_t g_SelfKey;
88#ifdef RTTHREAD_POSIX_WITH_POKE
89/** The signal we use for poking threads.
90 * This is set to -1 if no available signal was found. */
91static int g_iSigPokeThread = -1;
92#endif
93
94#ifdef IPRT_MAY_HAVE_PTHREAD_SET_NAME_NP
95# if defined(RT_OS_DARWIN)
96/**
97 * The Mac OS X (10.6 and later) variant of pthread_setname_np.
98 *
99 * @returns errno.h
100 * @param pszName The new thread name.
101 */
102typedef int (*PFNPTHREADSETNAME)(const char *pszName);
103# else
104/**
105 * The variant of pthread_setname_np most other unix-like systems implement.
106 *
107 * @returns errno.h
108 * @param hThread The thread.
109 * @param pszName The new thread name.
110 */
111typedef int (*PFNPTHREADSETNAME)(pthread_t hThread, const char *pszName);
112# endif
113
114/** Pointer to pthread_setname_np if found. */
115static PFNPTHREADSETNAME g_pfnThreadSetName = NULL;
116#endif /* IPRT_MAY_HAVE_PTHREAD_SET_NAME_NP */
117
118
119/*******************************************************************************
120* Internal Functions *
121*******************************************************************************/
122static void *rtThreadNativeMain(void *pvArgs);
123static void rtThreadKeyDestruct(void *pvValue);
124static void rtThreadPosixPokeSignal(int iSignal);
125
126
127DECLHIDDEN(int) rtThreadNativeInit(void)
128{
129 /*
130 * Allocate the TLS (key in posix terms) where we store the pointer to
131 * a threads RTTHREADINT structure.
132 */
133 int rc = pthread_key_create(&g_SelfKey, rtThreadKeyDestruct);
134 if (rc)
135 return VERR_NO_TLS_FOR_SELF;
136
137#ifdef RTTHREAD_POSIX_WITH_POKE
138 /*
139 * Try register the dummy signal handler for RTThreadPoke.
140 * Avoid SIGRTMIN thru SIGRTMIN+2 because of LinuxThreads.
141 */
142 static const int s_aiSigCandidates[] =
143 {
144# ifdef SIGRTMAX
145 SIGRTMAX-3,
146 SIGRTMAX-2,
147 SIGRTMAX-1,
148# endif
149# ifndef RT_OS_SOLARIS
150 SIGUSR2,
151# endif
152 SIGWINCH
153 };
154
155 g_iSigPokeThread = -1;
156 for (unsigned iSig = 0; iSig < RT_ELEMENTS(s_aiSigCandidates); iSig++)
157 {
158 struct sigaction SigActOld;
159 if (!sigaction(s_aiSigCandidates[iSig], NULL, &SigActOld))
160 {
161 if ( SigActOld.sa_handler == SIG_DFL
162 || SigActOld.sa_handler == rtThreadPosixPokeSignal)
163 {
164 struct sigaction SigAct;
165 RT_ZERO(SigAct);
166 SigAct.sa_handler = rtThreadPosixPokeSignal;
167 SigAct.sa_flags = 0;
168 sigfillset(&SigAct.sa_mask);
169
170 /* ASSUMES no sigaction race... (lazy bird) */
171 if (!sigaction(s_aiSigCandidates[iSig], &SigAct, NULL))
172 {
173 g_iSigPokeThread = s_aiSigCandidates[iSig];
174 break;
175 }
176 AssertMsgFailed(("rc=%Rrc errno=%d\n", RTErrConvertFromErrno(errno), errno));
177 }
178 }
179 else
180 AssertMsgFailed(("rc=%Rrc errno=%d\n", RTErrConvertFromErrno(errno), errno));
181 }
182#endif /* RTTHREAD_POSIX_WITH_POKE */
183
184#ifdef IPRT_MAY_HAVE_PTHREAD_SET_NAME_NP
185 if (RT_SUCCESS(rc))
186 g_pfnThreadSetName = (PFNPTHREADSETNAME)(uintptr_t)dlsym(RTLD_DEFAULT, "pthread_setname_np");
187#endif
188 return rc;
189}
190
191
192/**
193 * Destructor called when a thread terminates.
194 * @param pvValue The key value. PRTTHREAD in our case.
195 */
196static void rtThreadKeyDestruct(void *pvValue)
197{
198 /*
199 * Deal with alien threads.
200 */
201 PRTTHREADINT pThread = (PRTTHREADINT)pvValue;
202 if (pThread->fIntFlags & RTTHREADINT_FLAGS_ALIEN)
203 {
204 pthread_setspecific(g_SelfKey, pThread);
205 rtThreadTerminate(pThread, 0);
206 pthread_setspecific(g_SelfKey, NULL);
207 }
208}
209
210
211#ifdef RTTHREAD_POSIX_WITH_POKE
212/**
213 * Dummy signal handler for the poke signal.
214 *
215 * @param iSignal The signal number.
216 */
217static void rtThreadPosixPokeSignal(int iSignal)
218{
219 Assert(iSignal == g_iSigPokeThread);
220 NOREF(iSignal);
221}
222#endif
223
224
225/**
226 * Adopts a thread, this is called immediately after allocating the
227 * thread structure.
228 *
229 * @param pThread Pointer to the thread structure.
230 */
231DECLHIDDEN(int) rtThreadNativeAdopt(PRTTHREADINT pThread)
232{
233 /*
234 * Block SIGALRM - required for timer-posix.cpp.
235 * This is done to limit harm done by OSes which doesn't do special SIGALRM scheduling.
236 * It will not help much if someone creates threads directly using pthread_create. :/
237 */
238 sigset_t SigSet;
239 sigemptyset(&SigSet);
240 sigaddset(&SigSet, SIGALRM);
241 sigprocmask(SIG_BLOCK, &SigSet, NULL);
242#ifdef RTTHREAD_POSIX_WITH_POKE
243 if (g_iSigPokeThread != -1)
244 siginterrupt(g_iSigPokeThread, 1);
245#endif
246
247 int rc = pthread_setspecific(g_SelfKey, pThread);
248 if (!rc)
249 return VINF_SUCCESS;
250 return VERR_FAILED_TO_SET_SELF_TLS;
251}
252
253
254DECLHIDDEN(void) rtThreadNativeDestroy(PRTTHREADINT pThread)
255{
256 if (pThread == (PRTTHREADINT)pthread_getspecific(g_SelfKey))
257 pthread_setspecific(g_SelfKey, NULL);
258}
259
260
261/**
262 * Wrapper which unpacks the params and calls thread function.
263 */
264static void *rtThreadNativeMain(void *pvArgs)
265{
266 PRTTHREADINT pThread = (PRTTHREADINT)pvArgs;
267 pthread_t Self = pthread_self();
268 Assert((uintptr_t)Self == (RTNATIVETHREAD)Self && (uintptr_t)Self != NIL_RTNATIVETHREAD);
269
270#if defined(RT_OS_LINUX)
271 /*
272 * Set the TID.
273 */
274 pThread->tid = syscall(__NR_gettid);
275 ASMMemoryFence();
276#endif
277
278 /*
279 * Block SIGALRM - required for timer-posix.cpp.
280 * This is done to limit harm done by OSes which doesn't do special SIGALRM scheduling.
281 * It will not help much if someone creates threads directly using pthread_create. :/
282 */
283 sigset_t SigSet;
284 sigemptyset(&SigSet);
285 sigaddset(&SigSet, SIGALRM);
286 sigprocmask(SIG_BLOCK, &SigSet, NULL);
287#ifdef RTTHREAD_POSIX_WITH_POKE
288 if (g_iSigPokeThread != -1)
289 siginterrupt(g_iSigPokeThread, 1);
290#endif
291
292 /*
293 * Set the TLS entry and, if possible, the thread name.
294 */
295 int rc = pthread_setspecific(g_SelfKey, pThread);
296 AssertReleaseMsg(!rc, ("failed to set self TLS. rc=%d thread '%s'\n", rc, pThread->szName));
297
298#ifdef IPRT_MAY_HAVE_PTHREAD_SET_NAME_NP
299 if (g_pfnThreadSetName)
300# ifdef RT_OS_DARWIN
301 g_pfnThreadSetName(pThread->szName);
302# else
303 g_pfnThreadSetName(Self, pThread->szName);
304# endif
305#endif
306
307 /*
308 * Call common main.
309 */
310 rc = rtThreadMain(pThread, (uintptr_t)Self, &pThread->szName[0]);
311
312 pthread_setspecific(g_SelfKey, NULL);
313 pthread_exit((void *)(intptr_t)rc);
314 return (void *)(intptr_t)rc;
315}
316
317
318DECLHIDDEN(int) rtThreadNativeCreate(PRTTHREADINT pThread, PRTNATIVETHREAD pNativeThread)
319{
320 /*
321 * Set the default stack size.
322 */
323 if (!pThread->cbStack)
324 pThread->cbStack = 512*1024;
325
326#ifdef RT_OS_LINUX
327 pThread->tid = -1;
328#endif
329
330 /*
331 * Setup thread attributes.
332 */
333 pthread_attr_t ThreadAttr;
334 int rc = pthread_attr_init(&ThreadAttr);
335 if (!rc)
336 {
337 rc = pthread_attr_setdetachstate(&ThreadAttr, PTHREAD_CREATE_DETACHED);
338 if (!rc)
339 {
340 rc = pthread_attr_setstacksize(&ThreadAttr, pThread->cbStack);
341 if (!rc)
342 {
343 /*
344 * Create the thread.
345 */
346 pthread_t ThreadId;
347 rc = pthread_create(&ThreadId, &ThreadAttr, rtThreadNativeMain, pThread);
348 if (!rc)
349 {
350 *pNativeThread = (uintptr_t)ThreadId;
351 return VINF_SUCCESS;
352 }
353 }
354 }
355 pthread_attr_destroy(&ThreadAttr);
356 }
357 return RTErrConvertFromErrno(rc);
358}
359
360
361RTDECL(RTTHREAD) RTThreadSelf(void)
362{
363 PRTTHREADINT pThread = (PRTTHREADINT)pthread_getspecific(g_SelfKey);
364 /** @todo import alien threads? */
365 return pThread;
366}
367
368
369#ifdef RTTHREAD_POSIX_WITH_POKE
370RTDECL(int) RTThreadPoke(RTTHREAD hThread)
371{
372 AssertReturn(hThread != RTThreadSelf(), VERR_INVALID_PARAMETER);
373 PRTTHREADINT pThread = rtThreadGet(hThread);
374 AssertReturn(pThread, VERR_INVALID_HANDLE);
375
376 int rc;
377 if (g_iSigPokeThread != -1)
378 {
379 rc = pthread_kill((pthread_t)(uintptr_t)pThread->Core.Key, g_iSigPokeThread);
380 rc = RTErrConvertFromErrno(rc);
381 }
382 else
383 rc = VERR_NOT_SUPPORTED;
384
385 rtThreadRelease(pThread);
386 return rc;
387}
388#endif
389
390/** @todo move this into platform specific files. */
391RTR3DECL(int) RTThreadGetExecutionTimeMilli(uint64_t *pKernelTime, uint64_t *pUserTime)
392{
393#if defined(RT_OS_SOLARIS)
394 struct rusage ts;
395 int rc = getrusage(RUSAGE_LWP, &ts);
396 if (rc)
397 return RTErrConvertFromErrno(rc);
398
399 *pKernelTime = ts.ru_stime.tv_sec * 1000 + ts.ru_stime.tv_usec / 1000;
400 *pUserTime = ts.ru_utime.tv_sec * 1000 + ts.ru_utime.tv_usec / 1000;
401 return VINF_SUCCESS;
402
403#elif defined(RT_OS_LINUX) || defined(RT_OS_FREEBSD)
404 /* on Linux, getrusage(RUSAGE_THREAD, ...) is available since 2.6.26 */
405 struct timespec ts;
406 int rc = clock_gettime(CLOCK_THREAD_CPUTIME_ID, &ts);
407 if (rc)
408 return RTErrConvertFromErrno(rc);
409
410 *pKernelTime = 0;
411 *pUserTime = (uint64_t)ts.tv_sec * 1000 + ts.tv_nsec / 1000000;
412 return VINF_SUCCESS;
413
414#elif defined(RT_OS_DARWIN)
415 thread_basic_info ThreadInfo;
416 mach_msg_type_number_t Count = THREAD_BASIC_INFO_COUNT;
417 kern_return_t krc = thread_info(mach_thread_self(), THREAD_BASIC_INFO, (thread_info_t)&ThreadInfo, &Count);
418 AssertReturn(krc == KERN_SUCCESS, RTErrConvertFromDarwinKern(krc));
419
420 *pKernelTime = ThreadInfo.system_time.seconds * 1000 + ThreadInfo.system_time.microseconds / 1000;
421 *pUserTime = ThreadInfo.user_time.seconds * 1000 + ThreadInfo.user_time.microseconds / 1000;
422
423 return VINF_SUCCESS;
424#elif defined(RT_OS_HAIKU)
425 thread_info ThreadInfo;
426 status_t status = get_thread_info(find_thread(NULL), &ThreadInfo);
427 AssertReturn(status == B_OK, RTErrConvertFromErrno(status));
428
429 *pKernelTime = ThreadInfo.kernel_time / 1000;
430 *pUserTime = ThreadInfo.user_time / 1000;
431
432 return VINF_SUCCESS;
433#else
434 return VERR_NOT_IMPLEMENTED;
435#endif
436}
437
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