Added LGPL standard comment, and copyright notices where necessary.
[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/debug.h"
27
28 WINE_DEFAULT_DEBUG_CHANNEL(win32);
29
30 static SYSLEVEL Win16Mutex = { CRITICAL_SECTION_INIT("Win16Mutex"), 1 };
31
32 /* Global variable to save current TEB while in 16-bit code */
33 WORD SYSLEVEL_Win16CurrentTeb = 0;
34
35
36 /************************************************************************
37  *           GetpWin16Lock    (KERNEL32.93)
38  */
39 VOID WINAPI GetpWin16Lock(SYSLEVEL **lock)
40 {
41     *lock = &Win16Mutex;
42 }
43
44 /************************************************************************
45  *           GetpWin16Lock    (KERNEL.449)
46  */
47 SEGPTR WINAPI GetpWin16Lock16(void)
48 {
49     static SYSLEVEL *w16Mutex;
50     static SEGPTR segpWin16Mutex;
51
52     if (!segpWin16Mutex)
53     {
54         w16Mutex = &Win16Mutex;
55         segpWin16Mutex = MapLS( &w16Mutex );
56     }
57     return segpWin16Mutex;
58 }
59
60 /************************************************************************
61  *           _CreateSysLevel    (KERNEL.438)
62  */
63 VOID WINAPI _CreateSysLevel(SYSLEVEL *lock, INT level)
64 {
65     InitializeCriticalSection( &lock->crst );
66     lock->level = level;
67
68     TRACE("(%p, %d): handle is %d\n", 
69                   lock, level, lock->crst.LockSemaphore );
70 }
71
72 /************************************************************************
73  *           _EnterSysLevel    (KERNEL32.97)
74  *           _EnterSysLevel    (KERNEL.439)
75  */
76 VOID WINAPI _EnterSysLevel(SYSLEVEL *lock)
77 {
78     TEB *teb = NtCurrentTeb();
79     int i;
80
81     TRACE("(%p, level %d): thread %p (fs %04x, pid %ld) count before %ld\n", 
82                   lock, lock->level, teb->tid, teb->teb_sel, (long) getpid(),
83                   teb->sys_count[lock->level] );
84
85     for ( i = 3; i > lock->level; i-- )
86         if ( teb->sys_count[i] > 0 )
87         {
88             ERR("(%p, level %d): Holding %p, level %d. Expect deadlock!\n", 
89                         lock, lock->level, teb->sys_mutex[i], i );
90         }
91
92     EnterCriticalSection( &lock->crst );
93
94     teb->sys_count[lock->level]++;
95     teb->sys_mutex[lock->level] = lock;
96
97     TRACE("(%p, level %d): thread %p (fs %04x, pid %ld) count after  %ld\n",
98                   lock, lock->level, teb->tid, teb->teb_sel, (long) getpid(), 
99                   teb->sys_count[lock->level] );
100
101     if (lock == &Win16Mutex)
102         SYSLEVEL_Win16CurrentTeb = __get_fs();
103 }
104
105 /************************************************************************
106  *           _LeaveSysLevel    (KERNEL32.98)
107  *           _LeaveSysLevel    (KERNEL.440)
108  */
109 VOID WINAPI _LeaveSysLevel(SYSLEVEL *lock)
110 {
111     TEB *teb = NtCurrentTeb();
112
113     TRACE("(%p, level %d): thread %p (fs %04x, pid %ld) count before %ld\n", 
114                   lock, lock->level, teb->tid, teb->teb_sel, (long) getpid(),
115                   teb->sys_count[lock->level] );
116
117     if ( teb->sys_count[lock->level] <= 0 || teb->sys_mutex[lock->level] != lock )
118     {
119         ERR("(%p, level %d): Invalid state: count %ld mutex %p.\n",
120                     lock, lock->level, teb->sys_count[lock->level], 
121                     teb->sys_mutex[lock->level] );
122     }
123     else
124     {
125         if ( --teb->sys_count[lock->level] == 0 )
126             teb->sys_mutex[lock->level] = NULL;
127     }
128
129     LeaveCriticalSection( &lock->crst );
130
131     TRACE("(%p, level %d): thread %p (fs %04x, pid %ld) count after  %ld\n",
132                   lock, lock->level, teb->tid, teb->teb_sel, (long) getpid(), 
133                   teb->sys_count[lock->level] );
134 }
135
136 /************************************************************************
137  *              @ (KERNEL32.86)
138  */
139 VOID WINAPI _KERNEL32_86(SYSLEVEL *lock)
140 {
141     _LeaveSysLevel(lock);
142 }
143
144 /************************************************************************
145  *           _ConfirmSysLevel    (KERNEL32.95)
146  *           _ConfirmSysLevel    (KERNEL.436)
147  */
148 DWORD WINAPI _ConfirmSysLevel(SYSLEVEL *lock)
149 {
150     if ( lock && lock->crst.OwningThread == GetCurrentThreadId() )
151         return lock->crst.RecursionCount;
152     else
153         return 0L;
154 }
155
156 /************************************************************************
157  *           _CheckNotSysLevel    (KERNEL32.94)
158  *           _CheckNotSysLevel    (KERNEL.437)
159  */
160 VOID WINAPI _CheckNotSysLevel(SYSLEVEL *lock)
161 {
162     if (lock && lock->crst.OwningThread == GetCurrentThreadId() && lock->crst.RecursionCount)
163     {
164         ERR( "Holding lock %p level %d\n", lock, lock->level );
165         DbgBreakPoint();
166     }
167 }
168
169
170 /************************************************************************
171  *           _EnterWin16Lock                    [KERNEL.480]
172  */
173 VOID WINAPI _EnterWin16Lock(void)
174 {
175     _EnterSysLevel(&Win16Mutex);
176 }
177
178 /************************************************************************
179  *           _LeaveWin16Lock            [KERNEL.481]
180  */
181 VOID WINAPI _LeaveWin16Lock(void)
182 {
183     _LeaveSysLevel(&Win16Mutex);
184 }
185
186 /************************************************************************
187  *           _ConfirmWin16Lock    (KERNEL32.96)
188  */
189 DWORD WINAPI _ConfirmWin16Lock(void)
190 {
191     return _ConfirmSysLevel(&Win16Mutex);
192 }
193
194 /************************************************************************
195  *           ReleaseThunkLock    (KERNEL32.48)
196  */
197 VOID WINAPI ReleaseThunkLock(DWORD *mutex_count)
198 {
199     DWORD count = _ConfirmSysLevel(&Win16Mutex);
200     *mutex_count = count;
201
202     while (count-- > 0)
203         _LeaveSysLevel(&Win16Mutex);
204 }
205
206 /************************************************************************
207  *           RestoreThunkLock    (KERNEL32.49)
208  */
209 VOID WINAPI RestoreThunkLock(DWORD mutex_count)
210 {
211     while (mutex_count-- > 0)
212         _EnterSysLevel(&Win16Mutex);
213 }
214
215 /************************************************************************
216  *           SYSLEVEL_CheckNotLevel
217  */
218 VOID SYSLEVEL_CheckNotLevel( INT level )
219 {
220     INT i;
221
222     for ( i = 3; i >= level; i-- )
223         if ( NtCurrentTeb()->sys_count[i] > 0 )
224         {
225             ERR("(%d): Holding lock of level %d!\n", 
226                        level, i );
227             DbgBreakPoint();
228             break;
229         }
230 }