2 * Win32 'syslevel' routines
4 * Copyright 1998 Ulrich Weigand
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.
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.
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
27 #include <sys/types.h>
32 #include "wine/winbase16.h"
34 #include "kernel_private.h"
35 #include "wine/library.h"
36 #include "wine/debug.h"
38 WINE_DEFAULT_DEBUG_CHANNEL(win32);
40 static SYSLEVEL Win16Mutex;
41 static CRITICAL_SECTION_DEBUG critsect_debug =
43 0, 0, &Win16Mutex.crst,
44 { &critsect_debug.ProcessLocksList, &critsect_debug.ProcessLocksList },
45 0, 0, { 0, (DWORD)(__FILE__ ": Win16Mutex") }
47 static SYSLEVEL Win16Mutex = { { &critsect_debug, -1, 0, 0, 0, 0 }, 1 };
49 /* Global variable to save current TEB while in 16-bit code */
50 extern WORD SYSLEVEL_Win16CurrentTeb;
53 /************************************************************************
54 * GetpWin16Lock (KERNEL32.93)
56 VOID WINAPI GetpWin16Lock(SYSLEVEL **lock)
61 /************************************************************************
62 * GetpWin16Lock (KERNEL.449)
64 SEGPTR WINAPI GetpWin16Lock16(void)
66 static SYSLEVEL *w16Mutex;
67 static SEGPTR segpWin16Mutex;
71 w16Mutex = &Win16Mutex;
72 segpWin16Mutex = MapLS( &w16Mutex );
74 return segpWin16Mutex;
77 /************************************************************************
78 * _CreateSysLevel (KERNEL.438)
80 VOID WINAPI _CreateSysLevel(SYSLEVEL *lock, INT level)
82 RtlInitializeCriticalSection( &lock->crst );
85 TRACE("(%p, %d): handle is %p\n",
86 lock, level, lock->crst.LockSemaphore );
89 /************************************************************************
90 * _EnterSysLevel (KERNEL32.97)
91 * _EnterSysLevel (KERNEL.439)
93 VOID WINAPI _EnterSysLevel(SYSLEVEL *lock)
95 TEB *teb = NtCurrentTeb();
98 TRACE("(%p, level %d): thread %lx count before %ld\n",
99 lock, lock->level, GetCurrentThreadId(), teb->sys_count[lock->level] );
101 for ( i = 3; i > lock->level; i-- )
102 if ( teb->sys_count[i] > 0 )
104 ERR("(%p, level %d): Holding %p, level %d. Expect deadlock!\n",
105 lock, lock->level, teb->sys_mutex[i], i );
108 RtlEnterCriticalSection( &lock->crst );
110 teb->sys_count[lock->level]++;
111 teb->sys_mutex[lock->level] = lock;
113 TRACE("(%p, level %d): thread %lx count after %ld\n",
114 lock, lock->level, GetCurrentThreadId(), teb->sys_count[lock->level] );
117 if (lock == &Win16Mutex) SYSLEVEL_Win16CurrentTeb = wine_get_fs();
121 /************************************************************************
122 * _LeaveSysLevel (KERNEL32.98)
123 * _LeaveSysLevel (KERNEL.440)
125 VOID WINAPI _LeaveSysLevel(SYSLEVEL *lock)
127 TEB *teb = NtCurrentTeb();
129 TRACE("(%p, level %d): thread %lx count before %ld\n",
130 lock, lock->level, GetCurrentThreadId(), teb->sys_count[lock->level] );
132 if ( teb->sys_count[lock->level] <= 0 || teb->sys_mutex[lock->level] != lock )
134 ERR("(%p, level %d): Invalid state: count %ld mutex %p.\n",
135 lock, lock->level, teb->sys_count[lock->level],
136 teb->sys_mutex[lock->level] );
140 if ( --teb->sys_count[lock->level] == 0 )
141 teb->sys_mutex[lock->level] = NULL;
144 RtlLeaveCriticalSection( &lock->crst );
146 TRACE("(%p, level %d): thread %lx count after %ld\n",
147 lock, lock->level, GetCurrentThreadId(), teb->sys_count[lock->level] );
150 /************************************************************************
153 VOID WINAPI _KERNEL32_86(SYSLEVEL *lock)
155 _LeaveSysLevel(lock);
158 /************************************************************************
159 * _ConfirmSysLevel (KERNEL32.95)
160 * _ConfirmSysLevel (KERNEL.436)
162 DWORD WINAPI _ConfirmSysLevel(SYSLEVEL *lock)
164 if ( lock && lock->crst.OwningThread == (HANDLE)GetCurrentThreadId() )
165 return lock->crst.RecursionCount;
170 /************************************************************************
171 * _CheckNotSysLevel (KERNEL32.94)
172 * _CheckNotSysLevel (KERNEL.437)
174 VOID WINAPI _CheckNotSysLevel(SYSLEVEL *lock)
176 if (lock && lock->crst.OwningThread == (HANDLE)GetCurrentThreadId() &&
177 lock->crst.RecursionCount)
179 ERR( "Holding lock %p level %d\n", lock, lock->level );
185 /************************************************************************
186 * _EnterWin16Lock [KERNEL.480]
188 VOID WINAPI _EnterWin16Lock(void)
190 _EnterSysLevel(&Win16Mutex);
193 /************************************************************************
194 * _LeaveWin16Lock [KERNEL.481]
196 VOID WINAPI _LeaveWin16Lock(void)
198 _LeaveSysLevel(&Win16Mutex);
201 /************************************************************************
202 * _ConfirmWin16Lock (KERNEL32.96)
204 DWORD WINAPI _ConfirmWin16Lock(void)
206 return _ConfirmSysLevel(&Win16Mutex);
209 /************************************************************************
210 * ReleaseThunkLock (KERNEL32.48)
212 VOID WINAPI ReleaseThunkLock(DWORD *mutex_count)
214 DWORD count = _ConfirmSysLevel(&Win16Mutex);
215 *mutex_count = count;
218 _LeaveSysLevel(&Win16Mutex);
221 /************************************************************************
222 * RestoreThunkLock (KERNEL32.49)
224 VOID WINAPI RestoreThunkLock(DWORD mutex_count)
226 while (mutex_count-- > 0)
227 _EnterSysLevel(&Win16Mutex);
230 /************************************************************************
231 * SYSLEVEL_CheckNotLevel
233 VOID SYSLEVEL_CheckNotLevel( INT level )
237 for ( i = 3; i >= level; i-- )
238 if ( NtCurrentTeb()->sys_count[i] > 0 )
240 ERR("(%d): Holding lock of level %d!\n",