Alexandre Julliard | 767e6f6 | 1998-08-09 12:47:43 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Win32 'syslevel' routines |
| 3 | * |
| 4 | * Copyright 1998 Ulrich Weigand |
Alexandre Julliard | 0799c1a | 2002-03-09 23:29:33 +0000 | [diff] [blame] | 5 | * |
| 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. |
| 10 | * |
| 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. |
| 15 | * |
| 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 |
Alexandre Julliard | 767e6f6 | 1998-08-09 12:47:43 +0000 | [diff] [blame] | 19 | */ |
| 20 | |
Patrik Stridvall | d016f81 | 2002-08-17 00:43:16 +0000 | [diff] [blame] | 21 | #include "config.h" |
| 22 | |
| 23 | #ifdef HAVE_UNISTD_H |
| 24 | # include <unistd.h> |
| 25 | #endif |
Ulrich Weigand | c5b8b3a | 1999-05-22 16:28:47 +0000 | [diff] [blame] | 26 | #include <sys/types.h> |
Patrik Stridvall | 9c1de6d | 2002-09-12 22:07:02 +0000 | [diff] [blame] | 27 | #include "winternl.h" |
Alexandre Julliard | 767e6f6 | 1998-08-09 12:47:43 +0000 | [diff] [blame] | 28 | #include "syslevel.h" |
Alexandre Julliard | 767e6f6 | 1998-08-09 12:47:43 +0000 | [diff] [blame] | 29 | #include "stackframe.h" |
Alexandre Julliard | 5900867 | 2002-05-16 20:32:16 +0000 | [diff] [blame] | 30 | #include "wine/library.h" |
Alexandre Julliard | 0799c1a | 2002-03-09 23:29:33 +0000 | [diff] [blame] | 31 | #include "wine/debug.h" |
Alexandre Julliard | 767e6f6 | 1998-08-09 12:47:43 +0000 | [diff] [blame] | 32 | |
Alexandre Julliard | 0799c1a | 2002-03-09 23:29:33 +0000 | [diff] [blame] | 33 | WINE_DEFAULT_DEBUG_CHANNEL(win32); |
Patrik Stridvall | b4b9fae | 1999-04-19 14:56:29 +0000 | [diff] [blame] | 34 | |
Alexandre Julliard | 301df6b | 2001-08-16 18:12:56 +0000 | [diff] [blame] | 35 | static SYSLEVEL Win16Mutex = { CRITICAL_SECTION_INIT("Win16Mutex"), 1 }; |
Alexandre Julliard | 767e6f6 | 1998-08-09 12:47:43 +0000 | [diff] [blame] | 36 | |
Alexandre Julliard | 85ed45e | 1998-08-22 19:03:56 +0000 | [diff] [blame] | 37 | /* Global variable to save current TEB while in 16-bit code */ |
| 38 | WORD SYSLEVEL_Win16CurrentTeb = 0; |
| 39 | |
Alexandre Julliard | 767e6f6 | 1998-08-09 12:47:43 +0000 | [diff] [blame] | 40 | |
| 41 | /************************************************************************ |
Patrik Stridvall | 2d6457c | 2000-03-28 20:22:59 +0000 | [diff] [blame] | 42 | * GetpWin16Lock (KERNEL32.93) |
Alexandre Julliard | 767e6f6 | 1998-08-09 12:47:43 +0000 | [diff] [blame] | 43 | */ |
Ulrich Weigand | c5b8b3a | 1999-05-22 16:28:47 +0000 | [diff] [blame] | 44 | VOID WINAPI GetpWin16Lock(SYSLEVEL **lock) |
Alexandre Julliard | 767e6f6 | 1998-08-09 12:47:43 +0000 | [diff] [blame] | 45 | { |
| 46 | *lock = &Win16Mutex; |
| 47 | } |
| 48 | |
| 49 | /************************************************************************ |
Patrik Stridvall | 15a3b74 | 2001-04-27 18:03:51 +0000 | [diff] [blame] | 50 | * GetpWin16Lock (KERNEL.449) |
Alexandre Julliard | 767e6f6 | 1998-08-09 12:47:43 +0000 | [diff] [blame] | 51 | */ |
Alexandre Julliard | ab68797 | 2000-11-15 23:41:46 +0000 | [diff] [blame] | 52 | SEGPTR WINAPI GetpWin16Lock16(void) |
| 53 | { |
Alexandre Julliard | d7b7682 | 2001-12-20 00:19:40 +0000 | [diff] [blame] | 54 | static SYSLEVEL *w16Mutex; |
Alexandre Julliard | ab68797 | 2000-11-15 23:41:46 +0000 | [diff] [blame] | 55 | static SEGPTR segpWin16Mutex; |
Alexandre Julliard | d7b7682 | 2001-12-20 00:19:40 +0000 | [diff] [blame] | 56 | |
Alexandre Julliard | ab68797 | 2000-11-15 23:41:46 +0000 | [diff] [blame] | 57 | if (!segpWin16Mutex) |
| 58 | { |
Alexandre Julliard | d7b7682 | 2001-12-20 00:19:40 +0000 | [diff] [blame] | 59 | w16Mutex = &Win16Mutex; |
| 60 | segpWin16Mutex = MapLS( &w16Mutex ); |
Alexandre Julliard | ab68797 | 2000-11-15 23:41:46 +0000 | [diff] [blame] | 61 | } |
Alexandre Julliard | 767e6f6 | 1998-08-09 12:47:43 +0000 | [diff] [blame] | 62 | return segpWin16Mutex; |
| 63 | } |
| 64 | |
| 65 | /************************************************************************ |
Ulrich Weigand | c5b8b3a | 1999-05-22 16:28:47 +0000 | [diff] [blame] | 66 | * _CreateSysLevel (KERNEL.438) |
Alexandre Julliard | 767e6f6 | 1998-08-09 12:47:43 +0000 | [diff] [blame] | 67 | */ |
Ulrich Weigand | c5b8b3a | 1999-05-22 16:28:47 +0000 | [diff] [blame] | 68 | VOID WINAPI _CreateSysLevel(SYSLEVEL *lock, INT level) |
Alexandre Julliard | 767e6f6 | 1998-08-09 12:47:43 +0000 | [diff] [blame] | 69 | { |
Eric Pouech | c962a69 | 2003-06-23 18:12:28 +0000 | [diff] [blame^] | 70 | RtlInitializeCriticalSection( &lock->crst ); |
Ulrich Weigand | c5b8b3a | 1999-05-22 16:28:47 +0000 | [diff] [blame] | 71 | lock->level = level; |
| 72 | |
Andrew John Hughes | ed800c6 | 2002-11-21 03:45:01 +0000 | [diff] [blame] | 73 | TRACE("(%p, %d): handle is %p\n", |
Ulrich Weigand | c5b8b3a | 1999-05-22 16:28:47 +0000 | [diff] [blame] | 74 | lock, level, lock->crst.LockSemaphore ); |
| 75 | } |
| 76 | |
| 77 | /************************************************************************ |
Patrik Stridvall | dae8de6 | 2001-06-13 20:13:18 +0000 | [diff] [blame] | 78 | * _EnterSysLevel (KERNEL32.97) |
| 79 | * _EnterSysLevel (KERNEL.439) |
Ulrich Weigand | c5b8b3a | 1999-05-22 16:28:47 +0000 | [diff] [blame] | 80 | */ |
| 81 | VOID WINAPI _EnterSysLevel(SYSLEVEL *lock) |
| 82 | { |
Alexandre Julliard | 0a860a0 | 1999-06-22 11:43:42 +0000 | [diff] [blame] | 83 | TEB *teb = NtCurrentTeb(); |
Ulrich Weigand | c5b8b3a | 1999-05-22 16:28:47 +0000 | [diff] [blame] | 84 | int i; |
| 85 | |
Alexandre Julliard | 54f2287 | 2002-10-03 19:54:57 +0000 | [diff] [blame] | 86 | TRACE("(%p, level %d): thread %lx (fs %04x, pid %ld) count before %ld\n", |
Patrik Stridvall | 1ed4ecf | 1999-06-26 14:58:24 +0000 | [diff] [blame] | 87 | lock, lock->level, teb->tid, teb->teb_sel, (long) getpid(), |
Alexandre Julliard | 0a860a0 | 1999-06-22 11:43:42 +0000 | [diff] [blame] | 88 | teb->sys_count[lock->level] ); |
Ulrich Weigand | c5b8b3a | 1999-05-22 16:28:47 +0000 | [diff] [blame] | 89 | |
| 90 | for ( i = 3; i > lock->level; i-- ) |
Alexandre Julliard | 0a860a0 | 1999-06-22 11:43:42 +0000 | [diff] [blame] | 91 | if ( teb->sys_count[i] > 0 ) |
Ulrich Weigand | c5b8b3a | 1999-05-22 16:28:47 +0000 | [diff] [blame] | 92 | { |
Vincent Béron | 9a62491 | 2002-05-31 23:06:46 +0000 | [diff] [blame] | 93 | ERR("(%p, level %d): Holding %p, level %d. Expect deadlock!\n", |
Alexandre Julliard | 0a860a0 | 1999-06-22 11:43:42 +0000 | [diff] [blame] | 94 | lock, lock->level, teb->sys_mutex[i], i ); |
Ulrich Weigand | c5b8b3a | 1999-05-22 16:28:47 +0000 | [diff] [blame] | 95 | } |
| 96 | |
Eric Pouech | c962a69 | 2003-06-23 18:12:28 +0000 | [diff] [blame^] | 97 | RtlEnterCriticalSection( &lock->crst ); |
Ulrich Weigand | c5b8b3a | 1999-05-22 16:28:47 +0000 | [diff] [blame] | 98 | |
Alexandre Julliard | 0a860a0 | 1999-06-22 11:43:42 +0000 | [diff] [blame] | 99 | teb->sys_count[lock->level]++; |
| 100 | teb->sys_mutex[lock->level] = lock; |
Ulrich Weigand | c5b8b3a | 1999-05-22 16:28:47 +0000 | [diff] [blame] | 101 | |
Alexandre Julliard | 54f2287 | 2002-10-03 19:54:57 +0000 | [diff] [blame] | 102 | TRACE("(%p, level %d): thread %lx (fs %04x, pid %ld) count after %ld\n", |
Vincent Béron | 9a62491 | 2002-05-31 23:06:46 +0000 | [diff] [blame] | 103 | lock, lock->level, teb->tid, teb->teb_sel, (long) getpid(), |
Alexandre Julliard | 0a860a0 | 1999-06-22 11:43:42 +0000 | [diff] [blame] | 104 | teb->sys_count[lock->level] ); |
Alexandre Julliard | 85ed45e | 1998-08-22 19:03:56 +0000 | [diff] [blame] | 105 | |
| 106 | if (lock == &Win16Mutex) |
Alexandre Julliard | 5900867 | 2002-05-16 20:32:16 +0000 | [diff] [blame] | 107 | SYSLEVEL_Win16CurrentTeb = wine_get_fs(); |
Alexandre Julliard | 767e6f6 | 1998-08-09 12:47:43 +0000 | [diff] [blame] | 108 | } |
| 109 | |
| 110 | /************************************************************************ |
Patrik Stridvall | dae8de6 | 2001-06-13 20:13:18 +0000 | [diff] [blame] | 111 | * _LeaveSysLevel (KERNEL32.98) |
| 112 | * _LeaveSysLevel (KERNEL.440) |
Alexandre Julliard | 767e6f6 | 1998-08-09 12:47:43 +0000 | [diff] [blame] | 113 | */ |
Ulrich Weigand | c5b8b3a | 1999-05-22 16:28:47 +0000 | [diff] [blame] | 114 | VOID WINAPI _LeaveSysLevel(SYSLEVEL *lock) |
Alexandre Julliard | 767e6f6 | 1998-08-09 12:47:43 +0000 | [diff] [blame] | 115 | { |
Alexandre Julliard | 0a860a0 | 1999-06-22 11:43:42 +0000 | [diff] [blame] | 116 | TEB *teb = NtCurrentTeb(); |
Ulrich Weigand | c5b8b3a | 1999-05-22 16:28:47 +0000 | [diff] [blame] | 117 | |
Alexandre Julliard | 54f2287 | 2002-10-03 19:54:57 +0000 | [diff] [blame] | 118 | TRACE("(%p, level %d): thread %lx (fs %04x, pid %ld) count before %ld\n", |
Patrik Stridvall | 1ed4ecf | 1999-06-26 14:58:24 +0000 | [diff] [blame] | 119 | lock, lock->level, teb->tid, teb->teb_sel, (long) getpid(), |
Alexandre Julliard | 0a860a0 | 1999-06-22 11:43:42 +0000 | [diff] [blame] | 120 | teb->sys_count[lock->level] ); |
Ulrich Weigand | c5b8b3a | 1999-05-22 16:28:47 +0000 | [diff] [blame] | 121 | |
Alexandre Julliard | 0a860a0 | 1999-06-22 11:43:42 +0000 | [diff] [blame] | 122 | if ( teb->sys_count[lock->level] <= 0 || teb->sys_mutex[lock->level] != lock ) |
Ulrich Weigand | c5b8b3a | 1999-05-22 16:28:47 +0000 | [diff] [blame] | 123 | { |
Alexandre Julliard | 1565709 | 1999-05-23 10:25:25 +0000 | [diff] [blame] | 124 | ERR("(%p, level %d): Invalid state: count %ld mutex %p.\n", |
Vincent Béron | 9a62491 | 2002-05-31 23:06:46 +0000 | [diff] [blame] | 125 | lock, lock->level, teb->sys_count[lock->level], |
Alexandre Julliard | 0a860a0 | 1999-06-22 11:43:42 +0000 | [diff] [blame] | 126 | teb->sys_mutex[lock->level] ); |
Ulrich Weigand | c5b8b3a | 1999-05-22 16:28:47 +0000 | [diff] [blame] | 127 | } |
| 128 | else |
| 129 | { |
Alexandre Julliard | 0a860a0 | 1999-06-22 11:43:42 +0000 | [diff] [blame] | 130 | if ( --teb->sys_count[lock->level] == 0 ) |
| 131 | teb->sys_mutex[lock->level] = NULL; |
Ulrich Weigand | c5b8b3a | 1999-05-22 16:28:47 +0000 | [diff] [blame] | 132 | } |
| 133 | |
Eric Pouech | c962a69 | 2003-06-23 18:12:28 +0000 | [diff] [blame^] | 134 | RtlLeaveCriticalSection( &lock->crst ); |
Ulrich Weigand | c5b8b3a | 1999-05-22 16:28:47 +0000 | [diff] [blame] | 135 | |
Alexandre Julliard | 54f2287 | 2002-10-03 19:54:57 +0000 | [diff] [blame] | 136 | TRACE("(%p, level %d): thread %lx (fs %04x, pid %ld) count after %ld\n", |
Vincent Béron | 9a62491 | 2002-05-31 23:06:46 +0000 | [diff] [blame] | 137 | lock, lock->level, teb->tid, teb->teb_sel, (long) getpid(), |
Alexandre Julliard | 0a860a0 | 1999-06-22 11:43:42 +0000 | [diff] [blame] | 138 | teb->sys_count[lock->level] ); |
Alexandre Julliard | 767e6f6 | 1998-08-09 12:47:43 +0000 | [diff] [blame] | 139 | } |
| 140 | |
| 141 | /************************************************************************ |
Patrik Stridvall | 01d5e5b | 2001-07-02 19:59:40 +0000 | [diff] [blame] | 142 | * @ (KERNEL32.86) |
Alexandre Julliard | 767e6f6 | 1998-08-09 12:47:43 +0000 | [diff] [blame] | 143 | */ |
Ulrich Weigand | c5b8b3a | 1999-05-22 16:28:47 +0000 | [diff] [blame] | 144 | VOID WINAPI _KERNEL32_86(SYSLEVEL *lock) |
Alexandre Julliard | 767e6f6 | 1998-08-09 12:47:43 +0000 | [diff] [blame] | 145 | { |
| 146 | _LeaveSysLevel(lock); |
| 147 | } |
| 148 | |
| 149 | /************************************************************************ |
Patrik Stridvall | dae8de6 | 2001-06-13 20:13:18 +0000 | [diff] [blame] | 150 | * _ConfirmSysLevel (KERNEL32.95) |
| 151 | * _ConfirmSysLevel (KERNEL.436) |
Ulrich Weigand | c5b8b3a | 1999-05-22 16:28:47 +0000 | [diff] [blame] | 152 | */ |
| 153 | DWORD WINAPI _ConfirmSysLevel(SYSLEVEL *lock) |
| 154 | { |
Alexandre Julliard | 7375597 | 2002-07-31 19:26:03 +0000 | [diff] [blame] | 155 | if ( lock && lock->crst.OwningThread == (HANDLE)GetCurrentThreadId() ) |
Ulrich Weigand | c5b8b3a | 1999-05-22 16:28:47 +0000 | [diff] [blame] | 156 | return lock->crst.RecursionCount; |
| 157 | else |
| 158 | return 0L; |
| 159 | } |
| 160 | |
| 161 | /************************************************************************ |
Patrik Stridvall | dae8de6 | 2001-06-13 20:13:18 +0000 | [diff] [blame] | 162 | * _CheckNotSysLevel (KERNEL32.94) |
| 163 | * _CheckNotSysLevel (KERNEL.437) |
Ulrich Weigand | c5b8b3a | 1999-05-22 16:28:47 +0000 | [diff] [blame] | 164 | */ |
| 165 | VOID WINAPI _CheckNotSysLevel(SYSLEVEL *lock) |
| 166 | { |
Alexandre Julliard | 7375597 | 2002-07-31 19:26:03 +0000 | [diff] [blame] | 167 | if (lock && lock->crst.OwningThread == (HANDLE)GetCurrentThreadId() && |
| 168 | lock->crst.RecursionCount) |
Alexandre Julliard | 181e3d8 | 2001-08-15 23:32:47 +0000 | [diff] [blame] | 169 | { |
| 170 | ERR( "Holding lock %p level %d\n", lock, lock->level ); |
| 171 | DbgBreakPoint(); |
| 172 | } |
Ulrich Weigand | c5b8b3a | 1999-05-22 16:28:47 +0000 | [diff] [blame] | 173 | } |
| 174 | |
| 175 | |
| 176 | /************************************************************************ |
Alexandre Julliard | ab68797 | 2000-11-15 23:41:46 +0000 | [diff] [blame] | 177 | * _EnterWin16Lock [KERNEL.480] |
Alexandre Julliard | 767e6f6 | 1998-08-09 12:47:43 +0000 | [diff] [blame] | 178 | */ |
Alexandre Julliard | ab68797 | 2000-11-15 23:41:46 +0000 | [diff] [blame] | 179 | VOID WINAPI _EnterWin16Lock(void) |
Alexandre Julliard | 767e6f6 | 1998-08-09 12:47:43 +0000 | [diff] [blame] | 180 | { |
Alexandre Julliard | 767e6f6 | 1998-08-09 12:47:43 +0000 | [diff] [blame] | 181 | _EnterSysLevel(&Win16Mutex); |
Alexandre Julliard | 767e6f6 | 1998-08-09 12:47:43 +0000 | [diff] [blame] | 182 | } |
| 183 | |
| 184 | /************************************************************************ |
Alexandre Julliard | ab68797 | 2000-11-15 23:41:46 +0000 | [diff] [blame] | 185 | * _LeaveWin16Lock [KERNEL.481] |
Alexandre Julliard | 767e6f6 | 1998-08-09 12:47:43 +0000 | [diff] [blame] | 186 | */ |
Alexandre Julliard | ab68797 | 2000-11-15 23:41:46 +0000 | [diff] [blame] | 187 | VOID WINAPI _LeaveWin16Lock(void) |
Alexandre Julliard | 767e6f6 | 1998-08-09 12:47:43 +0000 | [diff] [blame] | 188 | { |
Alexandre Julliard | 767e6f6 | 1998-08-09 12:47:43 +0000 | [diff] [blame] | 189 | _LeaveSysLevel(&Win16Mutex); |
| 190 | } |
Alexandre Julliard | ab68797 | 2000-11-15 23:41:46 +0000 | [diff] [blame] | 191 | |
Alexandre Julliard | 767e6f6 | 1998-08-09 12:47:43 +0000 | [diff] [blame] | 192 | /************************************************************************ |
| 193 | * _ConfirmWin16Lock (KERNEL32.96) |
| 194 | */ |
| 195 | DWORD WINAPI _ConfirmWin16Lock(void) |
| 196 | { |
Ulrich Weigand | c5b8b3a | 1999-05-22 16:28:47 +0000 | [diff] [blame] | 197 | return _ConfirmSysLevel(&Win16Mutex); |
Alexandre Julliard | 767e6f6 | 1998-08-09 12:47:43 +0000 | [diff] [blame] | 198 | } |
| 199 | |
| 200 | /************************************************************************ |
| 201 | * ReleaseThunkLock (KERNEL32.48) |
| 202 | */ |
| 203 | VOID WINAPI ReleaseThunkLock(DWORD *mutex_count) |
| 204 | { |
Ulrich Weigand | c5b8b3a | 1999-05-22 16:28:47 +0000 | [diff] [blame] | 205 | DWORD count = _ConfirmSysLevel(&Win16Mutex); |
Alexandre Julliard | 767e6f6 | 1998-08-09 12:47:43 +0000 | [diff] [blame] | 206 | *mutex_count = count; |
| 207 | |
| 208 | while (count-- > 0) |
| 209 | _LeaveSysLevel(&Win16Mutex); |
| 210 | } |
| 211 | |
| 212 | /************************************************************************ |
| 213 | * RestoreThunkLock (KERNEL32.49) |
| 214 | */ |
| 215 | VOID WINAPI RestoreThunkLock(DWORD mutex_count) |
| 216 | { |
| 217 | while (mutex_count-- > 0) |
| 218 | _EnterSysLevel(&Win16Mutex); |
| 219 | } |
| 220 | |
| 221 | /************************************************************************ |
Ulrich Weigand | c5b8b3a | 1999-05-22 16:28:47 +0000 | [diff] [blame] | 222 | * SYSLEVEL_CheckNotLevel |
| 223 | */ |
| 224 | VOID SYSLEVEL_CheckNotLevel( INT level ) |
| 225 | { |
Ulrich Weigand | c5b8b3a | 1999-05-22 16:28:47 +0000 | [diff] [blame] | 226 | INT i; |
| 227 | |
| 228 | for ( i = 3; i >= level; i-- ) |
Alexandre Julliard | 0a860a0 | 1999-06-22 11:43:42 +0000 | [diff] [blame] | 229 | if ( NtCurrentTeb()->sys_count[i] > 0 ) |
Ulrich Weigand | c5b8b3a | 1999-05-22 16:28:47 +0000 | [diff] [blame] | 230 | { |
Vincent Béron | 9a62491 | 2002-05-31 23:06:46 +0000 | [diff] [blame] | 231 | ERR("(%d): Holding lock of level %d!\n", |
Ulrich Weigand | c5b8b3a | 1999-05-22 16:28:47 +0000 | [diff] [blame] | 232 | level, i ); |
Alexandre Julliard | afb49ea | 2000-09-29 20:48:04 +0000 | [diff] [blame] | 233 | DbgBreakPoint(); |
Ulrich Weigand | c5b8b3a | 1999-05-22 16:28:47 +0000 | [diff] [blame] | 234 | break; |
| 235 | } |
Alexandre Julliard | 767e6f6 | 1998-08-09 12:47:43 +0000 | [diff] [blame] | 236 | } |