msvcrt: Make tests for [w]makepath pass.
[wine] / dlls / msvcrt / lock.c
1 /*
2  * Copyright (c) 2002, TransGaming Technologies Inc.
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2.1 of the License, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with this library; if not, write to the Free Software
16  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
17  */
18
19 #include <stdarg.h>
20
21 #include "mtdll.h"
22
23 #include "wine/debug.h"
24 #include "windef.h"
25 #include "winbase.h"
26
27 WINE_DEFAULT_DEBUG_CHANNEL(msvcrt);
28
29 typedef struct
30 {
31   BOOL             bInit;
32   CRITICAL_SECTION crit;
33 } LOCKTABLEENTRY;
34
35 static LOCKTABLEENTRY lock_table[ _TOTAL_LOCKS ];
36
37 static inline void msvcrt_mlock_set_entry_initialized( int locknum, BOOL initialized )
38 {
39   lock_table[ locknum ].bInit = initialized;
40 }
41
42 static inline void msvcrt_initialize_mlock( int locknum )
43 {
44   InitializeCriticalSection( &(lock_table[ locknum ].crit) );
45   lock_table[ locknum ].crit.DebugInfo->Spare[0] = (DWORD_PTR)(__FILE__ ": LOCKTABLEENTRY.crit");
46   msvcrt_mlock_set_entry_initialized( locknum, TRUE );
47 }
48
49 static inline void msvcrt_uninitialize_mlock( int locknum )
50 {
51   lock_table[ locknum ].crit.DebugInfo->Spare[0] = 0;
52   DeleteCriticalSection( &(lock_table[ locknum ].crit) );
53   msvcrt_mlock_set_entry_initialized( locknum, FALSE );
54 }
55
56 /**********************************************************************
57  *     msvcrt_init_mt_locks (internal)
58  *
59  * Initialize the table lock. All other locks will be initialized
60  * upon first use.
61  *
62  */
63 void msvcrt_init_mt_locks(void)
64 {
65   int i;
66
67   TRACE( "initializing mtlocks\n" );
68
69   /* Initialize the table */
70   for( i=0; i < _TOTAL_LOCKS; i++ )
71   {
72     msvcrt_mlock_set_entry_initialized( i, FALSE );
73   }
74
75   /* Initialize our lock table lock */
76   msvcrt_initialize_mlock( _LOCKTAB_LOCK );
77 }
78
79 /**********************************************************************
80  *     msvcrt_free_mt_locks (internal)
81  *
82  * Uninitialize all mt locks. Assume that neither _lock or _unlock will
83  * be called once we're calling this routine (ie _LOCKTAB_LOCK can be deleted)
84  *
85  */
86 void msvcrt_free_mt_locks(void)
87 {
88   int i;
89
90   TRACE( ": uninitializing all mtlocks\n" );
91
92   /* Uninitialize the table */
93   for( i=0; i < _TOTAL_LOCKS; i++ )
94   {
95     if( lock_table[ i ].bInit )
96     {
97       msvcrt_uninitialize_mlock( i );
98     }
99   }
100 }
101
102
103 /**********************************************************************
104  *              _lock (MSVCRT.@)
105  */
106 void CDECL _lock( int locknum )
107 {
108   TRACE( "(%d)\n", locknum );
109
110   /* If the lock doesn't exist yet, create it */
111   if( lock_table[ locknum ].bInit == FALSE )
112   {
113     /* Lock while we're changing the lock table */
114     _lock( _LOCKTAB_LOCK );
115
116     /* Check again if we've got a bit of a race on lock creation */
117     if( lock_table[ locknum ].bInit == FALSE )
118     {
119       TRACE( ": creating lock #%d\n", locknum );
120       msvcrt_initialize_mlock( locknum );
121     }
122
123     /* Unlock ourselves */
124     _unlock( _LOCKTAB_LOCK );
125   }
126
127   EnterCriticalSection( &(lock_table[ locknum ].crit) );
128 }
129
130 /**********************************************************************
131  *              _unlock (MSVCRT.@)
132  *
133  * NOTE: There is no error detection to make sure the lock exists and is acquired.
134  */
135 void CDECL _unlock( int locknum )
136 {
137   TRACE( "(%d)\n", locknum );
138
139   LeaveCriticalSection( &(lock_table[ locknum ].crit) );
140 }