Fix signed/unsigned comparison warnings.
[wine] / dlls / kernel / 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., 59 Temple Place, Suite 330, Boston, MA  02111-1307  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 "winreg.h"
31 #include "winternl.h"
32 #include "wine/winbase16.h"
33 #include "thread.h"
34 #include "kernel_private.h"
35 #include "wine/library.h"
36 #include "wine/debug.h"
37
38 WINE_DEFAULT_DEBUG_CHANNEL(syslevel);
39
40 static SYSLEVEL Win16Mutex;
41 static CRITICAL_SECTION_DEBUG critsect_debug =
42 {
43     0, 0, &Win16Mutex.crst,
44     { &critsect_debug.ProcessLocksList, &critsect_debug.ProcessLocksList },
45       0, 0, { 0, (DWORD)(__FILE__ ": Win16Mutex") }
46 };
47 static SYSLEVEL Win16Mutex = { { &critsect_debug, -1, 0, 0, 0, 0 }, 1 };
48
49 #ifdef __i386__
50 extern unsigned int CallTo16_TebSelector;
51 #endif
52
53
54 /************************************************************************
55  *           GetpWin16Lock    (KERNEL32.93)
56  */
57 VOID WINAPI GetpWin16Lock(SYSLEVEL **lock)
58 {
59     *lock = &Win16Mutex;
60 }
61
62 /************************************************************************
63  *           GetpWin16Lock    (KERNEL.449)
64  */
65 SEGPTR WINAPI GetpWin16Lock16(void)
66 {
67     static SYSLEVEL *w16Mutex;
68     static SEGPTR segpWin16Mutex;
69
70     if (!segpWin16Mutex)
71     {
72         w16Mutex = &Win16Mutex;
73         segpWin16Mutex = MapLS( &w16Mutex );
74     }
75     return segpWin16Mutex;
76 }
77
78 /************************************************************************
79  *           _CreateSysLevel    (KERNEL.438)
80  */
81 VOID WINAPI _CreateSysLevel(SYSLEVEL *lock, INT level)
82 {
83     RtlInitializeCriticalSection( &lock->crst );
84     lock->level = level;
85
86     TRACE("(%p, %d): handle is %p\n",
87                   lock, level, lock->crst.LockSemaphore );
88 }
89
90 /************************************************************************
91  *           _EnterSysLevel    (KERNEL32.97)
92  *           _EnterSysLevel    (KERNEL.439)
93  */
94 VOID WINAPI _EnterSysLevel(SYSLEVEL *lock)
95 {
96     TEB *teb = NtCurrentTeb();
97     int i;
98
99     TRACE("(%p, level %d): thread %lx count before %ld\n",
100           lock, lock->level, GetCurrentThreadId(), teb->sys_count[lock->level] );
101
102     for ( i = 3; i > lock->level; i-- )
103         if ( teb->sys_count[i] > 0 )
104         {
105             ERR("(%p, level %d): Holding %p, level %d. Expect deadlock!\n",
106                         lock, lock->level, teb->sys_mutex[i], i );
107         }
108
109     RtlEnterCriticalSection( &lock->crst );
110
111     teb->sys_count[lock->level]++;
112     teb->sys_mutex[lock->level] = lock;
113
114     TRACE("(%p, level %d): thread %lx count after  %ld\n",
115           lock, lock->level, GetCurrentThreadId(), teb->sys_count[lock->level] );
116
117 #ifdef __i386__
118     if (lock == &Win16Mutex) CallTo16_TebSelector = wine_get_fs();
119 #endif
120 }
121
122 /************************************************************************
123  *           _LeaveSysLevel    (KERNEL32.98)
124  *           _LeaveSysLevel    (KERNEL.440)
125  */
126 VOID WINAPI _LeaveSysLevel(SYSLEVEL *lock)
127 {
128     TEB *teb = NtCurrentTeb();
129
130     TRACE("(%p, level %d): thread %lx count before %ld\n",
131           lock, lock->level, GetCurrentThreadId(), teb->sys_count[lock->level] );
132
133     if ( teb->sys_count[lock->level] <= 0 || teb->sys_mutex[lock->level] != lock )
134     {
135         ERR("(%p, level %d): Invalid state: count %ld mutex %p.\n",
136                     lock, lock->level, teb->sys_count[lock->level],
137                     teb->sys_mutex[lock->level] );
138     }
139     else
140     {
141         if ( --teb->sys_count[lock->level] == 0 )
142             teb->sys_mutex[lock->level] = NULL;
143     }
144
145     RtlLeaveCriticalSection( &lock->crst );
146
147     TRACE("(%p, level %d): thread %lx count after  %ld\n",
148           lock, lock->level, GetCurrentThreadId(), teb->sys_count[lock->level] );
149 }
150
151 /************************************************************************
152  *              @ (KERNEL32.86)
153  */
154 VOID WINAPI _KERNEL32_86(SYSLEVEL *lock)
155 {
156     _LeaveSysLevel(lock);
157 }
158
159 /************************************************************************
160  *           _ConfirmSysLevel    (KERNEL32.95)
161  *           _ConfirmSysLevel    (KERNEL.436)
162  */
163 DWORD WINAPI _ConfirmSysLevel(SYSLEVEL *lock)
164 {
165     if ( lock && lock->crst.OwningThread == (HANDLE)GetCurrentThreadId() )
166         return lock->crst.RecursionCount;
167     else
168         return 0L;
169 }
170
171 /************************************************************************
172  *           _CheckNotSysLevel    (KERNEL32.94)
173  *           _CheckNotSysLevel    (KERNEL.437)
174  */
175 VOID WINAPI _CheckNotSysLevel(SYSLEVEL *lock)
176 {
177     if (lock && lock->crst.OwningThread == (HANDLE)GetCurrentThreadId() &&
178         lock->crst.RecursionCount)
179     {
180         ERR( "Holding lock %p level %d\n", lock, lock->level );
181         DbgBreakPoint();
182     }
183 }
184
185
186 /************************************************************************
187  *           _EnterWin16Lock                    [KERNEL.480]
188  */
189 VOID WINAPI _EnterWin16Lock(void)
190 {
191     _EnterSysLevel(&Win16Mutex);
192 }
193
194 /************************************************************************
195  *           _LeaveWin16Lock            [KERNEL.481]
196  */
197 VOID WINAPI _LeaveWin16Lock(void)
198 {
199     _LeaveSysLevel(&Win16Mutex);
200 }
201
202 /************************************************************************
203  *           _ConfirmWin16Lock    (KERNEL32.96)
204  */
205 DWORD WINAPI _ConfirmWin16Lock(void)
206 {
207     return _ConfirmSysLevel(&Win16Mutex);
208 }
209
210 /************************************************************************
211  *           ReleaseThunkLock    (KERNEL32.48)
212  */
213 VOID WINAPI ReleaseThunkLock(DWORD *mutex_count)
214 {
215     DWORD count = _ConfirmSysLevel(&Win16Mutex);
216     *mutex_count = count;
217
218     while (count-- > 0)
219         _LeaveSysLevel(&Win16Mutex);
220 }
221
222 /************************************************************************
223  *           RestoreThunkLock    (KERNEL32.49)
224  */
225 VOID WINAPI RestoreThunkLock(DWORD mutex_count)
226 {
227     while (mutex_count-- > 0)
228         _EnterSysLevel(&Win16Mutex);
229 }
230
231 /************************************************************************
232  *           SYSLEVEL_CheckNotLevel
233  */
234 VOID SYSLEVEL_CheckNotLevel( INT level )
235 {
236     INT i;
237
238     for ( i = 3; i >= level; i-- )
239         if ( NtCurrentTeb()->sys_count[i] > 0 )
240         {
241             ERR("(%d): Holding lock of level %d!\n",
242                        level, i );
243             DbgBreakPoint();
244             break;
245         }
246 }