Convert initData as well.
[wine/multimedia.git] / scheduler / syslevel.c
blobca23f71326c31b7da6b5c833b72dce22de244cfa
1 /*
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
21 #include "config.h"
23 #ifdef HAVE_UNISTD_H
24 # include <unistd.h>
25 #endif
26 #include <sys/types.h>
27 #include "winternl.h"
28 #include "syslevel.h"
29 #include "stackframe.h"
30 #include "wine/library.h"
31 #include "wine/debug.h"
33 WINE_DEFAULT_DEBUG_CHANNEL(win32);
35 static SYSLEVEL Win16Mutex;
36 static CRITICAL_SECTION_DEBUG critsect_debug =
38 0, 0, &Win16Mutex.crst,
39 { &critsect_debug.ProcessLocksList, &critsect_debug.ProcessLocksList },
40 0, 0, { 0, (DWORD)(__FILE__ ": Win16Mutex") }
42 static SYSLEVEL Win16Mutex = { { &critsect_debug, -1, 0, 0, 0, 0 }, 1 };
44 /* Global variable to save current TEB while in 16-bit code */
45 WORD SYSLEVEL_Win16CurrentTeb = 0;
48 /************************************************************************
49 * GetpWin16Lock (KERNEL32.93)
51 VOID WINAPI GetpWin16Lock(SYSLEVEL **lock)
53 *lock = &Win16Mutex;
56 /************************************************************************
57 * GetpWin16Lock (KERNEL.449)
59 SEGPTR WINAPI GetpWin16Lock16(void)
61 static SYSLEVEL *w16Mutex;
62 static SEGPTR segpWin16Mutex;
64 if (!segpWin16Mutex)
66 w16Mutex = &Win16Mutex;
67 segpWin16Mutex = MapLS( &w16Mutex );
69 return segpWin16Mutex;
72 /************************************************************************
73 * _CreateSysLevel (KERNEL.438)
75 VOID WINAPI _CreateSysLevel(SYSLEVEL *lock, INT level)
77 RtlInitializeCriticalSection( &lock->crst );
78 lock->level = level;
80 TRACE("(%p, %d): handle is %p\n",
81 lock, level, lock->crst.LockSemaphore );
84 /************************************************************************
85 * _EnterSysLevel (KERNEL32.97)
86 * _EnterSysLevel (KERNEL.439)
88 VOID WINAPI _EnterSysLevel(SYSLEVEL *lock)
90 TEB *teb = NtCurrentTeb();
91 int i;
93 TRACE("(%p, level %d): thread %lx (fs %04x, pid %ld) count before %ld\n",
94 lock, lock->level, teb->tid, teb->teb_sel, (long) getpid(),
95 teb->sys_count[lock->level] );
97 for ( i = 3; i > lock->level; i-- )
98 if ( teb->sys_count[i] > 0 )
100 ERR("(%p, level %d): Holding %p, level %d. Expect deadlock!\n",
101 lock, lock->level, teb->sys_mutex[i], i );
104 RtlEnterCriticalSection( &lock->crst );
106 teb->sys_count[lock->level]++;
107 teb->sys_mutex[lock->level] = lock;
109 TRACE("(%p, level %d): thread %lx (fs %04x, pid %ld) count after %ld\n",
110 lock, lock->level, teb->tid, teb->teb_sel, (long) getpid(),
111 teb->sys_count[lock->level] );
113 if (lock == &Win16Mutex)
114 SYSLEVEL_Win16CurrentTeb = wine_get_fs();
117 /************************************************************************
118 * _LeaveSysLevel (KERNEL32.98)
119 * _LeaveSysLevel (KERNEL.440)
121 VOID WINAPI _LeaveSysLevel(SYSLEVEL *lock)
123 TEB *teb = NtCurrentTeb();
125 TRACE("(%p, level %d): thread %lx (fs %04x, pid %ld) count before %ld\n",
126 lock, lock->level, teb->tid, teb->teb_sel, (long) getpid(),
127 teb->sys_count[lock->level] );
129 if ( teb->sys_count[lock->level] <= 0 || teb->sys_mutex[lock->level] != lock )
131 ERR("(%p, level %d): Invalid state: count %ld mutex %p.\n",
132 lock, lock->level, teb->sys_count[lock->level],
133 teb->sys_mutex[lock->level] );
135 else
137 if ( --teb->sys_count[lock->level] == 0 )
138 teb->sys_mutex[lock->level] = NULL;
141 RtlLeaveCriticalSection( &lock->crst );
143 TRACE("(%p, level %d): thread %lx (fs %04x, pid %ld) count after %ld\n",
144 lock, lock->level, teb->tid, teb->teb_sel, (long) getpid(),
145 teb->sys_count[lock->level] );
148 /************************************************************************
149 * @ (KERNEL32.86)
151 VOID WINAPI _KERNEL32_86(SYSLEVEL *lock)
153 _LeaveSysLevel(lock);
156 /************************************************************************
157 * _ConfirmSysLevel (KERNEL32.95)
158 * _ConfirmSysLevel (KERNEL.436)
160 DWORD WINAPI _ConfirmSysLevel(SYSLEVEL *lock)
162 if ( lock && lock->crst.OwningThread == (HANDLE)GetCurrentThreadId() )
163 return lock->crst.RecursionCount;
164 else
165 return 0L;
168 /************************************************************************
169 * _CheckNotSysLevel (KERNEL32.94)
170 * _CheckNotSysLevel (KERNEL.437)
172 VOID WINAPI _CheckNotSysLevel(SYSLEVEL *lock)
174 if (lock && lock->crst.OwningThread == (HANDLE)GetCurrentThreadId() &&
175 lock->crst.RecursionCount)
177 ERR( "Holding lock %p level %d\n", lock, lock->level );
178 DbgBreakPoint();
183 /************************************************************************
184 * _EnterWin16Lock [KERNEL.480]
186 VOID WINAPI _EnterWin16Lock(void)
188 _EnterSysLevel(&Win16Mutex);
191 /************************************************************************
192 * _LeaveWin16Lock [KERNEL.481]
194 VOID WINAPI _LeaveWin16Lock(void)
196 _LeaveSysLevel(&Win16Mutex);
199 /************************************************************************
200 * _ConfirmWin16Lock (KERNEL32.96)
202 DWORD WINAPI _ConfirmWin16Lock(void)
204 return _ConfirmSysLevel(&Win16Mutex);
207 /************************************************************************
208 * ReleaseThunkLock (KERNEL32.48)
210 VOID WINAPI ReleaseThunkLock(DWORD *mutex_count)
212 DWORD count = _ConfirmSysLevel(&Win16Mutex);
213 *mutex_count = count;
215 while (count-- > 0)
216 _LeaveSysLevel(&Win16Mutex);
219 /************************************************************************
220 * RestoreThunkLock (KERNEL32.49)
222 VOID WINAPI RestoreThunkLock(DWORD mutex_count)
224 while (mutex_count-- > 0)
225 _EnterSysLevel(&Win16Mutex);
228 /************************************************************************
229 * SYSLEVEL_CheckNotLevel
231 VOID SYSLEVEL_CheckNotLevel( INT level )
233 INT i;
235 for ( i = 3; i >= level; i-- )
236 if ( NtCurrentTeb()->sys_count[i] > 0 )
238 ERR("(%d): Holding lock of level %d!\n",
239 level, i );
240 DbgBreakPoint();
241 break;