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.

480 lines
16 KiB

  1. /***
  2. *mlock.c - Multi-thread locking routines
  3. *
  4. * Copyright (c) 1987-2001, Microsoft Corporation. All rights reserved.
  5. *
  6. *Purpose:
  7. *
  8. *Revision History:
  9. * 05-07-90 JCR Module created.
  10. * 06-04-90 GJF Changed error message interface.
  11. * 08-08-90 GJF Removed 32 from API names.
  12. * 08-08-90 SBM _lockmap no longer 8 times required size
  13. * 10-08-90 GJF New-style function declarators. Removed questionable
  14. * return statements from void functions (weren't needed
  15. * and the compiler was bitching).
  16. * 10-09-90 GJF Thread ids are unsigned longs.
  17. * 06-06-91 GJF Adapted for Win32 [_WIN32_].
  18. * 09-29-91 GJF Fixed infinite recursion problem with DEBUG version
  19. * of _lock [_WIN32_].
  20. * 03-06-92 GJF Removed _[un]lock_fh() and _[un]lock_stream for Win32
  21. * targets.
  22. * 05-28-92 GJF Added _mtdeletelocks() for Win32 for DLLs with contain
  23. * the C runtime (e.g., crtdll.dll).
  24. * 10-06-92 SRW Make _locktable an array of PCRITICAL_SECTION pointers
  25. * instead of structures. Allocate each critical section
  26. * as it is needed.
  27. * 02-25-93 GJF Substantially revised. Restored static critical section
  28. * structures for some locks. Replaced bit-array scheme
  29. * of keeping track of locks. Removed Cruiser support and
  30. * replaced obsolete DEBUG code.
  31. * 03-03-93 GJF Made CRITICAL_SECTION structure for _HEAP_LOCK static.
  32. * 03-08-93 SKS Fix ptr use error in DEBUG version of _mtdeletelocks
  33. * 03-08-93 SKS Fix deletion of the special critical sections,
  34. * especially the heap lock.
  35. * 04-06-93 SKS Replace _CRTAPI* with __cdecl
  36. * 05-05-93 GJF Turned DEBUG code off.
  37. * 06-03-93 SRW Disable FPO optimizations for this file so it can call
  38. * CriticalSection routines on a checked build even though
  39. * the C Runtimes are compiled free.
  40. * 10-19-93 GJF Merged in NT SDK version. Enclosed #pragma-s in
  41. * #ifdef _M_IX86 / #endif. Replaced MTHREAD with _MT.
  42. * 04-12-94 GJF Made into empty functions for the Win32s version of
  43. * msvcrt*.dll.
  44. * 01-10-95 CFW Debug CRT allocs.
  45. * 01-30-95 GJF Made CRITICAL_SECTION structure for _SIGNAL_LOCK
  46. * static.
  47. * 03-06-95 GJF Added _[un]lock_file[2] to lock stdio files (__piob[]
  48. * entries).
  49. * 03-23-95 BWT Store the critsec in the locktable *after* it's
  50. * initialized.
  51. * 10-03-95 GJF Added comments to the effect that the _LC_*_LOCK
  52. * locks are obsolete.
  53. * 11-15-95 JWM Correct syntax error in 2nd '#pragma optimize()'.
  54. * 06-19-97 GJF Moved _[un]lock_file[2]() to stdio\_file.c to improve
  55. * granularity.
  56. * 05-13-99 PML Remove Win32s
  57. * 10-14-99 PML Replace InitializeCriticalSection with wrapper function
  58. * __crtInitCritSecAndSpinCount
  59. * 12-10-99 GB Added a new Lock _UNDNAME_LOCK for critical section in
  60. * unDName().
  61. * 03-06-00 PML Call __crtExitProcess instead of ExitProcess.
  62. * 02-20-01 PML vs7#172586 Avoid _RT_LOCK by preallocating all locks
  63. * that will be required, and returning failure back on
  64. * inability to allocate a lock.
  65. * 03-07-01 PML vs7#221122 Release preallocated locks after heap ones,
  66. * so _HEAP_LOCK is around while we're still freeing mem.
  67. * 03-22-01 PML Add _DEBUG_LOCK for _CrtSetReportHook2 (vs7#124998)
  68. *
  69. *******************************************************************************/
  70. #ifdef _MT
  71. #include <cruntime.h>
  72. #include <oscalls.h>
  73. #include <internal.h>
  74. #include <mtdll.h>
  75. #include <rterr.h>
  76. #include <stddef.h>
  77. #include <malloc.h>
  78. #include <limits.h>
  79. #include <stdio.h>
  80. #include <dbgint.h>
  81. #include <errno.h>
  82. /*
  83. * Local routines
  84. */
  85. void __cdecl _lockerr_exit(char *);
  86. /*
  87. * Global Data
  88. */
  89. /*
  90. * Statically allocated critical section structures for all preallocated locks.
  91. * These are most of the named locks before _STREAM_LOCKS, along with the locks
  92. * for stdin/stdout/stderr. These must be preallocated so we do not hit fatal
  93. * memory conditions on failing to initialize a critical section, except at
  94. * runtime startup, since these locks may be taken where we have no good way
  95. * to return a non-fatal error.
  96. */
  97. #define NUM_STD_FILE_LOCKS 3
  98. #define NUM_NON_PREALLOC_LOCKS 5
  99. #define NUM_PREALLOC_LOCKS \
  100. ( _STREAM_LOCKS + NUM_STD_FILE_LOCKS - NUM_NON_PREALLOC_LOCKS )
  101. static CRITICAL_SECTION lclcritsects[NUM_PREALLOC_LOCKS];
  102. /*
  103. * Lock Table
  104. * This table contains a pointer to the critical section management structure
  105. * for each lock.
  106. *
  107. * Locks marked lkPrealloc have their critical sections statically allocated
  108. * and initialized at startup in _mtinitlocks. Locks marked lkNormal must
  109. * be allocated when first used, via a call to _mtinitlocknum.
  110. */
  111. static struct {
  112. PCRITICAL_SECTION lock;
  113. enum { lkNormal = 0, lkPrealloc, lkDeleted } kind;
  114. } _locktable[_TOTAL_LOCKS] = {
  115. { NULL, lkPrealloc }, /* 0 == _SIGNAL_LOCK */
  116. { NULL, lkPrealloc }, /* 1 == _IOB_SCAN_LOCK */
  117. { NULL, lkNormal }, /* 2 == _TMPNAM_LOCK - not preallocated */
  118. { NULL, lkPrealloc }, /* 3 == _CONIO_LOCK */
  119. { NULL, lkPrealloc }, /* 4 == _HEAP_LOCK */
  120. { NULL, lkNormal }, /* 5 == _UNDNAME_LOCK - not preallocated */
  121. { NULL, lkPrealloc }, /* 6 == _TIME_LOCK */
  122. { NULL, lkPrealloc }, /* 7 == _ENV_LOCK */
  123. { NULL, lkPrealloc }, /* 8 == _EXIT_LOCK1 */
  124. { NULL, lkNormal }, /* 9 == _POPEN_LOCK - not preallocated */
  125. { NULL, lkPrealloc }, /* 10 == _LOCKTAB_LOCK */
  126. { NULL, lkNormal }, /* 11 == _OSFHND_LOCK - not preallocated */
  127. { NULL, lkPrealloc }, /* 12 == _SETLOCALE_LOCK */
  128. { NULL, lkPrealloc }, /* 13 == _MB_CP_LOCK */
  129. { NULL, lkPrealloc }, /* 14 == _TYPEINFO_LOCK */
  130. { NULL, lkNormal }, /* 15 == _DEBUG_LOCK - not preallocated */
  131. { NULL, lkPrealloc }, /* 16 == _STREAM_LOCKS+0 - stdin */
  132. { NULL, lkPrealloc }, /* 17 == _STREAM_LOCKS+1 - stdout */
  133. { NULL, lkPrealloc }, /* 18 == _STREAM_LOCKS+2 - stderr */
  134. /* { NULL, lkNormal }, /* ... */
  135. };
  136. #ifndef NT_BUILD
  137. #ifdef _M_IX86
  138. #pragma optimize("y",off)
  139. #endif
  140. #endif
  141. /***
  142. *_mtinitlocks() - Initialize multi-thread lock scheme
  143. *
  144. *Purpose:
  145. * Perform whatever initialization is required for the multi-thread
  146. * locking (synchronization) scheme. This routine should be called
  147. * exactly once, during startup, and this must be before any requests
  148. * are made to assert locks.
  149. *
  150. * NOTES: In Win32, the multi-thread locks are created individually,
  151. * each upon its first use. That is when any particular lock is asserted
  152. * for the first time, the underlying critical section is then allocated,
  153. * initialized and (finally) entered. This allocation and initialization
  154. * is protected under _LOCKTAB_LOCK. It is _mtinitlocks' job to set up
  155. * _LOCKTAB_LOCK.
  156. *
  157. * All other named (non-FILE) locks are also preallocated in _mtinitlocks.
  158. * That is because a failure to allocate a lock on its first use in _lock
  159. * triggers a fatal error, which cannot be permitted since that can bring
  160. * down a long-lived app without warning.
  161. *
  162. *Entry:
  163. * <none>
  164. *
  165. *Exit:
  166. * returns FALSE on failure
  167. *
  168. *Exceptions:
  169. *
  170. *******************************************************************************/
  171. int __cdecl _mtinitlocks (
  172. void
  173. )
  174. {
  175. int locknum;
  176. int idxPrealloc = 0;
  177. /*
  178. * Scan _locktable[] and allocate all entries marked lkPrealloc.
  179. */
  180. for ( locknum = 0 ; locknum < _TOTAL_LOCKS ; locknum++ ) {
  181. #ifdef DEBUG
  182. if ( _locktable[locknum].lock != NULL )
  183. _lockerr_exit("fatal error in _mtinitlocks #1\n");
  184. #endif /* DEBUG */
  185. if ( _locktable[locknum].kind == lkPrealloc ) {
  186. _locktable[locknum].lock = &lclcritsects[idxPrealloc++];
  187. if ( !__crtInitCritSecAndSpinCount( _locktable[locknum].lock,
  188. _CRT_SPINCOUNT ))
  189. {
  190. _locktable[locknum].lock = NULL;
  191. return FALSE;
  192. }
  193. }
  194. }
  195. #ifdef DEBUG
  196. if ( idxPrealloc != NUM_PREALLOC_LOCKS )
  197. _lockerr_exit("fatal error in _mtinitlocks #2\n");
  198. #endif /* DEBUG */
  199. return TRUE;
  200. }
  201. /***
  202. *_mtdeletelocks() - Delete all initialized locks
  203. *
  204. *Purpose:
  205. * Walks _locktable[] and _lockmap, and deletes every 'lock' (i.e.,
  206. * critical section) which has been initialized.
  207. *
  208. * This function is intended for use in DLLs containing the C runtime
  209. * (i.e., crtdll.dll and user DLLs built using libcmt.lib and the
  210. * special startup objects). It is to be called from within the DLL's
  211. * entrypoint function when that function is called with
  212. * DLL_PROCESS_DETACH.
  213. *
  214. *Entry:
  215. * <none>
  216. *
  217. *Exit:
  218. *
  219. *Exceptions:
  220. * behavior undefined/unknown if a lock is being held when this routine
  221. * is called.
  222. *
  223. *******************************************************************************/
  224. void __cdecl _mtdeletelocks(
  225. void
  226. )
  227. {
  228. int locknum;
  229. /*
  230. * Delete and free all normal locks that have been created.
  231. */
  232. for ( locknum = 0 ; locknum < _TOTAL_LOCKS ; locknum++ ) {
  233. if ( _locktable[locknum].lock != NULL &&
  234. _locktable[locknum].kind != lkPrealloc )
  235. {
  236. PCRITICAL_SECTION pcs = _locktable[locknum].lock;
  237. DeleteCriticalSection(pcs);
  238. /*
  239. * Free the memory for the CritSect after deleting it.
  240. */
  241. #ifdef DEBUG
  242. /* check that it's an undeleted normal lock */
  243. if ( _locktable[locknum].kind != lkNormal )
  244. _lockerr_exit("fatal error in _mtdeletelocks #1\n");
  245. /* mark as deleted */
  246. _locktable[locknum].kind = lkDeleted;
  247. #endif /* DEBUG */
  248. _free_crt(pcs);
  249. _locktable[locknum].lock = NULL;
  250. }
  251. }
  252. /*
  253. * Delete all preallocated locks after all normal ones are
  254. * freed (so preallocated _HEAP_LOCK outlives all heap usages).
  255. */
  256. for ( locknum = 0 ; locknum < _TOTAL_LOCKS ; locknum++ ) {
  257. if ( _locktable[locknum].lock != NULL &&
  258. _locktable[locknum].kind == lkPrealloc )
  259. {
  260. PCRITICAL_SECTION pcs = _locktable[locknum].lock;
  261. DeleteCriticalSection(pcs);
  262. }
  263. }
  264. }
  265. /***
  266. * _mtinitlocknum - Allocate a non-preallocated multi-thread lock
  267. *
  268. *Purpose:
  269. * Allocate a new, non-preallocated multi-thread lock. This should be
  270. * used whenever a new lock is known to be needed, so that failure to
  271. * allocate can return an error, instead of allowing _lock() to issue
  272. * a fatal _RT_LOCK instead.
  273. *
  274. * It is not an error to call this on a normal lock which has already
  275. * been allocated. It is used to ensure that certain named locks which
  276. * are not preallocated are available.
  277. *
  278. * It is also called by _lock, in case any other paths exist which call
  279. * _lock without calling _mtinitlocknum first. This is not expected,
  280. * and can allow fatal _RT_LOCK errors to be issued.
  281. *
  282. * Since a failure sets errno to ENOMEM, this should only be called
  283. * after the per-thread data has been set up (after _mtinit).
  284. *
  285. *Entry:
  286. * locknum = number of the lock to aquire
  287. *
  288. *Exit:
  289. * Returns FALSE on failure, and sets errno to ENOMEM.
  290. *
  291. *Exceptions:
  292. *
  293. *******************************************************************************/
  294. int __cdecl _mtinitlocknum (
  295. int locknum
  296. )
  297. {
  298. PCRITICAL_SECTION pcs;
  299. #ifdef DEBUG
  300. if ( _locktable[locknum].kind != lkNormal )
  301. _lockerr_exit("fatal error in _mtinitlocknum #1\n");
  302. #endif /* DEBUG */
  303. if ( _locktable[locknum].lock != NULL )
  304. return TRUE;
  305. if ( (pcs = _malloc_crt(sizeof(CRITICAL_SECTION))) == NULL ) {
  306. errno = ENOMEM;
  307. return FALSE;
  308. }
  309. _mlock(_LOCKTAB_LOCK);
  310. if ( _locktable[locknum].lock == NULL ) {
  311. if ( !__crtInitCritSecAndSpinCount(pcs, _CRT_SPINCOUNT) ) {
  312. _free_crt(pcs);
  313. _munlock(_LOCKTAB_LOCK);
  314. errno = ENOMEM;
  315. return FALSE;
  316. }
  317. _locktable[locknum].lock = pcs;
  318. }
  319. else {
  320. _free_crt(pcs);
  321. }
  322. _munlock(_LOCKTAB_LOCK);
  323. return TRUE;
  324. }
  325. /***
  326. * _lock - Acquire a multi-thread lock
  327. *
  328. *Purpose:
  329. * Acquire a multi-thread lock. If the lock has not already been
  330. * allocated, do so, but that is an internal CRT error, since all locks
  331. * should be allocated before first being acquired, either in
  332. * _mtinitlocks or individually in _mtinitlocknum.
  333. *
  334. * Note that it is legal for a thread to aquire _EXIT_LOCK1
  335. * multiple times.
  336. *
  337. *Entry:
  338. * locknum = number of the lock to aquire
  339. *
  340. *Exit:
  341. *
  342. *Exceptions:
  343. * A failure to allocate a new lock results in a fatal _RT_LOCK error.
  344. *
  345. *******************************************************************************/
  346. void __cdecl _lock (
  347. int locknum
  348. )
  349. {
  350. #ifdef DEBUG
  351. if ( _locktable[locknum].kind != lkNormal &&
  352. _locktable[locknum].kind != lkPrealloc )
  353. _lockerr_exit("fatal error in _lock #1\n");
  354. #endif /* DEBUG */
  355. /*
  356. * Create/open the lock, if necessary
  357. */
  358. if ( _locktable[locknum].lock == NULL ) {
  359. #ifdef DEBUG
  360. if ( _locktable[locknum].kind != lkNormal )
  361. _lockerr_exit("fatal error in _lock #2\n");
  362. /*
  363. * All locks should be allocated before first being acquired.
  364. * Failure to do so is an internal CRT error, which we silently
  365. * allow in a production CRT, but that can lead to fatal _RT_LOCK
  366. * errors which result in an ExitProcess call.
  367. */
  368. _lockerr_exit("fatal error in _lock #3\n");
  369. #endif /* DEBUG */
  370. if ( !_mtinitlocknum(locknum) )
  371. _amsg_exit( _RT_LOCK );
  372. }
  373. /*
  374. * Enter the critical section.
  375. */
  376. EnterCriticalSection( _locktable[locknum].lock );
  377. }
  378. /***
  379. * _unlock - Release multi-thread lock
  380. *
  381. *Purpose:
  382. * Note that it is legal for a thread to aquire _EXIT_LOCK1
  383. * multiple times.
  384. *
  385. *Entry:
  386. * locknum = number of the lock to release
  387. *
  388. *Exit:
  389. *
  390. *Exceptions:
  391. *
  392. *******************************************************************************/
  393. void __cdecl _unlock (
  394. int locknum
  395. )
  396. {
  397. /*
  398. * leave the critical section.
  399. */
  400. LeaveCriticalSection( _locktable[locknum].lock );
  401. }
  402. #ifndef NT_BUILD
  403. #ifdef _M_IX86
  404. #pragma optimize("y",on)
  405. #endif
  406. #endif
  407. /***
  408. *_lockerr_exit() - Write error message and die
  409. *
  410. *Purpose:
  411. * Attempt to write out the unexpected lock error message, then terminate
  412. * the program by a direct API call. This function is used in place of
  413. * amsg_exit(_RT_LOCK) when it is judged unsafe to allow further lock
  414. * or unlock calls.
  415. *
  416. *Entry:
  417. *
  418. *Exit:
  419. *
  420. *Exceptions:
  421. *
  422. *******************************************************************************/
  423. void __cdecl _lockerr_exit (
  424. char *msg
  425. )
  426. {
  427. FatalAppExit(0, msg); /* Die with message box */
  428. __crtExitProcess(255); /* Just die */
  429. }
  430. #endif /* _MT */