2 * Copyright (c) 2002, TransGaming Technologies Inc.
7 #include "debugtools.h"
10 DEFAULT_DEBUG_CHANNEL(msvcrt);
15 CRITICAL_SECTION crit;
18 static LOCKTABLEENTRY lock_table[ _TOTAL_LOCKS ];
20 static inline void msvcrt_mlock_set_entry_initialized( int locknum, BOOL initialized )
22 lock_table[ locknum ].bInit = initialized;
25 static inline void msvcrt_initialize_mlock( int locknum )
27 InitializeCriticalSection( &(lock_table[ locknum ].crit) );
28 msvcrt_mlock_set_entry_initialized( locknum, TRUE );
31 static inline void msvcrt_uninitialize_mlock( int locknum )
33 DeleteCriticalSection( &(lock_table[ locknum ].crit) );
34 msvcrt_mlock_set_entry_initialized( locknum, FALSE );
37 /**********************************************************************
38 * msvcrt_init_mt_locks (internal)
40 * Initialize the table lock. All other locks will be initialized
44 void msvcrt_init_mt_locks(void)
48 TRACE( "initializing mtlocks\n" );
50 /* Initialize the table */
51 for( i=0; i < _TOTAL_LOCKS; i++ )
53 msvcrt_mlock_set_entry_initialized( i, FALSE );
56 /* Initialize our lock table lock */
57 msvcrt_initialize_mlock( _LOCKTAB_LOCK );
60 /**********************************************************************
61 * msvcrt_free_mt_locks (internal)
63 * Uninitialize all mt locks. Assume that neither _lock or _unlock will
64 * be called once we're calling this routine (ie _LOCKTAB_LOCK can be deleted)
67 void msvcrt_free_mt_locks(void)
71 TRACE( ": uninitializing all mtlocks\n" );
73 /* Uninitialize the table */
74 for( i=0; i < _TOTAL_LOCKS; i++ )
76 if( lock_table[ i ].bInit == TRUE )
78 msvcrt_uninitialize_mlock( i );
84 /**********************************************************************
87 void _lock( int locknum )
89 TRACE( "(%d)\n", locknum );
91 /* If the lock doesn't exist yet, create it */
92 if( lock_table[ locknum ].bInit == FALSE )
94 /* Lock while we're changing the lock table */
95 _lock( _LOCKTAB_LOCK );
97 /* Check again if we've got a bit of a race on lock creation */
98 if( lock_table[ locknum ].bInit == FALSE )
100 TRACE( ": creating lock #%d\n", locknum );
101 msvcrt_initialize_mlock( locknum );
104 /* Unlock ourselves */
105 _unlock( _LOCKTAB_LOCK );
108 EnterCriticalSection( &(lock_table[ locknum ].crit) );
111 /**********************************************************************
114 * NOTE: There is no error detection to make sure the lock exists and is acquired.
116 void _unlock( int locknum )
118 TRACE( "(%d)\n", locknum );
120 LeaveCriticalSection( &(lock_table[ locknum ].crit) );