VirtualBox

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

Last change on this file since 90330 was 89870, checked in by vboxsync, 3 years ago

IPRT/thread-posix.cpp: On Solaris SIGRTMAX is defined to a function call, so avoid using it in a 'static const' table or will end up with the compiler serializing the initialization and the GA build failing. bugref:10008

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id Revision
File size: 22.2 KB
Line 
1/* $Id: thread-posix.cpp 89870 2021-06-23 20:51:30Z 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 - shouldn't need to exclude this now with the non-obtrusive init option. */
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 volatile 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# if !defined(RT_OS_LINUX) && !defined(RT_OS_SOLARIS) /* glibc defines SIGRTMAX to __libc_current_sigrtmax() and Solaris libc defines it relying on _sysconf(), causing compiler to deploy serialization here. */
170 static
171# endif
172 const int s_aiSigCandidates[] =
173 {
174# ifdef SIGRTMAX
175 SIGRTMAX-3,
176 SIGRTMAX-2,
177 SIGRTMAX-1,
178# endif
179# ifndef RT_OS_SOLARIS
180 SIGUSR2,
181# endif
182 SIGWINCH
183 };
184
185 g_iSigPokeThread = -1;
186 if (!RTR3InitIsUnobtrusive())
187 {
188 for (unsigned iSig = 0; iSig < RT_ELEMENTS(s_aiSigCandidates); iSig++)
189 {
190 struct sigaction SigActOld;
191 if (!sigaction(s_aiSigCandidates[iSig], NULL, &SigActOld))
192 {
193 if ( SigActOld.sa_handler == SIG_DFL
194 || SigActOld.sa_handler == rtThreadPosixPokeSignal)
195 {
196 struct sigaction SigAct;
197 RT_ZERO(SigAct);
198 SigAct.sa_handler = rtThreadPosixPokeSignal;
199 SigAct.sa_flags = 0; /* no SA_RESTART! */
200 sigfillset(&SigAct.sa_mask);
201
202 /* ASSUMES no sigaction race... (lazy bird) */
203 if (!sigaction(s_aiSigCandidates[iSig], &SigAct, NULL))
204 {
205 g_iSigPokeThread = s_aiSigCandidates[iSig];
206 break;
207 }
208 AssertMsgFailed(("rc=%Rrc errno=%d\n", RTErrConvertFromErrno(errno), errno));
209 }
210 }
211 else
212 AssertMsgFailed(("rc=%Rrc errno=%d\n", RTErrConvertFromErrno(errno), errno));
213 }
214 }
215}
216#endif /* RTTHREAD_POSIX_WITH_POKE */
217
218
219DECLHIDDEN(int) rtThreadNativeInit(void)
220{
221 /*
222 * Allocate the TLS (key in posix terms) where we store the pointer to
223 * a threads RTTHREADINT structure.
224 */
225 int rc = pthread_key_create(&g_SelfKey, rtThreadKeyDestruct);
226 if (rc)
227 return VERR_NO_TLS_FOR_SELF;
228
229#ifdef RTTHREAD_POSIX_WITH_POKE
230 rtThreadPosixSelectPokeSignal();
231#endif
232
233#ifdef IPRT_MAY_HAVE_PTHREAD_SET_NAME_NP
234 if (RT_SUCCESS(rc))
235 g_pfnThreadSetName = (PFNPTHREADSETNAME)(uintptr_t)dlsym(RTLD_DEFAULT, "pthread_setname_np");
236#endif
237 return rc;
238}
239
240static void rtThreadPosixBlockSignals(void)
241{
242 /*
243 * Block SIGALRM - required for timer-posix.cpp.
244 * This is done to limit harm done by OSes which doesn't do special SIGALRM scheduling.
245 * It will not help much if someone creates threads directly using pthread_create. :/
246 */
247 if (!RTR3InitIsUnobtrusive())
248 {
249 sigset_t SigSet;
250 sigemptyset(&SigSet);
251 sigaddset(&SigSet, SIGALRM);
252 sigprocmask(SIG_BLOCK, &SigSet, NULL);
253 }
254
255#ifdef RTTHREAD_POSIX_WITH_POKE
256 /*
257 * bird 2020-10-28: Not entirely sure we do this, but it makes sure the signal works
258 * on the new thread. Probably some pre-NPTL linux reasons.
259 */
260 if (g_iSigPokeThread != -1)
261 {
262# if 1 /* siginterrupt() is typically implemented as two sigaction calls, this should be faster and w/o deprecations: */
263 struct sigaction SigActOld;
264 RT_ZERO(SigActOld);
265
266 struct sigaction SigAct;
267 RT_ZERO(SigAct);
268 SigAct.sa_handler = rtThreadPosixPokeSignal;
269 SigAct.sa_flags = 0; /* no SA_RESTART! */
270 sigfillset(&SigAct.sa_mask);
271
272 int rc = sigaction(g_iSigPokeThread, &SigAct, &SigActOld);
273 AssertMsg(rc == 0, ("rc=%Rrc errno=%d\n", RTErrConvertFromErrno(errno), errno)); RT_NOREF(rc);
274 AssertMsg(rc || SigActOld.sa_handler == rtThreadPosixPokeSignal, ("%p\n", SigActOld.sa_handler));
275# else
276 siginterrupt(g_iSigPokeThread, 1);
277# endif
278 }
279#endif
280}
281
282DECLHIDDEN(void) rtThreadNativeReInitObtrusive(void)
283{
284#ifdef RTTHREAD_POSIX_WITH_POKE
285 Assert(!RTR3InitIsUnobtrusive());
286 rtThreadPosixSelectPokeSignal();
287#endif
288 rtThreadPosixBlockSignals();
289}
290
291
292/**
293 * Destructor called when a thread terminates.
294 * @param pvValue The key value. PRTTHREAD in our case.
295 */
296static void rtThreadKeyDestruct(void *pvValue)
297{
298 /*
299 * Deal with alien threads.
300 */
301 PRTTHREADINT pThread = (PRTTHREADINT)pvValue;
302 if (pThread->fIntFlags & RTTHREADINT_FLAGS_ALIEN)
303 {
304 pthread_setspecific(g_SelfKey, pThread);
305 rtThreadTerminate(pThread, 0);
306 pthread_setspecific(g_SelfKey, NULL);
307 }
308}
309
310
311#ifdef RTTHREAD_POSIX_WITH_POKE
312/**
313 * Dummy signal handler for the poke signal.
314 *
315 * @param iSignal The signal number.
316 */
317static void rtThreadPosixPokeSignal(int iSignal)
318{
319 Assert(iSignal == g_iSigPokeThread);
320 NOREF(iSignal);
321}
322#endif
323
324
325/**
326 * Adopts a thread, this is called immediately after allocating the
327 * thread structure.
328 *
329 * @param pThread Pointer to the thread structure.
330 */
331DECLHIDDEN(int) rtThreadNativeAdopt(PRTTHREADINT pThread)
332{
333 rtThreadPosixBlockSignals();
334
335 int rc = pthread_setspecific(g_SelfKey, pThread);
336 if (!rc)
337 return VINF_SUCCESS;
338 return VERR_FAILED_TO_SET_SELF_TLS;
339}
340
341
342DECLHIDDEN(void) rtThreadNativeDestroy(PRTTHREADINT pThread)
343{
344 if (pThread == (PRTTHREADINT)pthread_getspecific(g_SelfKey))
345 pthread_setspecific(g_SelfKey, NULL);
346}
347
348
349/**
350 * Wrapper which unpacks the params and calls thread function.
351 */
352static void *rtThreadNativeMain(void *pvArgs)
353{
354 PRTTHREADINT pThread = (PRTTHREADINT)pvArgs;
355 pthread_t Self = pthread_self();
356#if !defined(RT_OS_SOLARIS) /* On Solaris sizeof(pthread_t) = 4 and sizeof(NIL_RTNATIVETHREAD) = 8 */
357 Assert((uintptr_t)Self != NIL_RTNATIVETHREAD);
358#endif
359 Assert(Self == (pthread_t)(RTNATIVETHREAD)Self);
360
361#if defined(RT_OS_LINUX)
362 /*
363 * Set the TID.
364 */
365 pThread->tid = syscall(__NR_gettid);
366 ASMMemoryFence();
367#endif
368
369 rtThreadPosixBlockSignals();
370
371 /*
372 * Set the TLS entry and, if possible, the thread name.
373 */
374 int rc = pthread_setspecific(g_SelfKey, pThread);
375 AssertReleaseMsg(!rc, ("failed to set self TLS. rc=%d thread '%s'\n", rc, pThread->szName));
376
377#ifdef IPRT_MAY_HAVE_PTHREAD_SET_NAME_NP
378 if (g_pfnThreadSetName)
379# ifdef RT_OS_DARWIN
380 g_pfnThreadSetName(pThread->szName);
381# else
382 g_pfnThreadSetName(Self, pThread->szName);
383# endif
384#endif
385
386 /*
387 * Call common main.
388 */
389 rc = rtThreadMain(pThread, (uintptr_t)Self, &pThread->szName[0]);
390
391 pthread_setspecific(g_SelfKey, NULL);
392 pthread_exit((void *)(intptr_t)rc);
393 return (void *)(intptr_t)rc;
394}
395
396#ifdef RTTHREAD_POSIX_WITH_CREATE_PRIORITY_PROXY
397
398/**
399 * @callback_method_impl{FNRTTHREAD,
400 * Priority proxy thread that services g_hRTThreadPosixPriorityProxyQueue.}
401 */
402static DECLCALLBACK(int) rtThreadPosixPriorityProxyThread(PRTTHREADINT, void *)
403{
404 for (;;)
405 {
406 RTREQQUEUE hReqQueue = g_hRTThreadPosixPriorityProxyQueue;
407 if (hReqQueue != NIL_RTREQQUEUE)
408 RTReqQueueProcess(hReqQueue, RT_INDEFINITE_WAIT);
409 else
410 break;
411
412 int32_t rc = ASMAtomicUoReadS32(&g_rcPriorityProxyThreadStart);
413 if (rc != VINF_SUCCESS && rc != VERR_WRONG_ORDER)
414 break;
415 }
416
417 return VINF_SUCCESS;
418}
419
420
421/**
422 * Just returns a non-success status codes to force the thread to re-evaluate
423 * the global shutdown variable.
424 */
425static DECLCALLBACK(int) rtThreadPosixPriorityProxyStopper(void)
426{
427 return VERR_CANCELLED;
428}
429
430
431/**
432 * An atexit() callback that stops the proxy creation/priority thread.
433 */
434static void rtThreadStopProxyThread(void)
435{
436 /*
437 * Signal to the thread that it's time to shut down.
438 */
439 int32_t rc = ASMAtomicXchgS32(&g_rcPriorityProxyThreadStart, VERR_PROCESS_NOT_FOUND);
440 if (RT_SUCCESS(rc))
441 {
442 /*
443 * Grab the associated handles.
444 */
445 RTTHREAD hThread = g_hRTThreadPosixPriorityProxyThread;
446 RTREQQUEUE hQueue = g_hRTThreadPosixPriorityProxyQueue;
447 g_hRTThreadPosixPriorityProxyQueue = NIL_RTREQQUEUE;
448 g_hRTThreadPosixPriorityProxyThread = NIL_RTTHREAD;
449 ASMCompilerBarrier(); /* paranoia */
450
451 AssertReturnVoid(hThread != NIL_RTTHREAD);
452 AssertReturnVoid(hQueue != NIL_RTREQQUEUE);
453
454 /*
455 * Kick the thread so it gets out of any pending RTReqQueueProcess call ASAP.
456 */
457 rc = RTReqQueueCallEx(hQueue, NULL, 0 /*cMillies*/, RTREQFLAGS_IPRT_STATUS | RTREQFLAGS_NO_WAIT,
458 (PFNRT)rtThreadPosixPriorityProxyStopper, 0);
459
460 /*
461 * Wait for the thread to complete.
462 */
463 rc = RTThreadWait(hThread, RT_SUCCESS(rc) ? RT_MS_1SEC * 5 : 32, NULL);
464 if (RT_SUCCESS(rc))
465 RTReqQueueDestroy(hQueue);
466 /* else: just leak the stuff, we're exitting, so nobody cares... */
467 }
468}
469
470
471/**
472 * Ensure that the proxy priority proxy thread has been started.
473 *
474 * Since we will always start a proxy thread when asked to create a thread,
475 * there is no need for serialization here.
476 *
477 * @retval true if started
478 * @retval false if it failed to start (caller must handle this scenario).
479 */
480DECLHIDDEN(bool) rtThreadPosixPriorityProxyStart(void)
481{
482 /*
483 * Read the result.
484 */
485 int rc = ASMAtomicUoReadS32(&g_rcPriorityProxyThreadStart);
486 if (rc != VERR_TRY_AGAIN)
487 return RT_SUCCESS(rc);
488
489 /* If this triggers then there is a very unexpected race somewhere. It
490 should be harmless though. */
491 AssertReturn(ASMAtomicCmpXchgS32(&g_rcPriorityProxyThreadStart, VERR_WRONG_ORDER, VERR_TRY_AGAIN), false);
492
493 /*
494 * Not yet started, so do that.
495 */
496 rc = RTReqQueueCreate(&g_hRTThreadPosixPriorityProxyQueue);
497 if (RT_SUCCESS(rc))
498 {
499 rc = RTThreadCreate(&g_hRTThreadPosixPriorityProxyThread, rtThreadPosixPriorityProxyThread, NULL, 0 /*cbStack*/,
500 RTTHREADTYPE_DEFAULT, RTTHREADFLAGS_WAITABLE, "RTThrdPP");
501 if (RT_SUCCESS(rc))
502 {
503 ASMAtomicWriteS32(&g_rcPriorityProxyThreadStart, VINF_SUCCESS);
504
505 atexit(rtThreadStopProxyThread);
506 return true;
507 }
508 RTReqQueueCreate(&g_hRTThreadPosixPriorityProxyQueue);
509 }
510 ASMAtomicWriteS32(&g_rcPriorityProxyThreadStart, rc != VERR_WRONG_ORDER ? rc : VERR_PROCESS_NOT_FOUND);
511 return false;
512}
513
514
515/**
516 * Calls @a pfnFunction from the priority proxy thread.
517 *
518 * Caller must have called rtThreadPosixStartProxy() to check that the priority
519 * proxy thread is running.
520 *
521 * @returns
522 * @param pTargetThread The target thread, NULL if not applicable. This is
523 * so we can skip calls pertaining to the priority
524 * proxy thread itself.
525 * @param pfnFunction The function to call. Must return IPRT status code.
526 * @param cArgs Number of arguments (see also RTReqQueueCall).
527 * @param ... Arguments (see also RTReqQueueCall).
528 */
529DECLHIDDEN(int) rtThreadPosixPriorityProxyCall(PRTTHREADINT pTargetThread, PFNRT pfnFunction, int cArgs, ...)
530{
531 int rc;
532 if ( !pTargetThread
533 || pTargetThread->pfnThread != rtThreadPosixPriorityProxyThread)
534 {
535 va_list va;
536 va_start(va, cArgs);
537 PRTREQ pReq;
538 rc = RTReqQueueCallV(g_hRTThreadPosixPriorityProxyQueue, &pReq, RT_INDEFINITE_WAIT, RTREQFLAGS_IPRT_STATUS,
539 pfnFunction, cArgs, va);
540 va_end(va);
541 RTReqRelease(pReq);
542 }
543 else
544 rc = VINF_SUCCESS;
545 return rc;
546}
547
548#endif /* !RTTHREAD_POSIX_WITH_CREATE_PRIORITY_PROXY */
549
550/**
551 * Worker for rtThreadNativeCreate that's either called on the priority proxy
552 * thread or directly on the calling thread depending on the proxy state.
553 */
554static DECLCALLBACK(int) rtThreadNativeInternalCreate(PRTTHREADINT pThread, PRTNATIVETHREAD pNativeThread)
555{
556 /*
557 * Set the default stack size.
558 */
559 if (!pThread->cbStack)
560 pThread->cbStack = 512*1024;
561
562#ifdef RT_OS_LINUX
563 pThread->tid = -1;
564#endif
565
566 /*
567 * Setup thread attributes.
568 */
569 pthread_attr_t ThreadAttr;
570 int rc = pthread_attr_init(&ThreadAttr);
571 if (!rc)
572 {
573 rc = pthread_attr_setdetachstate(&ThreadAttr, PTHREAD_CREATE_DETACHED);
574 if (!rc)
575 {
576 rc = pthread_attr_setstacksize(&ThreadAttr, pThread->cbStack);
577 if (!rc)
578 {
579 /*
580 * Create the thread.
581 */
582 pthread_t ThreadId;
583 rc = pthread_create(&ThreadId, &ThreadAttr, rtThreadNativeMain, pThread);
584 if (!rc)
585 {
586 pthread_attr_destroy(&ThreadAttr);
587 *pNativeThread = (uintptr_t)ThreadId;
588 return VINF_SUCCESS;
589 }
590 }
591 }
592 pthread_attr_destroy(&ThreadAttr);
593 }
594 return RTErrConvertFromErrno(rc);
595}
596
597
598DECLHIDDEN(int) rtThreadNativeCreate(PRTTHREADINT pThread, PRTNATIVETHREAD pNativeThread)
599{
600#ifdef RTTHREAD_POSIX_WITH_CREATE_PRIORITY_PROXY
601 /*
602 * If we have a priority proxy thread, use it. Make sure to ignore the
603 * staring of the proxy thread itself.
604 */
605 if ( pThread->pfnThread != rtThreadPosixPriorityProxyThread
606 && rtThreadPosixPriorityProxyStart())
607 {
608 PRTREQ pReq;
609 int rc = RTReqQueueCall(g_hRTThreadPosixPriorityProxyQueue, &pReq, RT_INDEFINITE_WAIT,
610 (PFNRT)rtThreadNativeInternalCreate, 2, pThread, pNativeThread);
611 RTReqRelease(pReq);
612 return rc;
613 }
614
615 /*
616 * Fall back on creating it directly without regard to priority proxying.
617 */
618#endif
619 return rtThreadNativeInternalCreate(pThread, pNativeThread);
620}
621
622
623RTDECL(RTTHREAD) RTThreadSelf(void)
624{
625 PRTTHREADINT pThread = (PRTTHREADINT)pthread_getspecific(g_SelfKey);
626 /** @todo import alien threads? */
627 return pThread;
628}
629
630
631#ifdef RTTHREAD_POSIX_WITH_POKE
632RTDECL(int) RTThreadPoke(RTTHREAD hThread)
633{
634 AssertReturn(hThread != RTThreadSelf(), VERR_INVALID_PARAMETER);
635 PRTTHREADINT pThread = rtThreadGet(hThread);
636 AssertReturn(pThread, VERR_INVALID_HANDLE);
637
638 int rc;
639 if (g_iSigPokeThread != -1)
640 {
641 rc = pthread_kill((pthread_t)(uintptr_t)pThread->Core.Key, g_iSigPokeThread);
642 rc = RTErrConvertFromErrno(rc);
643 }
644 else
645 rc = VERR_NOT_SUPPORTED;
646
647 rtThreadRelease(pThread);
648 return rc;
649}
650#endif
651
652/** @todo move this into platform specific files. */
653RTR3DECL(int) RTThreadGetExecutionTimeMilli(uint64_t *pKernelTime, uint64_t *pUserTime)
654{
655#if defined(RT_OS_SOLARIS)
656 struct rusage ts;
657 int rc = getrusage(RUSAGE_LWP, &ts);
658 if (rc)
659 return RTErrConvertFromErrno(rc);
660
661 *pKernelTime = ts.ru_stime.tv_sec * 1000 + ts.ru_stime.tv_usec / 1000;
662 *pUserTime = ts.ru_utime.tv_sec * 1000 + ts.ru_utime.tv_usec / 1000;
663 return VINF_SUCCESS;
664
665#elif defined(RT_OS_LINUX) || defined(RT_OS_FREEBSD)
666 /* on Linux, getrusage(RUSAGE_THREAD, ...) is available since 2.6.26 */
667 struct timespec ts;
668 int rc = clock_gettime(CLOCK_THREAD_CPUTIME_ID, &ts);
669 if (rc)
670 return RTErrConvertFromErrno(rc);
671
672 *pKernelTime = 0;
673 *pUserTime = (uint64_t)ts.tv_sec * 1000 + ts.tv_nsec / 1000000;
674 return VINF_SUCCESS;
675
676#elif defined(RT_OS_DARWIN)
677 thread_basic_info ThreadInfo;
678 mach_msg_type_number_t Count = THREAD_BASIC_INFO_COUNT;
679 kern_return_t krc = thread_info(mach_thread_self(), THREAD_BASIC_INFO, (thread_info_t)&ThreadInfo, &Count);
680 AssertReturn(krc == KERN_SUCCESS, RTErrConvertFromDarwinKern(krc));
681
682 *pKernelTime = ThreadInfo.system_time.seconds * 1000 + ThreadInfo.system_time.microseconds / 1000;
683 *pUserTime = ThreadInfo.user_time.seconds * 1000 + ThreadInfo.user_time.microseconds / 1000;
684
685 return VINF_SUCCESS;
686#elif defined(RT_OS_HAIKU)
687 thread_info ThreadInfo;
688 status_t status = get_thread_info(find_thread(NULL), &ThreadInfo);
689 AssertReturn(status == B_OK, RTErrConvertFromErrno(status));
690
691 *pKernelTime = ThreadInfo.kernel_time / 1000;
692 *pUserTime = ThreadInfo.user_time / 1000;
693
694 return VINF_SUCCESS;
695#else
696 return VERR_NOT_IMPLEMENTED;
697#endif
698}
699
Note: See TracBrowser for help on using the repository browser.

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