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., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
22 #include <sys/types.h>
27 #include "wine/winbase16.h"
28 #include "kernel16_private.h"
29 #include "wine/debug.h"
31 WINE_DEFAULT_DEBUG_CHANNEL(syslevel
);
33 static SYSLEVEL Win16Mutex
;
34 static CRITICAL_SECTION_DEBUG critsect_debug
=
36 0, 0, &Win16Mutex
.crst
,
37 { &critsect_debug
.ProcessLocksList
, &critsect_debug
.ProcessLocksList
},
38 0, 0, { (DWORD_PTR
)(__FILE__
": Win16Mutex") }
40 static SYSLEVEL Win16Mutex
= { { &critsect_debug
, -1, 0, 0, 0, 0 }, 1 };
43 /************************************************************************
44 * GetpWin16Lock (KERNEL32.93)
46 VOID WINAPI
GetpWin16Lock(SYSLEVEL
**lock
)
51 /************************************************************************
52 * GetpWin16Lock (KERNEL.449)
54 SEGPTR WINAPI
GetpWin16Lock16(void)
56 static SYSLEVEL
*w16Mutex
;
57 static SEGPTR segpWin16Mutex
;
61 w16Mutex
= &Win16Mutex
;
62 segpWin16Mutex
= MapLS( &w16Mutex
);
64 return segpWin16Mutex
;
67 /************************************************************************
68 * _CreateSysLevel (KERNEL.438)
70 VOID WINAPI
_CreateSysLevel(SYSLEVEL
*lock
, INT level
)
72 RtlInitializeCriticalSection( &lock
->crst
);
75 TRACE("(%p, %d): handle is %p\n",
76 lock
, level
, lock
->crst
.LockSemaphore
);
79 /************************************************************************
80 * _EnterSysLevel (KERNEL32.97)
81 * _EnterSysLevel (KERNEL.439)
83 VOID WINAPI
_EnterSysLevel(SYSLEVEL
*lock
)
85 struct kernel_thread_data
*thread_data
= kernel_get_thread_data();
88 TRACE("(%p, level %d): thread %x count before %d\n",
89 lock
, lock
->level
, GetCurrentThreadId(), thread_data
->sys_count
[lock
->level
] );
91 for ( i
= 3; i
> lock
->level
; i
-- )
92 if ( thread_data
->sys_count
[i
] > 0 )
94 ERR("(%p, level %d): Holding %p, level %d. Expect deadlock!\n",
95 lock
, lock
->level
, thread_data
->sys_mutex
[i
], i
);
98 RtlEnterCriticalSection( &lock
->crst
);
100 thread_data
->sys_count
[lock
->level
]++;
101 thread_data
->sys_mutex
[lock
->level
] = lock
;
103 TRACE("(%p, level %d): thread %x count after %d\n",
104 lock
, lock
->level
, GetCurrentThreadId(), thread_data
->sys_count
[lock
->level
] );
106 if (lock
== &Win16Mutex
) CallTo16_TebSelector
= get_fs();
109 /************************************************************************
110 * _LeaveSysLevel (KERNEL32.98)
111 * _LeaveSysLevel (KERNEL.440)
113 VOID WINAPI
_LeaveSysLevel(SYSLEVEL
*lock
)
115 struct kernel_thread_data
*thread_data
= kernel_get_thread_data();
117 TRACE("(%p, level %d): thread %x count before %d\n",
118 lock
, lock
->level
, GetCurrentThreadId(), thread_data
->sys_count
[lock
->level
] );
120 if ( thread_data
->sys_count
[lock
->level
] <= 0 || thread_data
->sys_mutex
[lock
->level
] != lock
)
122 ERR("(%p, level %d): Invalid state: count %d mutex %p.\n",
123 lock
, lock
->level
, thread_data
->sys_count
[lock
->level
],
124 thread_data
->sys_mutex
[lock
->level
] );
128 if ( --thread_data
->sys_count
[lock
->level
] == 0 )
129 thread_data
->sys_mutex
[lock
->level
] = NULL
;
132 RtlLeaveCriticalSection( &lock
->crst
);
134 TRACE("(%p, level %d): thread %x count after %d\n",
135 lock
, lock
->level
, GetCurrentThreadId(), thread_data
->sys_count
[lock
->level
] );
138 /************************************************************************
141 VOID WINAPI
_KERNEL32_86(SYSLEVEL
*lock
)
143 _LeaveSysLevel(lock
);
146 /************************************************************************
147 * _ConfirmSysLevel (KERNEL32.95)
148 * _ConfirmSysLevel (KERNEL.436)
150 DWORD WINAPI
_ConfirmSysLevel(SYSLEVEL
*lock
)
152 if ( lock
&& lock
->crst
.OwningThread
== ULongToHandle(GetCurrentThreadId()) )
153 return lock
->crst
.RecursionCount
;
158 /************************************************************************
159 * _CheckNotSysLevel (KERNEL32.94)
160 * _CheckNotSysLevel (KERNEL.437)
162 VOID WINAPI
_CheckNotSysLevel(SYSLEVEL
*lock
)
164 if (lock
&& lock
->crst
.OwningThread
== ULongToHandle(GetCurrentThreadId()) &&
165 lock
->crst
.RecursionCount
)
167 ERR( "Holding lock %p level %d\n", lock
, lock
->level
);
173 /************************************************************************
174 * _EnterWin16Lock [KERNEL.480]
176 VOID WINAPI
_EnterWin16Lock(void)
178 _EnterSysLevel(&Win16Mutex
);
181 /************************************************************************
182 * _LeaveWin16Lock [KERNEL.481]
184 VOID WINAPI
_LeaveWin16Lock(void)
186 _LeaveSysLevel(&Win16Mutex
);
189 /************************************************************************
190 * _ConfirmWin16Lock (KERNEL32.96)
192 DWORD WINAPI
_ConfirmWin16Lock(void)
194 return _ConfirmSysLevel(&Win16Mutex
);
197 /************************************************************************
198 * ReleaseThunkLock (KERNEL32.48)
200 VOID WINAPI
ReleaseThunkLock(DWORD
*mutex_count
)
202 DWORD count
= _ConfirmSysLevel(&Win16Mutex
);
203 *mutex_count
= count
;
206 _LeaveSysLevel(&Win16Mutex
);
209 /************************************************************************
210 * RestoreThunkLock (KERNEL32.49)
212 VOID WINAPI
RestoreThunkLock(DWORD mutex_count
)
214 while (mutex_count
-- > 0)
215 _EnterSysLevel(&Win16Mutex
);
218 /************************************************************************
219 * SYSLEVEL_CheckNotLevel
221 VOID
SYSLEVEL_CheckNotLevel( INT level
)
225 for ( i
= 3; i
>= level
; i
-- )
226 if ( kernel_get_thread_data()->sys_count
[i
] > 0 )
228 ERR("(%d): Holding lock of level %d!\n",