VirtualBox

source: vbox/trunk/include/iprt/thread.h@ 9251

Last change on this file since 9251 was 8645, checked in by vboxsync, 17 years ago

Added lock counts to the threads.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 15.5 KB
Line 
1/** @file
2 * IPRT - Threads.
3 */
4
5/*
6 * Copyright (C) 2006-2007 Sun Microsystems, Inc.
7 *
8 * This file is part of VirtualBox Open Source Edition (OSE), as
9 * available from http://www.virtualbox.org. This file is free software;
10 * you can redistribute it and/or modify it under the terms of the GNU
11 * General Public License (GPL) as published by the Free Software
12 * Foundation, in version 2 as it comes in the "COPYING" file of the
13 * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
14 * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
15 *
16 * The contents of this file may alternatively be used under the terms
17 * of the Common Development and Distribution License Version 1.0
18 * (CDDL) only, as it comes in the "COPYING.CDDL" file of the
19 * VirtualBox OSE distribution, in which case the provisions of the
20 * CDDL are applicable instead of those of the GPL.
21 *
22 * You may elect to license modified versions of this file under the
23 * terms and conditions of either the GPL or the CDDL or both.
24 *
25 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa
26 * Clara, CA 95054 USA or visit http://www.sun.com if you need
27 * additional information or have any questions.
28 */
29
30#ifndef ___iprt_thread_h
31#define ___iprt_thread_h
32
33#include <iprt/cdefs.h>
34#include <iprt/types.h>
35
36#ifdef IN_GC
37# error "There are no threading APIs available Guest Context!"
38#endif
39
40
41
42__BEGIN_DECLS
43
44/** @defgroup grp_rt_thread RTThread - Thread Management
45 * @ingroup grp_rt
46 * @{
47 */
48
49/**
50 * Get the thread handle of the current thread.
51 *
52 * @returns Thread handle.
53 */
54RTDECL(RTTHREAD) RTThreadSelf(void);
55
56/**
57 * Get the native thread handle of the current thread.
58 *
59 * @returns Native thread handle.
60 */
61RTDECL(RTNATIVETHREAD) RTThreadNativeSelf(void);
62
63/**
64 * Millisecond granular sleep function.
65 *
66 * @returns VINF_SUCCESS on success.
67 * @returns VERR_INTERRUPTED if a signal or other asynchronous stuff happend
68 * which interrupt the peaceful sleep.
69 * @param cMillies Number of milliseconds to sleep.
70 * 0 milliseconds means yielding the timeslice - deprecated!
71 * @remark See RTThreadNanoSleep() for sleeping for smaller periods of time.
72 */
73RTDECL(int) RTThreadSleep(unsigned cMillies);
74
75/**
76 * Yields the CPU.
77 *
78 * @returns true if we yielded.
79 * @returns false if it's probable that we didn't yield.
80 */
81RTDECL(bool) RTThreadYield(void);
82
83
84
85/**
86 * Thread function.
87 *
88 * @returns 0 on success.
89 * @param ThreadSelf Thread handle to this thread.
90 * @param pvUser User argument.
91 */
92typedef DECLCALLBACK(int) FNRTTHREAD(RTTHREAD ThreadSelf, void *pvUser);
93/** Pointer to a FNRTTHREAD(). */
94typedef FNRTTHREAD *PFNRTTHREAD;
95
96/**
97 * Thread types.
98 * Besides identifying the purpose of the thread, the thread type is
99 * used to select the scheduling properties.
100 *
101 * The types in are placed in a rough order of ascending priority.
102 */
103typedef enum RTTHREADTYPE
104{
105 /** Invalid type. */
106 RTTHREADTYPE_INVALID = 0,
107 /** Infrequent poller thread.
108 * This type of thread will sleep for the most of the time, and do
109 * infrequent polls on resources at 0.5 sec or higher intervals.
110 */
111 RTTHREADTYPE_INFREQUENT_POLLER,
112 /** Main heavy worker thread.
113 * Thread of this type is driving asynchronous tasks in the Main
114 * API which takes a long time and might involve a bit of CPU. Like
115 * for instance creating a fixed sized VDI.
116 */
117 RTTHREADTYPE_MAIN_HEAVY_WORKER,
118 /** The emulation thread type.
119 * While being a thread with very high workload it still is vital
120 * that it gets scheduled frequently. When possible all other thread
121 * types except DEFAULT and GUI should interrupt this one ASAP when
122 * they become ready.
123 */
124 RTTHREADTYPE_EMULATION,
125 /** The default thread type.
126 * Since it doesn't say much about the purpose of the thread
127 * nothing special is normally done to the scheduling. This type
128 * should be avoided.
129 * The main thread is registered with default type during RTR3Init()
130 * and that's what the default process priority is derived from.
131 */
132 RTTHREADTYPE_DEFAULT,
133 /** The GUI thread type
134 * The GUI normally have a low workload but is frequently scheduled
135 * to handle events. When possible the scheduler should not leave
136 * threads of this kind waiting for too long (~50ms).
137 */
138 RTTHREADTYPE_GUI,
139 /** Main worker thread.
140 * Thread of this type is driving asynchronous tasks in the Main API.
141 * In most cases this means little work an a lot of waiting.
142 */
143 RTTHREADTYPE_MAIN_WORKER,
144 /** VRDP I/O thread.
145 * These threads are I/O threads in the RDP server will hang around
146 * waiting for data, process it and pass it on.
147 */
148 RTTHREADTYPE_VRDP_IO,
149 /** The debugger type.
150 * Threads involved in servicing the debugger. It must remain
151 * responsive even when things are running wild in.
152 */
153 RTTHREADTYPE_DEBUGGER,
154 /** Message pump thread.
155 * Thread pumping messages from one thread/process to another
156 * thread/process. The workload is very small, most of the time
157 * it's blocked waiting for messages to be procduced or processed.
158 * This type of thread will be favored after I/O threads.
159 */
160 RTTHREADTYPE_MSG_PUMP,
161 /** The I/O thread type.
162 * Doing I/O means shuffling data, waiting for request to arrive and
163 * for them to complete. The thread should be favored when competing
164 * with any other threads except timer threads.
165 */
166 RTTHREADTYPE_IO,
167 /** The timer thread type.
168 * A timer thread is mostly waiting for the timer to tick
169 * and then perform a little bit of work. Accuracy is important here,
170 * so the thread should be favoured over all threads. If premention can
171 * be configured at thread level, it could be made very short.
172 */
173 RTTHREADTYPE_TIMER,
174 /** Only used for validation. */
175 RTTHREADTYPE_END
176} RTTHREADTYPE;
177
178
179/**
180 * Thread creation flags.
181 */
182typedef enum RTTHREADFLAGS
183{
184 /**
185 * This flag is used to keep the thread structure around so it can
186 * be waited on after termination.
187 */
188 RTTHREADFLAGS_WAITABLE = RT_BIT(0),
189 /** The bit number corresponding to the RTTHREADFLAGS_WAITABLE mask. */
190 RTTHREADFLAGS_WAITABLE_BIT = 0,
191
192 /** Mask of valid flags, use for validation. */
193 RTTHREADFLAGS_MASK = RT_BIT(0)
194} RTTHREADFLAGS;
195
196
197/**
198 * Create a new thread.
199 *
200 * @returns iprt status code.
201 * @param pThread Where to store the thread handle to the new thread. (optional)
202 * @param pfnThread The thread function.
203 * @param pvUser User argument.
204 * @param cbStack The size of the stack for the new thread.
205 * Use 0 for the default stack size.
206 * @param enmType The thread type. Used for deciding scheduling attributes
207 * of the thread.
208 * @param fFlags Flags of the RTTHREADFLAGS type (ORed together).
209 * @param pszName Thread name.
210 *
211 * @remark When called in Ring-0, this API will create a new kernel thread and not a thread in
212 * the context of the calling process.
213 */
214RTDECL(int) RTThreadCreate(PRTTHREAD pThread, PFNRTTHREAD pfnThread, void *pvUser, size_t cbStack,
215 RTTHREADTYPE enmType, unsigned fFlags, const char *pszName);
216
217/**
218 * Gets the native thread id of a IPRT thread.
219 *
220 * @returns The native thread id.
221 * @param Thread The IPRT thread.
222 */
223RTDECL(RTNATIVETHREAD) RTThreadGetNative(RTTHREAD Thread);
224
225/**
226 * Gets the IPRT thread of a native thread.
227 *
228 * @returns The IPRT thread handle
229 * @returns NIL_RTTHREAD if not a thread known to IPRT.
230 * @param NativeThread The native thread handle/id.
231 */
232RTDECL(RTTHREAD) RTThreadFromNative(RTNATIVETHREAD NativeThread);
233
234/**
235 * Changes the type of the specified thread.
236 *
237 * @returns iprt status code.
238 * @param Thread The thread which type should be changed.
239 * @param enmType The new thread type.
240 * @remark In Ring-0 it only works if Thread == RTThreadSelf().
241 */
242RTDECL(int) RTThreadSetType(RTTHREAD Thread, RTTHREADTYPE enmType);
243
244/**
245 * Wait for the thread to terminate, resume on interruption.
246 *
247 * @returns iprt status code.
248 * Will not return VERR_INTERRUPTED.
249 * @param Thread The thread to wait for.
250 * @param cMillies The number of milliseconds to wait. Use RT_INDEFINITE_WAIT for
251 * an indefinite wait.
252 * @param prc Where to store the return code of the thread. Optional.
253 */
254RTDECL(int) RTThreadWait(RTTHREAD Thread, unsigned cMillies, int *prc);
255
256/**
257 * Wait for the thread to terminate, return on interruption.
258 *
259 * @returns iprt status code.
260 * @param Thread The thread to wait for.
261 * @param cMillies The number of milliseconds to wait. Use RT_INDEFINITE_WAIT for
262 * an indefinite wait.
263 * @param prc Where to store the return code of the thread. Optional.
264 */
265RTDECL(int) RTThreadWaitNoResume(RTTHREAD Thread, unsigned cMillies, int *prc);
266
267/**
268 * Gets the name of the current thread thread.
269 *
270 * @returns Pointer to readonly name string.
271 * @returns NULL on failure.
272 */
273RTDECL(const char *) RTThreadSelfName(void);
274
275/**
276 * Gets the name of a thread.
277 *
278 * @returns Pointer to readonly name string.
279 * @returns NULL on failure.
280 * @param Thread Thread handle of the thread to query the name of.
281 */
282RTDECL(const char *) RTThreadGetName(RTTHREAD Thread);
283
284/**
285 * Gets the type of the specified thread.
286 *
287 * @returns The thread type.
288 * @returns RTTHREADTYPE_INVALID if the thread handle is invalid.
289 * @param Thread The thread in question.
290 */
291RTDECL(RTTHREADTYPE) RTThreadGetType(RTTHREAD Thread);
292
293/**
294 * Sets the name of a thread.
295 *
296 * @returns iprt status code.
297 * @param Thread Thread handle of the thread to query the name of.
298 * @param pszName The thread name.
299 */
300RTDECL(int) RTThreadSetName(RTTHREAD Thread, const char *pszName);
301
302/**
303 * Signal the user event.
304 *
305 * @returns iprt status code.
306 */
307RTDECL(int) RTThreadUserSignal(RTTHREAD Thread);
308
309/**
310 * Wait for the user event.
311 *
312 * @returns iprt status code.
313 * @param Thread The thread to wait for.
314 * @param cMillies The number of milliseconds to wait. Use RT_INDEFINITE_WAIT for
315 * an indefinite wait.
316 */
317RTDECL(int) RTThreadUserWait(RTTHREAD Thread, unsigned cMillies);
318
319/**
320 * Wait for the user event, return on interruption.
321 *
322 * @returns iprt status code.
323 * @param Thread The thread to wait for.
324 * @param cMillies The number of milliseconds to wait. Use RT_INDEFINITE_WAIT for
325 * an indefinite wait.
326 */
327RTDECL(int) RTThreadUserWaitNoResume(RTTHREAD Thread, unsigned cMillies);
328
329/**
330 * Reset the user event.
331 *
332 * @returns iprt status code.
333 * @param Thread The thread to reset.
334 */
335RTDECL(int) RTThreadUserReset(RTTHREAD Thread);
336
337
338#ifdef IN_RING3
339
340/**
341 * Adopts a non-IPRT thread.
342 *
343 * @returns IPRT status code.
344 * @param enmType The thread type.
345 * @param fFlags The thread flags. RTTHREADFLAGS_WAITABLE is not currently allowed.
346 * @param pszName The thread name. Optional
347 * @param pThread Where to store the thread handle. Optional.
348 */
349RTDECL(int) RTThreadAdopt(RTTHREADTYPE enmType, unsigned fFlags, const char *pszName, PRTTHREAD pThread);
350
351/**
352 * Gets the affinity mask of the current thread.
353 *
354 * @returns The affinity mask (bit 0 = logical cpu 0).
355 */
356RTR3DECL(uint64_t) RTThreadGetAffinity(void);
357
358/**
359 * Sets the affinity mask of the current thread.
360 *
361 * @returns iprt status code.
362 * @param u64Mask Affinity mask (bit 0 = logical cpu 0).
363 */
364RTR3DECL(int) RTThreadSetAffinity(uint64_t u64Mask);
365
366/**
367 * Gets the number of write locks and critical sections the specified
368 * thread owns.
369 *
370 * This number does not include any nested lock/critect entries.
371 *
372 * Note that it probably will return 0 for non-strict builds since
373 * release builds doesn't do unnecessary diagnostic counting like this.
374 *
375 * @returns Number of locks on success (0+) and VERR_INVALID_HANDLER on failure
376 * @param Thread The thread we're inquiring about.
377 * @remarks Will only work for strict builds.
378 */
379RTDECL(int32_t) RTThreadGetWriteLockCount(RTTHREAD Thread);
380
381/**
382 * Works the THREADINT::cWriteLocks member, mostly internal.
383 *
384 * @param Thread The current thread.
385 */
386RTDECL(void) RTThreadWriteLockInc(RTTHREAD Thread);
387
388/**
389 * Works the THREADINT::cWriteLocks member, mostly internal.
390 *
391 * @param Thread The current thread.
392 */
393RTDECL(void) RTThreadWriteLockDec(RTTHREAD Thread);
394
395/**
396 * Gets the number of read locks the specified thread owns.
397 *
398 * Note that nesting read lock entry will be included in the
399 * total sum. And that it probably will return 0 for non-strict
400 * builds since release builds doesn't do unnecessary diagnostic
401 * counting like this.
402 *
403 * @returns Number of read locks on success (0+) and VERR_INVALID_HANDLER on failure
404 * @param Thread The thread we're inquiring about.
405 */
406RTDECL(int32_t) RTThreadGetReadLockCount(RTTHREAD Thread);
407
408/**
409 * Works the THREADINT::cReadLocks member.
410 *
411 * @param Thread The current thread.
412 */
413RTDECL(void) RTThreadReadLockInc(RTTHREAD Thread);
414
415/**
416 * Works the THREADINT::cReadLocks member.
417 *
418 * @param Thread The current thread.
419 */
420RTDECL(void) RTThreadReadLockDec(RTTHREAD Thread);
421
422
423/** @name Thread Local Storage
424 * @{
425 */
426/**
427 * Thread termination callback for destroying a non-zero TLS entry.
428 *
429 * @remarks It is not permittable to use any RTTls APIs at this time. Doing so
430 * may lead to endless loops, crashes, and other bad stuff.
431 *
432 * @param pvValue The current value.
433 */
434typedef DECLCALLBACK(void) FNRTTLSDTOR(void *pvValue);
435/** Pointer to a FNRTTLSDTOR. */
436typedef FNRTTLSDTOR *PFNRTTLSDTOR;
437
438/**
439 * Allocates a TLS entry.
440 *
441 * @returns the index of the allocated TLS entry.
442 * @returns NIL_RTTLS on failure.
443 */
444RTR3DECL(RTTLS) RTTlsAlloc(void);
445
446/**
447 * Allocates a TLS entry (with status code).
448 *
449 * @returns IPRT status code.
450 * @retval VERR_NOT_SUPPORTED if pfnDestructor is non-NULL and the platform
451 * doesn't support this feature.
452 *
453 * @param piTls Where to store the index of the allocated TLS entry.
454 * This is set to NIL_RTTLS on failure.
455 * @param pfnDestructor Optional callback function for cleaning up on
456 * thread termination. WARNING! This feature may not
457 * be implemented everywhere.
458 */
459RTR3DECL(int) RTTlsAllocEx(PRTTLS piTls, PFNRTTLSDTOR pfnDestructor);
460
461/**
462 * Frees a TLS entry.
463 *
464 * @returns IPRT status code.
465 * @param iTls The index of the TLS entry.
466 */
467RTR3DECL(int) RTTlsFree(RTTLS iTls);
468
469/**
470 * Get the value stored in a TLS entry.
471 *
472 * @returns value in given TLS entry.
473 * @returns NULL on failure.
474 * @param iTls The index of the TLS entry.
475 */
476RTR3DECL(void *) RTTlsGet(RTTLS iTls);
477
478/**
479 * Get the value stored in a TLS entry.
480 *
481 * @returns IPRT status code.
482 * @param iTls The index of the TLS entry.
483 * @param ppvValue Where to store the value.
484 */
485RTR3DECL(int) RTTlsGetEx(RTTLS iTls, void **ppvValue);
486
487/**
488 * Set the value stored in an allocated TLS entry.
489 *
490 * @returns IPRT status.
491 * @param iTls The index of the TLS entry.
492 * @param pvValue The value to store.
493 *
494 * @remarks Note that NULL is considered to special value.
495 */
496RTR3DECL(int) RTTlsSet(RTTLS iTls, void *pvValue);
497
498/** @} */
499
500#endif /* IN_RING3 */
501
502/** @} */
503
504__END_DECLS
505
506#endif
507
Note: See TracBrowser for help on using the repository browser.

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