VirtualBox

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

最後變更 在這個檔案從95190是 95190,由 vboxsync 提交於 3 年 前

IPRT/r3/darwin: Use pthread_sigmask instead of sigprocmask on darwin, as xnu is applying sigprocmask input to all threads of a process which isn't at all what we wish for.

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

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