blob: 2475677a25e0713167abac1197b17365de3223e4 [file] [log] [blame]
Alexandre Julliard767e6f61998-08-09 12:47:43 +00001/*
2 * Win32 'syslevel' routines
3 *
4 * Copyright 1998 Ulrich Weigand
Alexandre Julliard0799c1a2002-03-09 23:29:33 +00005 *
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
Alexandre Julliard767e6f61998-08-09 12:47:43 +000019 */
20
Patrik Stridvalld016f812002-08-17 00:43:16 +000021#include "config.h"
22
Alexandre Julliarde37c6e12003-09-05 23:08:26 +000023#include <stdarg.h>
Patrik Stridvalld016f812002-08-17 00:43:16 +000024#ifdef HAVE_UNISTD_H
25# include <unistd.h>
26#endif
Ulrich Weigandc5b8b3a1999-05-22 16:28:47 +000027#include <sys/types.h>
Alexandre Julliarde37c6e12003-09-05 23:08:26 +000028#include "windef.h"
29#include "winbase.h"
30#include "winreg.h"
Patrik Stridvall9c1de6d2002-09-12 22:07:02 +000031#include "winternl.h"
Alexandre Julliardbf979622003-08-27 02:52:17 +000032#include "wine/winbase16.h"
Alexandre Julliardbf979622003-08-27 02:52:17 +000033#include "thread.h"
Alexandre Julliardbff860c2003-09-18 04:39:13 +000034#include "kernel_private.h"
Alexandre Julliard59008672002-05-16 20:32:16 +000035#include "wine/library.h"
Alexandre Julliard0799c1a2002-03-09 23:29:33 +000036#include "wine/debug.h"
Alexandre Julliard767e6f61998-08-09 12:47:43 +000037
Dimitrie O. Paun475cdcb2004-02-26 05:30:54 +000038WINE_DEFAULT_DEBUG_CHANNEL(syslevel);
Patrik Stridvallb4b9fae1999-04-19 14:56:29 +000039
Alexandre Julliard19b6a492003-08-12 23:50:54 +000040static SYSLEVEL Win16Mutex;
41static 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};
47static SYSLEVEL Win16Mutex = { { &critsect_debug, -1, 0, 0, 0, 0 }, 1 };
Alexandre Julliard767e6f61998-08-09 12:47:43 +000048
Alexandre Julliard67d9f382003-10-08 22:59:22 +000049#ifdef __i386__
50extern unsigned int CallTo16_TebSelector;
Alexandre Julliard67d9f382003-10-08 22:59:22 +000051#endif
Alexandre Julliard85ed45e1998-08-22 19:03:56 +000052
Alexandre Julliard767e6f61998-08-09 12:47:43 +000053
54/************************************************************************
Patrik Stridvall2d6457c2000-03-28 20:22:59 +000055 * GetpWin16Lock (KERNEL32.93)
Alexandre Julliard767e6f61998-08-09 12:47:43 +000056 */
Ulrich Weigandc5b8b3a1999-05-22 16:28:47 +000057VOID WINAPI GetpWin16Lock(SYSLEVEL **lock)
Alexandre Julliard767e6f61998-08-09 12:47:43 +000058{
59 *lock = &Win16Mutex;
60}
61
62/************************************************************************
Patrik Stridvall15a3b742001-04-27 18:03:51 +000063 * GetpWin16Lock (KERNEL.449)
Alexandre Julliard767e6f61998-08-09 12:47:43 +000064 */
Alexandre Julliardab687972000-11-15 23:41:46 +000065SEGPTR WINAPI GetpWin16Lock16(void)
66{
Alexandre Julliardd7b76822001-12-20 00:19:40 +000067 static SYSLEVEL *w16Mutex;
Alexandre Julliardab687972000-11-15 23:41:46 +000068 static SEGPTR segpWin16Mutex;
Alexandre Julliardd7b76822001-12-20 00:19:40 +000069
Alexandre Julliardab687972000-11-15 23:41:46 +000070 if (!segpWin16Mutex)
71 {
Alexandre Julliardd7b76822001-12-20 00:19:40 +000072 w16Mutex = &Win16Mutex;
73 segpWin16Mutex = MapLS( &w16Mutex );
Alexandre Julliardab687972000-11-15 23:41:46 +000074 }
Alexandre Julliard767e6f61998-08-09 12:47:43 +000075 return segpWin16Mutex;
76}
77
78/************************************************************************
Ulrich Weigandc5b8b3a1999-05-22 16:28:47 +000079 * _CreateSysLevel (KERNEL.438)
Alexandre Julliard767e6f61998-08-09 12:47:43 +000080 */
Ulrich Weigandc5b8b3a1999-05-22 16:28:47 +000081VOID WINAPI _CreateSysLevel(SYSLEVEL *lock, INT level)
Alexandre Julliard767e6f61998-08-09 12:47:43 +000082{
Eric Pouechc962a692003-06-23 18:12:28 +000083 RtlInitializeCriticalSection( &lock->crst );
Ulrich Weigandc5b8b3a1999-05-22 16:28:47 +000084 lock->level = level;
85
Andrew John Hughesed800c62002-11-21 03:45:01 +000086 TRACE("(%p, %d): handle is %p\n",
Ulrich Weigandc5b8b3a1999-05-22 16:28:47 +000087 lock, level, lock->crst.LockSemaphore );
88}
89
90/************************************************************************
Patrik Stridvalldae8de62001-06-13 20:13:18 +000091 * _EnterSysLevel (KERNEL32.97)
92 * _EnterSysLevel (KERNEL.439)
Ulrich Weigandc5b8b3a1999-05-22 16:28:47 +000093 */
94VOID WINAPI _EnterSysLevel(SYSLEVEL *lock)
95{
Alexandre Julliard0a860a01999-06-22 11:43:42 +000096 TEB *teb = NtCurrentTeb();
Ulrich Weigandc5b8b3a1999-05-22 16:28:47 +000097 int i;
98
Alexandre Julliard02f28132003-08-27 23:14:29 +000099 TRACE("(%p, level %d): thread %lx count before %ld\n",
100 lock, lock->level, GetCurrentThreadId(), teb->sys_count[lock->level] );
Ulrich Weigandc5b8b3a1999-05-22 16:28:47 +0000101
102 for ( i = 3; i > lock->level; i-- )
Alexandre Julliard0a860a01999-06-22 11:43:42 +0000103 if ( teb->sys_count[i] > 0 )
Ulrich Weigandc5b8b3a1999-05-22 16:28:47 +0000104 {
Vincent Béron9a624912002-05-31 23:06:46 +0000105 ERR("(%p, level %d): Holding %p, level %d. Expect deadlock!\n",
Alexandre Julliard0a860a01999-06-22 11:43:42 +0000106 lock, lock->level, teb->sys_mutex[i], i );
Ulrich Weigandc5b8b3a1999-05-22 16:28:47 +0000107 }
108
Eric Pouechc962a692003-06-23 18:12:28 +0000109 RtlEnterCriticalSection( &lock->crst );
Ulrich Weigandc5b8b3a1999-05-22 16:28:47 +0000110
Alexandre Julliard0a860a01999-06-22 11:43:42 +0000111 teb->sys_count[lock->level]++;
112 teb->sys_mutex[lock->level] = lock;
Ulrich Weigandc5b8b3a1999-05-22 16:28:47 +0000113
Alexandre Julliard02f28132003-08-27 23:14:29 +0000114 TRACE("(%p, level %d): thread %lx count after %ld\n",
115 lock, lock->level, GetCurrentThreadId(), teb->sys_count[lock->level] );
Alexandre Julliard85ed45e1998-08-22 19:03:56 +0000116
Alexandre Julliardbff860c2003-09-18 04:39:13 +0000117#ifdef __i386__
Alexandre Julliard063cff82003-11-05 00:52:29 +0000118 if (lock == &Win16Mutex) CallTo16_TebSelector = wine_get_fs();
Alexandre Julliardbff860c2003-09-18 04:39:13 +0000119#endif
Alexandre Julliard767e6f61998-08-09 12:47:43 +0000120}
121
122/************************************************************************
Patrik Stridvalldae8de62001-06-13 20:13:18 +0000123 * _LeaveSysLevel (KERNEL32.98)
124 * _LeaveSysLevel (KERNEL.440)
Alexandre Julliard767e6f61998-08-09 12:47:43 +0000125 */
Ulrich Weigandc5b8b3a1999-05-22 16:28:47 +0000126VOID WINAPI _LeaveSysLevel(SYSLEVEL *lock)
Alexandre Julliard767e6f61998-08-09 12:47:43 +0000127{
Alexandre Julliard0a860a01999-06-22 11:43:42 +0000128 TEB *teb = NtCurrentTeb();
Ulrich Weigandc5b8b3a1999-05-22 16:28:47 +0000129
Alexandre Julliard02f28132003-08-27 23:14:29 +0000130 TRACE("(%p, level %d): thread %lx count before %ld\n",
131 lock, lock->level, GetCurrentThreadId(), teb->sys_count[lock->level] );
Ulrich Weigandc5b8b3a1999-05-22 16:28:47 +0000132
Alexandre Julliard0a860a01999-06-22 11:43:42 +0000133 if ( teb->sys_count[lock->level] <= 0 || teb->sys_mutex[lock->level] != lock )
Ulrich Weigandc5b8b3a1999-05-22 16:28:47 +0000134 {
Alexandre Julliard15657091999-05-23 10:25:25 +0000135 ERR("(%p, level %d): Invalid state: count %ld mutex %p.\n",
Vincent Béron9a624912002-05-31 23:06:46 +0000136 lock, lock->level, teb->sys_count[lock->level],
Alexandre Julliard0a860a01999-06-22 11:43:42 +0000137 teb->sys_mutex[lock->level] );
Ulrich Weigandc5b8b3a1999-05-22 16:28:47 +0000138 }
139 else
140 {
Alexandre Julliard0a860a01999-06-22 11:43:42 +0000141 if ( --teb->sys_count[lock->level] == 0 )
142 teb->sys_mutex[lock->level] = NULL;
Ulrich Weigandc5b8b3a1999-05-22 16:28:47 +0000143 }
144
Eric Pouechc962a692003-06-23 18:12:28 +0000145 RtlLeaveCriticalSection( &lock->crst );
Ulrich Weigandc5b8b3a1999-05-22 16:28:47 +0000146
Alexandre Julliard02f28132003-08-27 23:14:29 +0000147 TRACE("(%p, level %d): thread %lx count after %ld\n",
148 lock, lock->level, GetCurrentThreadId(), teb->sys_count[lock->level] );
Alexandre Julliard767e6f61998-08-09 12:47:43 +0000149}
150
151/************************************************************************
Patrik Stridvall01d5e5b2001-07-02 19:59:40 +0000152 * @ (KERNEL32.86)
Alexandre Julliard767e6f61998-08-09 12:47:43 +0000153 */
Ulrich Weigandc5b8b3a1999-05-22 16:28:47 +0000154VOID WINAPI _KERNEL32_86(SYSLEVEL *lock)
Alexandre Julliard767e6f61998-08-09 12:47:43 +0000155{
156 _LeaveSysLevel(lock);
157}
158
159/************************************************************************
Patrik Stridvalldae8de62001-06-13 20:13:18 +0000160 * _ConfirmSysLevel (KERNEL32.95)
161 * _ConfirmSysLevel (KERNEL.436)
Ulrich Weigandc5b8b3a1999-05-22 16:28:47 +0000162 */
163DWORD WINAPI _ConfirmSysLevel(SYSLEVEL *lock)
164{
Alexandre Julliard73755972002-07-31 19:26:03 +0000165 if ( lock && lock->crst.OwningThread == (HANDLE)GetCurrentThreadId() )
Ulrich Weigandc5b8b3a1999-05-22 16:28:47 +0000166 return lock->crst.RecursionCount;
167 else
168 return 0L;
169}
170
171/************************************************************************
Patrik Stridvalldae8de62001-06-13 20:13:18 +0000172 * _CheckNotSysLevel (KERNEL32.94)
173 * _CheckNotSysLevel (KERNEL.437)
Ulrich Weigandc5b8b3a1999-05-22 16:28:47 +0000174 */
175VOID WINAPI _CheckNotSysLevel(SYSLEVEL *lock)
176{
Alexandre Julliard73755972002-07-31 19:26:03 +0000177 if (lock && lock->crst.OwningThread == (HANDLE)GetCurrentThreadId() &&
178 lock->crst.RecursionCount)
Alexandre Julliard181e3d82001-08-15 23:32:47 +0000179 {
180 ERR( "Holding lock %p level %d\n", lock, lock->level );
181 DbgBreakPoint();
182 }
Ulrich Weigandc5b8b3a1999-05-22 16:28:47 +0000183}
184
185
186/************************************************************************
Alexandre Julliardab687972000-11-15 23:41:46 +0000187 * _EnterWin16Lock [KERNEL.480]
Alexandre Julliard767e6f61998-08-09 12:47:43 +0000188 */
Alexandre Julliardab687972000-11-15 23:41:46 +0000189VOID WINAPI _EnterWin16Lock(void)
Alexandre Julliard767e6f61998-08-09 12:47:43 +0000190{
Alexandre Julliard767e6f61998-08-09 12:47:43 +0000191 _EnterSysLevel(&Win16Mutex);
Alexandre Julliard767e6f61998-08-09 12:47:43 +0000192}
193
194/************************************************************************
Alexandre Julliardab687972000-11-15 23:41:46 +0000195 * _LeaveWin16Lock [KERNEL.481]
Alexandre Julliard767e6f61998-08-09 12:47:43 +0000196 */
Alexandre Julliardab687972000-11-15 23:41:46 +0000197VOID WINAPI _LeaveWin16Lock(void)
Alexandre Julliard767e6f61998-08-09 12:47:43 +0000198{
Alexandre Julliard767e6f61998-08-09 12:47:43 +0000199 _LeaveSysLevel(&Win16Mutex);
200}
Alexandre Julliardab687972000-11-15 23:41:46 +0000201
Alexandre Julliard767e6f61998-08-09 12:47:43 +0000202/************************************************************************
203 * _ConfirmWin16Lock (KERNEL32.96)
204 */
205DWORD WINAPI _ConfirmWin16Lock(void)
206{
Ulrich Weigandc5b8b3a1999-05-22 16:28:47 +0000207 return _ConfirmSysLevel(&Win16Mutex);
Alexandre Julliard767e6f61998-08-09 12:47:43 +0000208}
209
210/************************************************************************
211 * ReleaseThunkLock (KERNEL32.48)
212 */
213VOID WINAPI ReleaseThunkLock(DWORD *mutex_count)
214{
Ulrich Weigandc5b8b3a1999-05-22 16:28:47 +0000215 DWORD count = _ConfirmSysLevel(&Win16Mutex);
Alexandre Julliard767e6f61998-08-09 12:47:43 +0000216 *mutex_count = count;
217
218 while (count-- > 0)
219 _LeaveSysLevel(&Win16Mutex);
220}
221
222/************************************************************************
223 * RestoreThunkLock (KERNEL32.49)
224 */
225VOID WINAPI RestoreThunkLock(DWORD mutex_count)
226{
227 while (mutex_count-- > 0)
228 _EnterSysLevel(&Win16Mutex);
229}
230
231/************************************************************************
Ulrich Weigandc5b8b3a1999-05-22 16:28:47 +0000232 * SYSLEVEL_CheckNotLevel
233 */
234VOID SYSLEVEL_CheckNotLevel( INT level )
235{
Ulrich Weigandc5b8b3a1999-05-22 16:28:47 +0000236 INT i;
237
238 for ( i = 3; i >= level; i-- )
Alexandre Julliard0a860a01999-06-22 11:43:42 +0000239 if ( NtCurrentTeb()->sys_count[i] > 0 )
Ulrich Weigandc5b8b3a1999-05-22 16:28:47 +0000240 {
Vincent Béron9a624912002-05-31 23:06:46 +0000241 ERR("(%d): Holding lock of level %d!\n",
Ulrich Weigandc5b8b3a1999-05-22 16:28:47 +0000242 level, i );
Alexandre Julliardafb49ea2000-09-29 20:48:04 +0000243 DbgBreakPoint();
Ulrich Weigandc5b8b3a1999-05-22 16:28:47 +0000244 break;
245 }
Alexandre Julliard767e6f61998-08-09 12:47:43 +0000246}