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
22 #include <sys/types.h>
25 #include "stackframe.h"
26 #include "wine/library.h"
27 #include "wine/debug.h"
29 WINE_DEFAULT_DEBUG_CHANNEL(win32
);
31 static SYSLEVEL Win16Mutex
= { CRITICAL_SECTION_INIT("Win16Mutex"), 1 };
33 /* Global variable to save current TEB while in 16-bit code */
34 WORD SYSLEVEL_Win16CurrentTeb
= 0;
37 /************************************************************************
38 * GetpWin16Lock (KERNEL32.93)
40 VOID WINAPI
GetpWin16Lock(SYSLEVEL
**lock
)
45 /************************************************************************
46 * GetpWin16Lock (KERNEL.449)
48 SEGPTR WINAPI
GetpWin16Lock16(void)
50 static SYSLEVEL
*w16Mutex
;
51 static SEGPTR segpWin16Mutex
;
55 w16Mutex
= &Win16Mutex
;
56 segpWin16Mutex
= MapLS( &w16Mutex
);
58 return segpWin16Mutex
;
61 /************************************************************************
62 * _CreateSysLevel (KERNEL.438)
64 VOID WINAPI
_CreateSysLevel(SYSLEVEL
*lock
, INT level
)
66 InitializeCriticalSection( &lock
->crst
);
69 TRACE("(%p, %d): handle is %d\n",
70 lock
, level
, lock
->crst
.LockSemaphore
);
73 /************************************************************************
74 * _EnterSysLevel (KERNEL32.97)
75 * _EnterSysLevel (KERNEL.439)
77 VOID WINAPI
_EnterSysLevel(SYSLEVEL
*lock
)
79 TEB
*teb
= NtCurrentTeb();
82 TRACE("(%p, level %d): thread %p (fs %04x, pid %ld) count before %ld\n",
83 lock
, lock
->level
, teb
->tid
, teb
->teb_sel
, (long) getpid(),
84 teb
->sys_count
[lock
->level
] );
86 for ( i
= 3; i
> lock
->level
; i
-- )
87 if ( teb
->sys_count
[i
] > 0 )
89 ERR("(%p, level %d): Holding %p, level %d. Expect deadlock!\n",
90 lock
, lock
->level
, teb
->sys_mutex
[i
], i
);
93 EnterCriticalSection( &lock
->crst
);
95 teb
->sys_count
[lock
->level
]++;
96 teb
->sys_mutex
[lock
->level
] = lock
;
98 TRACE("(%p, level %d): thread %p (fs %04x, pid %ld) count after %ld\n",
99 lock
, lock
->level
, teb
->tid
, teb
->teb_sel
, (long) getpid(),
100 teb
->sys_count
[lock
->level
] );
102 if (lock
== &Win16Mutex
)
103 SYSLEVEL_Win16CurrentTeb
= wine_get_fs();
106 /************************************************************************
107 * _LeaveSysLevel (KERNEL32.98)
108 * _LeaveSysLevel (KERNEL.440)
110 VOID WINAPI
_LeaveSysLevel(SYSLEVEL
*lock
)
112 TEB
*teb
= NtCurrentTeb();
114 TRACE("(%p, level %d): thread %p (fs %04x, pid %ld) count before %ld\n",
115 lock
, lock
->level
, teb
->tid
, teb
->teb_sel
, (long) getpid(),
116 teb
->sys_count
[lock
->level
] );
118 if ( teb
->sys_count
[lock
->level
] <= 0 || teb
->sys_mutex
[lock
->level
] != lock
)
120 ERR("(%p, level %d): Invalid state: count %ld mutex %p.\n",
121 lock
, lock
->level
, teb
->sys_count
[lock
->level
],
122 teb
->sys_mutex
[lock
->level
] );
126 if ( --teb
->sys_count
[lock
->level
] == 0 )
127 teb
->sys_mutex
[lock
->level
] = NULL
;
130 LeaveCriticalSection( &lock
->crst
);
132 TRACE("(%p, level %d): thread %p (fs %04x, pid %ld) count after %ld\n",
133 lock
, lock
->level
, teb
->tid
, teb
->teb_sel
, (long) getpid(),
134 teb
->sys_count
[lock
->level
] );
137 /************************************************************************
140 VOID WINAPI
_KERNEL32_86(SYSLEVEL
*lock
)
142 _LeaveSysLevel(lock
);
145 /************************************************************************
146 * _ConfirmSysLevel (KERNEL32.95)
147 * _ConfirmSysLevel (KERNEL.436)
149 DWORD WINAPI
_ConfirmSysLevel(SYSLEVEL
*lock
)
151 if ( lock
&& lock
->crst
.OwningThread
== GetCurrentThreadId() )
152 return lock
->crst
.RecursionCount
;
157 /************************************************************************
158 * _CheckNotSysLevel (KERNEL32.94)
159 * _CheckNotSysLevel (KERNEL.437)
161 VOID WINAPI
_CheckNotSysLevel(SYSLEVEL
*lock
)
163 if (lock
&& lock
->crst
.OwningThread
== GetCurrentThreadId() && lock
->crst
.RecursionCount
)
165 ERR( "Holding lock %p level %d\n", lock
, lock
->level
);
171 /************************************************************************
172 * _EnterWin16Lock [KERNEL.480]
174 VOID WINAPI
_EnterWin16Lock(void)
176 _EnterSysLevel(&Win16Mutex
);
179 /************************************************************************
180 * _LeaveWin16Lock [KERNEL.481]
182 VOID WINAPI
_LeaveWin16Lock(void)
184 _LeaveSysLevel(&Win16Mutex
);
187 /************************************************************************
188 * _ConfirmWin16Lock (KERNEL32.96)
190 DWORD WINAPI
_ConfirmWin16Lock(void)
192 return _ConfirmSysLevel(&Win16Mutex
);
195 /************************************************************************
196 * ReleaseThunkLock (KERNEL32.48)
198 VOID WINAPI
ReleaseThunkLock(DWORD
*mutex_count
)
200 DWORD count
= _ConfirmSysLevel(&Win16Mutex
);
201 *mutex_count
= count
;
204 _LeaveSysLevel(&Win16Mutex
);
207 /************************************************************************
208 * RestoreThunkLock (KERNEL32.49)
210 VOID WINAPI
RestoreThunkLock(DWORD mutex_count
)
212 while (mutex_count
-- > 0)
213 _EnterSysLevel(&Win16Mutex
);
216 /************************************************************************
217 * SYSLEVEL_CheckNotLevel
219 VOID
SYSLEVEL_CheckNotLevel( INT level
)
223 for ( i
= 3; i
>= level
; i
-- )
224 if ( NtCurrentTeb()->sys_count
[i
] > 0 )
226 ERR("(%d): Holding lock of level %d!\n",