Replace SELECTOR_AllocBlock and SELECTOR_FreeBlock with standard Win16
[wine.git] / scheduler / syslevel.c
blobb25f63e68e3b3b71492cd31698a6029d15ccaaa9
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 <unistd.h>
22 #include <sys/types.h>
23 #include "ntddk.h"
24 #include "syslevel.h"
25 #include "stackframe.h"
26 #include "wine/debug.h"
28 WINE_DEFAULT_DEBUG_CHANNEL(win32);
30 static SYSLEVEL Win16Mutex = { CRITICAL_SECTION_INIT("Win16Mutex"), 1 };
32 /* Global variable to save current TEB while in 16-bit code */
33 WORD SYSLEVEL_Win16CurrentTeb = 0;
36 /************************************************************************
37 * GetpWin16Lock (KERNEL32.93)
39 VOID WINAPI GetpWin16Lock(SYSLEVEL **lock)
41 *lock = &Win16Mutex;
44 /************************************************************************
45 * GetpWin16Lock (KERNEL.449)
47 SEGPTR WINAPI GetpWin16Lock16(void)
49 static SYSLEVEL *w16Mutex;
50 static SEGPTR segpWin16Mutex;
52 if (!segpWin16Mutex)
54 w16Mutex = &Win16Mutex;
55 segpWin16Mutex = MapLS( &w16Mutex );
57 return segpWin16Mutex;
60 /************************************************************************
61 * _CreateSysLevel (KERNEL.438)
63 VOID WINAPI _CreateSysLevel(SYSLEVEL *lock, INT level)
65 InitializeCriticalSection( &lock->crst );
66 lock->level = level;
68 TRACE("(%p, %d): handle is %d\n",
69 lock, level, lock->crst.LockSemaphore );
72 /************************************************************************
73 * _EnterSysLevel (KERNEL32.97)
74 * _EnterSysLevel (KERNEL.439)
76 VOID WINAPI _EnterSysLevel(SYSLEVEL *lock)
78 TEB *teb = NtCurrentTeb();
79 int i;
81 TRACE("(%p, level %d): thread %p (fs %04x, pid %ld) count before %ld\n",
82 lock, lock->level, teb->tid, teb->teb_sel, (long) getpid(),
83 teb->sys_count[lock->level] );
85 for ( i = 3; i > lock->level; i-- )
86 if ( teb->sys_count[i] > 0 )
88 ERR("(%p, level %d): Holding %p, level %d. Expect deadlock!\n",
89 lock, lock->level, teb->sys_mutex[i], i );
92 EnterCriticalSection( &lock->crst );
94 teb->sys_count[lock->level]++;
95 teb->sys_mutex[lock->level] = lock;
97 TRACE("(%p, level %d): thread %p (fs %04x, pid %ld) count after %ld\n",
98 lock, lock->level, teb->tid, teb->teb_sel, (long) getpid(),
99 teb->sys_count[lock->level] );
101 if (lock == &Win16Mutex)
102 SYSLEVEL_Win16CurrentTeb = __get_fs();
105 /************************************************************************
106 * _LeaveSysLevel (KERNEL32.98)
107 * _LeaveSysLevel (KERNEL.440)
109 VOID WINAPI _LeaveSysLevel(SYSLEVEL *lock)
111 TEB *teb = NtCurrentTeb();
113 TRACE("(%p, level %d): thread %p (fs %04x, pid %ld) count before %ld\n",
114 lock, lock->level, teb->tid, teb->teb_sel, (long) getpid(),
115 teb->sys_count[lock->level] );
117 if ( teb->sys_count[lock->level] <= 0 || teb->sys_mutex[lock->level] != lock )
119 ERR("(%p, level %d): Invalid state: count %ld mutex %p.\n",
120 lock, lock->level, teb->sys_count[lock->level],
121 teb->sys_mutex[lock->level] );
123 else
125 if ( --teb->sys_count[lock->level] == 0 )
126 teb->sys_mutex[lock->level] = NULL;
129 LeaveCriticalSection( &lock->crst );
131 TRACE("(%p, level %d): thread %p (fs %04x, pid %ld) count after %ld\n",
132 lock, lock->level, teb->tid, teb->teb_sel, (long) getpid(),
133 teb->sys_count[lock->level] );
136 /************************************************************************
137 * @ (KERNEL32.86)
139 VOID WINAPI _KERNEL32_86(SYSLEVEL *lock)
141 _LeaveSysLevel(lock);
144 /************************************************************************
145 * _ConfirmSysLevel (KERNEL32.95)
146 * _ConfirmSysLevel (KERNEL.436)
148 DWORD WINAPI _ConfirmSysLevel(SYSLEVEL *lock)
150 if ( lock && lock->crst.OwningThread == GetCurrentThreadId() )
151 return lock->crst.RecursionCount;
152 else
153 return 0L;
156 /************************************************************************
157 * _CheckNotSysLevel (KERNEL32.94)
158 * _CheckNotSysLevel (KERNEL.437)
160 VOID WINAPI _CheckNotSysLevel(SYSLEVEL *lock)
162 if (lock && lock->crst.OwningThread == GetCurrentThreadId() && lock->crst.RecursionCount)
164 ERR( "Holding lock %p level %d\n", lock, lock->level );
165 DbgBreakPoint();
170 /************************************************************************
171 * _EnterWin16Lock [KERNEL.480]
173 VOID WINAPI _EnterWin16Lock(void)
175 _EnterSysLevel(&Win16Mutex);
178 /************************************************************************
179 * _LeaveWin16Lock [KERNEL.481]
181 VOID WINAPI _LeaveWin16Lock(void)
183 _LeaveSysLevel(&Win16Mutex);
186 /************************************************************************
187 * _ConfirmWin16Lock (KERNEL32.96)
189 DWORD WINAPI _ConfirmWin16Lock(void)
191 return _ConfirmSysLevel(&Win16Mutex);
194 /************************************************************************
195 * ReleaseThunkLock (KERNEL32.48)
197 VOID WINAPI ReleaseThunkLock(DWORD *mutex_count)
199 DWORD count = _ConfirmSysLevel(&Win16Mutex);
200 *mutex_count = count;
202 while (count-- > 0)
203 _LeaveSysLevel(&Win16Mutex);
206 /************************************************************************
207 * RestoreThunkLock (KERNEL32.49)
209 VOID WINAPI RestoreThunkLock(DWORD mutex_count)
211 while (mutex_count-- > 0)
212 _EnterSysLevel(&Win16Mutex);
215 /************************************************************************
216 * SYSLEVEL_CheckNotLevel
218 VOID SYSLEVEL_CheckNotLevel( INT level )
220 INT i;
222 for ( i = 3; i >= level; i-- )
223 if ( NtCurrentTeb()->sys_count[i] > 0 )
225 ERR("(%d): Holding lock of level %d!\n",
226 level, i );
227 DbgBreakPoint();
228 break;