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