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