Various cosmetic changes.
[wine] / dlls / msvcrt / lock.c
1 /*
2  * Copyright (c) 2002, TransGaming Technologies Inc.
3  */
4
5 #include "mtdll.h"
6
7 #include "debugtools.h"
8 #include "winbase.h"
9
10 DEFAULT_DEBUG_CHANNEL(msvcrt);
11
12 typedef struct
13 {
14   BOOL             bInit; 
15   CRITICAL_SECTION crit;
16 } LOCKTABLEENTRY;
17
18 static LOCKTABLEENTRY lock_table[ _TOTAL_LOCKS ];
19
20 static inline void msvcrt_mlock_set_entry_initialized( int locknum, BOOL initialized )
21 {
22   lock_table[ locknum ].bInit = initialized;
23 }
24
25 static inline void msvcrt_initialize_mlock( int locknum )
26 {
27   InitializeCriticalSection( &(lock_table[ locknum ].crit) );
28   msvcrt_mlock_set_entry_initialized( locknum, TRUE ); 
29 }
30
31 static inline void msvcrt_uninitialize_mlock( int locknum )
32 {
33   DeleteCriticalSection( &(lock_table[ locknum ].crit) );
34   msvcrt_mlock_set_entry_initialized( locknum, FALSE );
35 }
36
37 /**********************************************************************
38  *     msvcrt_init_mt_locks (internal)
39  *
40  * Initialize the table lock. All other locks will be initialized 
41  * upon first use.
42  *
43  */
44 void msvcrt_init_mt_locks(void)
45 {
46   int i;
47
48   TRACE( "initializing mtlocks\n" );
49
50   /* Initialize the table */
51   for( i=0; i < _TOTAL_LOCKS; i++ )
52   {
53     msvcrt_mlock_set_entry_initialized( i, FALSE );
54   }
55
56   /* Initialize our lock table lock */
57   msvcrt_initialize_mlock( _LOCKTAB_LOCK ); 
58 }
59
60 /**********************************************************************
61  *     msvcrt_free_mt_locks (internal)
62  *
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)
65  *
66  */
67 void msvcrt_free_mt_locks(void)
68 {
69   int i;
70
71   TRACE( ": uninitializing all mtlocks\n" );
72
73   /* Uninitialize the table */
74   for( i=0; i < _TOTAL_LOCKS; i++ )
75   {
76     if( lock_table[ i ].bInit == TRUE )
77     {
78       msvcrt_uninitialize_mlock( i );
79     }
80   }
81 }
82
83
84 /**********************************************************************
85  *              _lock (MSVCRT.@)
86  */
87 void _lock( int locknum )
88 {
89   TRACE( "(%d)\n", locknum );
90
91   /* If the lock doesn't exist yet, create it */
92   if( lock_table[ locknum ].bInit == FALSE )
93   {
94     /* Lock while we're changing the lock table */
95     _lock( _LOCKTAB_LOCK );
96
97     /* Check again if we've got a bit of a race on lock creation */
98     if( lock_table[ locknum ].bInit == FALSE )
99     {
100       TRACE( ": creating lock #%d\n", locknum );
101       msvcrt_initialize_mlock( locknum );
102     }
103
104     /* Unlock ourselves */
105     _unlock( _LOCKTAB_LOCK );
106   }
107
108   EnterCriticalSection( &(lock_table[ locknum ].crit) ); 
109 }
110
111 /**********************************************************************
112  *              _unlock (MSVCRT.@)
113  *
114  * NOTE: There is no error detection to make sure the lock exists and is acquired.
115  */
116 void _unlock( int locknum )
117 {
118   TRACE( "(%d)\n", locknum );
119
120   LeaveCriticalSection( &(lock_table[ locknum ].crit) );
121 }
122