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
26 #include <sys/types.h>
31 #include "wine/debug.h"
34 WINE_DEFAULT_DEBUG_CHANNEL(win32
);
35 WINE_DECLARE_DEBUG_CHANNEL(relay
);
37 /***********************************************************************
38 * InitializeCriticalSection (KERNEL32.@)
40 void WINAPI
InitializeCriticalSection( CRITICAL_SECTION
*crit
)
42 NTSTATUS ret
= RtlInitializeCriticalSection( crit
);
43 if (ret
) RtlRaiseStatus( ret
);
46 /***********************************************************************
47 * InitializeCriticalSectionAndSpinCount (KERNEL32.@)
49 BOOL WINAPI
InitializeCriticalSectionAndSpinCount( CRITICAL_SECTION
*crit
, DWORD spincount
)
51 NTSTATUS ret
= RtlInitializeCriticalSectionAndSpinCount( crit
, spincount
);
52 if (ret
) RtlRaiseStatus( ret
);
56 /***********************************************************************
57 * SetCriticalSectionSpinCount (KERNEL32.@)
58 * This function is available on NT4SP3 or later, but not Win98
61 DWORD WINAPI
SetCriticalSectionSpinCount( CRITICAL_SECTION
*crit
, DWORD spincount
)
63 ULONG_PTR oldspincount
= crit
->SpinCount
;
64 if(spincount
) FIXME("critsection=%p: spincount=%ld not supported\n", crit
, spincount
);
65 crit
->SpinCount
= spincount
;
69 /***********************************************************************
70 * MakeCriticalSectionGlobal (KERNEL32.@)
72 void WINAPI
MakeCriticalSectionGlobal( CRITICAL_SECTION
*crit
)
74 /* let's assume that only one thread at a time will try to do this */
75 HANDLE sem
= crit
->LockSemaphore
;
76 if (!sem
) NtCreateSemaphore( &sem
, SEMAPHORE_ALL_ACCESS
, NULL
, 0, 1 );
77 crit
->LockSemaphore
= ConvertToGlobalHandle( sem
);
81 /***********************************************************************
82 * ReinitializeCriticalSection (KERNEL32.@)
84 void WINAPI
ReinitializeCriticalSection( CRITICAL_SECTION
*crit
)
86 if ( !crit
->LockSemaphore
)
87 RtlInitializeCriticalSection( crit
);
91 /***********************************************************************
92 * UninitializeCriticalSection (KERNEL32.@)
94 void WINAPI
UninitializeCriticalSection( CRITICAL_SECTION
*crit
)
96 RtlDeleteCriticalSection( crit
);
101 /***********************************************************************
102 * InterlockedCompareExchange (KERNEL32.@)
104 /* LONG WINAPI InterlockedCompareExchange( PLONG dest, LONG xchg, LONG compare ); */
105 __ASM_GLOBAL_FUNC(InterlockedCompareExchange
,
106 "movl 12(%esp),%eax\n\t"
107 "movl 8(%esp),%ecx\n\t"
108 "movl 4(%esp),%edx\n\t"
109 "lock; cmpxchgl %ecx,(%edx)\n\t"
112 /***********************************************************************
113 * InterlockedExchange (KERNEL32.@)
115 /* LONG WINAPI InterlockedExchange( PLONG dest, LONG val ); */
116 __ASM_GLOBAL_FUNC(InterlockedExchange
,
117 "movl 8(%esp),%eax\n\t"
118 "movl 4(%esp),%edx\n\t"
119 "lock; xchgl %eax,(%edx)\n\t"
122 /***********************************************************************
123 * InterlockedExchangeAdd (KERNEL32.@)
125 /* LONG WINAPI InterlockedExchangeAdd( PLONG dest, LONG incr ); */
126 __ASM_GLOBAL_FUNC(InterlockedExchangeAdd
,
127 "movl 8(%esp),%eax\n\t"
128 "movl 4(%esp),%edx\n\t"
129 "lock; xaddl %eax,(%edx)\n\t"
132 /***********************************************************************
133 * InterlockedIncrement (KERNEL32.@)
135 /* LONG WINAPI InterlockedIncrement( PLONG dest ); */
136 __ASM_GLOBAL_FUNC(InterlockedIncrement
,
137 "movl 4(%esp),%edx\n\t"
139 "lock; xaddl %eax,(%edx)\n\t"
143 /***********************************************************************
144 * InterlockedDecrement (KERNEL32.@)
146 __ASM_GLOBAL_FUNC(InterlockedDecrement
,
147 "movl 4(%esp),%edx\n\t"
149 "lock; xaddl %eax,(%edx)\n\t"
153 #elif defined(__sparc__) && defined(__sun__)
156 * As the earlier Sparc processors lack necessary atomic instructions,
157 * I'm simply falling back to the library-provided _lwp_mutex routines
158 * to ensure mutual exclusion in a way appropriate for the current
161 * FIXME: If we have the compare-and-swap instruction (Sparc v9 and above)
162 * we could use this to speed up the Interlocked operations ...
166 static lwp_mutex_t interlocked_mutex
= DEFAULTMUTEX
;
168 /***********************************************************************
169 * InterlockedCompareExchange (KERNEL32.@)
171 LONG WINAPI
InterlockedCompareExchange( PLONG dest
, LONG xchg
, LONG compare
)
173 _lwp_mutex_lock( &interlocked_mutex
);
175 if ( *dest
== compare
)
180 _lwp_mutex_unlock( &interlocked_mutex
);
184 /***********************************************************************
185 * InterlockedExchange (KERNEL32.@)
187 LONG WINAPI
InterlockedExchange( PLONG dest
, LONG val
)
190 _lwp_mutex_lock( &interlocked_mutex
);
195 _lwp_mutex_unlock( &interlocked_mutex
);
199 /***********************************************************************
200 * InterlockedExchangeAdd (KERNEL32.@)
202 LONG WINAPI
InterlockedExchangeAdd( PLONG dest
, LONG incr
)
205 _lwp_mutex_lock( &interlocked_mutex
);
210 _lwp_mutex_unlock( &interlocked_mutex
);
214 /***********************************************************************
215 * InterlockedIncrement (KERNEL32.@)
217 LONG WINAPI
InterlockedIncrement( PLONG dest
)
220 _lwp_mutex_lock( &interlocked_mutex
);
224 _lwp_mutex_unlock( &interlocked_mutex
);
228 /***********************************************************************
229 * InterlockedDecrement (KERNEL32.@)
231 LONG WINAPI
InterlockedDecrement( PLONG dest
)
234 _lwp_mutex_lock( &interlocked_mutex
);
238 _lwp_mutex_unlock( &interlocked_mutex
);
243 #error You must implement the Interlocked* functions for your CPU