home *** CD-ROM | disk | FTP | other *** search
/ Tricks of the Windows Gam…ming Gurus (2nd Edition) / Disc2.iso / vc98 / crt / src / mtdll.h < prev    next >
C/C++ Source or Header  |  1998-06-17  |  11KB  |  357 lines

  1. /***
  2. *mtdll.h - DLL/Multi-thread include
  3. *
  4. *       Copyright (c) 1987-1997, Microsoft Corporation. All rights reserved.
  5. *
  6. *Purpose:
  7. *
  8. *       [Internal]
  9. *
  10. ****/
  11.  
  12. #if _MSC_VER > 1000
  13. #pragma once
  14. #endif  /* _MSC_VER > 1000 */
  15.  
  16. #ifndef _INC_MTDLL
  17. #define _INC_MTDLL
  18.  
  19. #ifndef _CRTBLD
  20. /*
  21.  * This is an internal C runtime header file. It is used when building
  22.  * the C runtimes only. It is not to be used as a public header file.
  23.  */
  24. #error ERROR: Use of C runtime library internal header file.
  25. #endif  /* _CRTBLD */
  26.  
  27. #ifdef __cplusplus
  28. extern "C" {
  29. #endif  /* __cplusplus */
  30.  
  31. #include <cruntime.h>
  32.  
  33. /* Define _CRTIMP */
  34.  
  35. #ifndef _CRTIMP
  36. #ifdef CRTDLL
  37. #define _CRTIMP __declspec(dllexport)
  38. #else  /* CRTDLL */
  39. #ifdef _DLL
  40. #define _CRTIMP __declspec(dllimport)
  41. #else  /* _DLL */
  42. #define _CRTIMP
  43. #endif  /* _DLL */
  44. #endif  /* CRTDLL */
  45. #endif  /* _CRTIMP */
  46.  
  47.  
  48. /* Define __cdecl for non-Microsoft compilers */
  49.  
  50. #if (!defined (_MSC_VER) && !defined (__cdecl))
  51. #define __cdecl
  52. #endif  /* (!defined (_MSC_VER) && !defined (__cdecl)) */
  53.  
  54.  
  55. /*
  56.  * Define the number of supported handles and streams. The definitions
  57.  * here must exactly match those in internal.h (for _NHANDLE_) and stdio.h
  58.  * (for _NSTREAM_).
  59.  */
  60.  
  61. #ifdef _WIN32
  62.  
  63. #define _IOB_ENTRIES    20
  64.  
  65. #else  /* _WIN32 */
  66.  
  67. /*
  68.  * Define the number of supported handles and streams. The definitions
  69.  * here must exactly match those in internal.h (for _NHANDLE_) and stdio.h
  70.  * (for _NSTREAM_).
  71.  */
  72.  
  73. #ifdef CRTDLL
  74. #define _NHANDLE_   512     /* *MUST* match the value under ifdef _DLL! */
  75. #define _NSTREAM_   128     /* *MUST* match the value under ifdef _DLL! */
  76. #else  /* CRTDLL */
  77. #ifdef _DLL
  78. #define _NHANDLE_   512
  79. #define _NSTREAM_   128
  80. #else  /* _DLL */
  81. #ifdef _MT
  82. #define _NHANDLE_   256
  83. #define _NSTREAM_   40
  84. #else  /* _MT */
  85. #define _NHANDLE_   64
  86. #define _NSTREAM_   20
  87. #endif  /* _MT */
  88. #endif  /* _DLL */
  89. #endif  /* CRTDLL */
  90.  
  91. #endif  /* _WIN32 */
  92.  
  93. /* Lock symbols */
  94.  
  95. /* ---- do not change lock #1 without changing emulator ---- */
  96. #define _SIGNAL_LOCK    1       /* lock for signal() & emulator SignalAddress */
  97.                                 /* emulator uses \math\include\mtdll.inc     */
  98.  
  99. #define _IOB_SCAN_LOCK  2       /* _iob[] table lock                */
  100. #define _TMPNAM_LOCK    3       /* lock global tempnam variables    */
  101. #define _INPUT_LOCK     4       /* lock for _input() routine        */
  102. #define _OUTPUT_LOCK    5       /* lock for _output() routine       */
  103. #define _CSCANF_LOCK    6       /* lock for _cscanf() routine       */
  104. #define _CPRINTF_LOCK   7       /* lock for _cprintf() routine      */
  105. #define _CONIO_LOCK     8       /* lock for conio routines          */
  106. #define _HEAP_LOCK      9       /* lock for heap allocator routines */
  107. /* #define _BHEAP_LOCK  10         Obsolete                         */
  108. #define _TIME_LOCK      11      /* lock for time functions          */
  109. #define _ENV_LOCK       12      /* lock for environment variables   */
  110. #define _EXIT_LOCK1     13      /* lock #1 for exit code            */
  111. /* #define _EXIT_LOCK2  14         Obsolete                         */
  112. /* #define _THREADDATA_LOCK 15     Obsolete                         */
  113. #define _POPEN_LOCK     16      /* lock for _popen/_pclose database */
  114. #define _LOCKTAB_LOCK   17      /* lock to protect semaphore lock table */
  115. #define _OSFHND_LOCK    18      /* lock to protect _osfhnd array    */
  116. #define _SETLOCALE_LOCK 19      /* lock for locale handles, etc.    */
  117. /* #define _LC_COLLATE_LOCK 20     Obsolete                         */
  118. /* #define _LC_CTYPE_LOCK  21      Obsolete                         */
  119. /* #define _LC_MONETARY_LOCK 22    Obsolete                         */
  120. /* #define _LC_NUMERIC_LOCK 23     Obsolete                         */
  121. /* #define _LC_TIME_LOCK   24      Obsolete                         */
  122. #define _MB_CP_LOCK     25      /* lock for multibyte code page     */
  123. #define _NLG_LOCK       26      /* lock for NLG notifications       */
  124. #define _TYPEINFO_LOCK  27      /* lock for type_info access        */
  125.  
  126. #define _STREAM_LOCKS   28      /* Table of stream locks            */
  127.  
  128. #ifdef _WIN32
  129. #define _LAST_STREAM_LOCK  (_STREAM_LOCKS+_IOB_ENTRIES-1)   /* Last stream lock */
  130. #else  /* _WIN32 */
  131. #define _LAST_STREAM_LOCK  (_STREAM_LOCKS+_NSTREAM_-1)  /* Last stream lock */
  132. #endif  /* _WIN32 */
  133.  
  134.  
  135. #ifdef _WIN32
  136.  
  137. #define _TOTAL_LOCKS        (_LAST_STREAM_LOCK+1)
  138.  
  139. #else  /* _WIN32 */
  140.  
  141. #define _FH_LOCKS           (_LAST_STREAM_LOCK+1)   /* Table of fh locks */
  142.  
  143. #define _LAST_FH_LOCK       (_FH_LOCKS+_NHANDLE_-1) /* Last fh lock      */
  144.  
  145. #define _TOTAL_LOCKS        (_LAST_FH_LOCK+1)       /* Total number of locks */
  146.  
  147. #endif  /* _WIN32 */
  148.  
  149.  
  150. #define _LOCK_BIT_INTS     (_TOTAL_LOCKS/(sizeof(unsigned)*8))+1   /* # of ints to hold lock bits */
  151.  
  152. #ifndef __assembler
  153.  
  154. /* Multi-thread macros and prototypes */
  155.  
  156. #if defined (_MT)
  157.  
  158.  
  159. #ifdef _WIN32
  160. /* need wchar_t for _wtoken field in _tiddata */
  161. #ifndef _WCHAR_T_DEFINED
  162. typedef unsigned short wchar_t;
  163. #define _WCHAR_T_DEFINED
  164. #endif  /* _WCHAR_T_DEFINED */
  165. #endif  /* _WIN32 */
  166.  
  167.  
  168. _CRTIMP extern unsigned long __cdecl __threadid(void);
  169. #define _threadid   (__threadid())
  170. _CRTIMP extern unsigned long __cdecl __threadhandle(void);
  171. #define _threadhandle   (__threadhandle())
  172.  
  173.  
  174.  
  175. /* Structure for each thread's data */
  176.  
  177. struct _tiddata {
  178.         unsigned long   _tid;       /* thread ID */
  179.  
  180.  
  181.         unsigned long   _thandle;   /* thread handle */
  182.  
  183.         int     _terrno;            /* errno value */
  184.         unsigned long   _tdoserrno; /* _doserrno value */
  185.         unsigned int    _fpds;      /* Floating Point data segment */
  186.         unsigned long   _holdrand;  /* rand() seed value */
  187.         char *      _token;         /* ptr to strtok() token */
  188. #ifdef _WIN32
  189.         wchar_t *   _wtoken;        /* ptr to wcstok() token */
  190. #endif  /* _WIN32 */
  191.         unsigned char * _mtoken;    /* ptr to _mbstok() token */
  192.  
  193.         /* following pointers get malloc'd at runtime */
  194.         char *      _errmsg;        /* ptr to strerror()/_strerror() buff */
  195.         char *      _namebuf0;      /* ptr to tmpnam() buffer */
  196. #ifdef _WIN32
  197.         wchar_t *   _wnamebuf0;     /* ptr to _wtmpnam() buffer */
  198. #endif  /* _WIN32 */
  199.         char *      _namebuf1;      /* ptr to tmpfile() buffer */
  200. #ifdef _WIN32
  201.         wchar_t *   _wnamebuf1;     /* ptr to _wtmpfile() buffer */
  202. #endif  /* _WIN32 */
  203.         char *      _asctimebuf;    /* ptr to asctime() buffer */
  204. #ifdef _WIN32
  205.         wchar_t *   _wasctimebuf;   /* ptr to _wasctime() buffer */
  206. #endif  /* _WIN32 */
  207.         void *      _gmtimebuf;     /* ptr to gmtime() structure */
  208.         char *      _cvtbuf;        /* ptr to ecvt()/fcvt buffer */
  209.  
  210.         /* following fields are needed by _beginthread code */
  211.         void *      _initaddr;      /* initial user thread address */
  212.         void *      _initarg;       /* initial user thread argument */
  213.  
  214.         /* following three fields are needed to support signal handling and
  215.          * runtime errors */
  216.         void *      _pxcptacttab;   /* ptr to exception-action table */
  217.         void *      _tpxcptinfoptrs; /* ptr to exception info pointers */
  218.         int         _tfpecode;      /* float point exception code */
  219.  
  220.         /* following field is needed by NLG routines */
  221.         unsigned long   _NLG_dwCode;
  222.  
  223.         /*
  224.          * Per-Thread data needed by C++ Exception Handling
  225.          */
  226.         void *      _terminate;     /* terminate() routine */
  227.         void *      _unexpected;    /* unexpected() routine */
  228.         void *      _translator;    /* S.E. translator */
  229.         void *      _curexception;  /* current exception */
  230.         void *      _curcontext;    /* current exception context */
  231. #if defined (_M_MRX000)
  232.         void *      _pFrameInfoChain;
  233.         void *      _pUnwindContext;
  234.         void *      _pExitContext;
  235.         int         _MipsPtdDelta;
  236.         int         _MipsPtdEpsilon;
  237. #elif defined (_M_PPC)
  238.         void *      _pExitContext;
  239.         void *      _pUnwindContext;
  240.         void *      _pFrameInfoChain;
  241.         int         _FrameInfo[6];
  242. #endif  /* defined (_M_PPC) */
  243.         };
  244.  
  245. typedef struct _tiddata * _ptiddata;
  246.  
  247. /*
  248.  * Declaration of TLS index used in storing pointers to per-thread data
  249.  * structures.
  250.  */
  251. extern unsigned long __tlsindex;
  252.  
  253. #ifdef _MT
  254.  
  255. #ifdef __cplusplus
  256. extern "C" {
  257. #endif  /* __cplusplus */
  258.  
  259. /*
  260.  * Flag indicating whether or not setlocale() is active. Its value is the
  261.  * number of setlocale() calls currently active.
  262.  */
  263. _CRTIMP extern int __setlc_active;
  264.  
  265. /*
  266.  * Flag indicating whether or not a function which references the locale
  267.  * without having locked it is active. Its value is the number of such
  268.  * functions.
  269.  */
  270. _CRTIMP extern int __unguarded_readlc_active;
  271.  
  272. #ifdef __cplusplus
  273. }
  274. #endif  /* __cplusplus */
  275.  
  276. #endif  /* _MT */
  277.  
  278.  
  279. /* macros */
  280.  
  281.  
  282. #define _lock_fh(fh)            _lock_fhandle(fh)
  283. #define _lock_str(s)            _lock_file(s)
  284. #define _lock_str2(i,s)         _lock_file2(i,s)
  285. #define _lock_fh_check(fh,flag)     if (flag) _lock_fhandle(fh)
  286. #define _mlock(l)               _lock(l)
  287. #define _munlock(l)             _unlock(l)
  288. #define _unlock_fh(fh)          _unlock_fhandle(fh)
  289. #define _unlock_str(s)          _unlock_file(s)
  290. #define _unlock_str2(i,s)       _unlock_file2(i,s)
  291. #define _unlock_fh_check(fh,flag)   if (flag) _unlock_fhandle(fh)
  292.  
  293. #define _lock_locale(llf)       \
  294.         InterlockedIncrement( &__unguarded_readlc_active );     \
  295.         if ( __setlc_active ) {         \
  296.             InterlockedDecrement( &__unguarded_readlc_active ); \
  297.             _lock( _SETLOCALE_LOCK );   \
  298.             llf = 1;                    \
  299.         }                               \
  300.         else                            \
  301.             llf = 0;
  302.  
  303. #define _unlock_locale(llf)     \
  304.         if ( llf )                          \
  305.             _unlock( _SETLOCALE_LOCK );     \
  306.         else                                \
  307.             InterlockedDecrement( &__unguarded_readlc_active );
  308.  
  309.  
  310.  
  311. /* multi-thread routines */
  312.  
  313. void __cdecl _lock(int);
  314. void __cdecl _lock_file(void *);
  315. void __cdecl _lock_file2(int, void *);
  316. void __cdecl _lock_fhandle(int);
  317. void __cdecl _lockexit(void);
  318. void __cdecl _unlock(int);
  319. void __cdecl _unlock_file(void *);
  320. void __cdecl _unlock_file2(int, void *);
  321. void __cdecl _unlock_fhandle(int);
  322. void __cdecl _unlockexit(void);
  323.  
  324. _ptiddata __cdecl _getptd(void);  /* return address of per-thread CRT data */
  325. void __cdecl _freeptd(_ptiddata); /* free up a per-thread CRT data block */
  326. void __cdecl _initptd(_ptiddata); /* initialize a per-thread CRT data block */
  327.  
  328.  
  329. #else  /* defined (_MT) */
  330.  
  331.  
  332. /* macros */
  333. #define _lock_fh(fh)
  334. #define _lock_str(s)
  335. #define _lock_str2(i,s)
  336. #define _lock_fh_check(fh,flag)
  337. #define _mlock(l)
  338. #define _munlock(l)
  339. #define _unlock_fh(fh)
  340. #define _unlock_str(s)
  341. #define _unlock_str2(i,s)
  342. #define _unlock_fh_check(fh,flag)
  343.  
  344. #define _lock_locale(llf)
  345. #define _unlock_locale(llf)
  346.  
  347. #endif  /* defined (_MT) */
  348.  
  349. #endif  /* __assembler */
  350.  
  351.  
  352. #ifdef __cplusplus
  353. }
  354. #endif  /* __cplusplus */
  355.  
  356. #endif  /* _INC_MTDLL */
  357.