Moved the selector access functions out of winnt.h into libwine.
[wine] / scheduler / syslevel.c
1 /*
2  * Win32 'syslevel' routines
3  *
4  * Copyright 1998 Ulrich Weigand
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
19  */
20
21 #include <unistd.h>
22 #include <sys/types.h>
23 #include "ntddk.h"
24 #include "syslevel.h"
25 #include "stackframe.h"
26 #include "wine/library.h"
27 #include "wine/debug.h"
28
29 WINE_DEFAULT_DEBUG_CHANNEL(win32);
30
31 static SYSLEVEL Win16Mutex = { CRITICAL_SECTION_INIT("Win16Mutex"), 1 };
32
33 /* Global variable to save current TEB while in 16-bit code */
34 WORD SYSLEVEL_Win16CurrentTeb = 0;
35
36
37 /************************************************************************
38  *           GetpWin16Lock    (KERNEL32.93)
39  */
40 VOID WINAPI GetpWin16Lock(SYSLEVEL **lock)
41 {
42     *lock = &Win16Mutex;
43 }
44
45 /************************************************************************
46  *           GetpWin16Lock    (KERNEL.449)
47  */
48 SEGPTR WINAPI GetpWin16Lock16(void)
49 {
50     static SYSLEVEL *w16Mutex;
51     static SEGPTR segpWin16Mutex;
52
53     if (!segpWin16Mutex)
54     {
55         w16Mutex = &Win16Mutex;
56         segpWin16Mutex = MapLS( &w16Mutex );
57     }
58     return segpWin16Mutex;
59 }
60
61 /************************************************************************
62  *           _CreateSysLevel    (KERNEL.438)
63  */
64 VOID WINAPI _CreateSysLevel(SYSLEVEL *lock, INT level)
65 {
66     InitializeCriticalSection( &lock->crst );
67     lock->level = level;
68
69     TRACE("(%p, %d): handle is %d\n", 
70                   lock, level, lock->crst.LockSemaphore );
71 }
72
73 /************************************************************************
74  *           _EnterSysLevel    (KERNEL32.97)
75  *           _EnterSysLevel    (KERNEL.439)
76  */
77 VOID WINAPI _EnterSysLevel(SYSLEVEL *lock)
78 {
79     TEB *teb = NtCurrentTeb();
80     int i;
81
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] );
85
86     for ( i = 3; i > lock->level; i-- )
87         if ( teb->sys_count[i] > 0 )
88         {
89             ERR("(%p, level %d): Holding %p, level %d. Expect deadlock!\n", 
90                         lock, lock->level, teb->sys_mutex[i], i );
91         }
92
93     EnterCriticalSection( &lock->crst );
94
95     teb->sys_count[lock->level]++;
96     teb->sys_mutex[lock->level] = lock;
97
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] );
101
102     if (lock == &Win16Mutex)
103         SYSLEVEL_Win16CurrentTeb = wine_get_fs();
104 }
105
106 /************************************************************************
107  *           _LeaveSysLevel    (KERNEL32.98)
108  *           _LeaveSysLevel    (KERNEL.440)
109  */
110 VOID WINAPI _LeaveSysLevel(SYSLEVEL *lock)
111 {
112     TEB *teb = NtCurrentTeb();
113
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] );
117
118     if ( teb->sys_count[lock->level] <= 0 || teb->sys_mutex[lock->level] != lock )
119     {
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] );
123     }
124     else
125     {
126         if ( --teb->sys_count[lock->level] == 0 )
127             teb->sys_mutex[lock->level] = NULL;
128     }
129
130     LeaveCriticalSection( &lock->crst );
131
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] );
135 }
136
137 /************************************************************************
138  *              @ (KERNEL32.86)
139  */
140 VOID WINAPI _KERNEL32_86(SYSLEVEL *lock)
141 {
142     _LeaveSysLevel(lock);
143 }
144
145 /************************************************************************
146  *           _ConfirmSysLevel    (KERNEL32.95)
147  *           _ConfirmSysLevel    (KERNEL.436)
148  */
149 DWORD WINAPI _ConfirmSysLevel(SYSLEVEL *lock)
150 {
151     if ( lock && lock->crst.OwningThread == GetCurrentThreadId() )
152         return lock->crst.RecursionCount;
153     else
154         return 0L;
155 }
156
157 /************************************************************************
158  *           _CheckNotSysLevel    (KERNEL32.94)
159  *           _CheckNotSysLevel    (KERNEL.437)
160  */
161 VOID WINAPI _CheckNotSysLevel(SYSLEVEL *lock)
162 {
163     if (lock && lock->crst.OwningThread == GetCurrentThreadId() && lock->crst.RecursionCount)
164     {
165         ERR( "Holding lock %p level %d\n", lock, lock->level );
166         DbgBreakPoint();
167     }
168 }
169
170
171 /************************************************************************
172  *           _EnterWin16Lock                    [KERNEL.480]
173  */
174 VOID WINAPI _EnterWin16Lock(void)
175 {
176     _EnterSysLevel(&Win16Mutex);
177 }
178
179 /************************************************************************
180  *           _LeaveWin16Lock            [KERNEL.481]
181  */
182 VOID WINAPI _LeaveWin16Lock(void)
183 {
184     _LeaveSysLevel(&Win16Mutex);
185 }
186
187 /************************************************************************
188  *           _ConfirmWin16Lock    (KERNEL32.96)
189  */
190 DWORD WINAPI _ConfirmWin16Lock(void)
191 {
192     return _ConfirmSysLevel(&Win16Mutex);
193 }
194
195 /************************************************************************
196  *           ReleaseThunkLock    (KERNEL32.48)
197  */
198 VOID WINAPI ReleaseThunkLock(DWORD *mutex_count)
199 {
200     DWORD count = _ConfirmSysLevel(&Win16Mutex);
201     *mutex_count = count;
202
203     while (count-- > 0)
204         _LeaveSysLevel(&Win16Mutex);
205 }
206
207 /************************************************************************
208  *           RestoreThunkLock    (KERNEL32.49)
209  */
210 VOID WINAPI RestoreThunkLock(DWORD mutex_count)
211 {
212     while (mutex_count-- > 0)
213         _EnterSysLevel(&Win16Mutex);
214 }
215
216 /************************************************************************
217  *           SYSLEVEL_CheckNotLevel
218  */
219 VOID SYSLEVEL_CheckNotLevel( INT level )
220 {
221     INT i;
222
223     for ( i = 3; i >= level; i-- )
224         if ( NtCurrentTeb()->sys_count[i] > 0 )
225         {
226             ERR("(%d): Holding lock of level %d!\n", 
227                        level, i );
228             DbgBreakPoint();
229             break;
230         }
231 }