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
26 #include <sys/types.h>
29 #include "stackframe.h"
30 #include "wine/library.h"
31 #include "wine/debug.h"
33 WINE_DEFAULT_DEBUG_CHANNEL(win32
);
35 static SYSLEVEL Win16Mutex
= { CRITICAL_SECTION_INIT("Win16Mutex"), 1 };
37 /* Global variable to save current TEB while in 16-bit code */
38 WORD SYSLEVEL_Win16CurrentTeb
= 0;
41 /************************************************************************
42 * GetpWin16Lock (KERNEL32.93)
44 VOID WINAPI
GetpWin16Lock(SYSLEVEL
**lock
)
49 /************************************************************************
50 * GetpWin16Lock (KERNEL.449)
52 SEGPTR WINAPI
GetpWin16Lock16(void)
54 static SYSLEVEL
*w16Mutex
;
55 static SEGPTR segpWin16Mutex
;
59 w16Mutex
= &Win16Mutex
;
60 segpWin16Mutex
= MapLS( &w16Mutex
);
62 return segpWin16Mutex
;
65 /************************************************************************
66 * _CreateSysLevel (KERNEL.438)
68 VOID WINAPI
_CreateSysLevel(SYSLEVEL
*lock
, INT level
)
70 InitializeCriticalSection( &lock
->crst
);
73 TRACE("(%p, %d): handle is %p\n",
74 lock
, level
, lock
->crst
.LockSemaphore
);
77 /************************************************************************
78 * _EnterSysLevel (KERNEL32.97)
79 * _EnterSysLevel (KERNEL.439)
81 VOID WINAPI
_EnterSysLevel(SYSLEVEL
*lock
)
83 TEB
*teb
= NtCurrentTeb();
86 TRACE("(%p, level %d): thread %lx (fs %04x, pid %ld) count before %ld\n",
87 lock
, lock
->level
, teb
->tid
, teb
->teb_sel
, (long) getpid(),
88 teb
->sys_count
[lock
->level
] );
90 for ( i
= 3; i
> lock
->level
; i
-- )
91 if ( teb
->sys_count
[i
] > 0 )
93 ERR("(%p, level %d): Holding %p, level %d. Expect deadlock!\n",
94 lock
, lock
->level
, teb
->sys_mutex
[i
], i
);
97 EnterCriticalSection( &lock
->crst
);
99 teb
->sys_count
[lock
->level
]++;
100 teb
->sys_mutex
[lock
->level
] = lock
;
102 TRACE("(%p, level %d): thread %lx (fs %04x, pid %ld) count after %ld\n",
103 lock
, lock
->level
, teb
->tid
, teb
->teb_sel
, (long) getpid(),
104 teb
->sys_count
[lock
->level
] );
106 if (lock
== &Win16Mutex
)
107 SYSLEVEL_Win16CurrentTeb
= wine_get_fs();
110 /************************************************************************
111 * _LeaveSysLevel (KERNEL32.98)
112 * _LeaveSysLevel (KERNEL.440)
114 VOID WINAPI
_LeaveSysLevel(SYSLEVEL
*lock
)
116 TEB
*teb
= NtCurrentTeb();
118 TRACE("(%p, level %d): thread %lx (fs %04x, pid %ld) count before %ld\n",
119 lock
, lock
->level
, teb
->tid
, teb
->teb_sel
, (long) getpid(),
120 teb
->sys_count
[lock
->level
] );
122 if ( teb
->sys_count
[lock
->level
] <= 0 || teb
->sys_mutex
[lock
->level
] != lock
)
124 ERR("(%p, level %d): Invalid state: count %ld mutex %p.\n",
125 lock
, lock
->level
, teb
->sys_count
[lock
->level
],
126 teb
->sys_mutex
[lock
->level
] );
130 if ( --teb
->sys_count
[lock
->level
] == 0 )
131 teb
->sys_mutex
[lock
->level
] = NULL
;
134 LeaveCriticalSection( &lock
->crst
);
136 TRACE("(%p, level %d): thread %lx (fs %04x, pid %ld) count after %ld\n",
137 lock
, lock
->level
, teb
->tid
, teb
->teb_sel
, (long) getpid(),
138 teb
->sys_count
[lock
->level
] );
141 /************************************************************************
144 VOID WINAPI
_KERNEL32_86(SYSLEVEL
*lock
)
146 _LeaveSysLevel(lock
);
149 /************************************************************************
150 * _ConfirmSysLevel (KERNEL32.95)
151 * _ConfirmSysLevel (KERNEL.436)
153 DWORD WINAPI
_ConfirmSysLevel(SYSLEVEL
*lock
)
155 if ( lock
&& lock
->crst
.OwningThread
== (HANDLE
)GetCurrentThreadId() )
156 return lock
->crst
.RecursionCount
;
161 /************************************************************************
162 * _CheckNotSysLevel (KERNEL32.94)
163 * _CheckNotSysLevel (KERNEL.437)
165 VOID WINAPI
_CheckNotSysLevel(SYSLEVEL
*lock
)
167 if (lock
&& lock
->crst
.OwningThread
== (HANDLE
)GetCurrentThreadId() &&
168 lock
->crst
.RecursionCount
)
170 ERR( "Holding lock %p level %d\n", lock
, lock
->level
);
176 /************************************************************************
177 * _EnterWin16Lock [KERNEL.480]
179 VOID WINAPI
_EnterWin16Lock(void)
181 _EnterSysLevel(&Win16Mutex
);
184 /************************************************************************
185 * _LeaveWin16Lock [KERNEL.481]
187 VOID WINAPI
_LeaveWin16Lock(void)
189 _LeaveSysLevel(&Win16Mutex
);
192 /************************************************************************
193 * _ConfirmWin16Lock (KERNEL32.96)
195 DWORD WINAPI
_ConfirmWin16Lock(void)
197 return _ConfirmSysLevel(&Win16Mutex
);
200 /************************************************************************
201 * ReleaseThunkLock (KERNEL32.48)
203 VOID WINAPI
ReleaseThunkLock(DWORD
*mutex_count
)
205 DWORD count
= _ConfirmSysLevel(&Win16Mutex
);
206 *mutex_count
= count
;
209 _LeaveSysLevel(&Win16Mutex
);
212 /************************************************************************
213 * RestoreThunkLock (KERNEL32.49)
215 VOID WINAPI
RestoreThunkLock(DWORD mutex_count
)
217 while (mutex_count
-- > 0)
218 _EnterSysLevel(&Win16Mutex
);
221 /************************************************************************
222 * SYSLEVEL_CheckNotLevel
224 VOID
SYSLEVEL_CheckNotLevel( INT level
)
228 for ( i
= 3; i
>= level
; i
-- )
229 if ( NtCurrentTeb()->sys_count
[i
] > 0 )
231 ERR("(%d): Holding lock of level %d!\n",