1 | /* $Id: thread.h 1315 2004-03-17 03:59:28Z bird $ */
|
---|
2 | /** @file
|
---|
3 | *
|
---|
4 | * LIBC Thread Handling.
|
---|
5 | *
|
---|
6 | * Copyright (c) 2004 knut st. osmundsen <bird-srcspam@anduin.net>
|
---|
7 | *
|
---|
8 | *
|
---|
9 | * This file is part of InnoTek LIBC.
|
---|
10 | *
|
---|
11 | * InnoTek LIBC is free software; you can redistribute it and/or modify
|
---|
12 | * it under the terms of the GNU General Public License as published by
|
---|
13 | * the Free Software Foundation; either version 2 of the License, or
|
---|
14 | * (at your option) any later version.
|
---|
15 | *
|
---|
16 | * InnoTek LIBC is distributed in the hope that it will be useful,
|
---|
17 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
18 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
---|
19 | * GNU General Public License for more details.
|
---|
20 | *
|
---|
21 | * You should have received a copy of the GNU General Public License
|
---|
22 | * along with InnoTek LIBC; if not, write to the Free Software
|
---|
23 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
---|
24 | *
|
---|
25 | */
|
---|
26 |
|
---|
27 | #ifndef __InnoTekLIBC_thread_h__
|
---|
28 | #define __InnoTekLIBC_thread_h__
|
---|
29 |
|
---|
30 | /*******************************************************************************
|
---|
31 | * Defined Constants And Macros *
|
---|
32 | *******************************************************************************/
|
---|
33 | /** Maximum number of TLS variables supported by LIBC.
|
---|
34 | * The current limit (128) is higher than for instance WinNT. But if
|
---|
35 | * you think we're wasting space, look at the page padding of the thread
|
---|
36 | * structure... */
|
---|
37 | #define __LIBC_TLS_MAX 128
|
---|
38 |
|
---|
39 |
|
---|
40 | /*******************************************************************************
|
---|
41 | * Header Files *
|
---|
42 | *******************************************************************************/
|
---|
43 | #include <sys/cdefs.h>
|
---|
44 | #include <time.h> /* struct tm; */
|
---|
45 | #include <signal.h>
|
---|
46 |
|
---|
47 |
|
---|
48 | /*******************************************************************************
|
---|
49 | * Structures and Typedefs *
|
---|
50 | *******************************************************************************/
|
---|
51 | struct _uheap;
|
---|
52 |
|
---|
53 | /**
|
---|
54 | * Per thread structure for LIBC.
|
---|
55 | *
|
---|
56 | * This structure contains buffers and variables which in a single threaded
|
---|
57 | * LIBC would be static.
|
---|
58 | *
|
---|
59 | * Members most frequently used have been put together at the front.
|
---|
60 | */
|
---|
61 | typedef struct __libc_thread
|
---|
62 | {
|
---|
63 | /** errno value. (Comes first, see errnofun.s.) */
|
---|
64 | int iErrNo;
|
---|
65 | /** Default tiled heap. */
|
---|
66 | struct _uheap * pTiledHeap;
|
---|
67 | /** Default regular heap. */
|
---|
68 | struct _uheap * pRegularHeap;
|
---|
69 | /** Old TLS variable. */
|
---|
70 | void *pvThreadStoreVar;
|
---|
71 | /** New TLS variable array. */
|
---|
72 | void *apvTLS[__LIBC_TLS_MAX];
|
---|
73 |
|
---|
74 | /** The nesting depth of the default logger. */
|
---|
75 | unsigned cDefLoggerDepth;
|
---|
76 | /** Current rand() seed value. */
|
---|
77 | unsigned int iRand;
|
---|
78 | /** Buffer used by asctime() and indirectly by ctime() . (Adds two 2 bytes for padding). */
|
---|
79 | char szAscTimeAndCTimeBuf[26+2];
|
---|
80 | /** Buffer used by gmtime() and localtime(). */
|
---|
81 | struct tm GmTimeAndLocalTimeBuf;
|
---|
82 | /** Buffer used by tmpnam(). */
|
---|
83 | char szTmpNamBuf[16];
|
---|
84 | /** Current posistion of strtok(). */
|
---|
85 | unsigned char *pszStrTokPos;
|
---|
86 | /** Buffer used by strerror() to format unknown error values. */
|
---|
87 | char szStrErrorBuf[28];
|
---|
88 | /** Buffer used by _getvol(). */
|
---|
89 | char szVolLabelBuf[12];
|
---|
90 | /** Buffer used by ttyname(). */
|
---|
91 | char szTTYNameBuf[32];
|
---|
92 |
|
---|
93 | /** Data used by the backends. */
|
---|
94 | union __libc_backend_data
|
---|
95 | {
|
---|
96 | struct __libc_sys
|
---|
97 | {
|
---|
98 | /** Blocked signal mask. */
|
---|
99 | sigset_t sig_blocked;
|
---|
100 | /** Pending signal mask. */
|
---|
101 | sigset_t sig_pending;
|
---|
102 | /** Signal actions. */
|
---|
103 | struct sigaction signals[NSIG];
|
---|
104 |
|
---|
105 | /** Directory find data entry.
|
---|
106 | * Used by __findfirst() and __findnext(). */
|
---|
107 | struct find_data
|
---|
108 | {
|
---|
109 | /** Directory handle. HDIR_CREATE if no session opened. */
|
---|
110 | unsigned long hdir;
|
---|
111 | /** Type of buffer content. FIL_STANDARDL or FIL_STANDARD,
|
---|
112 | * i.e. FILEFINDBUF3 or FILEFINDBUF3L. */
|
---|
113 | unsigned long fType;
|
---|
114 | /** Number of files left in the buffer. */
|
---|
115 | unsigned long cFiles;
|
---|
116 | /** Pointer to the next entry. Don't test on this, test on cFiles! */
|
---|
117 | const char *pchNext;
|
---|
118 | /** Buffer. */
|
---|
119 | char achBuffer[2048];
|
---|
120 | } fd;
|
---|
121 | } sys;
|
---|
122 | } b;
|
---|
123 |
|
---|
124 | /** Data used in special periods of a threads life. */
|
---|
125 | union
|
---|
126 | {
|
---|
127 | struct __libc_thread_startup
|
---|
128 | {
|
---|
129 | /** Thread argument. */
|
---|
130 | void *pvArg;
|
---|
131 | /** Thread routine. */
|
---|
132 | void (*pfnStart)(void *pvArg);
|
---|
133 | } startup;
|
---|
134 | } u;
|
---|
135 | } __LIBC_THREAD;
|
---|
136 |
|
---|
137 | #ifndef __LIBC_THREAD_DECLARED
|
---|
138 | #define __LIBC_THREAD_DECLARED
|
---|
139 | typedef struct __libc_thread *__LIBC_PTHREAD, **__LIBC_PPTHREAD;
|
---|
140 | #endif
|
---|
141 |
|
---|
142 |
|
---|
143 | /*******************************************************************************
|
---|
144 | * Global Variables *
|
---|
145 | *******************************************************************************/
|
---|
146 | __BEGIN_DECLS
|
---|
147 | /** Pointer to the TLS ULONG (OS/2 rules) which will point to the LIBC thread
|
---|
148 | * structure for the current thread.
|
---|
149 | * The TLS ULONG is allocated by __init_dll(). The thread structure it points to
|
---|
150 | * is allocated on demand. */
|
---|
151 | extern __LIBC_PPTHREAD __libc_gpTLS;
|
---|
152 | __END_DECLS
|
---|
153 |
|
---|
154 |
|
---|
155 | /*******************************************************************************
|
---|
156 | * External Functions *
|
---|
157 | *******************************************************************************/
|
---|
158 | __BEGIN_DECLS
|
---|
159 | /**
|
---|
160 | * Get the thread structure for the current thread.
|
---|
161 | *
|
---|
162 | * Will automatically allocate a thread structure if such is not yet done
|
---|
163 | * for the thread.
|
---|
164 | *
|
---|
165 | * @returns pointer to current thread struct.
|
---|
166 |
|
---|
167 | * @remark This API is considered to be internal to LIBC and is thus not
|
---|
168 | * exposed in the shared library version of LIBC. Please don't call it.
|
---|
169 | * External LIBs should use the __libc_TLS*() API.
|
---|
170 | */
|
---|
171 | #define __libc_threadCurrent() (*__libc_gpTLS ? *__libc_gpTLS : __libc_threadCurrentSlow())
|
---|
172 |
|
---|
173 |
|
---|
174 | /**
|
---|
175 | * Get the thread structure for the current thread.
|
---|
176 | *
|
---|
177 | * Used by the __libc_threadCurrent() macro for allocating a thread structure for the
|
---|
178 | * current thread when such doesn't exist.
|
---|
179 | *
|
---|
180 | * @returns pointer to current thread struct.
|
---|
181 |
|
---|
182 | * @remark This API is considered to be internal to LIBC and is thus not
|
---|
183 | * exposed in the shared library version of LIBC. Please don't call it.
|
---|
184 | * External LIBs should use the __libc_TLS*() API.
|
---|
185 | */
|
---|
186 | __LIBC_PTHREAD __libc_threadCurrentSlow(void);
|
---|
187 |
|
---|
188 |
|
---|
189 | /**
|
---|
190 | * Get the thread structure for the current thread.
|
---|
191 | *
|
---|
192 | * Do not create anything automatically.
|
---|
193 | *
|
---|
194 | * @returns pointer to current thread struct.
|
---|
195 | * @returns NULL if not initiated.
|
---|
196 | *
|
---|
197 | * @remark This API is considered to be internal to LIBC and is thus not
|
---|
198 | * exposed in the shared library version of LIBC. Please don't call it.
|
---|
199 | * External LIBs should use the __libc_TLS*() API.
|
---|
200 | */
|
---|
201 | #define __libc_threadCurrentNoAuto() (__libc_gpTLS ? *__libc_gpTLS : NULL)
|
---|
202 |
|
---|
203 |
|
---|
204 | /**
|
---|
205 | * Allocate and initialize a thread structure for a thread which is yet
|
---|
206 | * to be created.
|
---|
207 | *
|
---|
208 | * @returns Pointer to thread structure.
|
---|
209 | * @returns NULL on error. errno set.
|
---|
210 | */
|
---|
211 | __LIBC_PTHREAD __libc_threadAlloc(void);
|
---|
212 |
|
---|
213 |
|
---|
214 | /**
|
---|
215 | * Free a thread structure.
|
---|
216 | *
|
---|
217 | * @param pThrd Pointer to the thread structure to free.
|
---|
218 | * Must be valid.
|
---|
219 | * @remark If pThrd is for the current thread the thread must be
|
---|
220 | * in the very final termination stage.
|
---|
221 | */
|
---|
222 | void __libc_threadFree(__LIBC_PTHREAD pThrd);
|
---|
223 |
|
---|
224 |
|
---|
225 | /** @group InnoTek LIBC Thread Local Storage
|
---|
226 | * @{
|
---|
227 | */
|
---|
228 |
|
---|
229 | /**
|
---|
230 | * Allocates a TLS entry.
|
---|
231 | *
|
---|
232 | * @returns index of the allocated TLS index.
|
---|
233 | * @returns -1 on failure. errno set.
|
---|
234 | */
|
---|
235 | int __libc_TLSAlloc(void);
|
---|
236 |
|
---|
237 | /**
|
---|
238 | * Frees a TLS entry allocated by __libc_TLSAlloc().
|
---|
239 | *
|
---|
240 | * @returns 0 on success.
|
---|
241 | * @returns -1 on failure. errno set.
|
---|
242 | * @param iTLSIndex Value returned by __libc_TLSAlloc().
|
---|
243 | */
|
---|
244 | int __libc_TLSFree(int iIndex);
|
---|
245 |
|
---|
246 | /**
|
---|
247 | * Get the value stored in an allocated TLS entry.
|
---|
248 | *
|
---|
249 | * @returns value in given TLS entry.
|
---|
250 | * @returns NULL on failure with errno set.
|
---|
251 | * @param iTLSIndex Value returned by __libc_TLSAlloc().
|
---|
252 | */
|
---|
253 | void * __libc_TLSGet(int iIndex);
|
---|
254 |
|
---|
255 | /**
|
---|
256 | * Set the value stored in an allocated TLS entry.
|
---|
257 | *
|
---|
258 | * @returns 0 on success.
|
---|
259 | * @returns -1 on failure. errno set.
|
---|
260 | * @param iTLSIndex Value returned by __libc_TLSAlloc().
|
---|
261 | * @param pvValue Value to store.
|
---|
262 | */
|
---|
263 | int __libc_TLSSet(int iIndex, void *pvValue);
|
---|
264 |
|
---|
265 | /** @} */
|
---|
266 | __END_DECLS
|
---|
267 |
|
---|
268 | #endif
|
---|