blob: 7086a8bd80f7bfc6239be0bfd21bb0475a7c1f37 [file] [log] [blame]
Alexandre Julliard02e90081998-01-04 17:49:09 +00001/*
2 * Win32 critical sections
3 *
4 * Copyright 1998 Alexandre Julliard
5 */
6
Francois Gougete5ddd262001-10-14 16:18:52 +00007#include "config.h"
8
Alexandre Julliard02e90081998-01-04 17:49:09 +00009#include <assert.h>
10#include <errno.h>
11#include <stdio.h>
Alexandre Julliard60ce85c1998-02-01 18:33:27 +000012#include <sys/types.h>
Dimitrie O. Paun0b7a7bb2000-11-25 01:31:17 +000013
Alexandre Julliard02e90081998-01-04 17:49:09 +000014#include "winerror.h"
15#include "winbase.h"
Alexandre Julliard6f1b6421999-11-24 01:14:04 +000016#include "ntddk.h"
Alexandre Julliard06c275a1999-05-02 14:32:27 +000017#include "debugtools.h"
Alexandre Julliard02e90081998-01-04 17:49:09 +000018#include "thread.h"
19
Alexandre Julliard416a00d2000-04-08 20:50:06 +000020DEFAULT_DEBUG_CHANNEL(win32);
21DECLARE_DEBUG_CHANNEL(relay);
Patrik Stridvallb4b9fae1999-04-19 14:56:29 +000022
Alexandre Julliard416a00d2000-04-08 20:50:06 +000023/***********************************************************************
Patrik Stridvalldae8de62001-06-13 20:13:18 +000024 * InitializeCriticalSection (KERNEL32.@)
Alexandre Julliard02e90081998-01-04 17:49:09 +000025 */
26void WINAPI InitializeCriticalSection( CRITICAL_SECTION *crit )
27{
Alexandre Julliardbaa15562000-09-29 00:31:23 +000028 NTSTATUS ret = RtlInitializeCriticalSection( crit );
29 if (ret) RtlRaiseStatus( ret );
Alexandre Julliard02e90081998-01-04 17:49:09 +000030}
31
Alexandre Julliard02e90081998-01-04 17:49:09 +000032/***********************************************************************
Patrik Stridvall3ca98232001-06-20 23:03:14 +000033 * InitializeCriticalSectionAndSpinCount (KERNEL32.@)
David Elliott44f84b52000-10-29 01:24:54 +000034 */
35BOOL WINAPI InitializeCriticalSectionAndSpinCount( CRITICAL_SECTION *crit, DWORD spincount )
36{
37 NTSTATUS ret = RtlInitializeCriticalSectionAndSpinCount( crit, spincount );
38 if (ret) RtlRaiseStatus( ret );
39 return !ret;
40}
41
42/***********************************************************************
Patrik Stridvall3ca98232001-06-20 23:03:14 +000043 * SetCriticalSectionSpinCount (KERNEL32.@)
David Elliott44f84b52000-10-29 01:24:54 +000044 * This function is available on NT4SP3 or later, but not Win98
45 * It is SMP related
46 */
47DWORD WINAPI SetCriticalSectionSpinCount( CRITICAL_SECTION *crit, DWORD spincount )
48{
49 ULONG_PTR oldspincount = crit->SpinCount;
50 if(spincount) FIXME("critsection=%p: spincount=%ld not supported\n", crit, spincount);
51 crit->SpinCount = spincount;
52 return oldspincount;
53}
54
55/***********************************************************************
Patrik Stridvalldae8de62001-06-13 20:13:18 +000056 * MakeCriticalSectionGlobal (KERNEL32.@)
Alexandre Julliard02e90081998-01-04 17:49:09 +000057 */
58void WINAPI MakeCriticalSectionGlobal( CRITICAL_SECTION *crit )
59{
Alexandre Julliardbaa15562000-09-29 00:31:23 +000060 /* let's assume that only one thread at a time will try to do this */
61 HANDLE sem = crit->LockSemaphore;
Alexandre Julliardafb49ea2000-09-29 20:48:04 +000062 if (!sem) NtCreateSemaphore( &sem, SEMAPHORE_ALL_ACCESS, NULL, 0, 1 );
Alexandre Julliardbaa15562000-09-29 00:31:23 +000063 crit->LockSemaphore = ConvertToGlobalHandle( sem );
Alexandre Julliard02e90081998-01-04 17:49:09 +000064}
65
66
67/***********************************************************************
Patrik Stridvalldae8de62001-06-13 20:13:18 +000068 * ReinitializeCriticalSection (KERNEL32.@)
Alexandre Julliard60ce85c1998-02-01 18:33:27 +000069 */
70void WINAPI ReinitializeCriticalSection( CRITICAL_SECTION *crit )
71{
Ulrich Weiganda9880741999-04-25 11:03:59 +000072 if ( !crit->LockSemaphore )
Alexandre Julliardbaa15562000-09-29 00:31:23 +000073 RtlInitializeCriticalSection( crit );
Alexandre Julliard60ce85c1998-02-01 18:33:27 +000074}
Bertho Stultiens77f08f31999-04-15 16:42:50 +000075
76
77/***********************************************************************
Patrik Stridvalldae8de62001-06-13 20:13:18 +000078 * UninitializeCriticalSection (KERNEL32.@)
Bertho Stultiens77f08f31999-04-15 16:42:50 +000079 */
80void WINAPI UninitializeCriticalSection( CRITICAL_SECTION *crit )
81{
Alexandre Julliardbaa15562000-09-29 00:31:23 +000082 RtlDeleteCriticalSection( crit );
Bertho Stultiens77f08f31999-04-15 16:42:50 +000083}
Ulrich Weiganda9880741999-04-25 11:03:59 +000084
Alexandre Julliardca3c9ba2000-03-07 13:14:27 +000085#ifdef __i386__
86
Patrik Stridvall7980a502000-06-07 02:15:39 +000087/***********************************************************************
Patrik Stridvalldb923052001-07-24 00:58:52 +000088 * InterlockedCompareExchange (KERNEL32.@)
Patrik Stridvall7980a502000-06-07 02:15:39 +000089 */
Alexandre Julliarde08a1b52001-08-17 00:08:20 +000090/* LONG WINAPI InterlockedCompareExchange( PLONG dest, LONG xchg, LONG compare ); */
Alexandre Julliardca3c9ba2000-03-07 13:14:27 +000091__ASM_GLOBAL_FUNC(InterlockedCompareExchange,
92 "movl 12(%esp),%eax\n\t"
93 "movl 8(%esp),%ecx\n\t"
94 "movl 4(%esp),%edx\n\t"
95 "lock; cmpxchgl %ecx,(%edx)\n\t"
96 "ret $12");
97
Patrik Stridvall7980a502000-06-07 02:15:39 +000098/***********************************************************************
99 * InterlockedExchange (KERNEL32.@)
100 */
Patrik Stridvalldb923052001-07-24 00:58:52 +0000101/* LONG WINAPI InterlockedExchange( PLONG dest, LONG val ); */
Alexandre Julliardca3c9ba2000-03-07 13:14:27 +0000102__ASM_GLOBAL_FUNC(InterlockedExchange,
103 "movl 8(%esp),%eax\n\t"
104 "movl 4(%esp),%edx\n\t"
105 "lock; xchgl %eax,(%edx)\n\t"
106 "ret $8");
107
Patrik Stridvall7980a502000-06-07 02:15:39 +0000108/***********************************************************************
109 * InterlockedExchangeAdd (KERNEL32.@)
110 */
Patrik Stridvalldb923052001-07-24 00:58:52 +0000111/* LONG WINAPI InterlockedExchangeAdd( PLONG dest, LONG incr ); */
Alexandre Julliardca3c9ba2000-03-07 13:14:27 +0000112__ASM_GLOBAL_FUNC(InterlockedExchangeAdd,
113 "movl 8(%esp),%eax\n\t"
114 "movl 4(%esp),%edx\n\t"
115 "lock; xaddl %eax,(%edx)\n\t"
116 "ret $8");
117
Patrik Stridvall7980a502000-06-07 02:15:39 +0000118/***********************************************************************
119 * InterlockedIncrement (KERNEL32.@)
120 */
Patrik Stridvalldb923052001-07-24 00:58:52 +0000121/* LONG WINAPI InterlockedIncrement( PLONG dest ); */
Alexandre Julliardca3c9ba2000-03-07 13:14:27 +0000122__ASM_GLOBAL_FUNC(InterlockedIncrement,
123 "movl 4(%esp),%edx\n\t"
124 "movl $1,%eax\n\t"
125 "lock; xaddl %eax,(%edx)\n\t"
126 "incl %eax\n\t"
127 "ret $4");
128
Patrik Stridvall7980a502000-06-07 02:15:39 +0000129/***********************************************************************
Patrik Stridvalldb923052001-07-24 00:58:52 +0000130 * InterlockedDecrement (KERNEL32.@)
Patrik Stridvall7980a502000-06-07 02:15:39 +0000131 */
Alexandre Julliardca3c9ba2000-03-07 13:14:27 +0000132__ASM_GLOBAL_FUNC(InterlockedDecrement,
133 "movl 4(%esp),%edx\n\t"
134 "movl $-1,%eax\n\t"
135 "lock; xaddl %eax,(%edx)\n\t"
136 "decl %eax\n\t"
137 "ret $4");
138
Ulrich Weigandafd6a4b2000-06-04 01:48:05 +0000139#elif defined(__sparc__) && defined(__sun__)
140
141/*
142 * As the earlier Sparc processors lack necessary atomic instructions,
143 * I'm simply falling back to the library-provided _lwp_mutex routines
144 * to ensure mutual exclusion in a way appropriate for the current
145 * architecture.
146 *
147 * FIXME: If we have the compare-and-swap instruction (Sparc v9 and above)
148 * we could use this to speed up the Interlocked operations ...
149 */
150
151#include <synch.h>
152static lwp_mutex_t interlocked_mutex = DEFAULTMUTEX;
153
Patrik Stridvall7980a502000-06-07 02:15:39 +0000154/***********************************************************************
155 * InterlockedCompareExchange (KERNEL32.@)
156 */
Alexandre Julliarde994d502001-08-09 21:21:13 +0000157LONG WINAPI InterlockedCompareExchange( PLONG dest, LONG xchg, LONG compare )
Ulrich Weigandafd6a4b2000-06-04 01:48:05 +0000158{
159 _lwp_mutex_lock( &interlocked_mutex );
160
161 if ( *dest == compare )
162 *dest = xchg;
163 else
164 compare = *dest;
165
166 _lwp_mutex_unlock( &interlocked_mutex );
167 return compare;
168}
169
Patrik Stridvall7980a502000-06-07 02:15:39 +0000170/***********************************************************************
171 * InterlockedExchange (KERNEL32.@)
172 */
Ulrich Weigandafd6a4b2000-06-04 01:48:05 +0000173LONG WINAPI InterlockedExchange( PLONG dest, LONG val )
174{
175 LONG retv;
176 _lwp_mutex_lock( &interlocked_mutex );
177
178 retv = *dest;
179 *dest = val;
180
181 _lwp_mutex_unlock( &interlocked_mutex );
182 return retv;
183}
184
Patrik Stridvall7980a502000-06-07 02:15:39 +0000185/***********************************************************************
186 * InterlockedExchangeAdd (KERNEL32.@)
187 */
Ulrich Weigandafd6a4b2000-06-04 01:48:05 +0000188LONG WINAPI InterlockedExchangeAdd( PLONG dest, LONG incr )
189{
190 LONG retv;
191 _lwp_mutex_lock( &interlocked_mutex );
192
193 retv = *dest;
194 *dest += incr;
195
196 _lwp_mutex_unlock( &interlocked_mutex );
197 return retv;
198}
199
Patrik Stridvall7980a502000-06-07 02:15:39 +0000200/***********************************************************************
201 * InterlockedIncrement (KERNEL32.@)
202 */
Ulrich Weigandafd6a4b2000-06-04 01:48:05 +0000203LONG WINAPI InterlockedIncrement( PLONG dest )
204{
205 LONG retv;
206 _lwp_mutex_lock( &interlocked_mutex );
207
208 retv = ++*dest;
209
210 _lwp_mutex_unlock( &interlocked_mutex );
211 return retv;
212}
213
Patrik Stridvall7980a502000-06-07 02:15:39 +0000214/***********************************************************************
215 * InterlockedDecrement (KERNEL32.@)
216 */
Ulrich Weigandafd6a4b2000-06-04 01:48:05 +0000217LONG WINAPI InterlockedDecrement( PLONG dest )
218{
219 LONG retv;
220 _lwp_mutex_lock( &interlocked_mutex );
221
222 retv = --*dest;
223
224 _lwp_mutex_unlock( &interlocked_mutex );
225 return retv;
226}
227
228#else
Alexandre Julliardca3c9ba2000-03-07 13:14:27 +0000229#error You must implement the Interlocked* functions for your CPU
Ulrich Weigandafd6a4b2000-06-04 01:48:05 +0000230#endif