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
27 #include <sys/types.h>
31 #include "wine/winbase16.h"
32 #include "kernel16_private.h"
33 #include "wine/library.h"
34 #include "wine/debug.h"
36 WINE_DEFAULT_DEBUG_CHANNEL(syslevel
);
38 static SYSLEVEL Win16Mutex
;
39 static CRITICAL_SECTION_DEBUG critsect_debug
=
41 0, 0, &Win16Mutex
.crst
,
42 { &critsect_debug
.ProcessLocksList
, &critsect_debug
.ProcessLocksList
},
43 0, 0, { (DWORD_PTR
)(__FILE__
": Win16Mutex") }
45 static SYSLEVEL Win16Mutex
= { { &critsect_debug
, -1, 0, 0, 0, 0 }, 1 };
48 /************************************************************************
49 * GetpWin16Lock (KERNEL32.93)
51 VOID WINAPI
GetpWin16Lock(SYSLEVEL
**lock
)
56 /************************************************************************
57 * GetpWin16Lock (KERNEL.449)
59 SEGPTR WINAPI
GetpWin16Lock16(void)
61 static SYSLEVEL
*w16Mutex
;
62 static SEGPTR 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
);
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 struct kernel_thread_data
*thread_data
= kernel_get_thread_data();
93 TRACE("(%p, level %d): thread %x count before %d\n",
94 lock
, lock
->level
, GetCurrentThreadId(), thread_data
->sys_count
[lock
->level
] );
96 for ( i
= 3; i
> lock
->level
; i
-- )
97 if ( thread_data
->sys_count
[i
] > 0 )
99 ERR("(%p, level %d): Holding %p, level %d. Expect deadlock!\n",
100 lock
, lock
->level
, thread_data
->sys_mutex
[i
], i
);
103 RtlEnterCriticalSection( &lock
->crst
);
105 thread_data
->sys_count
[lock
->level
]++;
106 thread_data
->sys_mutex
[lock
->level
] = lock
;
108 TRACE("(%p, level %d): thread %x count after %d\n",
109 lock
, lock
->level
, GetCurrentThreadId(), thread_data
->sys_count
[lock
->level
] );
111 if (lock
== &Win16Mutex
) CallTo16_TebSelector
= wine_get_fs();
114 /************************************************************************
115 * _LeaveSysLevel (KERNEL32.98)
116 * _LeaveSysLevel (KERNEL.440)
118 VOID WINAPI
_LeaveSysLevel(SYSLEVEL
*lock
)
120 struct kernel_thread_data
*thread_data
= kernel_get_thread_data();
122 TRACE("(%p, level %d): thread %x count before %d\n",
123 lock
, lock
->level
, GetCurrentThreadId(), thread_data
->sys_count
[lock
->level
] );
125 if ( thread_data
->sys_count
[lock
->level
] <= 0 || thread_data
->sys_mutex
[lock
->level
] != lock
)
127 ERR("(%p, level %d): Invalid state: count %d mutex %p.\n",
128 lock
, lock
->level
, thread_data
->sys_count
[lock
->level
],
129 thread_data
->sys_mutex
[lock
->level
] );
133 if ( --thread_data
->sys_count
[lock
->level
] == 0 )
134 thread_data
->sys_mutex
[lock
->level
] = NULL
;
137 RtlLeaveCriticalSection( &lock
->crst
);
139 TRACE("(%p, level %d): thread %x count after %d\n",
140 lock
, lock
->level
, GetCurrentThreadId(), thread_data
->sys_count
[lock
->level
] );
143 /************************************************************************
146 VOID WINAPI
_KERNEL32_86(SYSLEVEL
*lock
)
148 _LeaveSysLevel(lock
);
151 /************************************************************************
152 * _ConfirmSysLevel (KERNEL32.95)
153 * _ConfirmSysLevel (KERNEL.436)
155 DWORD WINAPI
_ConfirmSysLevel(SYSLEVEL
*lock
)
157 if ( lock
&& lock
->crst
.OwningThread
== ULongToHandle(GetCurrentThreadId()) )
158 return lock
->crst
.RecursionCount
;
163 /************************************************************************
164 * _CheckNotSysLevel (KERNEL32.94)
165 * _CheckNotSysLevel (KERNEL.437)
167 VOID WINAPI
_CheckNotSysLevel(SYSLEVEL
*lock
)
169 if (lock
&& lock
->crst
.OwningThread
== ULongToHandle(GetCurrentThreadId()) &&
170 lock
->crst
.RecursionCount
)
172 ERR( "Holding lock %p level %d\n", lock
, lock
->level
);
178 /************************************************************************
179 * _EnterWin16Lock [KERNEL.480]
181 VOID WINAPI
_EnterWin16Lock(void)
183 _EnterSysLevel(&Win16Mutex
);
186 /************************************************************************
187 * _LeaveWin16Lock [KERNEL.481]
189 VOID WINAPI
_LeaveWin16Lock(void)
191 _LeaveSysLevel(&Win16Mutex
);
194 /************************************************************************
195 * _ConfirmWin16Lock (KERNEL32.96)
197 DWORD WINAPI
_ConfirmWin16Lock(void)
199 return _ConfirmSysLevel(&Win16Mutex
);
202 /************************************************************************
203 * ReleaseThunkLock (KERNEL32.48)
205 VOID WINAPI
ReleaseThunkLock(DWORD
*mutex_count
)
207 DWORD count
= _ConfirmSysLevel(&Win16Mutex
);
208 *mutex_count
= count
;
211 _LeaveSysLevel(&Win16Mutex
);
214 /************************************************************************
215 * RestoreThunkLock (KERNEL32.49)
217 VOID WINAPI
RestoreThunkLock(DWORD mutex_count
)
219 while (mutex_count
-- > 0)
220 _EnterSysLevel(&Win16Mutex
);
223 /************************************************************************
224 * SYSLEVEL_CheckNotLevel
226 VOID
SYSLEVEL_CheckNotLevel( INT level
)
230 for ( i
= 3; i
>= level
; i
-- )
231 if ( kernel_get_thread_data()->sys_count
[i
] > 0 )
233 ERR("(%d): Holding lock of level %d!\n",