VirtualBox

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

Last change on this file since 86727 was 86727, checked in by vboxsync, 4 years ago

IPRT/thread-posix.cpp: siginterrupt is deprecated, replace with sigaction. [build fix]

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id Revision
File size: 21.8 KB
Line 
1/* $Id: thread-posix.cpp 86727 2020-10-28 10:14:40Z vboxsync $ */
2/** @file
3 * IPRT - Threads, POSIX.
4 */
5
6/*
7 * Copyright (C) 2006-2020 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#include <stdlib.h>
36#if defined(RT_OS_LINUX)
37# include <unistd.h>
38# include <sys/syscall.h>
39#endif
40#if defined(RT_OS_SOLARIS)
41# include <sched.h>
42# include <sys/resource.h>
43#endif
44#if defined(RT_OS_DARWIN)
45# include <mach/thread_act.h>
46# include <mach/thread_info.h>
47# include <mach/host_info.h>
48# include <mach/mach_init.h>
49# include <mach/mach_host.h>
50#endif
51#if defined(RT_OS_DARWIN) /*|| defined(RT_OS_FREEBSD) - later */ \
52 || (defined(RT_OS_LINUX) && !defined(IN_RT_STATIC) /* static + dlsym = trouble */) \
53 || defined(IPRT_MAY_HAVE_PTHREAD_SET_NAME_NP)
54# define IPRT_MAY_HAVE_PTHREAD_SET_NAME_NP
55# include <dlfcn.h>
56#endif
57#if defined(RT_OS_HAIKU)
58# include <OS.h>
59#endif
60
61#include <iprt/thread.h>
62#include <iprt/log.h>
63#include <iprt/assert.h>
64#include <iprt/asm.h>
65#include <iprt/err.h>
66#include <iprt/initterm.h>
67#include <iprt/string.h>
68#include <iprt/semaphore.h>
69#include <iprt/list.h>
70#include <iprt/once.h>
71#include <iprt/critsect.h>
72#include <iprt/req.h>
73#include "internal/thread.h"
74
75
76/*********************************************************************************************************************************
77* Defined Constants And Macros *
78*********************************************************************************************************************************/
79#ifndef IN_GUEST
80/** Includes RTThreadPoke. */
81# define RTTHREAD_POSIX_WITH_POKE
82#endif
83
84
85/*********************************************************************************************************************************
86* Global Variables *
87*********************************************************************************************************************************/
88/** The pthread key in which we store the pointer to our own PRTTHREAD structure. */
89static pthread_key_t g_SelfKey;
90#ifdef RTTHREAD_POSIX_WITH_POKE
91/** The signal we use for poking threads.
92 * This is set to -1 if no available signal was found. */
93static int g_iSigPokeThread = -1;
94#endif
95
96#ifdef IPRT_MAY_HAVE_PTHREAD_SET_NAME_NP
97# if defined(RT_OS_DARWIN)
98/**
99 * The Mac OS X (10.6 and later) variant of pthread_setname_np.
100 *
101 * @returns errno.h
102 * @param pszName The new thread name.
103 */
104typedef int (*PFNPTHREADSETNAME)(const char *pszName);
105# else
106/**
107 * The variant of pthread_setname_np most other unix-like systems implement.
108 *
109 * @returns errno.h
110 * @param hThread The thread.
111 * @param pszName The new thread name.
112 */
113typedef int (*PFNPTHREADSETNAME)(pthread_t hThread, const char *pszName);
114# endif
115
116/** Pointer to pthread_setname_np if found. */
117static PFNPTHREADSETNAME g_pfnThreadSetName = NULL;
118#endif /* IPRT_MAY_HAVE_PTHREAD_SET_NAME_NP */
119
120#ifdef RTTHREAD_POSIX_WITH_CREATE_PRIORITY_PROXY
121/** Atomic indicator of whether the priority proxy thread has been (attempted) started.
122 *
123 * The priority proxy thread is started under these circumstances:
124 * - RTThreadCreate
125 * - RTThreadSetType
126 * - RTProcSetPriority
127 *
128 * Which means that we'll be single threaded when this is modified.
129 *
130 * Speical values:
131 * - VERR_TRY_AGAIN: Not yet started.
132 * - VERR_WRONG_ORDER: Starting.
133 * - VINF_SUCCESS: Started successfully.
134 * - VERR_PROCESS_NOT_FOUND: Stopping or stopped
135 * - Other error status if failed to start.
136 *
137 * @note We could potentially optimize this by only start it when we lower the
138 * priority of ourselves, the process, or a newly created thread. But
139 * that would means we would need to take multi-threading into account, so
140 * let's not do that for now.
141 */
142static int32_t volatile g_rcPriorityProxyThreadStart = VERR_TRY_AGAIN;
143/** The IPRT thread handle for the priority proxy. */
144static RTTHREAD g_hRTThreadPosixPriorityProxyThread = NIL_RTTHREAD;
145/** The priority proxy queue. */
146static RTREQQUEUE g_hRTThreadPosixPriorityProxyQueue = NIL_RTREQQUEUE;
147#endif /* RTTHREAD_POSIX_WITH_CREATE_PRIORITY_PROXY */
148
149
150/*********************************************************************************************************************************
151* Internal Functions *
152*********************************************************************************************************************************/
153static void *rtThreadNativeMain(void *pvArgs);
154static void rtThreadKeyDestruct(void *pvValue);
155#ifdef RTTHREAD_POSIX_WITH_POKE
156static void rtThreadPosixPokeSignal(int iSignal);
157#endif
158
159
160#ifdef RTTHREAD_POSIX_WITH_POKE
161/**
162 * Try register the dummy signal handler for RTThreadPoke.
163 */
164static void rtThreadPosixSelectPokeSignal(void)
165{
166 /*
167 * Note! Avoid SIGRTMIN thru SIGRTMIN+2 because of LinuxThreads.
168 */
169 static const int s_aiSigCandidates[] =
170 {
171# ifdef SIGRTMAX
172 SIGRTMAX-3,
173 SIGRTMAX-2,
174 SIGRTMAX-1,
175# endif
176# ifndef RT_OS_SOLARIS
177 SIGUSR2,
178# endif
179 SIGWINCH
180 };
181
182 g_iSigPokeThread = -1;
183 if (!RTR3InitIsUnobtrusive())
184 {
185 for (unsigned iSig = 0; iSig < RT_ELEMENTS(s_aiSigCandidates); iSig++)
186 {
187 struct sigaction SigActOld;
188 if (!sigaction(s_aiSigCandidates[iSig], NULL, &SigActOld))
189 {
190 if ( SigActOld.sa_handler == SIG_DFL
191 || SigActOld.sa_handler == rtThreadPosixPokeSignal)
192 {
193 struct sigaction SigAct;
194 RT_ZERO(SigAct);
195 SigAct.sa_handler = rtThreadPosixPokeSignal;
196 SigAct.sa_flags = 0; /* no SA_RESTART! */
197 sigfillset(&SigAct.sa_mask);
198
199 /* ASSUMES no sigaction race... (lazy bird) */
200 if (!sigaction(s_aiSigCandidates[iSig], &SigAct, NULL))
201 {
202 g_iSigPokeThread = s_aiSigCandidates[iSig];
203 break;
204 }
205 AssertMsgFailed(("rc=%Rrc errno=%d\n", RTErrConvertFromErrno(errno), errno));
206 }
207 }
208 else
209 AssertMsgFailed(("rc=%Rrc errno=%d\n", RTErrConvertFromErrno(errno), errno));
210 }
211 }
212}
213#endif /* RTTHREAD_POSIX_WITH_POKE */
214
215
216DECLHIDDEN(int) rtThreadNativeInit(void)
217{
218 /*
219 * Allocate the TLS (key in posix terms) where we store the pointer to
220 * a threads RTTHREADINT structure.
221 */
222 int rc = pthread_key_create(&g_SelfKey, rtThreadKeyDestruct);
223 if (rc)
224 return VERR_NO_TLS_FOR_SELF;
225
226#ifdef RTTHREAD_POSIX_WITH_POKE
227 rtThreadPosixSelectPokeSignal();
228#endif
229
230#ifdef IPRT_MAY_HAVE_PTHREAD_SET_NAME_NP
231 if (RT_SUCCESS(rc))
232 g_pfnThreadSetName = (PFNPTHREADSETNAME)(uintptr_t)dlsym(RTLD_DEFAULT, "pthread_setname_np");
233#endif
234 return rc;
235}
236
237static void rtThreadPosixBlockSignals(void)
238{
239 /*
240 * Block SIGALRM - required for timer-posix.cpp.
241 * This is done to limit harm done by OSes which doesn't do special SIGALRM scheduling.
242 * It will not help much if someone creates threads directly using pthread_create. :/
243 */
244 if (!RTR3InitIsUnobtrusive())
245 {
246 sigset_t SigSet;
247 sigemptyset(&SigSet);
248 sigaddset(&SigSet, SIGALRM);
249 sigprocmask(SIG_BLOCK, &SigSet, NULL);
250 }
251
252#ifdef RTTHREAD_POSIX_WITH_POKE
253 /*
254 * bird 2020-10-28: Not entirely sure we do this, but it makes sure the signal works
255 * on the new thread. Probably some pre-NPTL linux reasons.
256 */
257 if (g_iSigPokeThread != -1)
258 {
259# if 1 /* siginterrupt() is typically implemented as two sigaction calls, this should be faster and w/o deprecations: */
260 struct sigaction SigActOld;
261 RT_ZERO(SigActOld);
262
263 struct sigaction SigAct;
264 RT_ZERO(SigAct);
265 SigAct.sa_handler = rtThreadPosixPokeSignal;
266 SigAct.sa_flags = 0; /* no SA_RESTART! */
267 sigfillset(&SigAct.sa_mask);
268
269 int rc = sigaction(g_iSigPokeThread, &SigAct, &SigActOld);
270 AssertMsg(rc == 0, ("rc=%Rrc errno=%d\n", RTErrConvertFromErrno(errno), errno)); RT_NOREF(rc);
271 AssertMsg(rc || SigActOld.sa_handler == rtThreadPosixPokeSignal, ("%p\n", SigActOld.sa_handler));
272# else
273 siginterrupt(g_iSigPokeThread, 1);
274# endif
275 }
276#endif
277}
278
279DECLHIDDEN(void) rtThreadNativeReInitObtrusive(void)
280{
281#ifdef RTTHREAD_POSIX_WITH_POKE
282 Assert(!RTR3InitIsUnobtrusive());
283 rtThreadPosixSelectPokeSignal();
284#endif
285 rtThreadPosixBlockSignals();
286}
287
288
289/**
290 * Destructor called when a thread terminates.
291 * @param pvValue The key value. PRTTHREAD in our case.
292 */
293static void rtThreadKeyDestruct(void *pvValue)
294{
295 /*
296 * Deal with alien threads.
297 */
298 PRTTHREADINT pThread = (PRTTHREADINT)pvValue;
299 if (pThread->fIntFlags & RTTHREADINT_FLAGS_ALIEN)
300 {
301 pthread_setspecific(g_SelfKey, pThread);
302 rtThreadTerminate(pThread, 0);
303 pthread_setspecific(g_SelfKey, NULL);
304 }
305}
306
307
308#ifdef RTTHREAD_POSIX_WITH_POKE
309/**
310 * Dummy signal handler for the poke signal.
311 *
312 * @param iSignal The signal number.
313 */
314static void rtThreadPosixPokeSignal(int iSignal)
315{
316 Assert(iSignal == g_iSigPokeThread);
317 NOREF(iSignal);
318}
319#endif
320
321
322/**
323 * Adopts a thread, this is called immediately after allocating the
324 * thread structure.
325 *
326 * @param pThread Pointer to the thread structure.
327 */
328DECLHIDDEN(int) rtThreadNativeAdopt(PRTTHREADINT pThread)
329{
330 rtThreadPosixBlockSignals();
331
332 int rc = pthread_setspecific(g_SelfKey, pThread);
333 if (!rc)
334 return VINF_SUCCESS;
335 return VERR_FAILED_TO_SET_SELF_TLS;
336}
337
338
339DECLHIDDEN(void) rtThreadNativeDestroy(PRTTHREADINT pThread)
340{
341 if (pThread == (PRTTHREADINT)pthread_getspecific(g_SelfKey))
342 pthread_setspecific(g_SelfKey, NULL);
343}
344
345
346/**
347 * Wrapper which unpacks the params and calls thread function.
348 */
349static void *rtThreadNativeMain(void *pvArgs)
350{
351 PRTTHREADINT pThread = (PRTTHREADINT)pvArgs;
352 pthread_t Self = pthread_self();
353 Assert((uintptr_t)Self != NIL_RTNATIVETHREAD);
354 Assert(Self == (pthread_t)(RTNATIVETHREAD)Self);
355
356#if defined(RT_OS_LINUX)
357 /*
358 * Set the TID.
359 */
360 pThread->tid = syscall(__NR_gettid);
361 ASMMemoryFence();
362#endif
363
364 rtThreadPosixBlockSignals();
365
366 /*
367 * Set the TLS entry and, if possible, the thread name.
368 */
369 int rc = pthread_setspecific(g_SelfKey, pThread);
370 AssertReleaseMsg(!rc, ("failed to set self TLS. rc=%d thread '%s'\n", rc, pThread->szName));
371
372#ifdef IPRT_MAY_HAVE_PTHREAD_SET_NAME_NP
373 if (g_pfnThreadSetName)
374# ifdef RT_OS_DARWIN
375 g_pfnThreadSetName(pThread->szName);
376# else
377 g_pfnThreadSetName(Self, pThread->szName);
378# endif
379#endif
380
381 /*
382 * Call common main.
383 */
384 rc = rtThreadMain(pThread, (uintptr_t)Self, &pThread->szName[0]);
385
386 pthread_setspecific(g_SelfKey, NULL);
387 pthread_exit((void *)(intptr_t)rc);
388 return (void *)(intptr_t)rc;
389}
390
391#ifdef RTTHREAD_POSIX_WITH_CREATE_PRIORITY_PROXY
392
393/**
394 * @callback_method_impl{FNRTTHREAD,
395 * Priority proxy thread that services g_hRTThreadPosixPriorityProxyQueue.}
396 */
397static DECLCALLBACK(int) rtThreadPosixPriorityProxyThread(PRTTHREADINT, void *)
398{
399 for (;;)
400 {
401 RTREQQUEUE hReqQueue = g_hRTThreadPosixPriorityProxyQueue;
402 if (hReqQueue != NIL_RTREQQUEUE)
403 RTReqQueueProcess(hReqQueue, RT_INDEFINITE_WAIT);
404 else
405 break;
406
407 int32_t rc = ASMAtomicUoReadS32(&g_rcPriorityProxyThreadStart);
408 if (rc != VINF_SUCCESS && rc != VERR_WRONG_ORDER)
409 break;
410 }
411
412 return VINF_SUCCESS;
413}
414
415
416/**
417 * Just returns a non-success status codes to force the thread to re-evaluate
418 * the global shutdown variable.
419 */
420static DECLCALLBACK(int) rtThreadPosixPriorityProxyStopper(void)
421{
422 return VERR_CANCELLED;
423}
424
425
426/**
427 * An atexit() callback that stops the proxy creation/priority thread.
428 */
429static void rtThreadStopProxyThread(void)
430{
431 /*
432 * Signal to the thread that it's time to shut down.
433 */
434 int32_t rc = ASMAtomicXchgS32(&g_rcPriorityProxyThreadStart, VERR_PROCESS_NOT_FOUND);
435 if (RT_SUCCESS(rc))
436 {
437 /*
438 * Grab the associated handles.
439 */
440 RTTHREAD hThread = g_hRTThreadPosixPriorityProxyThread;
441 RTREQQUEUE hQueue = g_hRTThreadPosixPriorityProxyQueue;
442 g_hRTThreadPosixPriorityProxyQueue = NIL_RTREQQUEUE;
443 g_hRTThreadPosixPriorityProxyThread = NIL_RTTHREAD;
444 ASMCompilerBarrier(); /* paranoia */
445
446 AssertReturnVoid(hThread != NIL_RTTHREAD);
447 AssertReturnVoid(hQueue != NIL_RTREQQUEUE);
448
449 /*
450 * Kick the thread so it gets out of any pending RTReqQueueProcess call ASAP.
451 */
452 rc = RTReqQueueCallEx(hQueue, NULL, 0 /*cMillies*/, RTREQFLAGS_IPRT_STATUS | RTREQFLAGS_NO_WAIT,
453 (PFNRT)rtThreadPosixPriorityProxyStopper, 0);
454
455 /*
456 * Wait for the thread to complete.
457 */
458 rc = RTThreadWait(hThread, RT_SUCCESS(rc) ? RT_MS_1SEC * 5 : 32, NULL);
459 if (RT_SUCCESS(rc))
460 RTReqQueueDestroy(hQueue);
461 /* else: just leak the stuff, we're exitting, so nobody cares... */
462 }
463}
464
465
466/**
467 * Ensure that the proxy priority proxy thread has been started.
468 *
469 * Since we will always start a proxy thread when asked to create a thread,
470 * there is no need for serialization here.
471 *
472 * @retval true if started
473 * @retval false if it failed to start (caller must handle this scenario).
474 */
475DECLHIDDEN(bool) rtThreadPosixPriorityProxyStart(void)
476{
477 /*
478 * Read the result.
479 */
480 int rc = ASMAtomicUoReadS32(&g_rcPriorityProxyThreadStart);
481 if (rc != VERR_TRY_AGAIN)
482 return RT_SUCCESS(rc);
483
484 /* If this triggers then there is a very unexpected race somewhere. It
485 should be harmless though. */
486 AssertReturn(ASMAtomicCmpXchgS32(&g_rcPriorityProxyThreadStart, VERR_WRONG_ORDER, VERR_TRY_AGAIN), false);
487
488 /*
489 * Not yet started, so do that.
490 */
491 rc = RTReqQueueCreate(&g_hRTThreadPosixPriorityProxyQueue);
492 if (RT_SUCCESS(rc))
493 {
494 rc = RTThreadCreate(&g_hRTThreadPosixPriorityProxyThread, rtThreadPosixPriorityProxyThread, NULL, 0 /*cbStack*/,
495 RTTHREADTYPE_DEFAULT, RTTHREADFLAGS_WAITABLE, "RTThrdPP");
496 if (RT_SUCCESS(rc))
497 {
498 ASMAtomicWriteS32(&g_rcPriorityProxyThreadStart, VINF_SUCCESS);
499
500 atexit(rtThreadStopProxyThread);
501 return true;
502 }
503 RTReqQueueCreate(&g_hRTThreadPosixPriorityProxyQueue);
504 }
505 ASMAtomicWriteS32(&g_rcPriorityProxyThreadStart, rc != VERR_WRONG_ORDER ? rc : VERR_PROCESS_NOT_FOUND);
506 return false;
507}
508
509
510/**
511 * Calls @a pfnFunction from the priority proxy thread.
512 *
513 * Caller must have called rtThreadPosixStartProxy() to check that the priority
514 * proxy thread is running.
515 *
516 * @returns
517 * @param pTargetThread The target thread, NULL if not applicable. This is
518 * so we can skip calls pertaining to the priority
519 * proxy thread itself.
520 * @param pfnFunction The function to call. Must return IPRT status code.
521 * @param cArgs Number of arguments (see also RTReqQueueCall).
522 * @param ... Arguments (see also RTReqQueueCall).
523 */
524DECLHIDDEN(int) rtThreadPosixPriorityProxyCall(PRTTHREADINT pTargetThread, PFNRT pfnFunction, int cArgs, ...)
525{
526 int rc;
527 if ( !pTargetThread
528 || pTargetThread->pfnThread != rtThreadPosixPriorityProxyThread)
529 {
530 va_list va;
531 va_start(va, cArgs);
532 PRTREQ pReq;
533 rc = RTReqQueueCallV(g_hRTThreadPosixPriorityProxyQueue, &pReq, RT_INDEFINITE_WAIT, RTREQFLAGS_IPRT_STATUS,
534 pfnFunction, cArgs, va);
535 va_end(va);
536 RTReqRelease(pReq);
537 }
538 else
539 rc = VINF_SUCCESS;
540 return rc;
541}
542
543#endif /* !RTTHREAD_POSIX_WITH_CREATE_PRIORITY_PROXY */
544
545/**
546 * Worker for rtThreadNativeCreate that's either called on the priority proxy
547 * thread or directly on the calling thread depending on the proxy state.
548 */
549static DECLCALLBACK(int) rtThreadNativeInternalCreate(PRTTHREADINT pThread, PRTNATIVETHREAD pNativeThread)
550{
551 /*
552 * Set the default stack size.
553 */
554 if (!pThread->cbStack)
555 pThread->cbStack = 512*1024;
556
557#ifdef RT_OS_LINUX
558 pThread->tid = -1;
559#endif
560
561 /*
562 * Setup thread attributes.
563 */
564 pthread_attr_t ThreadAttr;
565 int rc = pthread_attr_init(&ThreadAttr);
566 if (!rc)
567 {
568 rc = pthread_attr_setdetachstate(&ThreadAttr, PTHREAD_CREATE_DETACHED);
569 if (!rc)
570 {
571 rc = pthread_attr_setstacksize(&ThreadAttr, pThread->cbStack);
572 if (!rc)
573 {
574 /*
575 * Create the thread.
576 */
577 pthread_t ThreadId;
578 rc = pthread_create(&ThreadId, &ThreadAttr, rtThreadNativeMain, pThread);
579 if (!rc)
580 {
581 pthread_attr_destroy(&ThreadAttr);
582 *pNativeThread = (uintptr_t)ThreadId;
583 return VINF_SUCCESS;
584 }
585 }
586 }
587 pthread_attr_destroy(&ThreadAttr);
588 }
589 return RTErrConvertFromErrno(rc);
590}
591
592
593DECLHIDDEN(int) rtThreadNativeCreate(PRTTHREADINT pThread, PRTNATIVETHREAD pNativeThread)
594{
595#ifdef RTTHREAD_POSIX_WITH_CREATE_PRIORITY_PROXY
596 /*
597 * If we have a priority proxy thread, use it. Make sure to ignore the
598 * staring of the proxy thread itself.
599 */
600 if ( pThread->pfnThread != rtThreadPosixPriorityProxyThread
601 && rtThreadPosixPriorityProxyStart())
602 {
603 PRTREQ pReq;
604 int rc = RTReqQueueCall(g_hRTThreadPosixPriorityProxyQueue, &pReq, RT_INDEFINITE_WAIT,
605 (PFNRT)rtThreadNativeInternalCreate, 2, pThread, pNativeThread);
606 RTReqRelease(pReq);
607 return rc;
608 }
609
610 /*
611 * Fall back on creating it directly without regard to priority proxying.
612 */
613#endif
614 return rtThreadNativeInternalCreate(pThread, pNativeThread);
615}
616
617
618RTDECL(RTTHREAD) RTThreadSelf(void)
619{
620 PRTTHREADINT pThread = (PRTTHREADINT)pthread_getspecific(g_SelfKey);
621 /** @todo import alien threads? */
622 return pThread;
623}
624
625
626#ifdef RTTHREAD_POSIX_WITH_POKE
627RTDECL(int) RTThreadPoke(RTTHREAD hThread)
628{
629 AssertReturn(hThread != RTThreadSelf(), VERR_INVALID_PARAMETER);
630 PRTTHREADINT pThread = rtThreadGet(hThread);
631 AssertReturn(pThread, VERR_INVALID_HANDLE);
632
633 int rc;
634 if (g_iSigPokeThread != -1)
635 {
636 rc = pthread_kill((pthread_t)(uintptr_t)pThread->Core.Key, g_iSigPokeThread);
637 rc = RTErrConvertFromErrno(rc);
638 }
639 else
640 rc = VERR_NOT_SUPPORTED;
641
642 rtThreadRelease(pThread);
643 return rc;
644}
645#endif
646
647/** @todo move this into platform specific files. */
648RTR3DECL(int) RTThreadGetExecutionTimeMilli(uint64_t *pKernelTime, uint64_t *pUserTime)
649{
650#if defined(RT_OS_SOLARIS)
651 struct rusage ts;
652 int rc = getrusage(RUSAGE_LWP, &ts);
653 if (rc)
654 return RTErrConvertFromErrno(rc);
655
656 *pKernelTime = ts.ru_stime.tv_sec * 1000 + ts.ru_stime.tv_usec / 1000;
657 *pUserTime = ts.ru_utime.tv_sec * 1000 + ts.ru_utime.tv_usec / 1000;
658 return VINF_SUCCESS;
659
660#elif defined(RT_OS_LINUX) || defined(RT_OS_FREEBSD)
661 /* on Linux, getrusage(RUSAGE_THREAD, ...) is available since 2.6.26 */
662 struct timespec ts;
663 int rc = clock_gettime(CLOCK_THREAD_CPUTIME_ID, &ts);
664 if (rc)
665 return RTErrConvertFromErrno(rc);
666
667 *pKernelTime = 0;
668 *pUserTime = (uint64_t)ts.tv_sec * 1000 + ts.tv_nsec / 1000000;
669 return VINF_SUCCESS;
670
671#elif defined(RT_OS_DARWIN)
672 thread_basic_info ThreadInfo;
673 mach_msg_type_number_t Count = THREAD_BASIC_INFO_COUNT;
674 kern_return_t krc = thread_info(mach_thread_self(), THREAD_BASIC_INFO, (thread_info_t)&ThreadInfo, &Count);
675 AssertReturn(krc == KERN_SUCCESS, RTErrConvertFromDarwinKern(krc));
676
677 *pKernelTime = ThreadInfo.system_time.seconds * 1000 + ThreadInfo.system_time.microseconds / 1000;
678 *pUserTime = ThreadInfo.user_time.seconds * 1000 + ThreadInfo.user_time.microseconds / 1000;
679
680 return VINF_SUCCESS;
681#elif defined(RT_OS_HAIKU)
682 thread_info ThreadInfo;
683 status_t status = get_thread_info(find_thread(NULL), &ThreadInfo);
684 AssertReturn(status == B_OK, RTErrConvertFromErrno(status));
685
686 *pKernelTime = ThreadInfo.kernel_time / 1000;
687 *pUserTime = ThreadInfo.user_time / 1000;
688
689 return VINF_SUCCESS;
690#else
691 return VERR_NOT_IMPLEMENTED;
692#endif
693}
694
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