msi: Don't print the function name twice in a WARN.
[wine] / dlls / krnl386.exe16 / syslevel.c
1 /*
2  * Win32 'syslevel' routines
3  *
4  * Copyright 1998 Ulrich Weigand
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or (at your option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
19  */
20
21 #include "config.h"
22
23 #include <stdarg.h>
24 #ifdef HAVE_UNISTD_H
25 # include <unistd.h>
26 #endif
27 #include <sys/types.h>
28 #include "windef.h"
29 #include "winbase.h"
30 #include "winternl.h"
31 #include "wine/winbase16.h"
32 #include "kernel16_private.h"
33 #include "wine/library.h"
34 #include "wine/debug.h"
35
36 WINE_DEFAULT_DEBUG_CHANNEL(syslevel);
37
38 static SYSLEVEL Win16Mutex;
39 static CRITICAL_SECTION_DEBUG critsect_debug =
40 {
41     0, 0, &Win16Mutex.crst,
42     { &critsect_debug.ProcessLocksList, &critsect_debug.ProcessLocksList },
43       0, 0, { (DWORD_PTR)(__FILE__ ": Win16Mutex") }
44 };
45 static SYSLEVEL Win16Mutex = { { &critsect_debug, -1, 0, 0, 0, 0 }, 1 };
46
47
48 /************************************************************************
49  *           GetpWin16Lock    (KERNEL32.93)
50  */
51 VOID WINAPI GetpWin16Lock(SYSLEVEL **lock)
52 {
53     *lock = &Win16Mutex;
54 }
55
56 /************************************************************************
57  *           GetpWin16Lock    (KERNEL.449)
58  */
59 SEGPTR WINAPI GetpWin16Lock16(void)
60 {
61     static SYSLEVEL *w16Mutex;
62     static SEGPTR segpWin16Mutex;
63
64     if (!segpWin16Mutex)
65     {
66         w16Mutex = &Win16Mutex;
67         segpWin16Mutex = MapLS( &w16Mutex );
68     }
69     return segpWin16Mutex;
70 }
71
72 /************************************************************************
73  *           _CreateSysLevel    (KERNEL.438)
74  */
75 VOID WINAPI _CreateSysLevel(SYSLEVEL *lock, INT level)
76 {
77     RtlInitializeCriticalSection( &lock->crst );
78     lock->level = level;
79
80     TRACE("(%p, %d): handle is %p\n",
81                   lock, level, lock->crst.LockSemaphore );
82 }
83
84 /************************************************************************
85  *           _EnterSysLevel    (KERNEL32.97)
86  *           _EnterSysLevel    (KERNEL.439)
87  */
88 VOID WINAPI _EnterSysLevel(SYSLEVEL *lock)
89 {
90     struct kernel_thread_data *thread_data = kernel_get_thread_data();
91     int i;
92
93     TRACE("(%p, level %d): thread %x count before %d\n",
94           lock, lock->level, GetCurrentThreadId(), thread_data->sys_count[lock->level] );
95
96     for ( i = 3; i > lock->level; i-- )
97         if ( thread_data->sys_count[i] > 0 )
98         {
99             ERR("(%p, level %d): Holding %p, level %d. Expect deadlock!\n",
100                         lock, lock->level, thread_data->sys_mutex[i], i );
101         }
102
103     RtlEnterCriticalSection( &lock->crst );
104
105     thread_data->sys_count[lock->level]++;
106     thread_data->sys_mutex[lock->level] = lock;
107
108     TRACE("(%p, level %d): thread %x count after  %d\n",
109           lock, lock->level, GetCurrentThreadId(), thread_data->sys_count[lock->level] );
110
111     if (lock == &Win16Mutex) CallTo16_TebSelector = wine_get_fs();
112 }
113
114 /************************************************************************
115  *           _LeaveSysLevel    (KERNEL32.98)
116  *           _LeaveSysLevel    (KERNEL.440)
117  */
118 VOID WINAPI _LeaveSysLevel(SYSLEVEL *lock)
119 {
120     struct kernel_thread_data *thread_data = kernel_get_thread_data();
121
122     TRACE("(%p, level %d): thread %x count before %d\n",
123           lock, lock->level, GetCurrentThreadId(), thread_data->sys_count[lock->level] );
124
125     if ( thread_data->sys_count[lock->level] <= 0 || thread_data->sys_mutex[lock->level] != lock )
126     {
127         ERR("(%p, level %d): Invalid state: count %d mutex %p.\n",
128                     lock, lock->level, thread_data->sys_count[lock->level],
129                     thread_data->sys_mutex[lock->level] );
130     }
131     else
132     {
133         if ( --thread_data->sys_count[lock->level] == 0 )
134             thread_data->sys_mutex[lock->level] = NULL;
135     }
136
137     RtlLeaveCriticalSection( &lock->crst );
138
139     TRACE("(%p, level %d): thread %x count after  %d\n",
140           lock, lock->level, GetCurrentThreadId(), thread_data->sys_count[lock->level] );
141 }
142
143 /************************************************************************
144  *              @ (KERNEL32.86)
145  */
146 VOID WINAPI _KERNEL32_86(SYSLEVEL *lock)
147 {
148     _LeaveSysLevel(lock);
149 }
150
151 /************************************************************************
152  *           _ConfirmSysLevel    (KERNEL32.95)
153  *           _ConfirmSysLevel    (KERNEL.436)
154  */
155 DWORD WINAPI _ConfirmSysLevel(SYSLEVEL *lock)
156 {
157     if ( lock && lock->crst.OwningThread == ULongToHandle(GetCurrentThreadId()) )
158         return lock->crst.RecursionCount;
159     else
160         return 0L;
161 }
162
163 /************************************************************************
164  *           _CheckNotSysLevel    (KERNEL32.94)
165  *           _CheckNotSysLevel    (KERNEL.437)
166  */
167 VOID WINAPI _CheckNotSysLevel(SYSLEVEL *lock)
168 {
169     if (lock && lock->crst.OwningThread == ULongToHandle(GetCurrentThreadId()) &&
170         lock->crst.RecursionCount)
171     {
172         ERR( "Holding lock %p level %d\n", lock, lock->level );
173         DbgBreakPoint();
174     }
175 }
176
177
178 /************************************************************************
179  *           _EnterWin16Lock                    [KERNEL.480]
180  */
181 VOID WINAPI _EnterWin16Lock(void)
182 {
183     _EnterSysLevel(&Win16Mutex);
184 }
185
186 /************************************************************************
187  *           _LeaveWin16Lock            [KERNEL.481]
188  */
189 VOID WINAPI _LeaveWin16Lock(void)
190 {
191     _LeaveSysLevel(&Win16Mutex);
192 }
193
194 /************************************************************************
195  *           _ConfirmWin16Lock    (KERNEL32.96)
196  */
197 DWORD WINAPI _ConfirmWin16Lock(void)
198 {
199     return _ConfirmSysLevel(&Win16Mutex);
200 }
201
202 /************************************************************************
203  *           ReleaseThunkLock    (KERNEL32.48)
204  */
205 VOID WINAPI ReleaseThunkLock(DWORD *mutex_count)
206 {
207     DWORD count = _ConfirmSysLevel(&Win16Mutex);
208     *mutex_count = count;
209
210     while (count-- > 0)
211         _LeaveSysLevel(&Win16Mutex);
212 }
213
214 /************************************************************************
215  *           RestoreThunkLock    (KERNEL32.49)
216  */
217 VOID WINAPI RestoreThunkLock(DWORD mutex_count)
218 {
219     while (mutex_count-- > 0)
220         _EnterSysLevel(&Win16Mutex);
221 }
222
223 /************************************************************************
224  *           SYSLEVEL_CheckNotLevel
225  */
226 VOID SYSLEVEL_CheckNotLevel( INT level )
227 {
228     INT i;
229
230     for ( i = 3; i >= level; i-- )
231         if ( kernel_get_thread_data()->sys_count[i] > 0 )
232         {
233             ERR("(%d): Holding lock of level %d!\n",
234                        level, i );
235             DbgBreakPoint();
236             break;
237         }
238 }