Source code of Windows XP (NT5)
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

552 lines
20 KiB

  1. /***
  2. *mtdll.h - DLL/Multi-thread include
  3. *
  4. * Copyright (c) 1987-2001, Microsoft Corporation. All rights reserved.
  5. *
  6. *Purpose:
  7. *
  8. * [Internal]
  9. *
  10. *Revision History:
  11. * 10-27-87 JCR Module created.
  12. * 11-13-87 SKS Added _HEAP_LOCK
  13. * 12-15-87 JCR Added _EXIT_LOCK
  14. * 01-07-88 BCM Added _SIGNAL_LOCK; upped MAXTHREADID from 16 to 32
  15. * 02-01-88 JCR Added _dll_mlock/_dll_munlock macros
  16. * 05-02-88 JCR Added _BHEAP_LOCK
  17. * 06-17-88 JCR Corrected prototypes for special mthread debug routines
  18. * 08-15-88 JCR _check_lock now returns int, not void
  19. * 08-22-88 GJF Modified to also work for the 386 (small model only)
  20. * 06-05-89 JCR 386 mthread support
  21. * 06-09-89 JCR 386: Added values to _tiddata struc (for _beginthread)
  22. * 07-13-89 JCR 386: Added _LOCKTAB_LOCK
  23. * 08-17-89 GJF Cleanup, now specific to OS/2 2.0 (i.e., 386 flat model)
  24. * 10-30-89 GJF Fixed copyright
  25. * 01-02-90 JCR Moved a bunch of definitions from os2dll.inc
  26. * 04-06-90 GJF Added _INC_OS2DLL stuff and #include <cruntime.h>. Made
  27. * all function _CALLTYPE2 (for now).
  28. * 04-10-90 GJF Added prototypes for _[un]lockexit().
  29. * 08-16-90 SBM Made _terrno and _tdoserrno int, not unsigned
  30. * 09-14-90 GJF Added _pxcptacttab, _pxcptinfoptr and _fpecode fields
  31. * to _tiddata struct.
  32. * 10-09-90 GJF Thread ids are of type unsigned long.
  33. * 12-06-90 SRW Added _OSFHND_LOCK
  34. * 06-04-91 GJF Win32 version of multi-thread types and prototypes.
  35. * 08-15-91 GJF Made _tdoserrno an unsigned long for Win32.
  36. * 08-20-91 JCR C++ and ANSI naming
  37. * 09-29-91 GJF Conditionally added prototypes for _getptd_lk
  38. * and _getptd1_lk for Win32 under DEBUG.
  39. * 10-03-91 JCR Added _cvtbuf to _tiddata structure
  40. * 02-17-92 GJF For Win32, replaced _NFILE_ with _NHANDLE_ and
  41. * _NSTREAM_.
  42. * 03-06-92 GJF For Win32, made _[un]mlock_[fh|stream]() macros
  43. * directly call _[un]lock().
  44. * 03-17-92 GJF Dropped _namebuf field from _tiddata structure for
  45. * Win32.
  46. * 08-05-92 GJF Function calling type and variable type macros.
  47. * 12-03-91 ETC Added _wtoken to _tiddata, added intl LOCK's;
  48. * added definition of wchar_t (needed for _wtoken).
  49. * 08-14-92 KRS Port ETC's _wtoken change from other tree.
  50. * 08-21-92 GJF Merged 08-05-92 and 08-14-92 versions.
  51. * 12-03-92 KRS Added _mtoken field for MTHREAD _mbstok().
  52. * 01-21-93 GJF Removed support for C6-386's _cdecl.
  53. * 02-25-93 GJF Purged Cruiser support and many outdated definitions
  54. * and declarations.
  55. * 04-07-93 SKS Add _CRTIMP keyword for CRT DLL model
  56. * 10-11-93 GJF Support NT and Cuda builds. Also, purged some old
  57. * non-Win32 support (it was incomplete anyway) and
  58. * replace MTHREAD with _MT.
  59. * 10-13-93 SKS Change name from <MTDLL.H> to <MTDLL.H>
  60. * 10-27-93 SKS Add Per-Thread Variables for C++ Exception Handling
  61. * 12-13-93 SKS Add _freeptd(), which frees per-thread CRT data
  62. * 12-17-93 CFW Add Per-Thread Variable for _wasctime().
  63. * 04-15-93 CFW Add _MB_CP_LOCK.
  64. * 04-21-94 GJF Made declaration of __tlsindex and definition of the
  65. * lock macros conditional on ndef DLL_FOR_WIN32S.
  66. * Also, conditionally include win32s.h.
  67. * 12-14-94 SKS Increase file handle and FILE * limits for MSVCRT30.DLL
  68. * 02-14-95 CFW Clean up Mac merge.
  69. * 03-06-95 GJF Added _[un]lock_file[2] prototypes, _[un]lock_str2
  70. * macros, and changed the _[un]lock_str macros.
  71. * 03-13-95 GJF _IOB_ENTRIES replaced _NSTREAM_ as the number of
  72. * stdio locks in _locktable[].
  73. * 03-29-95 CFW Add error message to internal headers.
  74. * 04-13-95 DAK Add NT Kernel EH support
  75. * 04-18-95 SKS Add 5 per-thread variables for MIPS EH use
  76. * 05-02-95 SKS Add _initptd() which initializes per-thread data
  77. * 05-08-95 CFW Official ANSI C++ new handler added.
  78. * 05-19-95 DAK More Kernel EH work.
  79. * 06-05-95 JWM _NLG_dwcode & _NLG_LOCK added.
  80. * 06-11-95 GJF The critical sections for file handles are now in the
  81. * ioinfo struct rather than the lock table.
  82. * 07-20-95 CFW Remove _MBCS ifdef - caused ctime/wctime bug.
  83. * 10-03-95 GJF Support for new scheme to lock locale including
  84. * _[un]lock_locale() macros and decls of _setlc_active
  85. * and __unguarded_readlc_active. Also commented out
  86. * obsolete *_LOCK macros.
  87. * 10-19-95 BWT Fixup _NTSUBSET_ usage.
  88. * 12-07-95 SKS Fix misspelling of _NTSUBSET_ (final _ was missing)
  89. * 12-14-95 JWM Add "#pragma once".
  90. * 05-02-96 SKS Variables _setlc_active and __unguarded_readlc_active
  91. * are used by MSVCP42*.DLL and so must be _CRTIMP.
  92. * 07-16-96 GJF Locale locking wasn't really thread safe. Replaced ++
  93. * and -- with InterlockedIncrement and
  94. * InterlockedDecrement API, respectively.
  95. * 07-18-96 GJF Further mod to locale locking to fix race condition.
  96. * 02-05-97 GJF Cleaned out obsolete support for Win32s, _CRTAPI* and
  97. * _NTSDK. Also, replaced #if defined with #ifdef where
  98. * appropriate.
  99. * 10-07-97 RDL Added IA64.
  100. * 02-02-98 GJF Changes for Win64: changed _thandle type to uintptr_t.
  101. * 04-27-98 GJF Added support for per-thread mbc information.
  102. * 07-28-98 JWM Added __pceh to per-thread data for comerr support.
  103. * 09-10-98 GJF Added support for per-thread locale information.
  104. * 12-05-98 JWM Pulled all comerr support.
  105. * 03-24-99 GJF More reference counters for threadlocinfo.
  106. * 04-24-99 PML Added lconv_intl_refcount to threadlocinfo.
  107. * 05-17-99 PML Remove all Macintosh support.
  108. * 10-06-99 PML Add _W64 modifier to types which are 32 bits in Win32,
  109. * 64 bits in Win64.
  110. * 11-24-99 GB Add _werrmsg in struct _tiddata for error support in
  111. * wide char version of strerror.
  112. * 12-10-99 GB Add _ProcessingThrow in struct _tiddata.
  113. * 12-10-99 GB Added a new Lock _UNDNAME_LOCK for critical section in
  114. * unDName().
  115. * 26-01-00 GB Added lc_clike in threadlocinfostruct.
  116. * 06-08-00 PML Remove threadmbcinfo.{pprev,pnext}. Rename
  117. * THREADLOCALEINFO to _THREADLOCALEINFO and
  118. * THREADMBCINFO to _THREADMBCINFO.
  119. * 09-06-00 GB deleted _wctype and _pwctype from threadlocinfo.
  120. * 01-29-01 GB Added _func function version of data variable in
  121. * _lock_locale to work with STATIC_CPPLIB
  122. * 02-20-01 PML vs7#172586 Avoid _RT_LOCK by preallocating all locks
  123. * that will be required, and returning failure back on
  124. * inability to allocate a lock.
  125. * 03-22-01 PML Add _DEBUG_LOCK for _CrtSetReportHook2 (vs7#124998)
  126. * 03-25-01 PML Add ww_caltype & ww_lcid to __lc_time_data (vs7#196892)
  127. *
  128. ****/
  129. #if _MSC_VER > 1000 /*IFSTRIP=IGN*/
  130. #pragma once
  131. #endif
  132. #ifndef _INC_MTDLL
  133. #define _INC_MTDLL
  134. #ifndef _CRTBLD
  135. /*
  136. * This is an internal C runtime header file. It is used when building
  137. * the C runtimes only. It is not to be used as a public header file.
  138. */
  139. #error ERROR: Use of C runtime library internal header file.
  140. #endif /* _CRTBLD */
  141. #ifdef __cplusplus
  142. extern "C" {
  143. #endif
  144. #include <cruntime.h>
  145. #include <windows.h>
  146. #if !defined(_W64)
  147. #if !defined(__midl) && (defined(_X86_) || defined(_M_IX86)) && _MSC_VER >= 1300 /*IFSTRIP=IGN*/
  148. #define _W64 __w64
  149. #else
  150. #define _W64
  151. #endif
  152. #endif
  153. /* Define _CRTIMP */
  154. #ifndef _CRTIMP
  155. #ifdef CRTDLL
  156. #define _CRTIMP __declspec(dllexport)
  157. #else /* ndef CRTDLL */
  158. #ifdef _DLL
  159. #define _CRTIMP __declspec(dllimport)
  160. #else /* ndef _DLL */
  161. #define _CRTIMP
  162. #endif /* _DLL */
  163. #endif /* CRTDLL */
  164. #endif /* _CRTIMP */
  165. /* Define __cdecl for non-Microsoft compilers */
  166. #if ( !defined(_MSC_VER) && !defined(__cdecl) )
  167. #define __cdecl
  168. #endif
  169. #ifndef _UINTPTR_T_DEFINED
  170. #ifdef _WIN64
  171. typedef unsigned __int64 uintptr_t;
  172. #else
  173. typedef _W64 unsigned int uintptr_t;
  174. #endif
  175. #define _UINTPTR_T_DEFINED
  176. #endif
  177. /*
  178. * Define the number of supported handles and streams. The definitions
  179. * here must exactly match those in internal.h (for _NHANDLE_) and stdio.h
  180. * (for _NSTREAM_).
  181. */
  182. #define _IOB_ENTRIES 20
  183. /* Lock symbols */
  184. #define _SIGNAL_LOCK 0 /* lock for signal() */
  185. #define _IOB_SCAN_LOCK 1 /* _iob[] table lock */
  186. #define _TMPNAM_LOCK 2 /* lock global tempnam variables */
  187. #define _CONIO_LOCK 3 /* lock for conio routines */
  188. #define _HEAP_LOCK 4 /* lock for heap allocator routines */
  189. #define _UNDNAME_LOCK 5 /* lock for unDName() routine */
  190. #define _TIME_LOCK 6 /* lock for time functions */
  191. #define _ENV_LOCK 7 /* lock for environment variables */
  192. #define _EXIT_LOCK1 8 /* lock #1 for exit code */
  193. #define _POPEN_LOCK 9 /* lock for _popen/_pclose database */
  194. #define _LOCKTAB_LOCK 10 /* lock to protect semaphore lock table */
  195. #define _OSFHND_LOCK 11 /* lock to protect _osfhnd array */
  196. #define _SETLOCALE_LOCK 12 /* lock for locale handles, etc. */
  197. #define _MB_CP_LOCK 13 /* lock for multibyte code page */
  198. #define _TYPEINFO_LOCK 14 /* lock for type_info access */
  199. #define _DEBUG_LOCK 15 /* lock for debug global structs */
  200. #define _STREAM_LOCKS 16 /* Table of stream locks */
  201. #define _LAST_STREAM_LOCK (_STREAM_LOCKS+_IOB_ENTRIES-1) /* Last stream lock */
  202. #define _TOTAL_LOCKS (_LAST_STREAM_LOCK+1)
  203. #define _LOCK_BIT_INTS (_TOTAL_LOCKS/(sizeof(unsigned)*8))+1 /* # of ints to hold lock bits */
  204. #ifndef __assembler
  205. /* Multi-thread macros and prototypes */
  206. #if defined(_MT) || defined(_NTSUBSET_)
  207. /* need wchar_t for _wtoken field in _tiddata */
  208. #ifndef _WCHAR_T_DEFINED
  209. typedef unsigned short wchar_t;
  210. #define _WCHAR_T_DEFINED
  211. #endif
  212. #ifdef ANSI_NEW_HANDLER
  213. /* ANSI C++ new handler */
  214. #ifndef _ANSI_NH_DEFINED
  215. typedef void (__cdecl * new_handler) ();
  216. #define _ANSI_NH_DEFINED
  217. #endif
  218. #endif /* ANSI_NEW_HANDLER */
  219. #ifdef _MT
  220. #ifndef _THREADMBCINFO
  221. typedef struct threadmbcinfostruct {
  222. int refcount;
  223. int mbcodepage;
  224. int ismbcodepage;
  225. int mblcid;
  226. unsigned short mbulinfo[6];
  227. char mbctype[257];
  228. char mbcasemap[256];
  229. } threadmbcinfo;
  230. typedef threadmbcinfo * pthreadmbcinfo;
  231. #define _THREADMBCINFO
  232. #endif
  233. #endif
  234. #ifndef __LC_TIME_DATA
  235. struct __lc_time_data {
  236. char *wday_abbr[7];
  237. char *wday[7];
  238. char *month_abbr[12];
  239. char *month[12];
  240. char *ampm[2];
  241. char *ww_sdatefmt;
  242. char *ww_ldatefmt;
  243. char *ww_timefmt;
  244. LCID ww_lcid;
  245. int ww_caltype;
  246. #ifdef _MT
  247. int refcount;
  248. #endif
  249. };
  250. #define __LC_TIME_DATA
  251. #endif
  252. #ifdef _MT
  253. #ifndef _THREADLOCALEINFO
  254. typedef struct threadlocaleinfostruct {
  255. int refcount;
  256. UINT lc_codepage;
  257. UINT lc_collate_cp;
  258. LCID lc_handle[6]; /* 6 == LC_MAX - LC_MIN + 1 */
  259. int lc_clike;
  260. int mb_cur_max;
  261. int * lconv_intl_refcount;
  262. int * lconv_num_refcount;
  263. int * lconv_mon_refcount;
  264. struct lconv * lconv;
  265. struct lconv * lconv_intl;
  266. int * ctype1_refcount;
  267. unsigned short * ctype1;
  268. const unsigned short * pctype;
  269. struct __lc_time_data * lc_time_curr;
  270. struct __lc_time_data * lc_time_intl;
  271. } threadlocinfo;
  272. typedef threadlocinfo * pthreadlocinfo;
  273. #define _THREADLOCALEINFO
  274. #endif
  275. #endif
  276. _CRTIMP extern unsigned long __cdecl __threadid(void);
  277. #define _threadid (__threadid())
  278. _CRTIMP extern uintptr_t __cdecl __threadhandle(void);
  279. #define _threadhandle (__threadhandle())
  280. #ifdef _NTSUBSET_
  281. /* Standard exception handler for NT kernel */
  282. #ifdef __cplusplus
  283. extern "C"
  284. #endif /* __cplusplus */
  285. void _cdecl SystemExceptionTranslator( unsigned int uiWhat,
  286. struct _EXCEPTION_POINTERS * pexcept );
  287. #endif /* def _NTSUBSET_ */
  288. /* Structure for each thread's data */
  289. struct _tiddata {
  290. unsigned long _tid; /* thread ID */
  291. #ifdef _NTSUBSET_
  292. struct _tiddata *_next; /* maintain a linked list */
  293. #else
  294. uintptr_t _thandle; /* thread handle */
  295. int _terrno; /* errno value */
  296. unsigned long _tdoserrno; /* _doserrno value */
  297. unsigned int _fpds; /* Floating Point data segment */
  298. unsigned long _holdrand; /* rand() seed value */
  299. char * _token; /* ptr to strtok() token */
  300. wchar_t * _wtoken; /* ptr to wcstok() token */
  301. unsigned char * _mtoken; /* ptr to _mbstok() token */
  302. #ifdef ANSI_NEW_HANDLER
  303. new_handler _newh; /* ptr to ANSI C++ new handler function */
  304. #endif /* ANSI_NEW_HANDLER */
  305. /* following pointers get malloc'd at runtime */
  306. char * _errmsg; /* ptr to strerror()/_strerror() buff */
  307. wchar_t * _werrmsg; /* ptr to _wcserror()/__wcserror() buff */
  308. char * _namebuf0; /* ptr to tmpnam() buffer */
  309. wchar_t * _wnamebuf0; /* ptr to _wtmpnam() buffer */
  310. char * _namebuf1; /* ptr to tmpfile() buffer */
  311. wchar_t * _wnamebuf1; /* ptr to _wtmpfile() buffer */
  312. char * _asctimebuf; /* ptr to asctime() buffer */
  313. wchar_t * _wasctimebuf; /* ptr to _wasctime() buffer */
  314. void * _gmtimebuf; /* ptr to gmtime() structure */
  315. char * _cvtbuf; /* ptr to ecvt()/fcvt buffer */
  316. /* following fields are needed by _beginthread code */
  317. void * _initaddr; /* initial user thread address */
  318. void * _initarg; /* initial user thread argument */
  319. /* following three fields are needed to support signal handling and
  320. * runtime errors */
  321. void * _pxcptacttab; /* ptr to exception-action table */
  322. void * _tpxcptinfoptrs; /* ptr to exception info pointers */
  323. int _tfpecode; /* float point exception code */
  324. /* pointer to the copy of the multibyte character information used by
  325. * the thread */
  326. pthreadmbcinfo ptmbcinfo;
  327. /* pointer to the copy of the locale informaton used by the thead */
  328. pthreadlocinfo ptlocinfo;
  329. #endif /* !_NTSUBSET_ */
  330. /* following field is needed by NLG routines */
  331. unsigned long _NLG_dwCode;
  332. /*
  333. * Per-Thread data needed by C++ Exception Handling
  334. */
  335. void * _terminate; /* terminate() routine */
  336. void * _unexpected; /* unexpected() routine */
  337. void * _translator; /* S.E. translator */
  338. void * _curexception; /* current exception */
  339. void * _curcontext; /* current exception context */
  340. int _ProcessingThrow; /* for uncaught_exception */
  341. #if defined(_M_MRX000)
  342. void * _pFrameInfoChain;
  343. void * _pUnwindContext;
  344. void * _pExitContext;
  345. int _MipsPtdDelta;
  346. int _MipsPtdEpsilon;
  347. #elif defined(_M_PPC)
  348. void * _pExitContext;
  349. void * _pUnwindContext;
  350. void * _pFrameInfoChain;
  351. int _FrameInfo[6];
  352. #elif defined(_M_IA64) || defined(_M_AMD64)
  353. void * _pExitContext;
  354. void * _pUnwindContext;
  355. void * _pFrameInfoChain;
  356. unsigned __int64 _ImageBase;
  357. unsigned __int64 _TargetGp;
  358. unsigned __int64 _ThrowImageBase;
  359. #elif defined(_M_IX86)
  360. void * _pFrameInfoChain;
  361. #endif
  362. };
  363. typedef struct _tiddata * _ptiddata;
  364. /*
  365. * Declaration of TLS index used in storing pointers to per-thread data
  366. * structures.
  367. */
  368. #ifndef _NTSUBSET_
  369. extern unsigned long __tlsindex;
  370. #ifdef _MT
  371. #ifdef __cplusplus
  372. extern "C" {
  373. #endif /* __cplusplus */
  374. /*
  375. * Flag indicating whether or not setlocale() is active. Its value is the
  376. * number of setlocale() calls currently active.
  377. */
  378. _CRTIMP extern int __setlc_active;
  379. /*
  380. * Flag indicating whether or not a function which references the locale
  381. * without having locked it is active. Its value is the number of such
  382. * functions.
  383. */
  384. _CRTIMP extern int __unguarded_readlc_active;
  385. #ifdef __cplusplus
  386. }
  387. #endif
  388. #endif /* _MT */
  389. #endif /* _NTSUBSET_ */
  390. /* macros */
  391. #ifdef _NTSUBSET_
  392. /*
  393. * #define all lock macros to nothing.
  394. */
  395. #define _lock_fh(fh)
  396. #define _lock_str(s)
  397. #define _lock_str2(i,s)
  398. #define _lock_fh_check(fh,flag)
  399. #define _mlock(l)
  400. #define _munlock(l)
  401. #define _unlock_fh(fh)
  402. #define _unlock_str(s)
  403. #define _unlock_str2(i,s)
  404. #define _unlock_fh_check(fh,flag)
  405. #define _lock_locale(llf)
  406. #define _unlock_locale(llf)
  407. #else /* ndef _NTSUBSET_ */
  408. #define _lock_fh(fh) _lock_fhandle(fh)
  409. #define _lock_str(s) _lock_file(s)
  410. #define _lock_str2(i,s) _lock_file2(i,s)
  411. #define _lock_fh_check(fh,flag) if (flag) _lock_fhandle(fh)
  412. #define _mlock(l) _lock(l)
  413. #define _munlock(l) _unlock(l)
  414. #define _unlock_fh(fh) _unlock_fhandle(fh)
  415. #define _unlock_str(s) _unlock_file(s)
  416. #define _unlock_str2(i,s) _unlock_file2(i,s)
  417. #define _unlock_fh_check(fh,flag) if (flag) _unlock_fhandle(fh)
  418. // This is only used with STDCPP stuff only
  419. #define _lock_locale(llf) \
  420. InterlockedIncrement( ___unguarded_readlc_active_add_func() ); \
  421. if ( ___setlc_active_func() ) { \
  422. InterlockedDecrement( ___unguarded_readlc_active_add_func() ); \
  423. _lock( _SETLOCALE_LOCK ); \
  424. llf = 1; \
  425. } \
  426. else \
  427. llf = 0;
  428. #define _unlock_locale(llf) \
  429. if ( llf ) \
  430. _unlock( _SETLOCALE_LOCK ); \
  431. else \
  432. InterlockedDecrement( ___unguarded_readlc_active_add_func() );
  433. #endif /* _NTSUBSET_ */
  434. /* multi-thread routines */
  435. void __cdecl _lock(int);
  436. void __cdecl _lock_file(void *);
  437. void __cdecl _lock_file2(int, void *);
  438. int __cdecl _lock_fhandle(int);
  439. void __cdecl _lockexit(void);
  440. void __cdecl _unlock(int);
  441. void __cdecl _unlock_file(void *);
  442. void __cdecl _unlock_file2(int, void *);
  443. void __cdecl _unlock_fhandle(int);
  444. void __cdecl _unlockexit(void);
  445. int __cdecl _mtinitlocknum(int);
  446. _ptiddata __cdecl _getptd(void); /* return address of per-thread CRT data */
  447. void __cdecl _freeptd(_ptiddata); /* free up a per-thread CRT data block */
  448. void __cdecl _initptd(_ptiddata); /* initialize a per-thread CRT data block */
  449. /* These functions are for enabling STATIC_CPPLIB functionality */
  450. _CRTIMP int __cdecl ___setlc_active_func(void);
  451. _CRTIMP int * __cdecl ___unguarded_readlc_active_add_func(void);
  452. #else /* not _MT && not _NTSUBSET_ */
  453. /* macros */
  454. #define _lock_fh(fh)
  455. #define _lock_str(s)
  456. #define _lock_str2(i,s)
  457. #define _lock_fh_check(fh,flag)
  458. #define _mlock(l)
  459. #define _munlock(l)
  460. #define _unlock_fh(fh)
  461. #define _unlock_str(s)
  462. #define _unlock_str2(i,s)
  463. #define _unlock_fh_check(fh,flag)
  464. #define _lock_locale(llf)
  465. #define _unlock_locale(llf)
  466. #endif /* _MT */
  467. #endif /* __assembler */
  468. #ifdef __cplusplus
  469. }
  470. #endif
  471. #endif /* _INC_MTDLL */