| 1 | /* $Id: thread.h 1287 2004-03-11 03:05:55Z bird $ */
|
|---|
| 2 | /** @file
|
|---|
| 3 | *
|
|---|
| 4 | * LIBC Thread Handling.
|
|---|
| 5 | *
|
|---|
| 6 | * Copyright (c) 2004 knut st. osmundsen <[email protected]>
|
|---|
| 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_threadCurrentSlow
|
|---|
| 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 | /** Current rand() seed value. */
|
|---|
| 75 | unsigned int iRand;
|
|---|
| 76 | /** Buffer used by asctime() and indirectly by ctime() . (Adds two 2 bytes for padding). */
|
|---|
| 77 | char szAscTimeAndCTimeBuf[26+2];
|
|---|
| 78 | /** Buffer used by gmtime() and localtime(). */
|
|---|
| 79 | struct tm GmTimeAndLocalTimeBuf;
|
|---|
| 80 | /** Buffer used by tmpnam(). */
|
|---|
| 81 | char szTmpNamBuf[16];
|
|---|
| 82 | /** Current posistion of strtok(). */
|
|---|
| 83 | unsigned char *pszStrTokPos;
|
|---|
| 84 | /** Buffer used by strerror() to format unknown error values. */
|
|---|
| 85 | char szStrErrorBuf[28];
|
|---|
| 86 | /** Buffer used by _getvol(). */
|
|---|
| 87 | char szVolLabelBuf[12];
|
|---|
| 88 | /** Buffer used by ttyname(). */
|
|---|
| 89 | char szTTYNameBuf[32];
|
|---|
| 90 |
|
|---|
| 91 | /** Data used by the backends. */
|
|---|
| 92 | union __libc_backend_data
|
|---|
| 93 | {
|
|---|
| 94 | struct __libc_sys
|
|---|
| 95 | {
|
|---|
| 96 | /** Blocked signal mask. */
|
|---|
| 97 | sigset_t sig_blocked;
|
|---|
| 98 | /** Pending signal mask. */
|
|---|
| 99 | sigset_t sig_pending;
|
|---|
| 100 | /** Signal actions. */
|
|---|
| 101 | struct sigaction signals[NSIG];
|
|---|
| 102 |
|
|---|
| 103 | /** Directory find data entry.
|
|---|
| 104 | * Used by __findfirst() and __findnext(). */
|
|---|
| 105 | struct find_data
|
|---|
| 106 | {
|
|---|
| 107 | /** Directory handle. HDIR_CREATE if no session opened. */
|
|---|
| 108 | unsigned long hdir;
|
|---|
| 109 | /** Type of buffer content. FIL_STANDARDL or FIL_STANDARD,
|
|---|
| 110 | * i.e. FILEFINDBUF3 or FILEFINDBUF3L. */
|
|---|
| 111 | unsigned long fType;
|
|---|
| 112 | /** Number of files left in the buffer. */
|
|---|
| 113 | unsigned long cFiles;
|
|---|
| 114 | /** Pointer to the next entry. Don't test on this, test on cFiles! */
|
|---|
| 115 | const char *pchNext;
|
|---|
| 116 | /** Buffer. */
|
|---|
| 117 | char achBuffer[2048];
|
|---|
| 118 | } fd;
|
|---|
| 119 | } sys;
|
|---|
| 120 | } b;
|
|---|
| 121 |
|
|---|
| 122 | /** Data used in special periods of a threads life. */
|
|---|
| 123 | union
|
|---|
| 124 | {
|
|---|
| 125 | struct __libc_thread_startup
|
|---|
| 126 | {
|
|---|
| 127 | /** Thread argument. */
|
|---|
| 128 | void *pvArg;
|
|---|
| 129 | /** Thread routine. */
|
|---|
| 130 | void (*pfnStart)(void *pvArg);
|
|---|
| 131 | } startup;
|
|---|
| 132 | } u;
|
|---|
| 133 | } __LIBC_THREAD;
|
|---|
| 134 |
|
|---|
| 135 | #ifndef __LIBC_THREAD_DECLARED
|
|---|
| 136 | #define __LIBC_THREAD_DECLARED
|
|---|
| 137 | typedef struct __libc_threadCurrentSlow *__LIBC_PTHREAD, **__LIBC_PPTHREAD;
|
|---|
| 138 | #endif
|
|---|
| 139 |
|
|---|
| 140 |
|
|---|
| 141 | /*******************************************************************************
|
|---|
| 142 | * Global Variables *
|
|---|
| 143 | *******************************************************************************/
|
|---|
| 144 | __BEGIN_DECLS
|
|---|
| 145 | /** Pointer to the TLS ULONG (OS/2 rules) which will point to the LIBC thread
|
|---|
| 146 | * structure for the current thread.
|
|---|
| 147 | * The TLS ULONG is allocated by __init_dll(). The thread structure it points to
|
|---|
| 148 | * is allocated on demand. */
|
|---|
| 149 | extern __LIBC_PPTHREAD __libc_gpTLS;
|
|---|
| 150 | __END_DECLS
|
|---|
| 151 |
|
|---|
| 152 |
|
|---|
| 153 | /*******************************************************************************
|
|---|
| 154 | * External Functions *
|
|---|
| 155 | *******************************************************************************/
|
|---|
| 156 | __BEGIN_DECLS
|
|---|
| 157 | /**
|
|---|
| 158 | * Get the thread structure for the current thread.
|
|---|
| 159 | *
|
|---|
| 160 | * Will automatically allocate a thread structure if such is not yet done
|
|---|
| 161 | * for the thread.
|
|---|
| 162 | *
|
|---|
| 163 | * @returns pointer to current thread struct.
|
|---|
| 164 |
|
|---|
| 165 | * @remark This API is considered to be internal to LIBC and is thus not
|
|---|
| 166 | * exposed in the shared library version of LIBC. Please don't call it.
|
|---|
| 167 | * External LIBs should use the __libc_TLS*() API.
|
|---|
| 168 | */
|
|---|
| 169 | #define __libc_threadCurrent() (*__libc_gpTLS ? *__libc_gpTLS : __libc_threadCurrentSlow())
|
|---|
| 170 |
|
|---|
| 171 |
|
|---|
| 172 | /**
|
|---|
| 173 | * Get the thread structure for the current thread.
|
|---|
| 174 | *
|
|---|
| 175 | * Used by the __libc_threadCurrent() macro for allocating a thread structure for the
|
|---|
| 176 | * current thread when such doesn't exist.
|
|---|
| 177 | *
|
|---|
| 178 | * @returns pointer to current thread struct.
|
|---|
| 179 |
|
|---|
| 180 | * @remark This API is considered to be internal to LIBC and is thus not
|
|---|
| 181 | * exposed in the shared library version of LIBC. Please don't call it.
|
|---|
| 182 | * External LIBs should use the __libc_TLS*() API.
|
|---|
| 183 | */
|
|---|
| 184 | __LIBC_PTHREAD __libc_threadCurrentSlow(void);
|
|---|
| 185 |
|
|---|
| 186 |
|
|---|
| 187 | /**
|
|---|
| 188 | * Get the thread structure for the current thread.
|
|---|
| 189 | *
|
|---|
| 190 | * Do not create anything automatically.
|
|---|
| 191 | *
|
|---|
| 192 | * @returns pointer to current thread struct.
|
|---|
| 193 | * @returns NULL if not initiated.
|
|---|
| 194 | *
|
|---|
| 195 | * @remark This API is considered to be internal to LIBC and is thus not
|
|---|
| 196 | * exposed in the shared library version of LIBC. Please don't call it.
|
|---|
| 197 | * External LIBs should use the __libc_TLS*() API.
|
|---|
| 198 | */
|
|---|
| 199 | #define __libc_threadCurrentNoAuto() (__libc_gpTLS ? *__libc_gpTLS : NULL)
|
|---|
| 200 |
|
|---|
| 201 |
|
|---|
| 202 | /**
|
|---|
| 203 | * Allocate and initialize a thread structure for a thread which is yet
|
|---|
| 204 | * to be created.
|
|---|
| 205 | *
|
|---|
| 206 | * @returns Pointer to thread structure.
|
|---|
| 207 | * @returns NULL on error. errno set.
|
|---|
| 208 | */
|
|---|
| 209 | __LIBC_PTHREAD __libc_threadAlloc(void);
|
|---|
| 210 |
|
|---|
| 211 |
|
|---|
| 212 | /**
|
|---|
| 213 | * Free a thread structure.
|
|---|
| 214 | *
|
|---|
| 215 | * @param pThrd Pointer to the thread structure to free.
|
|---|
| 216 | * Must be valid.
|
|---|
| 217 | * @remark If pThrd is for the current thread the thread must be
|
|---|
| 218 | * in the very final termination stage.
|
|---|
| 219 | */
|
|---|
| 220 | void __libc_threadFree(__LIBC_PTHREAD pThrd);
|
|---|
| 221 |
|
|---|
| 222 |
|
|---|
| 223 | /** @group InnoTek LIBC Thread Local Storage
|
|---|
| 224 | * @{
|
|---|
| 225 | */
|
|---|
| 226 |
|
|---|
| 227 | /**
|
|---|
| 228 | * Allocates a TLS entry.
|
|---|
| 229 | *
|
|---|
| 230 | * @returns index of the allocated TLS index.
|
|---|
| 231 | * @returns -1 on failure. errno set.
|
|---|
| 232 | */
|
|---|
| 233 | int __libc_TLSAlloc(void);
|
|---|
| 234 |
|
|---|
| 235 | /**
|
|---|
| 236 | * Frees a TLS entry allocated by __libc_TLSAlloc().
|
|---|
| 237 | *
|
|---|
| 238 | * @returns 0 on success.
|
|---|
| 239 | * @returns -1 on failure. errno set.
|
|---|
| 240 | * @param iTLSIndex Value returned by __libc_TLSAlloc().
|
|---|
| 241 | */
|
|---|
| 242 | int __libc_TLSFree(int iIndex);
|
|---|
| 243 |
|
|---|
| 244 | /**
|
|---|
| 245 | * Get the value stored in an allocated TLS entry.
|
|---|
| 246 | *
|
|---|
| 247 | * @returns value in given TLS entry.
|
|---|
| 248 | * @returns NULL on failure with errno set.
|
|---|
| 249 | * @param iTLSIndex Value returned by __libc_TLSAlloc().
|
|---|
| 250 | */
|
|---|
| 251 | void * __libc_TLSGet(int iIndex);
|
|---|
| 252 |
|
|---|
| 253 | /**
|
|---|
| 254 | * Set the value stored in an allocated TLS entry.
|
|---|
| 255 | *
|
|---|
| 256 | * @returns 0 on success.
|
|---|
| 257 | * @returns -1 on failure. errno set.
|
|---|
| 258 | * @param iTLSIndex Value returned by __libc_TLSAlloc().
|
|---|
| 259 | * @param pvValue Value to store.
|
|---|
| 260 | */
|
|---|
| 261 | int __libc_TLSSet(int iIndex, void *pvValue);
|
|---|
| 262 |
|
|---|
| 263 | /** @} */
|
|---|
| 264 | __END_DECLS
|
|---|
| 265 |
|
|---|
| 266 | #endif
|
|---|