1 | /* $Id: thread.cpp 95200 2022-06-05 20:16:39Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * IPRT - Threads, common routines.
|
---|
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.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 <iprt/thread.h>
|
---|
33 | #include "internal/iprt.h"
|
---|
34 |
|
---|
35 | #include <iprt/log.h>
|
---|
36 | #include <iprt/avl.h>
|
---|
37 | #include <iprt/alloc.h>
|
---|
38 | #include <iprt/assert.h>
|
---|
39 | #include <iprt/lockvalidator.h>
|
---|
40 | #include <iprt/semaphore.h>
|
---|
41 | #ifdef IN_RING0
|
---|
42 | # include <iprt/spinlock.h>
|
---|
43 | #endif
|
---|
44 | #include <iprt/asm.h>
|
---|
45 | #include <iprt/err.h>
|
---|
46 | #include <iprt/string.h>
|
---|
47 | #include "internal/magics.h"
|
---|
48 | #include "internal/thread.h"
|
---|
49 | #include "internal/sched.h"
|
---|
50 | #include "internal/process.h"
|
---|
51 | #ifdef RT_WITH_ICONV_CACHE
|
---|
52 | # include "internal/string.h"
|
---|
53 | #endif
|
---|
54 |
|
---|
55 |
|
---|
56 | /*********************************************************************************************************************************
|
---|
57 | * Defined Constants And Macros *
|
---|
58 | *********************************************************************************************************************************/
|
---|
59 | #ifdef IN_RING0
|
---|
60 | # define RT_THREAD_LOCK_RW() RTSpinlockAcquire(g_ThreadSpinlock)
|
---|
61 | # define RT_THREAD_UNLOCK_RW() RTSpinlockRelease(g_ThreadSpinlock)
|
---|
62 | # define RT_THREAD_LOCK_RD() RTSpinlockAcquire(g_ThreadSpinlock)
|
---|
63 | # define RT_THREAD_UNLOCK_RD() RTSpinlockRelease(g_ThreadSpinlock)
|
---|
64 | #else
|
---|
65 | # define RT_THREAD_LOCK_RW() rtThreadLockRW()
|
---|
66 | # define RT_THREAD_UNLOCK_RW() rtThreadUnLockRW()
|
---|
67 | # define RT_THREAD_LOCK_RD() rtThreadLockRD()
|
---|
68 | # define RT_THREAD_UNLOCK_RD() rtThreadUnLockRD()
|
---|
69 | #endif
|
---|
70 |
|
---|
71 |
|
---|
72 | /*********************************************************************************************************************************
|
---|
73 | * Global Variables *
|
---|
74 | *********************************************************************************************************************************/
|
---|
75 | /** Indicates whether we've been initialized or not. */
|
---|
76 | static bool g_frtThreadInitialized;
|
---|
77 | #ifdef IN_RING3
|
---|
78 | /** The RW lock protecting the tree. */
|
---|
79 | static RTSEMRW g_ThreadRWSem = NIL_RTSEMRW;
|
---|
80 | #else
|
---|
81 | /** The spinlocks protecting the tree. */
|
---|
82 | static RTSPINLOCK g_ThreadSpinlock = NIL_RTSPINLOCK;
|
---|
83 | #endif
|
---|
84 | /** The AVL thread containing the threads. */
|
---|
85 | static PAVLPVNODECORE g_ThreadTree;
|
---|
86 | /** The number of threads in the tree (for ring-0 termination kludge). */
|
---|
87 | static uint32_t volatile g_cThreadInTree;
|
---|
88 | /** Counters for each thread type. */
|
---|
89 | DECL_HIDDEN_DATA(uint32_t volatile) g_acRTThreadTypeStats[RTTHREADTYPE_END];
|
---|
90 |
|
---|
91 |
|
---|
92 | /*********************************************************************************************************************************
|
---|
93 | * Internal Functions *
|
---|
94 | *********************************************************************************************************************************/
|
---|
95 | static void rtThreadDestroy(PRTTHREADINT pThread);
|
---|
96 | #ifdef IN_RING3
|
---|
97 | static int rtThreadAdopt(RTTHREADTYPE enmType, unsigned fFlags, uint32_t fIntFlags, const char *pszName);
|
---|
98 | #endif
|
---|
99 | static void rtThreadRemoveLocked(PRTTHREADINT pThread);
|
---|
100 | static PRTTHREADINT rtThreadAlloc(RTTHREADTYPE enmType, unsigned fFlags, uint32_t fIntFlags, const char *pszName);
|
---|
101 |
|
---|
102 |
|
---|
103 | /** @page pg_rt_thread IPRT Thread Internals
|
---|
104 | *
|
---|
105 | * IPRT provides interface to whatever native threading that the host provides,
|
---|
106 | * preferably using a CRT level interface to better integrate with other libraries.
|
---|
107 | *
|
---|
108 | * Internally IPRT keeps track of threads by means of the RTTHREADINT structure.
|
---|
109 | * All the RTTHREADINT structures are kept in a AVL tree which is protected by a
|
---|
110 | * read/write lock for efficient access. A thread is inserted into the tree in
|
---|
111 | * three places in the code. The main thread is 'adopted' by IPRT on rtR3Init()
|
---|
112 | * by rtThreadAdopt(). When creating a new thread there the child and the parent
|
---|
113 | * race inserting the thread, this is rtThreadMain() and RTThreadCreate.
|
---|
114 | *
|
---|
115 | * RTTHREADINT objects are using reference counting as a mean of sticking around
|
---|
116 | * till no-one needs them any longer. Waitable threads is created with one extra
|
---|
117 | * reference so they won't go away until they are waited on. This introduces a
|
---|
118 | * major problem if we use the host thread identifier as key in the AVL tree - the
|
---|
119 | * host may reuse the thread identifier before the thread was waited on. So, on
|
---|
120 | * most platforms we are using the RTTHREADINT pointer as key and not the
|
---|
121 | * thread id. RTThreadSelf() then have to be implemented using a pointer stored
|
---|
122 | * in thread local storage (TLS).
|
---|
123 | *
|
---|
124 | * In Ring-0 we only try keep track of kernel threads created by RTThreadCreate
|
---|
125 | * at the moment. There we really only need the 'join' feature, but doing things
|
---|
126 | * the same way allow us to name threads and similar stuff.
|
---|
127 | */
|
---|
128 |
|
---|
129 |
|
---|
130 | /**
|
---|
131 | * Initializes the thread database.
|
---|
132 | *
|
---|
133 | * @returns iprt status code.
|
---|
134 | */
|
---|
135 | DECLHIDDEN(int) rtThreadInit(void)
|
---|
136 | {
|
---|
137 | #ifdef IN_RING3
|
---|
138 | int rc = VINF_ALREADY_INITIALIZED;
|
---|
139 | if (g_ThreadRWSem == NIL_RTSEMRW)
|
---|
140 | {
|
---|
141 | /*
|
---|
142 | * We assume the caller is the 1st thread, which we'll call 'main'.
|
---|
143 | * But first, we'll create the semaphore.
|
---|
144 | */
|
---|
145 | rc = RTSemRWCreateEx(&g_ThreadRWSem, RTSEMRW_FLAGS_NO_LOCK_VAL, NIL_RTLOCKVALCLASS, RTLOCKVAL_SUB_CLASS_NONE, NULL);
|
---|
146 | if (RT_SUCCESS(rc))
|
---|
147 | {
|
---|
148 | rc = rtThreadNativeInit();
|
---|
149 | if (RT_SUCCESS(rc))
|
---|
150 | rc = rtThreadAdopt(RTTHREADTYPE_DEFAULT, 0, RTTHREADINT_FLAGS_MAIN, "main");
|
---|
151 | if (RT_SUCCESS(rc))
|
---|
152 | rc = rtSchedNativeCalcDefaultPriority(RTTHREADTYPE_DEFAULT);
|
---|
153 | if (RT_SUCCESS(rc))
|
---|
154 | {
|
---|
155 | g_frtThreadInitialized = true;
|
---|
156 | return VINF_SUCCESS;
|
---|
157 | }
|
---|
158 |
|
---|
159 | /* failed, clear out */
|
---|
160 | RTSemRWDestroy(g_ThreadRWSem);
|
---|
161 | g_ThreadRWSem = NIL_RTSEMRW;
|
---|
162 | }
|
---|
163 | }
|
---|
164 |
|
---|
165 | #elif defined(IN_RING0)
|
---|
166 | int rc;
|
---|
167 | /*
|
---|
168 | * Create the spinlock and to native init.
|
---|
169 | */
|
---|
170 | Assert(g_ThreadSpinlock == NIL_RTSPINLOCK);
|
---|
171 | rc = RTSpinlockCreate(&g_ThreadSpinlock, RTSPINLOCK_FLAGS_INTERRUPT_SAFE, "RTThread");
|
---|
172 | if (RT_SUCCESS(rc))
|
---|
173 | {
|
---|
174 | rc = rtThreadNativeInit();
|
---|
175 | if (RT_SUCCESS(rc))
|
---|
176 | {
|
---|
177 | g_frtThreadInitialized = true;
|
---|
178 | return VINF_SUCCESS;
|
---|
179 | }
|
---|
180 |
|
---|
181 | /* failed, clear out */
|
---|
182 | RTSpinlockDestroy(g_ThreadSpinlock);
|
---|
183 | g_ThreadSpinlock = NIL_RTSPINLOCK;
|
---|
184 | }
|
---|
185 | #else
|
---|
186 | # error "!IN_RING0 && !IN_RING3"
|
---|
187 | #endif
|
---|
188 | return rc;
|
---|
189 | }
|
---|
190 |
|
---|
191 |
|
---|
192 | #ifdef IN_RING3
|
---|
193 | /**
|
---|
194 | * Called when IPRT was first initialized in unobtrusive mode and later changed
|
---|
195 | * to obtrustive.
|
---|
196 | *
|
---|
197 | * This is only applicable in ring-3.
|
---|
198 | */
|
---|
199 | DECLHIDDEN(void) rtThreadReInitObtrusive(void)
|
---|
200 | {
|
---|
201 | rtThreadNativeReInitObtrusive();
|
---|
202 | }
|
---|
203 | #endif
|
---|
204 |
|
---|
205 |
|
---|
206 | /**
|
---|
207 | * Terminates the thread database.
|
---|
208 | */
|
---|
209 | DECLHIDDEN(void) rtThreadTerm(void)
|
---|
210 | {
|
---|
211 | #ifdef IN_RING3
|
---|
212 | /* we don't cleanup here yet */
|
---|
213 |
|
---|
214 | #elif defined(IN_RING0)
|
---|
215 | /* just destroy the spinlock and assume the thread is fine... */
|
---|
216 | RTSpinlockDestroy(g_ThreadSpinlock);
|
---|
217 | g_ThreadSpinlock = NIL_RTSPINLOCK;
|
---|
218 | if (g_ThreadTree != NULL)
|
---|
219 | RTAssertMsg2Weak("WARNING: g_ThreadTree=%p\n", g_ThreadTree);
|
---|
220 | #endif
|
---|
221 | }
|
---|
222 |
|
---|
223 |
|
---|
224 | #ifdef IN_RING3
|
---|
225 |
|
---|
226 | DECLINLINE(void) rtThreadLockRW(void)
|
---|
227 | {
|
---|
228 | if (g_ThreadRWSem == NIL_RTSEMRW)
|
---|
229 | rtThreadInit();
|
---|
230 | int rc = RTSemRWRequestWrite(g_ThreadRWSem, RT_INDEFINITE_WAIT);
|
---|
231 | AssertReleaseRC(rc);
|
---|
232 | }
|
---|
233 |
|
---|
234 |
|
---|
235 | DECLINLINE(void) rtThreadLockRD(void)
|
---|
236 | {
|
---|
237 | if (g_ThreadRWSem == NIL_RTSEMRW)
|
---|
238 | rtThreadInit();
|
---|
239 | int rc = RTSemRWRequestRead(g_ThreadRWSem, RT_INDEFINITE_WAIT);
|
---|
240 | AssertReleaseRC(rc);
|
---|
241 | }
|
---|
242 |
|
---|
243 |
|
---|
244 | DECLINLINE(void) rtThreadUnLockRW(void)
|
---|
245 | {
|
---|
246 | int rc = RTSemRWReleaseWrite(g_ThreadRWSem);
|
---|
247 | AssertReleaseRC(rc);
|
---|
248 | }
|
---|
249 |
|
---|
250 |
|
---|
251 | DECLINLINE(void) rtThreadUnLockRD(void)
|
---|
252 | {
|
---|
253 | int rc = RTSemRWReleaseRead(g_ThreadRWSem);
|
---|
254 | AssertReleaseRC(rc);
|
---|
255 | }
|
---|
256 |
|
---|
257 |
|
---|
258 | /**
|
---|
259 | * Adopts the calling thread.
|
---|
260 | * No locks are taken or released by this function.
|
---|
261 | */
|
---|
262 | static int rtThreadAdopt(RTTHREADTYPE enmType, unsigned fFlags, uint32_t fIntFlags, const char *pszName)
|
---|
263 | {
|
---|
264 | int rc;
|
---|
265 | PRTTHREADINT pThread;
|
---|
266 | Assert(!(fFlags & RTTHREADFLAGS_WAITABLE));
|
---|
267 | fFlags &= ~RTTHREADFLAGS_WAITABLE;
|
---|
268 |
|
---|
269 | /*
|
---|
270 | * Allocate and insert the thread.
|
---|
271 | * (It is vital that rtThreadNativeAdopt updates the TLS before
|
---|
272 | * we try inserting the thread because of locking.)
|
---|
273 | */
|
---|
274 | rc = VERR_NO_MEMORY;
|
---|
275 | pThread = rtThreadAlloc(enmType, fFlags, RTTHREADINT_FLAGS_ALIEN | fIntFlags, pszName);
|
---|
276 | if (pThread)
|
---|
277 | {
|
---|
278 | RTNATIVETHREAD NativeThread = RTThreadNativeSelf();
|
---|
279 | rc = rtThreadNativeAdopt(pThread);
|
---|
280 | if (RT_SUCCESS(rc))
|
---|
281 | {
|
---|
282 | rtThreadInsert(pThread, NativeThread);
|
---|
283 | rtThreadSetState(pThread, RTTHREADSTATE_RUNNING);
|
---|
284 | rtThreadRelease(pThread);
|
---|
285 | }
|
---|
286 | else
|
---|
287 | rtThreadDestroy(pThread);
|
---|
288 | }
|
---|
289 | return rc;
|
---|
290 | }
|
---|
291 |
|
---|
292 | /**
|
---|
293 | * Adopts a non-IPRT thread.
|
---|
294 | *
|
---|
295 | * @returns IPRT status code.
|
---|
296 | * @param enmType The thread type.
|
---|
297 | * @param fFlags The thread flags. RTTHREADFLAGS_WAITABLE is not currently allowed.
|
---|
298 | * @param pszName The thread name. Optional.
|
---|
299 | * @param pThread Where to store the thread handle. Optional.
|
---|
300 | */
|
---|
301 | RTDECL(int) RTThreadAdopt(RTTHREADTYPE enmType, unsigned fFlags, const char *pszName, PRTTHREAD pThread)
|
---|
302 | {
|
---|
303 | int rc;
|
---|
304 | RTTHREAD Thread;
|
---|
305 |
|
---|
306 | AssertReturn(!(fFlags & RTTHREADFLAGS_WAITABLE), VERR_INVALID_FLAGS);
|
---|
307 | AssertPtrNullReturn(pszName, VERR_INVALID_POINTER);
|
---|
308 | AssertPtrNullReturn(pThread, VERR_INVALID_POINTER);
|
---|
309 |
|
---|
310 | rc = VINF_SUCCESS;
|
---|
311 | Thread = RTThreadSelf();
|
---|
312 | if (Thread == NIL_RTTHREAD)
|
---|
313 | {
|
---|
314 | /* generate a name if none was given. */
|
---|
315 | char szName[RTTHREAD_NAME_LEN];
|
---|
316 | if (!pszName || !*pszName)
|
---|
317 | {
|
---|
318 | static uint32_t s_i32AlienId = 0;
|
---|
319 | uint32_t i32Id = ASMAtomicIncU32(&s_i32AlienId);
|
---|
320 | RTStrPrintf(szName, sizeof(szName), "ALIEN-%RX32", i32Id);
|
---|
321 | pszName = szName;
|
---|
322 | }
|
---|
323 |
|
---|
324 | /* try adopt it */
|
---|
325 | rc = rtThreadAdopt(enmType, fFlags, 0, pszName);
|
---|
326 | Thread = RTThreadSelf();
|
---|
327 | Log(("RTThreadAdopt: %RTthrd %RTnthrd '%s' enmType=%d fFlags=%#x rc=%Rrc\n",
|
---|
328 | Thread, RTThreadNativeSelf(), pszName, enmType, fFlags, rc));
|
---|
329 | }
|
---|
330 | else
|
---|
331 | Log(("RTThreadAdopt: %RTthrd %RTnthrd '%s' enmType=%d fFlags=%#x - already adopted!\n",
|
---|
332 | Thread, RTThreadNativeSelf(), pszName, enmType, fFlags));
|
---|
333 |
|
---|
334 | if (pThread)
|
---|
335 | *pThread = Thread;
|
---|
336 | return rc;
|
---|
337 | }
|
---|
338 | RT_EXPORT_SYMBOL(RTThreadAdopt);
|
---|
339 |
|
---|
340 |
|
---|
341 | /**
|
---|
342 | * Get the thread handle of the current thread, automatically adopting alien
|
---|
343 | * threads.
|
---|
344 | *
|
---|
345 | * @returns Thread handle.
|
---|
346 | */
|
---|
347 | RTDECL(RTTHREAD) RTThreadSelfAutoAdopt(void)
|
---|
348 | {
|
---|
349 | RTTHREAD hSelf = RTThreadSelf();
|
---|
350 | if (RT_UNLIKELY(hSelf == NIL_RTTHREAD))
|
---|
351 | RTThreadAdopt(RTTHREADTYPE_DEFAULT, 0, NULL, &hSelf);
|
---|
352 | return hSelf;
|
---|
353 | }
|
---|
354 | RT_EXPORT_SYMBOL(RTThreadSelfAutoAdopt);
|
---|
355 |
|
---|
356 | #endif /* IN_RING3 */
|
---|
357 |
|
---|
358 | /**
|
---|
359 | * Allocates a per thread data structure and initializes the basic fields.
|
---|
360 | *
|
---|
361 | * @returns Pointer to per thread data structure.
|
---|
362 | * This is reference once.
|
---|
363 | * @returns NULL on failure.
|
---|
364 | * @param enmType The thread type.
|
---|
365 | * @param fFlags The thread flags.
|
---|
366 | * @param fIntFlags The internal thread flags.
|
---|
367 | * @param pszName Pointer to the thread name.
|
---|
368 | */
|
---|
369 | PRTTHREADINT rtThreadAlloc(RTTHREADTYPE enmType, unsigned fFlags, uint32_t fIntFlags, const char *pszName)
|
---|
370 | {
|
---|
371 | PRTTHREADINT pThread = (PRTTHREADINT)RTMemAllocZ(sizeof(RTTHREADINT));
|
---|
372 | if (pThread)
|
---|
373 | {
|
---|
374 | size_t cchName;
|
---|
375 | int rc;
|
---|
376 |
|
---|
377 | pThread->Core.Key = (void*)NIL_RTTHREAD;
|
---|
378 | pThread->u32Magic = RTTHREADINT_MAGIC;
|
---|
379 | cchName = strlen(pszName);
|
---|
380 | if (cchName >= RTTHREAD_NAME_LEN)
|
---|
381 | cchName = RTTHREAD_NAME_LEN - 1;
|
---|
382 | memcpy(pThread->szName, pszName, cchName);
|
---|
383 | pThread->szName[cchName] = '\0';
|
---|
384 | pThread->cRefs = 2 + !!(fFlags & RTTHREADFLAGS_WAITABLE); /* And extra reference if waitable. */
|
---|
385 | pThread->rc = VERR_PROCESS_RUNNING; /** @todo get a better error code! */
|
---|
386 | pThread->enmType = enmType;
|
---|
387 | pThread->fFlags = fFlags;
|
---|
388 | pThread->fIntFlags = fIntFlags;
|
---|
389 | pThread->enmState = RTTHREADSTATE_INITIALIZING;
|
---|
390 | pThread->fReallySleeping = false;
|
---|
391 | #ifdef IN_RING3
|
---|
392 | rtLockValidatorInitPerThread(&pThread->LockValidator);
|
---|
393 | #endif
|
---|
394 | #ifdef RT_WITH_ICONV_CACHE
|
---|
395 | rtStrIconvCacheInit(pThread);
|
---|
396 | #endif
|
---|
397 | rc = RTSemEventMultiCreate(&pThread->EventUser);
|
---|
398 | if (RT_SUCCESS(rc))
|
---|
399 | {
|
---|
400 | rc = RTSemEventMultiCreate(&pThread->EventTerminated);
|
---|
401 | if (RT_SUCCESS(rc))
|
---|
402 | return pThread;
|
---|
403 | RTSemEventMultiDestroy(pThread->EventUser);
|
---|
404 | }
|
---|
405 | RTMemFree(pThread);
|
---|
406 | }
|
---|
407 | return NULL;
|
---|
408 | }
|
---|
409 |
|
---|
410 |
|
---|
411 | /**
|
---|
412 | * Insert the per thread data structure into the tree.
|
---|
413 | *
|
---|
414 | * This can be called from both the thread it self and the parent,
|
---|
415 | * thus it must handle insertion failures in a nice manner.
|
---|
416 | *
|
---|
417 | * @param pThread Pointer to thread structure allocated by rtThreadAlloc().
|
---|
418 | * @param NativeThread The native thread id.
|
---|
419 | */
|
---|
420 | DECLHIDDEN(void) rtThreadInsert(PRTTHREADINT pThread, RTNATIVETHREAD NativeThread)
|
---|
421 | {
|
---|
422 | Assert(pThread);
|
---|
423 | Assert(pThread->u32Magic == RTTHREADINT_MAGIC);
|
---|
424 |
|
---|
425 | {
|
---|
426 | RT_THREAD_LOCK_RW();
|
---|
427 |
|
---|
428 | /*
|
---|
429 | * Do not insert a terminated thread.
|
---|
430 | *
|
---|
431 | * This may happen if the thread finishes before the RTThreadCreate call
|
---|
432 | * gets this far. Since the OS may quickly reuse the native thread ID
|
---|
433 | * it should not be reinserted at this point.
|
---|
434 | */
|
---|
435 | if (rtThreadGetState(pThread) != RTTHREADSTATE_TERMINATED)
|
---|
436 | {
|
---|
437 | /*
|
---|
438 | * Before inserting we must check if there is a thread with this id
|
---|
439 | * in the tree already. We're racing parent and child on insert here
|
---|
440 | * so that the handle is valid in both ends when they return / start.
|
---|
441 | *
|
---|
442 | * If it's not ourself we find, it's a dead alien thread and we will
|
---|
443 | * unlink it from the tree. Alien threads will be released at this point.
|
---|
444 | */
|
---|
445 | PRTTHREADINT pThreadOther = (PRTTHREADINT)RTAvlPVGet(&g_ThreadTree, (void *)NativeThread);
|
---|
446 | if (pThreadOther != pThread)
|
---|
447 | {
|
---|
448 | bool fRc;
|
---|
449 | /* remove dead alien if any */
|
---|
450 | if (pThreadOther)
|
---|
451 | {
|
---|
452 | AssertMsg(pThreadOther->fIntFlags & RTTHREADINT_FLAGS_ALIEN, ("%p:%s; %p:%s\n", pThread, pThread->szName, pThreadOther, pThreadOther->szName));
|
---|
453 | ASMAtomicBitClear(&pThread->fIntFlags, RTTHREADINT_FLAG_IN_TREE_BIT);
|
---|
454 | rtThreadRemoveLocked(pThreadOther);
|
---|
455 | if (pThreadOther->fIntFlags & RTTHREADINT_FLAGS_ALIEN)
|
---|
456 | rtThreadRelease(pThreadOther);
|
---|
457 | }
|
---|
458 |
|
---|
459 | /* insert the thread */
|
---|
460 | ASMAtomicWritePtr(&pThread->Core.Key, (void *)NativeThread);
|
---|
461 | fRc = RTAvlPVInsert(&g_ThreadTree, &pThread->Core);
|
---|
462 | ASMAtomicOrU32(&pThread->fIntFlags, RTTHREADINT_FLAG_IN_TREE);
|
---|
463 | if (fRc)
|
---|
464 | {
|
---|
465 | ASMAtomicIncU32(&g_cThreadInTree);
|
---|
466 | ASMAtomicIncU32(&g_acRTThreadTypeStats[pThread->enmType]);
|
---|
467 | }
|
---|
468 |
|
---|
469 | AssertReleaseMsg(fRc, ("Lock problem? %p (%RTnthrd) %s\n", pThread, NativeThread, pThread->szName));
|
---|
470 | NOREF(fRc);
|
---|
471 | }
|
---|
472 | }
|
---|
473 |
|
---|
474 | RT_THREAD_UNLOCK_RW();
|
---|
475 | }
|
---|
476 | }
|
---|
477 |
|
---|
478 |
|
---|
479 | /**
|
---|
480 | * Removes the thread from the AVL tree, call owns the tree lock
|
---|
481 | * and has cleared the RTTHREADINT_FLAG_IN_TREE bit.
|
---|
482 | *
|
---|
483 | * @param pThread The thread to remove.
|
---|
484 | */
|
---|
485 | static void rtThreadRemoveLocked(PRTTHREADINT pThread)
|
---|
486 | {
|
---|
487 | PRTTHREADINT pThread2 = (PRTTHREADINT)RTAvlPVRemove(&g_ThreadTree, pThread->Core.Key);
|
---|
488 | AssertMsg(pThread2 == pThread, ("%p(%s) != %p (%p/%s)\n", pThread2, pThread2 ? pThread2->szName : "<null>",
|
---|
489 | pThread, pThread->Core.Key, pThread->szName));
|
---|
490 | if (pThread2)
|
---|
491 | {
|
---|
492 | ASMAtomicDecU32(&g_cThreadInTree);
|
---|
493 | ASMAtomicDecU32(&g_acRTThreadTypeStats[pThread->enmType]);
|
---|
494 | }
|
---|
495 | }
|
---|
496 |
|
---|
497 |
|
---|
498 | /**
|
---|
499 | * Removes the thread from the AVL tree.
|
---|
500 | *
|
---|
501 | * @param pThread The thread to remove.
|
---|
502 | */
|
---|
503 | static void rtThreadRemove(PRTTHREADINT pThread)
|
---|
504 | {
|
---|
505 | RT_THREAD_LOCK_RW();
|
---|
506 | if (ASMAtomicBitTestAndClear(&pThread->fIntFlags, RTTHREADINT_FLAG_IN_TREE_BIT))
|
---|
507 | rtThreadRemoveLocked(pThread);
|
---|
508 | RT_THREAD_UNLOCK_RW();
|
---|
509 | }
|
---|
510 |
|
---|
511 |
|
---|
512 | /**
|
---|
513 | * Checks if a thread is alive or not.
|
---|
514 | *
|
---|
515 | * @returns true if the thread is alive (or we don't really know).
|
---|
516 | * @returns false if the thread has surely terminate.
|
---|
517 | */
|
---|
518 | DECLINLINE(bool) rtThreadIsAlive(PRTTHREADINT pThread)
|
---|
519 | {
|
---|
520 | return !(pThread->fIntFlags & RTTHREADINT_FLAGS_TERMINATED);
|
---|
521 | }
|
---|
522 |
|
---|
523 |
|
---|
524 | /**
|
---|
525 | * Gets a thread by it's native ID.
|
---|
526 | *
|
---|
527 | * @returns pointer to the thread structure.
|
---|
528 | * @returns NULL if not a thread IPRT knows.
|
---|
529 | * @param NativeThread The native thread id.
|
---|
530 | */
|
---|
531 | DECLHIDDEN(PRTTHREADINT) rtThreadGetByNative(RTNATIVETHREAD NativeThread)
|
---|
532 | {
|
---|
533 | PRTTHREADINT pThread;
|
---|
534 | /*
|
---|
535 | * Simple tree lookup.
|
---|
536 | */
|
---|
537 | RT_THREAD_LOCK_RD();
|
---|
538 | pThread = (PRTTHREADINT)RTAvlPVGet(&g_ThreadTree, (void *)NativeThread);
|
---|
539 | RT_THREAD_UNLOCK_RD();
|
---|
540 | return pThread;
|
---|
541 | }
|
---|
542 |
|
---|
543 |
|
---|
544 | /**
|
---|
545 | * Gets the per thread data structure for a thread handle.
|
---|
546 | *
|
---|
547 | * @returns Pointer to the per thread data structure for Thread.
|
---|
548 | * The caller must release the thread using rtThreadRelease().
|
---|
549 | * @returns NULL if Thread was not found.
|
---|
550 | * @param Thread Thread id which structure is to be returned.
|
---|
551 | */
|
---|
552 | DECLHIDDEN(PRTTHREADINT) rtThreadGet(RTTHREAD Thread)
|
---|
553 | {
|
---|
554 | if ( Thread != NIL_RTTHREAD
|
---|
555 | && RT_VALID_PTR(Thread))
|
---|
556 | {
|
---|
557 | PRTTHREADINT pThread = (PRTTHREADINT)Thread;
|
---|
558 | if ( pThread->u32Magic == RTTHREADINT_MAGIC
|
---|
559 | && pThread->cRefs > 0)
|
---|
560 | {
|
---|
561 | ASMAtomicIncU32(&pThread->cRefs);
|
---|
562 | return pThread;
|
---|
563 | }
|
---|
564 | }
|
---|
565 |
|
---|
566 | AssertMsgFailed(("Thread=%RTthrd\n", Thread));
|
---|
567 | return NULL;
|
---|
568 | }
|
---|
569 |
|
---|
570 | /**
|
---|
571 | * Release a per thread data structure.
|
---|
572 | *
|
---|
573 | * @returns New reference count.
|
---|
574 | * @param pThread The thread structure to release.
|
---|
575 | */
|
---|
576 | DECLHIDDEN(uint32_t) rtThreadRelease(PRTTHREADINT pThread)
|
---|
577 | {
|
---|
578 | uint32_t cRefs;
|
---|
579 |
|
---|
580 | Assert(pThread);
|
---|
581 | if (pThread->cRefs >= 1)
|
---|
582 | {
|
---|
583 | cRefs = ASMAtomicDecU32(&pThread->cRefs);
|
---|
584 | if (!cRefs)
|
---|
585 | rtThreadDestroy(pThread);
|
---|
586 | }
|
---|
587 | else
|
---|
588 | {
|
---|
589 | cRefs = 0;
|
---|
590 | AssertFailed();
|
---|
591 | }
|
---|
592 | return cRefs;
|
---|
593 | }
|
---|
594 |
|
---|
595 |
|
---|
596 | /**
|
---|
597 | * Destroys the per thread data.
|
---|
598 | *
|
---|
599 | * @param pThread The thread to destroy.
|
---|
600 | */
|
---|
601 | static void rtThreadDestroy(PRTTHREADINT pThread)
|
---|
602 | {
|
---|
603 | RTSEMEVENTMULTI hEvt1, hEvt2;
|
---|
604 | /*
|
---|
605 | * Remove it from the tree and mark it as dead.
|
---|
606 | *
|
---|
607 | * Threads that has seen rtThreadTerminate and should already have been
|
---|
608 | * removed from the tree. There is probably no thread that should
|
---|
609 | * require removing here. However, be careful making sure that cRefs
|
---|
610 | * isn't 0 if we do or we'll blow up because the strict locking code
|
---|
611 | * will be calling us back.
|
---|
612 | */
|
---|
613 | if (ASMBitTest(&pThread->fIntFlags, RTTHREADINT_FLAG_IN_TREE_BIT))
|
---|
614 | {
|
---|
615 | ASMAtomicIncU32(&pThread->cRefs);
|
---|
616 | rtThreadRemove(pThread);
|
---|
617 | ASMAtomicDecU32(&pThread->cRefs);
|
---|
618 | }
|
---|
619 |
|
---|
620 | /*
|
---|
621 | * Invalidate the thread structure.
|
---|
622 | */
|
---|
623 | #ifdef IN_RING3
|
---|
624 | rtLockValidatorSerializeDestructEnter();
|
---|
625 |
|
---|
626 | rtLockValidatorDeletePerThread(&pThread->LockValidator);
|
---|
627 | #endif
|
---|
628 | #ifdef RT_WITH_ICONV_CACHE
|
---|
629 | rtStrIconvCacheDestroy(pThread);
|
---|
630 | #endif
|
---|
631 | ASMAtomicXchgU32(&pThread->u32Magic, RTTHREADINT_MAGIC_DEAD);
|
---|
632 | ASMAtomicWritePtr(&pThread->Core.Key, (void *)NIL_RTTHREAD);
|
---|
633 | pThread->enmType = RTTHREADTYPE_INVALID;
|
---|
634 | hEvt1 = pThread->EventUser;
|
---|
635 | pThread->EventUser = NIL_RTSEMEVENTMULTI;
|
---|
636 | hEvt2 = pThread->EventTerminated;
|
---|
637 | pThread->EventTerminated = NIL_RTSEMEVENTMULTI;
|
---|
638 |
|
---|
639 | #ifdef IN_RING3
|
---|
640 | rtLockValidatorSerializeDestructLeave();
|
---|
641 | #endif
|
---|
642 |
|
---|
643 | /*
|
---|
644 | * Destroy semaphore resources and free the bugger.
|
---|
645 | */
|
---|
646 | RTSemEventMultiDestroy(hEvt1);
|
---|
647 | if (hEvt2 != NIL_RTSEMEVENTMULTI)
|
---|
648 | RTSemEventMultiDestroy(hEvt2);
|
---|
649 |
|
---|
650 | rtThreadNativeDestroy(pThread);
|
---|
651 | RTMemFree(pThread);
|
---|
652 | }
|
---|
653 |
|
---|
654 |
|
---|
655 | /**
|
---|
656 | * Terminates the thread.
|
---|
657 | * Called by the thread wrapper function when the thread terminates.
|
---|
658 | *
|
---|
659 | * @param pThread The thread structure.
|
---|
660 | * @param rc The thread result code.
|
---|
661 | */
|
---|
662 | DECLHIDDEN(void) rtThreadTerminate(PRTTHREADINT pThread, int rc)
|
---|
663 | {
|
---|
664 | Assert(pThread->cRefs >= 1);
|
---|
665 |
|
---|
666 | /*
|
---|
667 | * Destroy TLS entries.
|
---|
668 | */
|
---|
669 | #ifdef IPRT_WITH_GENERIC_TLS
|
---|
670 | rtThreadTlsDestruction(pThread);
|
---|
671 | #elif defined(RT_OS_WINDOWS) && defined(IN_RING3)
|
---|
672 | rtThreadWinTlsDestruction();
|
---|
673 | #endif
|
---|
674 |
|
---|
675 | /*
|
---|
676 | * Set the rc, mark it terminated and signal anyone waiting.
|
---|
677 | */
|
---|
678 | pThread->rc = rc;
|
---|
679 | rtThreadSetState(pThread, RTTHREADSTATE_TERMINATED);
|
---|
680 | ASMAtomicOrU32(&pThread->fIntFlags, RTTHREADINT_FLAGS_TERMINATED);
|
---|
681 | if (pThread->EventTerminated != NIL_RTSEMEVENTMULTI)
|
---|
682 | RTSemEventMultiSignal(pThread->EventTerminated);
|
---|
683 |
|
---|
684 | /*
|
---|
685 | * Remove the thread from the tree so that there will be no
|
---|
686 | * key clashes in the AVL tree and release our reference to ourself.
|
---|
687 | */
|
---|
688 | rtThreadRemove(pThread);
|
---|
689 | rtThreadRelease(pThread);
|
---|
690 | }
|
---|
691 |
|
---|
692 |
|
---|
693 | /**
|
---|
694 | * The common thread main function.
|
---|
695 | * This is called by rtThreadNativeMain().
|
---|
696 | *
|
---|
697 | * @returns The status code of the thread.
|
---|
698 | * pThread is dereference by the thread before returning!
|
---|
699 | * @param pThread The thread structure.
|
---|
700 | * @param NativeThread The native thread id.
|
---|
701 | * @param pszThreadName The name of the thread (purely a dummy for backtrace).
|
---|
702 | */
|
---|
703 | DECL_HIDDEN_CALLBACK(int) rtThreadMain(PRTTHREADINT pThread, RTNATIVETHREAD NativeThread, const char *pszThreadName)
|
---|
704 | {
|
---|
705 | int rc;
|
---|
706 | NOREF(pszThreadName);
|
---|
707 | rtThreadInsert(pThread, NativeThread);
|
---|
708 | Log(("rtThreadMain: Starting: pThread=%p NativeThread=%RTnthrd Name=%s pfnThread=%p pvUser=%p\n",
|
---|
709 | pThread, NativeThread, pThread->szName, pThread->pfnThread, pThread->pvUser));
|
---|
710 |
|
---|
711 | /*
|
---|
712 | * Change the priority.
|
---|
713 | */
|
---|
714 | rc = rtThreadNativeSetPriority(pThread, pThread->enmType);
|
---|
715 | #ifdef IN_RING3
|
---|
716 | AssertMsgRC(rc, ("Failed to set priority of thread %p (%RTnthrd / %s) to enmType=%d enmPriority=%d rc=%Rrc\n",
|
---|
717 | pThread, NativeThread, pThread->szName, pThread->enmType, g_enmProcessPriority, rc));
|
---|
718 | #else
|
---|
719 | AssertMsgRC(rc, ("Failed to set priority of thread %p (%RTnthrd / %s) to enmType=%d rc=%Rrc\n",
|
---|
720 | pThread, NativeThread, pThread->szName, pThread->enmType, rc));
|
---|
721 | #endif
|
---|
722 |
|
---|
723 | /*
|
---|
724 | * Call thread function and terminate when it returns.
|
---|
725 | */
|
---|
726 | rtThreadSetState(pThread, RTTHREADSTATE_RUNNING);
|
---|
727 | rc = pThread->pfnThread(pThread, pThread->pvUser);
|
---|
728 |
|
---|
729 | /*
|
---|
730 | * Paranoia checks for leftover resources.
|
---|
731 | */
|
---|
732 | #ifdef RTSEMRW_STRICT
|
---|
733 | int32_t cWrite = ASMAtomicReadS32(&pThread->cWriteLocks);
|
---|
734 | Assert(!cWrite);
|
---|
735 | int32_t cRead = ASMAtomicReadS32(&pThread->cReadLocks);
|
---|
736 | Assert(!cRead);
|
---|
737 | #endif
|
---|
738 |
|
---|
739 | Log(("rtThreadMain: Terminating: rc=%d pThread=%p NativeThread=%RTnthrd Name=%s pfnThread=%p pvUser=%p\n",
|
---|
740 | rc, pThread, NativeThread, pThread->szName, pThread->pfnThread, pThread->pvUser));
|
---|
741 | rtThreadTerminate(pThread, rc);
|
---|
742 | return rc;
|
---|
743 | }
|
---|
744 |
|
---|
745 |
|
---|
746 | /**
|
---|
747 | * Create a new thread.
|
---|
748 | *
|
---|
749 | * @returns iprt status code.
|
---|
750 | * @param pThread Where to store the thread handle to the new thread. (optional)
|
---|
751 | * @param pfnThread The thread function.
|
---|
752 | * @param pvUser User argument.
|
---|
753 | * @param cbStack The size of the stack for the new thread.
|
---|
754 | * Use 0 for the default stack size.
|
---|
755 | * @param enmType The thread type. Used for deciding scheduling attributes
|
---|
756 | * of the thread.
|
---|
757 | * @param fFlags Flags of the RTTHREADFLAGS type (ORed together).
|
---|
758 | * @param pszName Thread name.
|
---|
759 | */
|
---|
760 | RTDECL(int) RTThreadCreate(PRTTHREAD pThread, PFNRTTHREAD pfnThread, void *pvUser, size_t cbStack,
|
---|
761 | RTTHREADTYPE enmType, unsigned fFlags, const char *pszName)
|
---|
762 | {
|
---|
763 | int rc;
|
---|
764 | PRTTHREADINT pThreadInt;
|
---|
765 |
|
---|
766 | LogFlow(("RTThreadCreate: pThread=%p pfnThread=%p pvUser=%p cbStack=%#x enmType=%d fFlags=%#x pszName=%p:{%s}\n",
|
---|
767 | pThread, pfnThread, pvUser, cbStack, enmType, fFlags, pszName, pszName));
|
---|
768 |
|
---|
769 | /*
|
---|
770 | * Validate input.
|
---|
771 | */
|
---|
772 | AssertPtrNullReturn(pThread, VERR_INVALID_POINTER);
|
---|
773 | AssertPtrReturn(pfnThread, VERR_INVALID_POINTER);
|
---|
774 | AssertMsgReturn(pszName && *pszName != '\0' && strlen(pszName) < RTTHREAD_NAME_LEN,
|
---|
775 | ("pszName=%s (max len is %d because of logging)\n", pszName, RTTHREAD_NAME_LEN - 1),
|
---|
776 | VERR_INVALID_PARAMETER);
|
---|
777 | AssertMsgReturn(!(fFlags & ~RTTHREADFLAGS_MASK), ("fFlags=%#x\n", fFlags), VERR_INVALID_FLAGS);
|
---|
778 |
|
---|
779 | /*
|
---|
780 | * Allocate thread argument.
|
---|
781 | */
|
---|
782 | pThreadInt = rtThreadAlloc(enmType, fFlags, 0, pszName);
|
---|
783 | if (pThreadInt)
|
---|
784 | {
|
---|
785 | RTNATIVETHREAD NativeThread;
|
---|
786 |
|
---|
787 | pThreadInt->pfnThread = pfnThread;
|
---|
788 | pThreadInt->pvUser = pvUser;
|
---|
789 | pThreadInt->cbStack = cbStack;
|
---|
790 |
|
---|
791 | rc = rtThreadNativeCreate(pThreadInt, &NativeThread);
|
---|
792 | if (RT_SUCCESS(rc))
|
---|
793 | {
|
---|
794 | rtThreadInsert(pThreadInt, NativeThread);
|
---|
795 | rtThreadRelease(pThreadInt);
|
---|
796 | Log(("RTThreadCreate: Created thread %p (%p) %s\n", pThreadInt, NativeThread, pszName));
|
---|
797 | if (pThread)
|
---|
798 | *pThread = pThreadInt;
|
---|
799 | return VINF_SUCCESS;
|
---|
800 | }
|
---|
801 |
|
---|
802 | pThreadInt->cRefs = 1;
|
---|
803 | rtThreadRelease(pThreadInt);
|
---|
804 | }
|
---|
805 | else
|
---|
806 | rc = VERR_NO_TMP_MEMORY;
|
---|
807 | LogFlow(("RTThreadCreate: Failed to create thread, rc=%Rrc\n", rc));
|
---|
808 | AssertReleaseRC(rc);
|
---|
809 | return rc;
|
---|
810 | }
|
---|
811 | RT_EXPORT_SYMBOL(RTThreadCreate);
|
---|
812 |
|
---|
813 |
|
---|
814 | /**
|
---|
815 | * Create a new thread.
|
---|
816 | *
|
---|
817 | * Same as RTThreadCreate except the name is given in the RTStrPrintfV form.
|
---|
818 | *
|
---|
819 | * @returns iprt status code.
|
---|
820 | * @param pThread See RTThreadCreate.
|
---|
821 | * @param pfnThread See RTThreadCreate.
|
---|
822 | * @param pvUser See RTThreadCreate.
|
---|
823 | * @param cbStack See RTThreadCreate.
|
---|
824 | * @param enmType See RTThreadCreate.
|
---|
825 | * @param fFlags See RTThreadCreate.
|
---|
826 | * @param pszNameFmt Thread name format.
|
---|
827 | * @param va Format arguments.
|
---|
828 | */
|
---|
829 | RTDECL(int) RTThreadCreateV(PRTTHREAD pThread, PFNRTTHREAD pfnThread, void *pvUser, size_t cbStack,
|
---|
830 | RTTHREADTYPE enmType, uint32_t fFlags, const char *pszNameFmt, va_list va)
|
---|
831 | {
|
---|
832 | char szName[RTTHREAD_NAME_LEN * 2];
|
---|
833 | RTStrPrintfV(szName, sizeof(szName), pszNameFmt, va);
|
---|
834 | return RTThreadCreate(pThread, pfnThread, pvUser, cbStack, enmType, fFlags, szName);
|
---|
835 | }
|
---|
836 | RT_EXPORT_SYMBOL(RTThreadCreateV);
|
---|
837 |
|
---|
838 |
|
---|
839 | /**
|
---|
840 | * Create a new thread.
|
---|
841 | *
|
---|
842 | * Same as RTThreadCreate except the name is given in the RTStrPrintf form.
|
---|
843 | *
|
---|
844 | * @returns iprt status code.
|
---|
845 | * @param pThread See RTThreadCreate.
|
---|
846 | * @param pfnThread See RTThreadCreate.
|
---|
847 | * @param pvUser See RTThreadCreate.
|
---|
848 | * @param cbStack See RTThreadCreate.
|
---|
849 | * @param enmType See RTThreadCreate.
|
---|
850 | * @param fFlags See RTThreadCreate.
|
---|
851 | * @param pszNameFmt Thread name format.
|
---|
852 | * @param ... Format arguments.
|
---|
853 | */
|
---|
854 | RTDECL(int) RTThreadCreateF(PRTTHREAD pThread, PFNRTTHREAD pfnThread, void *pvUser, size_t cbStack,
|
---|
855 | RTTHREADTYPE enmType, uint32_t fFlags, const char *pszNameFmt, ...)
|
---|
856 | {
|
---|
857 | va_list va;
|
---|
858 | int rc;
|
---|
859 | va_start(va, pszNameFmt);
|
---|
860 | rc = RTThreadCreateV(pThread, pfnThread, pvUser, cbStack, enmType, fFlags, pszNameFmt, va);
|
---|
861 | va_end(va);
|
---|
862 | return rc;
|
---|
863 | }
|
---|
864 | RT_EXPORT_SYMBOL(RTThreadCreateF);
|
---|
865 |
|
---|
866 |
|
---|
867 | /**
|
---|
868 | * Gets the native thread id of a IPRT thread.
|
---|
869 | *
|
---|
870 | * @returns The native thread id.
|
---|
871 | * @param Thread The IPRT thread.
|
---|
872 | */
|
---|
873 | RTDECL(RTNATIVETHREAD) RTThreadGetNative(RTTHREAD Thread)
|
---|
874 | {
|
---|
875 | PRTTHREADINT pThread = rtThreadGet(Thread);
|
---|
876 | if (pThread)
|
---|
877 | {
|
---|
878 | RTNATIVETHREAD NativeThread = (RTNATIVETHREAD)pThread->Core.Key;
|
---|
879 | rtThreadRelease(pThread);
|
---|
880 | return NativeThread;
|
---|
881 | }
|
---|
882 | return NIL_RTNATIVETHREAD;
|
---|
883 | }
|
---|
884 | RT_EXPORT_SYMBOL(RTThreadGetNative);
|
---|
885 |
|
---|
886 |
|
---|
887 | /**
|
---|
888 | * Gets the IPRT thread of a native thread.
|
---|
889 | *
|
---|
890 | * @returns The IPRT thread handle
|
---|
891 | * @returns NIL_RTTHREAD if not a thread known to IPRT.
|
---|
892 | * @param NativeThread The native thread handle/id.
|
---|
893 | */
|
---|
894 | RTDECL(RTTHREAD) RTThreadFromNative(RTNATIVETHREAD NativeThread)
|
---|
895 | {
|
---|
896 | PRTTHREADINT pThread = rtThreadGetByNative(NativeThread);
|
---|
897 | if (pThread)
|
---|
898 | return pThread;
|
---|
899 | return NIL_RTTHREAD;
|
---|
900 | }
|
---|
901 | RT_EXPORT_SYMBOL(RTThreadFromNative);
|
---|
902 |
|
---|
903 |
|
---|
904 | /**
|
---|
905 | * Gets the name of the current thread thread.
|
---|
906 | *
|
---|
907 | * @returns Pointer to readonly name string.
|
---|
908 | * @returns NULL on failure.
|
---|
909 | */
|
---|
910 | RTDECL(const char *) RTThreadSelfName(void)
|
---|
911 | {
|
---|
912 | RTTHREAD Thread = RTThreadSelf();
|
---|
913 | if (Thread != NIL_RTTHREAD)
|
---|
914 | {
|
---|
915 | PRTTHREADINT pThread = rtThreadGet(Thread);
|
---|
916 | if (pThread)
|
---|
917 | {
|
---|
918 | const char *pszName = pThread->szName;
|
---|
919 | rtThreadRelease(pThread);
|
---|
920 | return pszName;
|
---|
921 | }
|
---|
922 | }
|
---|
923 | return NULL;
|
---|
924 | }
|
---|
925 | RT_EXPORT_SYMBOL(RTThreadSelfName);
|
---|
926 |
|
---|
927 |
|
---|
928 | /**
|
---|
929 | * Gets the name of a thread.
|
---|
930 | *
|
---|
931 | * @returns Pointer to readonly name string.
|
---|
932 | * @returns NULL on failure.
|
---|
933 | * @param Thread Thread handle of the thread to query the name of.
|
---|
934 | */
|
---|
935 | RTDECL(const char *) RTThreadGetName(RTTHREAD Thread)
|
---|
936 | {
|
---|
937 | PRTTHREADINT pThread;
|
---|
938 | if (Thread == NIL_RTTHREAD)
|
---|
939 | return NULL;
|
---|
940 | pThread = rtThreadGet(Thread);
|
---|
941 | if (pThread)
|
---|
942 | {
|
---|
943 | const char *szName = pThread->szName;
|
---|
944 | rtThreadRelease(pThread);
|
---|
945 | return szName;
|
---|
946 | }
|
---|
947 | return NULL;
|
---|
948 | }
|
---|
949 | RT_EXPORT_SYMBOL(RTThreadGetName);
|
---|
950 |
|
---|
951 |
|
---|
952 | /**
|
---|
953 | * Sets the name of a thread.
|
---|
954 | *
|
---|
955 | * @returns iprt status code.
|
---|
956 | * @param Thread Thread handle of the thread to query the name of.
|
---|
957 | * @param pszName The thread name.
|
---|
958 | */
|
---|
959 | RTDECL(int) RTThreadSetName(RTTHREAD Thread, const char *pszName)
|
---|
960 | {
|
---|
961 | /*
|
---|
962 | * Validate input.
|
---|
963 | */
|
---|
964 | PRTTHREADINT pThread;
|
---|
965 | size_t cchName = strlen(pszName);
|
---|
966 | if (cchName >= RTTHREAD_NAME_LEN)
|
---|
967 | {
|
---|
968 | AssertMsgFailed(("pszName=%s is too long, max is %d\n", pszName, RTTHREAD_NAME_LEN - 1));
|
---|
969 | return VERR_INVALID_PARAMETER;
|
---|
970 | }
|
---|
971 | pThread = rtThreadGet(Thread);
|
---|
972 | if (!pThread)
|
---|
973 | return VERR_INVALID_HANDLE;
|
---|
974 |
|
---|
975 | /*
|
---|
976 | * Update the name.
|
---|
977 | */
|
---|
978 | pThread->szName[cchName] = '\0'; /* paranoia */
|
---|
979 | memcpy(pThread->szName, pszName, cchName);
|
---|
980 | rtThreadRelease(pThread);
|
---|
981 | return VINF_SUCCESS;
|
---|
982 | }
|
---|
983 | RT_EXPORT_SYMBOL(RTThreadSetName);
|
---|
984 |
|
---|
985 |
|
---|
986 | /**
|
---|
987 | * Checks if the specified thread is the main thread.
|
---|
988 | *
|
---|
989 | * @returns true if it is, false if it isn't.
|
---|
990 | *
|
---|
991 | * @param hThread The thread handle.
|
---|
992 | *
|
---|
993 | * @remarks This function may not return the correct value when rtR3Init was
|
---|
994 | * called on a thread of the than the main one. This could for
|
---|
995 | * instance happen when the DLL/DYLIB/SO containing IPRT is dynamically
|
---|
996 | * loaded at run time by a different thread.
|
---|
997 | */
|
---|
998 | RTDECL(bool) RTThreadIsMain(RTTHREAD hThread)
|
---|
999 | {
|
---|
1000 | if (hThread != NIL_RTTHREAD)
|
---|
1001 | {
|
---|
1002 | PRTTHREADINT pThread = rtThreadGet(hThread);
|
---|
1003 | if (pThread)
|
---|
1004 | {
|
---|
1005 | bool fRc = !!(pThread->fIntFlags & RTTHREADINT_FLAGS_MAIN);
|
---|
1006 | rtThreadRelease(pThread);
|
---|
1007 | return fRc;
|
---|
1008 | }
|
---|
1009 | }
|
---|
1010 | return false;
|
---|
1011 | }
|
---|
1012 | RT_EXPORT_SYMBOL(RTThreadIsMain);
|
---|
1013 |
|
---|
1014 |
|
---|
1015 | RTDECL(bool) RTThreadIsSelfAlive(void)
|
---|
1016 | {
|
---|
1017 | if (g_frtThreadInitialized)
|
---|
1018 | {
|
---|
1019 | RTTHREAD hSelf = RTThreadSelf();
|
---|
1020 | if (hSelf != NIL_RTTHREAD)
|
---|
1021 | {
|
---|
1022 | /*
|
---|
1023 | * Inspect the thread state. ASSUMES thread state order.
|
---|
1024 | */
|
---|
1025 | RTTHREADSTATE enmState = rtThreadGetState(hSelf);
|
---|
1026 | if ( enmState >= RTTHREADSTATE_RUNNING
|
---|
1027 | && enmState <= RTTHREADSTATE_END)
|
---|
1028 | return true;
|
---|
1029 | }
|
---|
1030 | }
|
---|
1031 | return false;
|
---|
1032 | }
|
---|
1033 | RT_EXPORT_SYMBOL(RTThreadIsSelfAlive);
|
---|
1034 |
|
---|
1035 |
|
---|
1036 | RTDECL(bool) RTThreadIsSelfKnown(void)
|
---|
1037 | {
|
---|
1038 | if (g_frtThreadInitialized)
|
---|
1039 | {
|
---|
1040 | RTTHREAD hSelf = RTThreadSelf();
|
---|
1041 | if (hSelf != NIL_RTTHREAD)
|
---|
1042 | return true;
|
---|
1043 | }
|
---|
1044 | return false;
|
---|
1045 | }
|
---|
1046 | RT_EXPORT_SYMBOL(RTThreadIsSelfKnown);
|
---|
1047 |
|
---|
1048 |
|
---|
1049 | RTDECL(bool) RTThreadIsInitialized(void)
|
---|
1050 | {
|
---|
1051 | return g_frtThreadInitialized;
|
---|
1052 | }
|
---|
1053 | RT_EXPORT_SYMBOL(RTThreadIsInitialized);
|
---|
1054 |
|
---|
1055 |
|
---|
1056 | /**
|
---|
1057 | * Signal the user event.
|
---|
1058 | *
|
---|
1059 | * @returns iprt status code.
|
---|
1060 | */
|
---|
1061 | RTDECL(int) RTThreadUserSignal(RTTHREAD Thread)
|
---|
1062 | {
|
---|
1063 | int rc;
|
---|
1064 | PRTTHREADINT pThread = rtThreadGet(Thread);
|
---|
1065 | if (pThread)
|
---|
1066 | {
|
---|
1067 | rc = RTSemEventMultiSignal(pThread->EventUser);
|
---|
1068 | rtThreadRelease(pThread);
|
---|
1069 | }
|
---|
1070 | else
|
---|
1071 | rc = VERR_INVALID_HANDLE;
|
---|
1072 | return rc;
|
---|
1073 | }
|
---|
1074 | RT_EXPORT_SYMBOL(RTThreadUserSignal);
|
---|
1075 |
|
---|
1076 |
|
---|
1077 | /**
|
---|
1078 | * Wait for the user event, resume on interruption.
|
---|
1079 | *
|
---|
1080 | * @returns iprt status code.
|
---|
1081 | * @param Thread The thread to wait for.
|
---|
1082 | * @param cMillies The number of milliseconds to wait. Use RT_INDEFINITE_WAIT for
|
---|
1083 | * an indefinite wait.
|
---|
1084 | */
|
---|
1085 | RTDECL(int) RTThreadUserWait(RTTHREAD Thread, RTMSINTERVAL cMillies)
|
---|
1086 | {
|
---|
1087 | int rc;
|
---|
1088 | PRTTHREADINT pThread = rtThreadGet(Thread);
|
---|
1089 | if (pThread)
|
---|
1090 | {
|
---|
1091 | rc = RTSemEventMultiWait(pThread->EventUser, cMillies);
|
---|
1092 | rtThreadRelease(pThread);
|
---|
1093 | }
|
---|
1094 | else
|
---|
1095 | rc = VERR_INVALID_HANDLE;
|
---|
1096 | return rc;
|
---|
1097 | }
|
---|
1098 | RT_EXPORT_SYMBOL(RTThreadUserWait);
|
---|
1099 |
|
---|
1100 |
|
---|
1101 | /**
|
---|
1102 | * Wait for the user event, return on interruption.
|
---|
1103 | *
|
---|
1104 | * @returns iprt status code.
|
---|
1105 | * @param Thread The thread to wait for.
|
---|
1106 | * @param cMillies The number of milliseconds to wait. Use RT_INDEFINITE_WAIT for
|
---|
1107 | * an indefinite wait.
|
---|
1108 | */
|
---|
1109 | RTDECL(int) RTThreadUserWaitNoResume(RTTHREAD Thread, RTMSINTERVAL cMillies)
|
---|
1110 | {
|
---|
1111 | int rc;
|
---|
1112 | PRTTHREADINT pThread = rtThreadGet(Thread);
|
---|
1113 | if (pThread)
|
---|
1114 | {
|
---|
1115 | rc = RTSemEventMultiWaitNoResume(pThread->EventUser, cMillies);
|
---|
1116 | rtThreadRelease(pThread);
|
---|
1117 | }
|
---|
1118 | else
|
---|
1119 | rc = VERR_INVALID_HANDLE;
|
---|
1120 | return rc;
|
---|
1121 | }
|
---|
1122 | RT_EXPORT_SYMBOL(RTThreadUserWaitNoResume);
|
---|
1123 |
|
---|
1124 |
|
---|
1125 | /**
|
---|
1126 | * Reset the user event.
|
---|
1127 | *
|
---|
1128 | * @returns iprt status code.
|
---|
1129 | * @param Thread The thread to reset.
|
---|
1130 | */
|
---|
1131 | RTDECL(int) RTThreadUserReset(RTTHREAD Thread)
|
---|
1132 | {
|
---|
1133 | int rc;
|
---|
1134 | PRTTHREADINT pThread = rtThreadGet(Thread);
|
---|
1135 | if (pThread)
|
---|
1136 | {
|
---|
1137 | rc = RTSemEventMultiReset(pThread->EventUser);
|
---|
1138 | rtThreadRelease(pThread);
|
---|
1139 | }
|
---|
1140 | else
|
---|
1141 | rc = VERR_INVALID_HANDLE;
|
---|
1142 | return rc;
|
---|
1143 | }
|
---|
1144 | RT_EXPORT_SYMBOL(RTThreadUserReset);
|
---|
1145 |
|
---|
1146 |
|
---|
1147 | /**
|
---|
1148 | * Wait for the thread to terminate.
|
---|
1149 | *
|
---|
1150 | * @returns iprt status code.
|
---|
1151 | * @param Thread The thread to wait for.
|
---|
1152 | * @param cMillies The number of milliseconds to wait. Use RT_INDEFINITE_WAIT for
|
---|
1153 | * an indefinite wait.
|
---|
1154 | * @param prc Where to store the return code of the thread. Optional.
|
---|
1155 | * @param fAutoResume Whether or not to resume the wait on VERR_INTERRUPTED.
|
---|
1156 | */
|
---|
1157 | static int rtThreadWait(RTTHREAD Thread, RTMSINTERVAL cMillies, int *prc, bool fAutoResume)
|
---|
1158 | {
|
---|
1159 | int rc = VERR_INVALID_HANDLE;
|
---|
1160 | if (Thread != NIL_RTTHREAD)
|
---|
1161 | {
|
---|
1162 | PRTTHREADINT pThread = rtThreadGet(Thread);
|
---|
1163 | if (pThread)
|
---|
1164 | {
|
---|
1165 | if (pThread->fFlags & RTTHREADFLAGS_WAITABLE)
|
---|
1166 | {
|
---|
1167 | #if defined(IN_RING3) && defined(RT_OS_WINDOWS)
|
---|
1168 | if (RT_LIKELY(rtThreadNativeIsAliveKludge(pThread)))
|
---|
1169 | #endif
|
---|
1170 | {
|
---|
1171 | if (fAutoResume)
|
---|
1172 | rc = RTSemEventMultiWait(pThread->EventTerminated, cMillies);
|
---|
1173 | else
|
---|
1174 | rc = RTSemEventMultiWaitNoResume(pThread->EventTerminated, cMillies);
|
---|
1175 | }
|
---|
1176 | #if defined(IN_RING3) && defined(RT_OS_WINDOWS)
|
---|
1177 | else
|
---|
1178 | {
|
---|
1179 | rc = VINF_SUCCESS;
|
---|
1180 | if (pThread->rc == VERR_PROCESS_RUNNING)
|
---|
1181 | pThread->rc = VERR_THREAD_IS_DEAD;
|
---|
1182 | }
|
---|
1183 | #endif
|
---|
1184 | if (RT_SUCCESS(rc))
|
---|
1185 | {
|
---|
1186 | if (prc)
|
---|
1187 | *prc = pThread->rc;
|
---|
1188 |
|
---|
1189 | /*
|
---|
1190 | * If the thread is marked as waitable, we'll do one additional
|
---|
1191 | * release in order to free up the thread structure (see how we
|
---|
1192 | * init cRef in rtThreadAlloc()).
|
---|
1193 | */
|
---|
1194 | if (ASMAtomicBitTestAndClear(&pThread->fFlags, RTTHREADFLAGS_WAITABLE_BIT))
|
---|
1195 | {
|
---|
1196 | rtThreadRelease(pThread);
|
---|
1197 | #ifdef IN_RING0
|
---|
1198 | /*
|
---|
1199 | * IPRT termination kludge. Call native code to make sure
|
---|
1200 | * the last thread is really out of IPRT to prevent it from
|
---|
1201 | * crashing after we destroyed the spinlock in rtThreadTerm.
|
---|
1202 | */
|
---|
1203 | if ( ASMAtomicReadU32(&g_cThreadInTree) == 1
|
---|
1204 | && ASMAtomicReadU32(&pThread->cRefs) > 1)
|
---|
1205 | rtThreadNativeWaitKludge(pThread);
|
---|
1206 | #endif
|
---|
1207 | }
|
---|
1208 | }
|
---|
1209 | }
|
---|
1210 | else
|
---|
1211 | {
|
---|
1212 | rc = VERR_THREAD_NOT_WAITABLE;
|
---|
1213 | AssertRC(rc);
|
---|
1214 | }
|
---|
1215 | rtThreadRelease(pThread);
|
---|
1216 | }
|
---|
1217 | }
|
---|
1218 | return rc;
|
---|
1219 | }
|
---|
1220 |
|
---|
1221 |
|
---|
1222 | /**
|
---|
1223 | * Wait for the thread to terminate, resume on interruption.
|
---|
1224 | *
|
---|
1225 | * @returns iprt status code.
|
---|
1226 | * Will not return VERR_INTERRUPTED.
|
---|
1227 | * @param Thread The thread to wait for.
|
---|
1228 | * @param cMillies The number of milliseconds to wait. Use RT_INDEFINITE_WAIT for
|
---|
1229 | * an indefinite wait.
|
---|
1230 | * @param prc Where to store the return code of the thread. Optional.
|
---|
1231 | */
|
---|
1232 | RTDECL(int) RTThreadWait(RTTHREAD Thread, RTMSINTERVAL cMillies, int *prc)
|
---|
1233 | {
|
---|
1234 | int rc = rtThreadWait(Thread, cMillies, prc, true);
|
---|
1235 | Assert(rc != VERR_INTERRUPTED);
|
---|
1236 | return rc;
|
---|
1237 | }
|
---|
1238 | RT_EXPORT_SYMBOL(RTThreadWait);
|
---|
1239 |
|
---|
1240 |
|
---|
1241 | /**
|
---|
1242 | * Wait for the thread to terminate, return on interruption.
|
---|
1243 | *
|
---|
1244 | * @returns iprt status code.
|
---|
1245 | * @param Thread The thread to wait for.
|
---|
1246 | * @param cMillies The number of milliseconds to wait. Use RT_INDEFINITE_WAIT for
|
---|
1247 | * an indefinite wait.
|
---|
1248 | * @param prc Where to store the return code of the thread. Optional.
|
---|
1249 | */
|
---|
1250 | RTDECL(int) RTThreadWaitNoResume(RTTHREAD Thread, RTMSINTERVAL cMillies, int *prc)
|
---|
1251 | {
|
---|
1252 | return rtThreadWait(Thread, cMillies, prc, false);
|
---|
1253 | }
|
---|
1254 | RT_EXPORT_SYMBOL(RTThreadWaitNoResume);
|
---|
1255 |
|
---|
1256 |
|
---|
1257 | /**
|
---|
1258 | * Changes the type of the specified thread.
|
---|
1259 | *
|
---|
1260 | * @returns iprt status code.
|
---|
1261 | * @param Thread The thread which type should be changed.
|
---|
1262 | * @param enmType The new thread type.
|
---|
1263 | */
|
---|
1264 | RTDECL(int) RTThreadSetType(RTTHREAD Thread, RTTHREADTYPE enmType)
|
---|
1265 | {
|
---|
1266 | /*
|
---|
1267 | * Validate input.
|
---|
1268 | */
|
---|
1269 | int rc;
|
---|
1270 | if ( enmType > RTTHREADTYPE_INVALID
|
---|
1271 | && enmType < RTTHREADTYPE_END)
|
---|
1272 | {
|
---|
1273 | PRTTHREADINT pThread = rtThreadGet(Thread);
|
---|
1274 | if (pThread)
|
---|
1275 | {
|
---|
1276 | if (rtThreadIsAlive(pThread))
|
---|
1277 | {
|
---|
1278 | /*
|
---|
1279 | * Do the job.
|
---|
1280 | */
|
---|
1281 | RT_THREAD_LOCK_RW();
|
---|
1282 | rc = rtThreadNativeSetPriority(pThread, enmType);
|
---|
1283 | if (RT_SUCCESS(rc))
|
---|
1284 | ASMAtomicXchgSize(&pThread->enmType, enmType);
|
---|
1285 | RT_THREAD_UNLOCK_RW();
|
---|
1286 | if (RT_FAILURE(rc))
|
---|
1287 | Log(("RTThreadSetType: failed on thread %p (%s), rc=%Rrc!!!\n", Thread, pThread->szName, rc));
|
---|
1288 | }
|
---|
1289 | else
|
---|
1290 | rc = VERR_THREAD_IS_DEAD;
|
---|
1291 | rtThreadRelease(pThread);
|
---|
1292 | }
|
---|
1293 | else
|
---|
1294 | rc = VERR_INVALID_HANDLE;
|
---|
1295 | }
|
---|
1296 | else
|
---|
1297 | {
|
---|
1298 | AssertMsgFailed(("enmType=%d\n", enmType));
|
---|
1299 | rc = VERR_INVALID_PARAMETER;
|
---|
1300 | }
|
---|
1301 | return rc;
|
---|
1302 | }
|
---|
1303 | RT_EXPORT_SYMBOL(RTThreadSetType);
|
---|
1304 |
|
---|
1305 |
|
---|
1306 | /**
|
---|
1307 | * Gets the type of the specified thread.
|
---|
1308 | *
|
---|
1309 | * @returns The thread type.
|
---|
1310 | * @returns RTTHREADTYPE_INVALID if the thread handle is invalid.
|
---|
1311 | * @param Thread The thread in question.
|
---|
1312 | */
|
---|
1313 | RTDECL(RTTHREADTYPE) RTThreadGetType(RTTHREAD Thread)
|
---|
1314 | {
|
---|
1315 | RTTHREADTYPE enmType = RTTHREADTYPE_INVALID;
|
---|
1316 | PRTTHREADINT pThread = rtThreadGet(Thread);
|
---|
1317 | if (pThread)
|
---|
1318 | {
|
---|
1319 | enmType = pThread->enmType;
|
---|
1320 | rtThreadRelease(pThread);
|
---|
1321 | }
|
---|
1322 | return enmType;
|
---|
1323 | }
|
---|
1324 | RT_EXPORT_SYMBOL(RTThreadGetType);
|
---|
1325 |
|
---|
1326 | #ifdef IN_RING3
|
---|
1327 |
|
---|
1328 | /**
|
---|
1329 | * Recalculates scheduling attributes for the default process
|
---|
1330 | * priority using the specified priority type for the calling thread.
|
---|
1331 | *
|
---|
1332 | * The scheduling attributes are targeted at threads and they are protected
|
---|
1333 | * by the thread read-write semaphore, that's why RTProc is forwarding the
|
---|
1334 | * operation to RTThread.
|
---|
1335 | *
|
---|
1336 | * @returns iprt status code.
|
---|
1337 | * @remarks Will only work for strict builds.
|
---|
1338 | */
|
---|
1339 | int rtThreadDoCalcDefaultPriority(RTTHREADTYPE enmType)
|
---|
1340 | {
|
---|
1341 | RT_THREAD_LOCK_RW();
|
---|
1342 | int rc = rtSchedNativeCalcDefaultPriority(enmType);
|
---|
1343 | RT_THREAD_UNLOCK_RW();
|
---|
1344 | return rc;
|
---|
1345 | }
|
---|
1346 |
|
---|
1347 |
|
---|
1348 | /**
|
---|
1349 | * Thread enumerator - sets the priority of one thread.
|
---|
1350 | *
|
---|
1351 | * @returns 0 to continue.
|
---|
1352 | * @returns !0 to stop. In our case a VERR_ code.
|
---|
1353 | * @param pNode The thread node.
|
---|
1354 | * @param pvUser The new priority.
|
---|
1355 | */
|
---|
1356 | static DECLCALLBACK(int) rtThreadSetPriorityOne(PAVLPVNODECORE pNode, void *pvUser)
|
---|
1357 | {
|
---|
1358 | PRTTHREADINT pThread = (PRTTHREADINT)pNode;
|
---|
1359 | if (!rtThreadIsAlive(pThread))
|
---|
1360 | return VINF_SUCCESS;
|
---|
1361 | int rc = rtThreadNativeSetPriority(pThread, pThread->enmType);
|
---|
1362 | if (RT_SUCCESS(rc)) /* hide any warnings */
|
---|
1363 | return VINF_SUCCESS;
|
---|
1364 | NOREF(pvUser);
|
---|
1365 | return rc;
|
---|
1366 | }
|
---|
1367 |
|
---|
1368 |
|
---|
1369 | /**
|
---|
1370 | * Attempts to alter the priority of the current process.
|
---|
1371 | *
|
---|
1372 | * The scheduling attributes are targeted at threads and they are protected
|
---|
1373 | * by the thread read-write semaphore, that's why RTProc is forwarding the
|
---|
1374 | * operation to RTThread. This operation also involves updating all thread
|
---|
1375 | * which is much faster done from RTThread.
|
---|
1376 | *
|
---|
1377 | * @returns iprt status code.
|
---|
1378 | * @param enmPriority The new priority.
|
---|
1379 | */
|
---|
1380 | DECLHIDDEN(int) rtThreadDoSetProcPriority(RTPROCPRIORITY enmPriority)
|
---|
1381 | {
|
---|
1382 | LogFlow(("rtThreadDoSetProcPriority: enmPriority=%d\n", enmPriority));
|
---|
1383 |
|
---|
1384 | /*
|
---|
1385 | * First validate that we're allowed by the OS to use all the
|
---|
1386 | * scheduling attributes defined by the specified process priority.
|
---|
1387 | */
|
---|
1388 | RT_THREAD_LOCK_RW();
|
---|
1389 | int rc = rtProcNativeSetPriority(enmPriority);
|
---|
1390 | if (RT_SUCCESS(rc))
|
---|
1391 | {
|
---|
1392 | /*
|
---|
1393 | * Update the priority of existing thread.
|
---|
1394 | */
|
---|
1395 | rc = RTAvlPVDoWithAll(&g_ThreadTree, true, rtThreadSetPriorityOne, NULL);
|
---|
1396 | if (RT_SUCCESS(rc))
|
---|
1397 | ASMAtomicXchgSize(&g_enmProcessPriority, enmPriority);
|
---|
1398 | else
|
---|
1399 | {
|
---|
1400 | /*
|
---|
1401 | * Failed, restore the priority.
|
---|
1402 | */
|
---|
1403 | rtProcNativeSetPriority(g_enmProcessPriority);
|
---|
1404 | RTAvlPVDoWithAll(&g_ThreadTree, true, rtThreadSetPriorityOne, NULL);
|
---|
1405 | }
|
---|
1406 | }
|
---|
1407 | RT_THREAD_UNLOCK_RW();
|
---|
1408 | LogFlow(("rtThreadDoSetProcPriority: returns %Rrc\n", rc));
|
---|
1409 | return rc;
|
---|
1410 | }
|
---|
1411 |
|
---|
1412 |
|
---|
1413 | /**
|
---|
1414 | * Change the thread state to blocking.
|
---|
1415 | *
|
---|
1416 | * @param hThread The current thread.
|
---|
1417 | * @param enmState The sleep state.
|
---|
1418 | * @param fReallySleeping Really going to sleep now.
|
---|
1419 | */
|
---|
1420 | RTDECL(void) RTThreadBlocking(RTTHREAD hThread, RTTHREADSTATE enmState, bool fReallySleeping)
|
---|
1421 | {
|
---|
1422 | Assert(RTTHREAD_IS_SLEEPING(enmState));
|
---|
1423 | PRTTHREADINT pThread = hThread;
|
---|
1424 | if (pThread != NIL_RTTHREAD)
|
---|
1425 | {
|
---|
1426 | Assert(pThread == RTThreadSelf());
|
---|
1427 | if (rtThreadGetState(pThread) == RTTHREADSTATE_RUNNING)
|
---|
1428 | rtThreadSetState(pThread, enmState);
|
---|
1429 | ASMAtomicWriteBool(&pThread->fReallySleeping, fReallySleeping);
|
---|
1430 | }
|
---|
1431 | }
|
---|
1432 | RT_EXPORT_SYMBOL(RTThreadBlocking);
|
---|
1433 |
|
---|
1434 |
|
---|
1435 | /**
|
---|
1436 | * Unblocks a thread.
|
---|
1437 | *
|
---|
1438 | * This function is paired with rtThreadBlocking.
|
---|
1439 | *
|
---|
1440 | * @param hThread The current thread.
|
---|
1441 | * @param enmCurState The current state, used to check for nested blocking.
|
---|
1442 | * The new state will be running.
|
---|
1443 | */
|
---|
1444 | RTDECL(void) RTThreadUnblocked(RTTHREAD hThread, RTTHREADSTATE enmCurState)
|
---|
1445 | {
|
---|
1446 | PRTTHREADINT pThread = hThread;
|
---|
1447 | if (pThread != NIL_RTTHREAD)
|
---|
1448 | {
|
---|
1449 | Assert(pThread == RTThreadSelf());
|
---|
1450 | ASMAtomicWriteBool(&pThread->fReallySleeping, false);
|
---|
1451 |
|
---|
1452 | RTTHREADSTATE enmActualState = rtThreadGetState(pThread);
|
---|
1453 | if (enmActualState == enmCurState)
|
---|
1454 | {
|
---|
1455 | rtThreadSetState(pThread, RTTHREADSTATE_RUNNING);
|
---|
1456 | if ( pThread->LockValidator.pRec
|
---|
1457 | && pThread->LockValidator.enmRecState == enmCurState)
|
---|
1458 | ASMAtomicWriteNullPtr(&pThread->LockValidator.pRec);
|
---|
1459 | }
|
---|
1460 | /* This is a bit ugly... :-/ */
|
---|
1461 | else if ( ( enmActualState == RTTHREADSTATE_TERMINATED
|
---|
1462 | || enmActualState == RTTHREADSTATE_INITIALIZING)
|
---|
1463 | && pThread->LockValidator.pRec)
|
---|
1464 | ASMAtomicWriteNullPtr(&pThread->LockValidator.pRec);
|
---|
1465 | Assert( pThread->LockValidator.pRec == NULL
|
---|
1466 | || RTTHREAD_IS_SLEEPING(enmActualState));
|
---|
1467 | }
|
---|
1468 | }
|
---|
1469 | RT_EXPORT_SYMBOL(RTThreadUnblocked);
|
---|
1470 |
|
---|
1471 |
|
---|
1472 | /**
|
---|
1473 | * Get the current thread state.
|
---|
1474 | *
|
---|
1475 | * @returns The thread state.
|
---|
1476 | * @param hThread The thread.
|
---|
1477 | */
|
---|
1478 | RTDECL(RTTHREADSTATE) RTThreadGetState(RTTHREAD hThread)
|
---|
1479 | {
|
---|
1480 | RTTHREADSTATE enmState = RTTHREADSTATE_INVALID;
|
---|
1481 | PRTTHREADINT pThread = rtThreadGet(hThread);
|
---|
1482 | if (pThread)
|
---|
1483 | {
|
---|
1484 | enmState = rtThreadGetState(pThread);
|
---|
1485 | rtThreadRelease(pThread);
|
---|
1486 | }
|
---|
1487 | return enmState;
|
---|
1488 | }
|
---|
1489 | RT_EXPORT_SYMBOL(RTThreadGetState);
|
---|
1490 |
|
---|
1491 |
|
---|
1492 | RTDECL(RTTHREADSTATE) RTThreadGetReallySleeping(RTTHREAD hThread)
|
---|
1493 | {
|
---|
1494 | RTTHREADSTATE enmState = RTTHREADSTATE_INVALID;
|
---|
1495 | PRTTHREADINT pThread = rtThreadGet(hThread);
|
---|
1496 | if (pThread)
|
---|
1497 | {
|
---|
1498 | enmState = rtThreadGetState(pThread);
|
---|
1499 | if (!ASMAtomicUoReadBool(&pThread->fReallySleeping))
|
---|
1500 | enmState = RTTHREADSTATE_RUNNING;
|
---|
1501 | rtThreadRelease(pThread);
|
---|
1502 | }
|
---|
1503 | return enmState;
|
---|
1504 | }
|
---|
1505 | RT_EXPORT_SYMBOL(RTThreadGetReallySleeping);
|
---|
1506 |
|
---|
1507 |
|
---|
1508 | /**
|
---|
1509 | * Translate a thread state into a string.
|
---|
1510 | *
|
---|
1511 | * @returns Pointer to a read-only string containing the state name.
|
---|
1512 | * @param enmState The state.
|
---|
1513 | */
|
---|
1514 | RTDECL(const char *) RTThreadStateName(RTTHREADSTATE enmState)
|
---|
1515 | {
|
---|
1516 | switch (enmState)
|
---|
1517 | {
|
---|
1518 | case RTTHREADSTATE_INVALID: return "INVALID";
|
---|
1519 | case RTTHREADSTATE_INITIALIZING: return "INITIALIZING";
|
---|
1520 | case RTTHREADSTATE_TERMINATED: return "TERMINATED";
|
---|
1521 | case RTTHREADSTATE_RUNNING: return "RUNNING";
|
---|
1522 | case RTTHREADSTATE_CRITSECT: return "CRITSECT";
|
---|
1523 | case RTTHREADSTATE_EVENT: return "EVENT";
|
---|
1524 | case RTTHREADSTATE_EVENT_MULTI: return "EVENT_MULTI";
|
---|
1525 | case RTTHREADSTATE_FAST_MUTEX: return "FAST_MUTEX";
|
---|
1526 | case RTTHREADSTATE_MUTEX: return "MUTEX";
|
---|
1527 | case RTTHREADSTATE_RW_READ: return "RW_READ";
|
---|
1528 | case RTTHREADSTATE_RW_WRITE: return "RW_WRITE";
|
---|
1529 | case RTTHREADSTATE_SLEEP: return "SLEEP";
|
---|
1530 | case RTTHREADSTATE_SPIN_MUTEX: return "SPIN_MUTEX";
|
---|
1531 | default: return "UnknownThreadState";
|
---|
1532 | }
|
---|
1533 | }
|
---|
1534 | RT_EXPORT_SYMBOL(RTThreadStateName);
|
---|
1535 |
|
---|
1536 | #endif /* IN_RING3 */
|
---|
1537 | #ifdef IPRT_WITH_GENERIC_TLS
|
---|
1538 |
|
---|
1539 | /**
|
---|
1540 | * Thread enumerator - clears a TLS entry.
|
---|
1541 | *
|
---|
1542 | * @returns 0.
|
---|
1543 | * @param pNode The thread node.
|
---|
1544 | * @param pvUser The TLS index.
|
---|
1545 | */
|
---|
1546 | static DECLCALLBACK(int) rtThreadClearTlsEntryCallback(PAVLPVNODECORE pNode, void *pvUser)
|
---|
1547 | {
|
---|
1548 | PRTTHREADINT pThread = (PRTTHREADINT)pNode;
|
---|
1549 | RTTLS iTls = (RTTLS)(uintptr_t)pvUser;
|
---|
1550 | ASMAtomicWriteNullPtr(&pThread->apvTlsEntries[iTls]);
|
---|
1551 | return 0;
|
---|
1552 | }
|
---|
1553 |
|
---|
1554 |
|
---|
1555 | /**
|
---|
1556 | * Helper for the generic TLS implementation that clears a given TLS
|
---|
1557 | * entry on all threads.
|
---|
1558 | *
|
---|
1559 | * @param iTls The TLS entry. (valid)
|
---|
1560 | */
|
---|
1561 | DECLHIDDEN(void) rtThreadClearTlsEntry(RTTLS iTls)
|
---|
1562 | {
|
---|
1563 | RT_THREAD_LOCK_RD();
|
---|
1564 | RTAvlPVDoWithAll(&g_ThreadTree, true /* fFromLeft*/, rtThreadClearTlsEntryCallback, (void *)(uintptr_t)iTls);
|
---|
1565 | RT_THREAD_UNLOCK_RD();
|
---|
1566 | }
|
---|
1567 |
|
---|
1568 | #endif /* IPRT_WITH_GENERIC_TLS */
|
---|
1569 |
|
---|
1570 |
|
---|
1571 | #if defined(RT_OS_WINDOWS) && defined(IN_RING3)
|
---|
1572 |
|
---|
1573 | /**
|
---|
1574 | * Thread enumeration callback for RTThreadNameThreads
|
---|
1575 | */
|
---|
1576 | static DECLCALLBACK(int) rtThreadNameThreadCallback(PAVLPVNODECORE pNode, void *pvUser)
|
---|
1577 | {
|
---|
1578 | PRTTHREADINT pThread = (PRTTHREADINT)pNode;
|
---|
1579 | rtThreadNativeInformDebugger(pThread);
|
---|
1580 | RT_NOREF_PV(pvUser);
|
---|
1581 | return 0;
|
---|
1582 | }
|
---|
1583 |
|
---|
1584 | /**
|
---|
1585 | * A function that can be called from the windows debugger to get the names of
|
---|
1586 | * all threads when attaching to a process.
|
---|
1587 | *
|
---|
1588 | * Usage: .call VBoxRT!RTThreadNameThreads()
|
---|
1589 | *
|
---|
1590 | * @returns 0
|
---|
1591 | * @remarks Do not call from source code as it skips locks.
|
---|
1592 | */
|
---|
1593 | extern "C" RTDECL(int) RTThreadNameThreads(void);
|
---|
1594 | RTDECL(int) RTThreadNameThreads(void)
|
---|
1595 | {
|
---|
1596 | return RTAvlPVDoWithAll(&g_ThreadTree, true /* fFromLeft*/, rtThreadNameThreadCallback, NULL);
|
---|
1597 | }
|
---|
1598 |
|
---|
1599 | #endif
|
---|