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 "kernel16_private.h"
34 #include "wine/library.h"
35 #include "wine/debug.h"
37 WINE_DEFAULT_DEBUG_CHANNEL(syslevel);
39 static SYSLEVEL Win16Mutex;
40 static CRITICAL_SECTION_DEBUG critsect_debug =
42 0, 0, &Win16Mutex.crst,
43 { &critsect_debug.ProcessLocksList, &critsect_debug.ProcessLocksList },
44 0, 0, { 0, (DWORD)(__FILE__ ": Win16Mutex") }
46 static SYSLEVEL Win16Mutex = { { &critsect_debug, -1, 0, 0, 0, 0 }, 1 };
49 /************************************************************************
50 * GetpWin16Lock (KERNEL32.93)
52 VOID WINAPI GetpWin16Lock(SYSLEVEL **lock)
57 /************************************************************************
58 * GetpWin16Lock (KERNEL.449)
60 SEGPTR WINAPI GetpWin16Lock16(void)
62 static SYSLEVEL *w16Mutex;
63 static SEGPTR segpWin16Mutex;
67 w16Mutex = &Win16Mutex;
68 segpWin16Mutex = MapLS( &w16Mutex );
70 return segpWin16Mutex;
73 /************************************************************************
74 * _CreateSysLevel (KERNEL.438)
76 VOID WINAPI _CreateSysLevel(SYSLEVEL *lock, INT level)
78 RtlInitializeCriticalSection( &lock->crst );
81 TRACE("(%p, %d): handle is %p\n",
82 lock, level, lock->crst.LockSemaphore );
85 /************************************************************************
86 * _EnterSysLevel (KERNEL32.97)
87 * _EnterSysLevel (KERNEL.439)
89 VOID WINAPI _EnterSysLevel(SYSLEVEL *lock)
91 struct kernel_thread_data *thread_data = kernel_get_thread_data();
94 TRACE("(%p, level %d): thread %lx count before %ld\n",
95 lock, lock->level, GetCurrentThreadId(), thread_data->sys_count[lock->level] );
97 for ( i = 3; i > lock->level; i-- )
98 if ( thread_data->sys_count[i] > 0 )
100 ERR("(%p, level %d): Holding %p, level %d. Expect deadlock!\n",
101 lock, lock->level, thread_data->sys_mutex[i], i );
104 RtlEnterCriticalSection( &lock->crst );
106 thread_data->sys_count[lock->level]++;
107 thread_data->sys_mutex[lock->level] = lock;
109 TRACE("(%p, level %d): thread %lx count after %ld\n",
110 lock, lock->level, GetCurrentThreadId(), thread_data->sys_count[lock->level] );
113 if (lock == &Win16Mutex) CallTo16_TebSelector = wine_get_fs();
117 /************************************************************************
118 * _LeaveSysLevel (KERNEL32.98)
119 * _LeaveSysLevel (KERNEL.440)
121 VOID WINAPI _LeaveSysLevel(SYSLEVEL *lock)
123 struct kernel_thread_data *thread_data = kernel_get_thread_data();
125 TRACE("(%p, level %d): thread %lx count before %ld\n",
126 lock, lock->level, GetCurrentThreadId(), thread_data->sys_count[lock->level] );
128 if ( thread_data->sys_count[lock->level] <= 0 || thread_data->sys_mutex[lock->level] != lock )
130 ERR("(%p, level %d): Invalid state: count %ld mutex %p.\n",
131 lock, lock->level, thread_data->sys_count[lock->level],
132 thread_data->sys_mutex[lock->level] );
136 if ( --thread_data->sys_count[lock->level] == 0 )
137 thread_data->sys_mutex[lock->level] = NULL;
140 RtlLeaveCriticalSection( &lock->crst );
142 TRACE("(%p, level %d): thread %lx count after %ld\n",
143 lock, lock->level, GetCurrentThreadId(), thread_data->sys_count[lock->level] );
146 /************************************************************************
149 VOID WINAPI _KERNEL32_86(SYSLEVEL *lock)
151 _LeaveSysLevel(lock);
154 /************************************************************************
155 * _ConfirmSysLevel (KERNEL32.95)
156 * _ConfirmSysLevel (KERNEL.436)
158 DWORD WINAPI _ConfirmSysLevel(SYSLEVEL *lock)
160 if ( lock && lock->crst.OwningThread == (HANDLE)GetCurrentThreadId() )
161 return lock->crst.RecursionCount;
166 /************************************************************************
167 * _CheckNotSysLevel (KERNEL32.94)
168 * _CheckNotSysLevel (KERNEL.437)
170 VOID WINAPI _CheckNotSysLevel(SYSLEVEL *lock)
172 if (lock && lock->crst.OwningThread == (HANDLE)GetCurrentThreadId() &&
173 lock->crst.RecursionCount)
175 ERR( "Holding lock %p level %d\n", lock, lock->level );
181 /************************************************************************
182 * _EnterWin16Lock [KERNEL.480]
184 VOID WINAPI _EnterWin16Lock(void)
186 _EnterSysLevel(&Win16Mutex);
189 /************************************************************************
190 * _LeaveWin16Lock [KERNEL.481]
192 VOID WINAPI _LeaveWin16Lock(void)
194 _LeaveSysLevel(&Win16Mutex);
197 /************************************************************************
198 * _ConfirmWin16Lock (KERNEL32.96)
200 DWORD WINAPI _ConfirmWin16Lock(void)
202 return _ConfirmSysLevel(&Win16Mutex);
205 /************************************************************************
206 * ReleaseThunkLock (KERNEL32.48)
208 VOID WINAPI ReleaseThunkLock(DWORD *mutex_count)
210 DWORD count = _ConfirmSysLevel(&Win16Mutex);
211 *mutex_count = count;
214 _LeaveSysLevel(&Win16Mutex);
217 /************************************************************************
218 * RestoreThunkLock (KERNEL32.49)
220 VOID WINAPI RestoreThunkLock(DWORD mutex_count)
222 while (mutex_count-- > 0)
223 _EnterSysLevel(&Win16Mutex);
226 /************************************************************************
227 * SYSLEVEL_CheckNotLevel
229 VOID SYSLEVEL_CheckNotLevel( INT level )
233 for ( i = 3; i >= level; i-- )
234 if ( kernel_get_thread_data()->sys_count[i] > 0 )
236 ERR("(%d): Holding lock of level %d!\n",