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>
31 #include "wine/winbase16.h"
32 #include "kernel_private.h"
33 #include "wine/library.h"
34 #include "wine/debug.h"
36 WINE_DEFAULT_DEBUG_CHANNEL(syslevel);
38 static SYSLEVEL Win16Mutex;
39 static CRITICAL_SECTION_DEBUG critsect_debug =
41 0, 0, &Win16Mutex.crst,
42 { &critsect_debug.ProcessLocksList, &critsect_debug.ProcessLocksList },
43 0, 0, { 0, (DWORD)(__FILE__ ": Win16Mutex") }
45 static SYSLEVEL Win16Mutex = { { &critsect_debug, -1, 0, 0, 0, 0 }, 1 };
48 extern unsigned int CallTo16_TebSelector;
52 /************************************************************************
53 * GetpWin16Lock (KERNEL32.93)
55 VOID WINAPI GetpWin16Lock(SYSLEVEL **lock)
60 /************************************************************************
61 * GetpWin16Lock (KERNEL.449)
63 SEGPTR WINAPI GetpWin16Lock16(void)
65 static SYSLEVEL *w16Mutex;
66 static SEGPTR segpWin16Mutex;
70 w16Mutex = &Win16Mutex;
71 segpWin16Mutex = MapLS( &w16Mutex );
73 return segpWin16Mutex;
76 /************************************************************************
77 * _CreateSysLevel (KERNEL.438)
79 VOID WINAPI _CreateSysLevel(SYSLEVEL *lock, INT level)
81 RtlInitializeCriticalSection( &lock->crst );
84 TRACE("(%p, %d): handle is %p\n",
85 lock, level, lock->crst.LockSemaphore );
88 /************************************************************************
89 * _EnterSysLevel (KERNEL32.97)
90 * _EnterSysLevel (KERNEL.439)
92 VOID WINAPI _EnterSysLevel(SYSLEVEL *lock)
94 struct kernel_thread_data *thread_data = kernel_get_thread_data();
97 TRACE("(%p, level %d): thread %lx count before %ld\n",
98 lock, lock->level, GetCurrentThreadId(), thread_data->sys_count[lock->level] );
100 for ( i = 3; i > lock->level; i-- )
101 if ( thread_data->sys_count[i] > 0 )
103 ERR("(%p, level %d): Holding %p, level %d. Expect deadlock!\n",
104 lock, lock->level, thread_data->sys_mutex[i], i );
107 RtlEnterCriticalSection( &lock->crst );
109 thread_data->sys_count[lock->level]++;
110 thread_data->sys_mutex[lock->level] = lock;
112 TRACE("(%p, level %d): thread %lx count after %ld\n",
113 lock, lock->level, GetCurrentThreadId(), thread_data->sys_count[lock->level] );
116 if (lock == &Win16Mutex) CallTo16_TebSelector = wine_get_fs();
120 /************************************************************************
121 * _LeaveSysLevel (KERNEL32.98)
122 * _LeaveSysLevel (KERNEL.440)
124 VOID WINAPI _LeaveSysLevel(SYSLEVEL *lock)
126 struct kernel_thread_data *thread_data = kernel_get_thread_data();
128 TRACE("(%p, level %d): thread %lx count before %ld\n",
129 lock, lock->level, GetCurrentThreadId(), thread_data->sys_count[lock->level] );
131 if ( thread_data->sys_count[lock->level] <= 0 || thread_data->sys_mutex[lock->level] != lock )
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] );
139 if ( --thread_data->sys_count[lock->level] == 0 )
140 thread_data->sys_mutex[lock->level] = NULL;
143 RtlLeaveCriticalSection( &lock->crst );
145 TRACE("(%p, level %d): thread %lx count after %ld\n",
146 lock, lock->level, GetCurrentThreadId(), thread_data->sys_count[lock->level] );
149 /************************************************************************
152 VOID WINAPI _KERNEL32_86(SYSLEVEL *lock)
154 _LeaveSysLevel(lock);
157 /************************************************************************
158 * _ConfirmSysLevel (KERNEL32.95)
159 * _ConfirmSysLevel (KERNEL.436)
161 DWORD WINAPI _ConfirmSysLevel(SYSLEVEL *lock)
163 if ( lock && lock->crst.OwningThread == (HANDLE)GetCurrentThreadId() )
164 return lock->crst.RecursionCount;
169 /************************************************************************
170 * _CheckNotSysLevel (KERNEL32.94)
171 * _CheckNotSysLevel (KERNEL.437)
173 VOID WINAPI _CheckNotSysLevel(SYSLEVEL *lock)
175 if (lock && lock->crst.OwningThread == (HANDLE)GetCurrentThreadId() &&
176 lock->crst.RecursionCount)
178 ERR( "Holding lock %p level %d\n", lock, lock->level );
184 /************************************************************************
185 * _EnterWin16Lock [KERNEL.480]
187 VOID WINAPI _EnterWin16Lock(void)
189 _EnterSysLevel(&Win16Mutex);
192 /************************************************************************
193 * _LeaveWin16Lock [KERNEL.481]
195 VOID WINAPI _LeaveWin16Lock(void)
197 _LeaveSysLevel(&Win16Mutex);
200 /************************************************************************
201 * _ConfirmWin16Lock (KERNEL32.96)
203 DWORD WINAPI _ConfirmWin16Lock(void)
205 return _ConfirmSysLevel(&Win16Mutex);
208 /************************************************************************
209 * ReleaseThunkLock (KERNEL32.48)
211 VOID WINAPI ReleaseThunkLock(DWORD *mutex_count)
213 DWORD count = _ConfirmSysLevel(&Win16Mutex);
214 *mutex_count = count;
217 _LeaveSysLevel(&Win16Mutex);
220 /************************************************************************
221 * RestoreThunkLock (KERNEL32.49)
223 VOID WINAPI RestoreThunkLock(DWORD mutex_count)
225 while (mutex_count-- > 0)
226 _EnterSysLevel(&Win16Mutex);
229 /************************************************************************
230 * SYSLEVEL_CheckNotLevel
232 VOID SYSLEVEL_CheckNotLevel( INT level )
236 for ( i = 3; i >= level; i-- )
237 if ( kernel_get_thread_data()->sys_count[i] > 0 )
239 ERR("(%d): Holding lock of level %d!\n",