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