VirtualBox

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

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

Some small Solaris-specific build warning fixes:

Additions/solaris/DRM: Remove unused global variable 'g_pDip' from vboxvideo_drm.c.

Additions/solaris/SharedFolders: Add additional #ifdef/#endif around sffs_print() to silence warning: 'sffs_print' defined but not used

IPRT/coredump-solaris: Remove unnecessary and incorrect tag from the ELFWRITENOTE structure declaration which the compiler flags as 'declaration of 'ELFWRITENOTE' shadows a previous local'.

IPRT/thread-posix.cpp: Only call 'Assert((uintptr_t)Self != NIL_RTNATIVETHREAD)' on non-Solaris platforms since the size of pthread_t is implementation-dependent and on Solaris sizeof(pthread_t) = 4 whereas on Linux/MacOS/FreeBSD it is 8. The compiler thus flags this on Solaris as 'comparison is always true due to limited range of data type'.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id Revision
File size: 21.9 KB
Line 
1/* $Id: thread-posix.cpp 88189 2021-03-18 11:07:36Z 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#if !defined(RT_OS_SOLARIS) /* On Solaris sizeof(pthread_t) = 4 and sizeof(NIL_RTNATIVETHREAD) = 8 */
354 Assert((uintptr_t)Self != NIL_RTNATIVETHREAD);
355#endif
356 Assert(Self == (pthread_t)(RTNATIVETHREAD)Self);
357
358#if defined(RT_OS_LINUX)
359 /*
360 * Set the TID.
361 */
362 pThread->tid = syscall(__NR_gettid);
363 ASMMemoryFence();
364#endif
365
366 rtThreadPosixBlockSignals();
367
368 /*
369 * Set the TLS entry and, if possible, the thread name.
370 */
371 int rc = pthread_setspecific(g_SelfKey, pThread);
372 AssertReleaseMsg(!rc, ("failed to set self TLS. rc=%d thread '%s'\n", rc, pThread->szName));
373
374#ifdef IPRT_MAY_HAVE_PTHREAD_SET_NAME_NP
375 if (g_pfnThreadSetName)
376# ifdef RT_OS_DARWIN
377 g_pfnThreadSetName(pThread->szName);
378# else
379 g_pfnThreadSetName(Self, pThread->szName);
380# endif
381#endif
382
383 /*
384 * Call common main.
385 */
386 rc = rtThreadMain(pThread, (uintptr_t)Self, &pThread->szName[0]);
387
388 pthread_setspecific(g_SelfKey, NULL);
389 pthread_exit((void *)(intptr_t)rc);
390 return (void *)(intptr_t)rc;
391}
392
393#ifdef RTTHREAD_POSIX_WITH_CREATE_PRIORITY_PROXY
394
395/**
396 * @callback_method_impl{FNRTTHREAD,
397 * Priority proxy thread that services g_hRTThreadPosixPriorityProxyQueue.}
398 */
399static DECLCALLBACK(int) rtThreadPosixPriorityProxyThread(PRTTHREADINT, void *)
400{
401 for (;;)
402 {
403 RTREQQUEUE hReqQueue = g_hRTThreadPosixPriorityProxyQueue;
404 if (hReqQueue != NIL_RTREQQUEUE)
405 RTReqQueueProcess(hReqQueue, RT_INDEFINITE_WAIT);
406 else
407 break;
408
409 int32_t rc = ASMAtomicUoReadS32(&g_rcPriorityProxyThreadStart);
410 if (rc != VINF_SUCCESS && rc != VERR_WRONG_ORDER)
411 break;
412 }
413
414 return VINF_SUCCESS;
415}
416
417
418/**
419 * Just returns a non-success status codes to force the thread to re-evaluate
420 * the global shutdown variable.
421 */
422static DECLCALLBACK(int) rtThreadPosixPriorityProxyStopper(void)
423{
424 return VERR_CANCELLED;
425}
426
427
428/**
429 * An atexit() callback that stops the proxy creation/priority thread.
430 */
431static void rtThreadStopProxyThread(void)
432{
433 /*
434 * Signal to the thread that it's time to shut down.
435 */
436 int32_t rc = ASMAtomicXchgS32(&g_rcPriorityProxyThreadStart, VERR_PROCESS_NOT_FOUND);
437 if (RT_SUCCESS(rc))
438 {
439 /*
440 * Grab the associated handles.
441 */
442 RTTHREAD hThread = g_hRTThreadPosixPriorityProxyThread;
443 RTREQQUEUE hQueue = g_hRTThreadPosixPriorityProxyQueue;
444 g_hRTThreadPosixPriorityProxyQueue = NIL_RTREQQUEUE;
445 g_hRTThreadPosixPriorityProxyThread = NIL_RTTHREAD;
446 ASMCompilerBarrier(); /* paranoia */
447
448 AssertReturnVoid(hThread != NIL_RTTHREAD);
449 AssertReturnVoid(hQueue != NIL_RTREQQUEUE);
450
451 /*
452 * Kick the thread so it gets out of any pending RTReqQueueProcess call ASAP.
453 */
454 rc = RTReqQueueCallEx(hQueue, NULL, 0 /*cMillies*/, RTREQFLAGS_IPRT_STATUS | RTREQFLAGS_NO_WAIT,
455 (PFNRT)rtThreadPosixPriorityProxyStopper, 0);
456
457 /*
458 * Wait for the thread to complete.
459 */
460 rc = RTThreadWait(hThread, RT_SUCCESS(rc) ? RT_MS_1SEC * 5 : 32, NULL);
461 if (RT_SUCCESS(rc))
462 RTReqQueueDestroy(hQueue);
463 /* else: just leak the stuff, we're exitting, so nobody cares... */
464 }
465}
466
467
468/**
469 * Ensure that the proxy priority proxy thread has been started.
470 *
471 * Since we will always start a proxy thread when asked to create a thread,
472 * there is no need for serialization here.
473 *
474 * @retval true if started
475 * @retval false if it failed to start (caller must handle this scenario).
476 */
477DECLHIDDEN(bool) rtThreadPosixPriorityProxyStart(void)
478{
479 /*
480 * Read the result.
481 */
482 int rc = ASMAtomicUoReadS32(&g_rcPriorityProxyThreadStart);
483 if (rc != VERR_TRY_AGAIN)
484 return RT_SUCCESS(rc);
485
486 /* If this triggers then there is a very unexpected race somewhere. It
487 should be harmless though. */
488 AssertReturn(ASMAtomicCmpXchgS32(&g_rcPriorityProxyThreadStart, VERR_WRONG_ORDER, VERR_TRY_AGAIN), false);
489
490 /*
491 * Not yet started, so do that.
492 */
493 rc = RTReqQueueCreate(&g_hRTThreadPosixPriorityProxyQueue);
494 if (RT_SUCCESS(rc))
495 {
496 rc = RTThreadCreate(&g_hRTThreadPosixPriorityProxyThread, rtThreadPosixPriorityProxyThread, NULL, 0 /*cbStack*/,
497 RTTHREADTYPE_DEFAULT, RTTHREADFLAGS_WAITABLE, "RTThrdPP");
498 if (RT_SUCCESS(rc))
499 {
500 ASMAtomicWriteS32(&g_rcPriorityProxyThreadStart, VINF_SUCCESS);
501
502 atexit(rtThreadStopProxyThread);
503 return true;
504 }
505 RTReqQueueCreate(&g_hRTThreadPosixPriorityProxyQueue);
506 }
507 ASMAtomicWriteS32(&g_rcPriorityProxyThreadStart, rc != VERR_WRONG_ORDER ? rc : VERR_PROCESS_NOT_FOUND);
508 return false;
509}
510
511
512/**
513 * Calls @a pfnFunction from the priority proxy thread.
514 *
515 * Caller must have called rtThreadPosixStartProxy() to check that the priority
516 * proxy thread is running.
517 *
518 * @returns
519 * @param pTargetThread The target thread, NULL if not applicable. This is
520 * so we can skip calls pertaining to the priority
521 * proxy thread itself.
522 * @param pfnFunction The function to call. Must return IPRT status code.
523 * @param cArgs Number of arguments (see also RTReqQueueCall).
524 * @param ... Arguments (see also RTReqQueueCall).
525 */
526DECLHIDDEN(int) rtThreadPosixPriorityProxyCall(PRTTHREADINT pTargetThread, PFNRT pfnFunction, int cArgs, ...)
527{
528 int rc;
529 if ( !pTargetThread
530 || pTargetThread->pfnThread != rtThreadPosixPriorityProxyThread)
531 {
532 va_list va;
533 va_start(va, cArgs);
534 PRTREQ pReq;
535 rc = RTReqQueueCallV(g_hRTThreadPosixPriorityProxyQueue, &pReq, RT_INDEFINITE_WAIT, RTREQFLAGS_IPRT_STATUS,
536 pfnFunction, cArgs, va);
537 va_end(va);
538 RTReqRelease(pReq);
539 }
540 else
541 rc = VINF_SUCCESS;
542 return rc;
543}
544
545#endif /* !RTTHREAD_POSIX_WITH_CREATE_PRIORITY_PROXY */
546
547/**
548 * Worker for rtThreadNativeCreate that's either called on the priority proxy
549 * thread or directly on the calling thread depending on the proxy state.
550 */
551static DECLCALLBACK(int) rtThreadNativeInternalCreate(PRTTHREADINT pThread, PRTNATIVETHREAD pNativeThread)
552{
553 /*
554 * Set the default stack size.
555 */
556 if (!pThread->cbStack)
557 pThread->cbStack = 512*1024;
558
559#ifdef RT_OS_LINUX
560 pThread->tid = -1;
561#endif
562
563 /*
564 * Setup thread attributes.
565 */
566 pthread_attr_t ThreadAttr;
567 int rc = pthread_attr_init(&ThreadAttr);
568 if (!rc)
569 {
570 rc = pthread_attr_setdetachstate(&ThreadAttr, PTHREAD_CREATE_DETACHED);
571 if (!rc)
572 {
573 rc = pthread_attr_setstacksize(&ThreadAttr, pThread->cbStack);
574 if (!rc)
575 {
576 /*
577 * Create the thread.
578 */
579 pthread_t ThreadId;
580 rc = pthread_create(&ThreadId, &ThreadAttr, rtThreadNativeMain, pThread);
581 if (!rc)
582 {
583 pthread_attr_destroy(&ThreadAttr);
584 *pNativeThread = (uintptr_t)ThreadId;
585 return VINF_SUCCESS;
586 }
587 }
588 }
589 pthread_attr_destroy(&ThreadAttr);
590 }
591 return RTErrConvertFromErrno(rc);
592}
593
594
595DECLHIDDEN(int) rtThreadNativeCreate(PRTTHREADINT pThread, PRTNATIVETHREAD pNativeThread)
596{
597#ifdef RTTHREAD_POSIX_WITH_CREATE_PRIORITY_PROXY
598 /*
599 * If we have a priority proxy thread, use it. Make sure to ignore the
600 * staring of the proxy thread itself.
601 */
602 if ( pThread->pfnThread != rtThreadPosixPriorityProxyThread
603 && rtThreadPosixPriorityProxyStart())
604 {
605 PRTREQ pReq;
606 int rc = RTReqQueueCall(g_hRTThreadPosixPriorityProxyQueue, &pReq, RT_INDEFINITE_WAIT,
607 (PFNRT)rtThreadNativeInternalCreate, 2, pThread, pNativeThread);
608 RTReqRelease(pReq);
609 return rc;
610 }
611
612 /*
613 * Fall back on creating it directly without regard to priority proxying.
614 */
615#endif
616 return rtThreadNativeInternalCreate(pThread, pNativeThread);
617}
618
619
620RTDECL(RTTHREAD) RTThreadSelf(void)
621{
622 PRTTHREADINT pThread = (PRTTHREADINT)pthread_getspecific(g_SelfKey);
623 /** @todo import alien threads? */
624 return pThread;
625}
626
627
628#ifdef RTTHREAD_POSIX_WITH_POKE
629RTDECL(int) RTThreadPoke(RTTHREAD hThread)
630{
631 AssertReturn(hThread != RTThreadSelf(), VERR_INVALID_PARAMETER);
632 PRTTHREADINT pThread = rtThreadGet(hThread);
633 AssertReturn(pThread, VERR_INVALID_HANDLE);
634
635 int rc;
636 if (g_iSigPokeThread != -1)
637 {
638 rc = pthread_kill((pthread_t)(uintptr_t)pThread->Core.Key, g_iSigPokeThread);
639 rc = RTErrConvertFromErrno(rc);
640 }
641 else
642 rc = VERR_NOT_SUPPORTED;
643
644 rtThreadRelease(pThread);
645 return rc;
646}
647#endif
648
649/** @todo move this into platform specific files. */
650RTR3DECL(int) RTThreadGetExecutionTimeMilli(uint64_t *pKernelTime, uint64_t *pUserTime)
651{
652#if defined(RT_OS_SOLARIS)
653 struct rusage ts;
654 int rc = getrusage(RUSAGE_LWP, &ts);
655 if (rc)
656 return RTErrConvertFromErrno(rc);
657
658 *pKernelTime = ts.ru_stime.tv_sec * 1000 + ts.ru_stime.tv_usec / 1000;
659 *pUserTime = ts.ru_utime.tv_sec * 1000 + ts.ru_utime.tv_usec / 1000;
660 return VINF_SUCCESS;
661
662#elif defined(RT_OS_LINUX) || defined(RT_OS_FREEBSD)
663 /* on Linux, getrusage(RUSAGE_THREAD, ...) is available since 2.6.26 */
664 struct timespec ts;
665 int rc = clock_gettime(CLOCK_THREAD_CPUTIME_ID, &ts);
666 if (rc)
667 return RTErrConvertFromErrno(rc);
668
669 *pKernelTime = 0;
670 *pUserTime = (uint64_t)ts.tv_sec * 1000 + ts.tv_nsec / 1000000;
671 return VINF_SUCCESS;
672
673#elif defined(RT_OS_DARWIN)
674 thread_basic_info ThreadInfo;
675 mach_msg_type_number_t Count = THREAD_BASIC_INFO_COUNT;
676 kern_return_t krc = thread_info(mach_thread_self(), THREAD_BASIC_INFO, (thread_info_t)&ThreadInfo, &Count);
677 AssertReturn(krc == KERN_SUCCESS, RTErrConvertFromDarwinKern(krc));
678
679 *pKernelTime = ThreadInfo.system_time.seconds * 1000 + ThreadInfo.system_time.microseconds / 1000;
680 *pUserTime = ThreadInfo.user_time.seconds * 1000 + ThreadInfo.user_time.microseconds / 1000;
681
682 return VINF_SUCCESS;
683#elif defined(RT_OS_HAIKU)
684 thread_info ThreadInfo;
685 status_t status = get_thread_info(find_thread(NULL), &ThreadInfo);
686 AssertReturn(status == B_OK, RTErrConvertFromErrno(status));
687
688 *pKernelTime = ThreadInfo.kernel_time / 1000;
689 *pUserTime = ThreadInfo.user_time / 1000;
690
691 return VINF_SUCCESS;
692#else
693 return VERR_NOT_IMPLEMENTED;
694#endif
695}
696
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