VirtualBox

source: vbox/trunk/src/VBox/Runtime/include/internal/thread.h@ 2413

Last change on this file since 2413 was 1816, checked in by vboxsync, 18 years ago

moved magics to a common header to avoid duplicating the same defines all over the place.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 6.9 KB
Line 
1/* $Id: thread.h 1816 2007-03-29 18:59:35Z vboxsync $ */
2/** @file
3 * InnoTek Portable Runtime - Internal RTThread header.
4 */
5
6/*
7 * Copyright (C) 2006 InnoTek Systemberatung GmbH
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 as published by the Free Software Foundation,
13 * in version 2 as it comes in the "COPYING" file of the VirtualBox OSE
14 * distribution. VirtualBox OSE is distributed in the hope that it will
15 * be useful, but WITHOUT ANY WARRANTY of any kind.
16 *
17 * If you received this file as part of a commercial VirtualBox
18 * distribution, then only the terms of your commercial VirtualBox
19 * license agreement apply instead of the previous paragraph.
20 */
21
22#ifndef __thread_h__
23#define __thread_h__
24
25#include <iprt/types.h>
26#include <iprt/thread.h>
27#include <iprt/avl.h>
28#ifdef IN_RING3
29# include <iprt/process.h>
30# include <iprt/critsect.h>
31#endif
32#include "internal/magics.h"
33
34__BEGIN_DECLS
35
36
37
38/**
39 * The thread state.
40 */
41typedef enum RTTHREADSTATE
42{
43 /** The usual invalid 0 value. */
44 RTTHREADSTATE_INVALID = 0,
45 /** The thread is being initialized. */
46 RTTHREADSTATE_INITIALIZING,
47 /** The thread has terminated */
48 RTTHREADSTATE_TERMINATED,
49 /** Probably running. */
50 RTTHREADSTATE_RUNNING,
51 /** Waiting on a critical section. */
52 RTTHREADSTATE_CRITSECT,
53 /** Waiting on a mutex. */
54 RTTHREADSTATE_MUTEX,
55 /** Waiting on a event semaphore. */
56 RTTHREADSTATE_EVENT,
57 /** Waiting on a event multiple wakeup semaphore. */
58 RTTHREADSTATE_EVENTMULTI,
59 /** The thread is sleeping. */
60 RTTHREADSTATE_SLEEP,
61 /** The usual 32-bit size hack. */
62 RTTHREADSTATE_32BIT_HACK = 0x7fffffff
63} RTTHREADSTATE;
64
65
66/** Checks if a thread state indicates that the thread is sleeping. */
67#define RTTHREAD_IS_SLEEPING(enmState) ( (enmState) == RTTHREADSTATE_CRITSECT \
68 || (enmState) == RTTHREADSTATE_MUTEX \
69 || (enmState) == RTTHREADSTATE_EVENT \
70 || (enmState) == RTTHREADSTATE_EVENTMULTI \
71 || (enmState) == RTTHREADSTATE_SLEEP \
72 )
73
74/** Max thread name length. */
75#define RTTHREAD_NAME_LEN 16
76
77/**
78 * Internal represenation of a thread.
79 */
80typedef struct RTTHREADINT
81{
82 /** Avl node core - the key is the native thread id. */
83 AVLPVNODECORE Core;
84 /** Magic value (RTTHREADINT_MAGIC). */
85 uint32_t u32Magic;
86 /** Reference counter. */
87 uint32_t volatile cRefs;
88 /** The current thread state. */
89 RTTHREADSTATE volatile enmState;
90#if defined(__WIN__) && defined(IN_RING3)
91 /** The thread handle.
92 * This is not valid until the create function has returned! */
93 uintptr_t hThread;
94#endif
95 /** The user event semaphore. */
96 RTSEMEVENTMULTI EventUser;
97 /** The terminated event semaphore. */
98 RTSEMEVENTMULTI EventTerminated;
99 /** The thread type. */
100 RTTHREADTYPE enmType;
101 /** The thread creation flags. (RTTHREADFLAGS) */
102 unsigned fFlags;
103 /** Internal flags. (RTTHREADINT_FLAGS_ *) */
104 uint32_t fIntFlags;
105 /** The result code. */
106 int rc;
107 /** Thread function. */
108 PFNRTTHREAD pfnThread;
109 /** Thread function argument. */
110 void *pvUser;
111 /** Actual stack size. */
112 size_t cbStack;
113#ifdef IN_RING3
114 /** What we're blocking on. */
115 union RTTHREADINTBLOCKID
116 {
117 uint64_t u64;
118 PRTCRITSECT pCritSect;
119 RTSEMEVENT Event;
120 RTSEMEVENTMULTI EventMulti;
121 RTSEMMUTEX Mutex;
122 } Block;
123 /** Where we're blocking. */
124 const char volatile *pszBlockFile;
125 /** Where we're blocking. */
126 unsigned volatile uBlockLine;
127 /** Where we're blocking. */
128 RTUINTPTR volatile uBlockId;
129#endif /* IN_RING3 */
130 /** Thread name. */
131 char szName[RTTHREAD_NAME_LEN];
132} RTTHREADINT, *PRTTHREADINT;
133
134
135/** @name RTTHREADINT::fIntFlags Masks and Bits.
136 * @{ */
137/** Set if the thread is an alien thread.
138 * Clear if the thread was created by IPRT. */
139#define RTTHREADINT_FLAGS_ALIEN BIT(0)
140/** Set if the thread has terminated.
141 * Clear if the thread is running. */
142#define RTTHREADINT_FLAGS_TERMINATED BIT(1)
143/** This bit is set if the thread is in the AVL tree. */
144#define RTTHREADINT_FLAG_IN_TREE_BIT 2
145/** @copydoc RTTHREADINT_FLAG_IN_TREE_BIT */
146#define RTTHREADINT_FLAG_IN_TREE BIT(RTTHREADINT_FLAG_IN_TREE_BIT)
147/** @} */
148
149
150/**
151 * Initialize the native part of the thread management.
152 *
153 * Generally a TLS entry will be allocated at this point (Ring-3).
154 *
155 * @returns iprt status code.
156 */
157int rtThreadNativeInit(void);
158
159/**
160 * Create a native thread.
161 * This creates the thread as described in pThreadInt and stores the thread id in *pThread.
162 *
163 * @returns iprt status code.
164 * @param pThreadInt The thread data structure for the thread.
165 * @param pNativeThread Where to store the native thread identifier.
166 */
167int rtThreadNativeCreate(PRTTHREADINT pThreadInt, PRTNATIVETHREAD pNativeThread);
168
169/**
170 * Adopts a thread, this is called immediately after allocating the
171 * thread structure.
172 *
173 * @param pThread Pointer to the thread structure.
174 */
175int rtThreadNativeAdopt(PRTTHREADINT pThread);
176
177/**
178 * Sets the priority of the thread according to the thread type
179 * and current process priority.
180 *
181 * The RTTHREADINT::enmType member has not yet been updated and will be updated by
182 * the caller on a successful return.
183 *
184 * @returns iprt status code.
185 * @param Thread The thread in question.
186 * @param enmType The thread type.
187 * @remark Located in sched.
188 */
189int rtThreadNativeSetPriority(PRTTHREADINT pThread, RTTHREADTYPE enmType);
190
191#ifdef IN_RING3
192# ifdef __WIN__
193/**
194 * Callback for when a native thread is detaching.
195 *
196 * It give the Win32/64 backend a chance to terminate alien
197 * threads properly.
198 */
199void rtThreadNativeDetach(void);
200# endif
201#endif /* !IN_RING0 */
202
203
204/* thread.cpp */
205int rtThreadMain(PRTTHREADINT pThread, RTNATIVETHREAD NativeThread, const char *pszThreadName);
206void rtThreadBlocking(PRTTHREADINT pThread, RTTHREADSTATE enmState, uint64_t u64Block,
207 const char *pszFile, unsigned uLine, RTUINTPTR uId);
208void rtThreadUnblocked(PRTTHREADINT pThread, RTTHREADSTATE enmCurState);
209uint32_t rtThreadRelease(PRTTHREADINT pThread);
210void rtThreadTerminate(PRTTHREADINT pThread, int rc);
211PRTTHREADINT rtThreadGetByNative(RTNATIVETHREAD NativeThread);
212PRTTHREADINT rtThreadGet(RTTHREAD Thread);
213int rtThreadInit(void);
214void rtThreadTerm(void);
215void rtThreadInsert(PRTTHREADINT pThread, RTNATIVETHREAD NativeThread);
216#ifdef IN_RING3
217int rtThreadDoSetProcPriority(RTPROCPRIORITY enmPriority);
218#endif /* !IN_RING0 */
219
220__END_DECLS
221
222#endif
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