2 * Win32 critical sections
4 * Copyright 1998 Alexandre Julliard
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 "wine/port.h"
27 #include <sys/types.h>
32 #include "wine/debug.h"
35 WINE_DEFAULT_DEBUG_CHANNEL(win32);
36 WINE_DECLARE_DEBUG_CHANNEL(relay);
38 /***********************************************************************
39 * InitializeCriticalSection (KERNEL32.@)
41 void WINAPI InitializeCriticalSection( CRITICAL_SECTION *crit )
43 NTSTATUS ret = RtlInitializeCriticalSection( crit );
44 if (ret) RtlRaiseStatus( ret );
47 /***********************************************************************
48 * InitializeCriticalSectionAndSpinCount (KERNEL32.@)
50 BOOL WINAPI InitializeCriticalSectionAndSpinCount( CRITICAL_SECTION *crit, DWORD spincount )
52 NTSTATUS ret = RtlInitializeCriticalSectionAndSpinCount( crit, spincount );
53 if (ret) RtlRaiseStatus( ret );
57 /***********************************************************************
58 * SetCriticalSectionSpinCount (KERNEL32.@)
59 * This function is available on NT4SP3 or later, but not Win98
62 DWORD WINAPI SetCriticalSectionSpinCount( CRITICAL_SECTION *crit, DWORD spincount )
64 ULONG_PTR oldspincount = crit->SpinCount;
65 if(spincount) FIXME("critsection=%p: spincount=%ld not supported\n", crit, spincount);
66 crit->SpinCount = spincount;
70 /***********************************************************************
71 * MakeCriticalSectionGlobal (KERNEL32.@)
73 void WINAPI MakeCriticalSectionGlobal( CRITICAL_SECTION *crit )
75 /* let's assume that only one thread at a time will try to do this */
76 HANDLE sem = crit->LockSemaphore;
77 if (!sem) NtCreateSemaphore( &sem, SEMAPHORE_ALL_ACCESS, NULL, 0, 1 );
78 crit->LockSemaphore = ConvertToGlobalHandle( sem );
82 /***********************************************************************
83 * ReinitializeCriticalSection (KERNEL32.@)
85 void WINAPI ReinitializeCriticalSection( CRITICAL_SECTION *crit )
87 if ( !crit->LockSemaphore )
88 RtlInitializeCriticalSection( crit );
92 /***********************************************************************
93 * UninitializeCriticalSection (KERNEL32.@)
95 void WINAPI UninitializeCriticalSection( CRITICAL_SECTION *crit )
97 RtlDeleteCriticalSection( crit );
102 /***********************************************************************
103 * InterlockedCompareExchange (KERNEL32.@)
105 /* LONG WINAPI InterlockedCompareExchange( PLONG dest, LONG xchg, LONG compare ); */
106 __ASM_GLOBAL_FUNC(InterlockedCompareExchange,
107 "movl 12(%esp),%eax\n\t"
108 "movl 8(%esp),%ecx\n\t"
109 "movl 4(%esp),%edx\n\t"
110 "lock; cmpxchgl %ecx,(%edx)\n\t"
113 /***********************************************************************
114 * InterlockedExchange (KERNEL32.@)
116 /* LONG WINAPI InterlockedExchange( PLONG dest, LONG val ); */
117 __ASM_GLOBAL_FUNC(InterlockedExchange,
118 "movl 8(%esp),%eax\n\t"
119 "movl 4(%esp),%edx\n\t"
120 "lock; xchgl %eax,(%edx)\n\t"
123 /***********************************************************************
124 * InterlockedExchangeAdd (KERNEL32.@)
126 /* LONG WINAPI InterlockedExchangeAdd( PLONG dest, LONG incr ); */
127 __ASM_GLOBAL_FUNC(InterlockedExchangeAdd,
128 "movl 8(%esp),%eax\n\t"
129 "movl 4(%esp),%edx\n\t"
130 "lock; xaddl %eax,(%edx)\n\t"
133 /***********************************************************************
134 * InterlockedIncrement (KERNEL32.@)
136 /* LONG WINAPI InterlockedIncrement( PLONG dest ); */
137 __ASM_GLOBAL_FUNC(InterlockedIncrement,
138 "movl 4(%esp),%edx\n\t"
140 "lock; xaddl %eax,(%edx)\n\t"
144 /***********************************************************************
145 * InterlockedDecrement (KERNEL32.@)
147 __ASM_GLOBAL_FUNC(InterlockedDecrement,
148 "movl 4(%esp),%edx\n\t"
150 "lock; xaddl %eax,(%edx)\n\t"
156 /***********************************************************************
157 * InterlockedCompareExchange (KERNEL32.@)
159 LONG WINAPI InterlockedCompareExchange( PLONG dest, LONG xchg, LONG compare )
161 return interlocked_cmpxchg( dest, xchg, compare );
164 /***********************************************************************
165 * InterlockedExchange (KERNEL32.@)
167 LONG WINAPI InterlockedExchange( PLONG dest, LONG val )
169 return interlocked_xchg( dest, val );
172 /***********************************************************************
173 * InterlockedExchangeAdd (KERNEL32.@)
175 LONG WINAPI InterlockedExchangeAdd( PLONG dest, LONG incr )
177 return interlocked_xchg_add( dest, incr );
180 /***********************************************************************
181 * InterlockedIncrement (KERNEL32.@)
183 LONG WINAPI InterlockedIncrement( PLONG dest )
185 return interlocked_xchg_add( dest, 1 ) + 1;
188 /***********************************************************************
189 * InterlockedDecrement (KERNEL32.@)
191 LONG WINAPI InterlockedDecrement( PLONG dest )
193 return interlocked_xchg_add( dest, -1 ) - 1;
196 #endif /* __i386__ */