2000-09-29 02:31:23 +02:00
|
|
|
/*
|
|
|
|
* Win32 critical sections
|
|
|
|
*
|
|
|
|
* Copyright 1998 Alexandre Julliard
|
2002-03-10 00:29:33 +01:00
|
|
|
*
|
|
|
|
* This library is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU Lesser General Public
|
|
|
|
* License as published by the Free Software Foundation; either
|
|
|
|
* version 2.1 of the License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This library is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
* Lesser General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Lesser General Public
|
|
|
|
* License along with this library; if not, write to the Free Software
|
2006-05-18 14:49:52 +02:00
|
|
|
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
|
2000-09-29 02:31:23 +02:00
|
|
|
*/
|
|
|
|
|
2002-04-25 23:40:56 +02:00
|
|
|
#include "config.h"
|
|
|
|
#include "wine/port.h"
|
|
|
|
|
2000-09-29 02:31:23 +02:00
|
|
|
#include <assert.h>
|
2005-09-28 20:21:48 +02:00
|
|
|
#include <errno.h>
|
2003-09-06 01:08:26 +02:00
|
|
|
#include <stdarg.h>
|
2000-09-29 02:31:23 +02:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <sys/types.h>
|
2010-12-27 12:01:52 +01:00
|
|
|
#ifdef HAVE_SYS_SYSCALL_H
|
|
|
|
#include <sys/syscall.h>
|
|
|
|
#endif
|
2005-09-28 20:21:48 +02:00
|
|
|
#include <time.h>
|
2005-11-28 17:32:54 +01:00
|
|
|
#include "ntstatus.h"
|
|
|
|
#define WIN32_NO_STATUS
|
2003-09-06 01:08:26 +02:00
|
|
|
#include "windef.h"
|
2002-09-13 00:07:02 +02:00
|
|
|
#include "winternl.h"
|
2002-03-10 00:29:33 +01:00
|
|
|
#include "wine/debug.h"
|
2003-08-13 01:50:54 +02:00
|
|
|
#include "ntdll_misc.h"
|
2000-09-29 02:31:23 +02:00
|
|
|
|
2002-03-10 00:29:33 +01:00
|
|
|
WINE_DEFAULT_DEBUG_CHANNEL(ntdll);
|
|
|
|
WINE_DECLARE_DEBUG_CHANNEL(relay);
|
2000-09-29 02:31:23 +02:00
|
|
|
|
2007-03-17 11:28:32 +01:00
|
|
|
static inline LONG interlocked_inc( PLONG dest )
|
2000-09-29 02:31:23 +02:00
|
|
|
{
|
2008-01-12 17:05:50 +01:00
|
|
|
return interlocked_xchg_add( dest, 1 ) + 1;
|
2000-09-29 02:31:23 +02:00
|
|
|
}
|
|
|
|
|
2007-03-17 11:28:32 +01:00
|
|
|
static inline LONG interlocked_dec( PLONG dest )
|
2000-09-29 02:31:23 +02:00
|
|
|
{
|
2008-01-12 17:05:50 +01:00
|
|
|
return interlocked_xchg_add( dest, -1 ) - 1;
|
2000-09-29 02:31:23 +02:00
|
|
|
}
|
|
|
|
|
2007-03-17 11:28:32 +01:00
|
|
|
static inline void small_pause(void)
|
2004-06-18 01:11:08 +02:00
|
|
|
{
|
|
|
|
#ifdef __i386__
|
|
|
|
__asm__ __volatile__( "rep;nop" : : : "memory" );
|
|
|
|
#else
|
|
|
|
__asm__ __volatile__( "" : : : "memory" );
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2010-12-27 12:01:52 +01:00
|
|
|
#ifdef __linux__
|
2005-09-28 20:21:48 +02:00
|
|
|
|
2011-08-03 20:27:43 +02:00
|
|
|
static int wait_op = 128; /*FUTEX_WAIT|FUTEX_PRIVATE_FLAG*/
|
|
|
|
static int wake_op = 129; /*FUTEX_WAKE|FUTEX_PRIVATE_FLAG*/
|
|
|
|
|
2005-09-28 20:21:48 +02:00
|
|
|
static inline int futex_wait( int *addr, int val, struct timespec *timeout )
|
|
|
|
{
|
2013-01-21 21:37:02 +01:00
|
|
|
return syscall( __NR_futex, addr, wait_op, val, timeout, 0, 0 );
|
2005-09-28 20:21:48 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
static inline int futex_wake( int *addr, int val )
|
|
|
|
{
|
2013-01-21 21:37:02 +01:00
|
|
|
return syscall( __NR_futex, addr, wake_op, val, NULL, 0, 0 );
|
2005-09-28 20:21:48 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
static inline int use_futexes(void)
|
|
|
|
{
|
|
|
|
static int supported = -1;
|
|
|
|
|
2010-12-27 12:01:52 +01:00
|
|
|
if (supported == -1)
|
|
|
|
{
|
|
|
|
futex_wait( &supported, 10, NULL );
|
2011-08-03 20:27:43 +02:00
|
|
|
if (errno == ENOSYS)
|
|
|
|
{
|
|
|
|
wait_op = 0; /*FUTEX_WAIT*/
|
|
|
|
wake_op = 1; /*FUTEX_WAKE*/
|
|
|
|
futex_wait( &supported, 10, NULL );
|
|
|
|
}
|
2010-12-27 12:01:52 +01:00
|
|
|
supported = (errno != ENOSYS);
|
|
|
|
}
|
2005-09-28 20:21:48 +02:00
|
|
|
return supported;
|
|
|
|
}
|
|
|
|
|
2006-06-27 17:39:50 +02:00
|
|
|
static inline NTSTATUS fast_wait( RTL_CRITICAL_SECTION *crit, int timeout )
|
|
|
|
{
|
|
|
|
int val;
|
|
|
|
struct timespec timespec;
|
|
|
|
|
|
|
|
if (!use_futexes()) return STATUS_NOT_IMPLEMENTED;
|
|
|
|
|
|
|
|
timespec.tv_sec = timeout;
|
|
|
|
timespec.tv_nsec = 0;
|
|
|
|
while ((val = interlocked_cmpxchg( (int *)&crit->LockSemaphore, 0, 1 )) != 1)
|
|
|
|
{
|
|
|
|
/* note: this may wait longer than specified in case of signals or */
|
|
|
|
/* multiple wake-ups, but that shouldn't be a problem */
|
2010-12-27 12:01:52 +01:00
|
|
|
if (futex_wait( (int *)&crit->LockSemaphore, val, ×pec ) == -1 && errno == ETIMEDOUT)
|
2006-06-27 17:39:50 +02:00
|
|
|
return STATUS_TIMEOUT;
|
|
|
|
}
|
|
|
|
return STATUS_WAIT_0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline NTSTATUS fast_wake( RTL_CRITICAL_SECTION *crit )
|
|
|
|
{
|
|
|
|
if (!use_futexes()) return STATUS_NOT_IMPLEMENTED;
|
|
|
|
|
|
|
|
*(int *)&crit->LockSemaphore = 1;
|
|
|
|
futex_wake( (int *)&crit->LockSemaphore, 1 );
|
|
|
|
return STATUS_SUCCESS;
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline void close_semaphore( RTL_CRITICAL_SECTION *crit )
|
|
|
|
{
|
|
|
|
if (!use_futexes()) NtClose( crit->LockSemaphore );
|
|
|
|
}
|
|
|
|
|
|
|
|
#elif defined(__APPLE__)
|
|
|
|
|
|
|
|
#include <mach/mach.h>
|
|
|
|
#include <mach/task.h>
|
|
|
|
#include <mach/semaphore.h>
|
|
|
|
|
|
|
|
static inline semaphore_t get_mach_semaphore( RTL_CRITICAL_SECTION *crit )
|
|
|
|
{
|
|
|
|
semaphore_t ret = *(int *)&crit->LockSemaphore;
|
|
|
|
if (!ret)
|
|
|
|
{
|
|
|
|
semaphore_t sem;
|
|
|
|
if (semaphore_create( mach_task_self(), &sem, SYNC_POLICY_FIFO, 0 )) return 0;
|
|
|
|
if (!(ret = interlocked_cmpxchg( (int *)&crit->LockSemaphore, sem, 0 )))
|
|
|
|
ret = sem;
|
|
|
|
else
|
|
|
|
semaphore_destroy( mach_task_self(), sem ); /* somebody beat us to it */
|
|
|
|
}
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline NTSTATUS fast_wait( RTL_CRITICAL_SECTION *crit, int timeout )
|
|
|
|
{
|
|
|
|
mach_timespec_t timespec;
|
|
|
|
semaphore_t sem = get_mach_semaphore( crit );
|
|
|
|
|
|
|
|
timespec.tv_sec = timeout;
|
|
|
|
timespec.tv_nsec = 0;
|
|
|
|
for (;;)
|
|
|
|
{
|
|
|
|
switch( semaphore_timedwait( sem, timespec ))
|
|
|
|
{
|
|
|
|
case KERN_SUCCESS:
|
|
|
|
return STATUS_WAIT_0;
|
|
|
|
case KERN_ABORTED:
|
|
|
|
continue; /* got a signal, restart */
|
|
|
|
case KERN_OPERATION_TIMED_OUT:
|
|
|
|
return STATUS_TIMEOUT;
|
|
|
|
default:
|
|
|
|
return STATUS_INVALID_HANDLE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline NTSTATUS fast_wake( RTL_CRITICAL_SECTION *crit )
|
|
|
|
{
|
|
|
|
semaphore_t sem = get_mach_semaphore( crit );
|
|
|
|
semaphore_signal( sem );
|
|
|
|
return STATUS_SUCCESS;
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline void close_semaphore( RTL_CRITICAL_SECTION *crit )
|
|
|
|
{
|
|
|
|
semaphore_destroy( mach_task_self(), *(int *)&crit->LockSemaphore );
|
|
|
|
}
|
|
|
|
|
|
|
|
#else /* __APPLE__ */
|
|
|
|
|
|
|
|
static inline NTSTATUS fast_wait( RTL_CRITICAL_SECTION *crit, int timeout )
|
|
|
|
{
|
|
|
|
return STATUS_NOT_IMPLEMENTED;
|
|
|
|
}
|
2005-09-28 20:21:48 +02:00
|
|
|
|
2006-06-27 17:39:50 +02:00
|
|
|
static inline NTSTATUS fast_wake( RTL_CRITICAL_SECTION *crit )
|
|
|
|
{
|
|
|
|
return STATUS_NOT_IMPLEMENTED;
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline void close_semaphore( RTL_CRITICAL_SECTION *crit )
|
|
|
|
{
|
|
|
|
NtClose( crit->LockSemaphore );
|
|
|
|
}
|
2005-09-28 20:21:48 +02:00
|
|
|
|
|
|
|
#endif
|
|
|
|
|
2000-09-29 02:31:23 +02:00
|
|
|
/***********************************************************************
|
|
|
|
* get_semaphore
|
|
|
|
*/
|
|
|
|
static inline HANDLE get_semaphore( RTL_CRITICAL_SECTION *crit )
|
|
|
|
{
|
|
|
|
HANDLE ret = crit->LockSemaphore;
|
|
|
|
if (!ret)
|
|
|
|
{
|
|
|
|
HANDLE sem;
|
|
|
|
if (NtCreateSemaphore( &sem, SEMAPHORE_ALL_ACCESS, NULL, 0, 1 )) return 0;
|
2008-01-12 17:05:50 +01:00
|
|
|
if (!(ret = interlocked_cmpxchg_ptr( &crit->LockSemaphore, sem, 0 )))
|
2000-09-29 02:31:23 +02:00
|
|
|
ret = sem;
|
|
|
|
else
|
|
|
|
NtClose(sem); /* somebody beat us to it */
|
|
|
|
}
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2005-09-28 20:21:48 +02:00
|
|
|
/***********************************************************************
|
|
|
|
* wait_semaphore
|
|
|
|
*/
|
|
|
|
static inline NTSTATUS wait_semaphore( RTL_CRITICAL_SECTION *crit, int timeout )
|
|
|
|
{
|
2006-06-27 17:39:50 +02:00
|
|
|
NTSTATUS ret;
|
2005-09-28 20:21:48 +02:00
|
|
|
|
2006-06-27 17:39:50 +02:00
|
|
|
/* debug info is cleared by MakeCriticalSectionGlobal */
|
|
|
|
if (!crit->DebugInfo || ((ret = fast_wait( crit, timeout )) == STATUS_NOT_IMPLEMENTED))
|
2005-09-28 20:21:48 +02:00
|
|
|
{
|
|
|
|
HANDLE sem = get_semaphore( crit );
|
|
|
|
LARGE_INTEGER time;
|
2013-08-22 10:53:45 +02:00
|
|
|
select_op_t select_op;
|
2005-09-28 20:21:48 +02:00
|
|
|
|
|
|
|
time.QuadPart = timeout * (LONGLONG)-10000000;
|
2013-08-22 10:53:45 +02:00
|
|
|
select_op.wait.op = SELECT_WAIT;
|
|
|
|
select_op.wait.handles[0] = wine_server_obj_handle( sem );
|
2013-08-22 11:27:15 +02:00
|
|
|
ret = server_select( &select_op, offsetof( select_op_t, wait.handles[1] ), 0, &time );
|
2005-09-28 20:21:48 +02:00
|
|
|
}
|
2006-06-27 17:39:50 +02:00
|
|
|
return ret;
|
2005-09-28 20:21:48 +02:00
|
|
|
}
|
|
|
|
|
2000-09-29 02:31:23 +02:00
|
|
|
/***********************************************************************
|
|
|
|
* RtlInitializeCriticalSection (NTDLL.@)
|
2003-03-18 19:35:48 +01:00
|
|
|
*
|
2004-09-06 22:26:23 +02:00
|
|
|
* Initialises a new critical section.
|
2003-03-18 19:35:48 +01:00
|
|
|
*
|
|
|
|
* PARAMS
|
|
|
|
* crit [O] Critical section to initialise
|
|
|
|
*
|
2004-09-06 22:26:23 +02:00
|
|
|
* RETURNS
|
2003-03-18 19:35:48 +01:00
|
|
|
* STATUS_SUCCESS.
|
2004-09-06 22:26:23 +02:00
|
|
|
*
|
|
|
|
* SEE
|
2008-06-20 09:19:09 +02:00
|
|
|
* RtlInitializeCriticalSectionEx(),
|
2004-09-06 22:26:23 +02:00
|
|
|
* RtlInitializeCriticalSectionAndSpinCount(), RtlDeleteCriticalSection(),
|
|
|
|
* RtlEnterCriticalSection(), RtlLeaveCriticalSection(),
|
|
|
|
* RtlTryEnterCriticalSection(), RtlSetCriticalSectionSpinCount()
|
2000-09-29 02:31:23 +02:00
|
|
|
*/
|
|
|
|
NTSTATUS WINAPI RtlInitializeCriticalSection( RTL_CRITICAL_SECTION *crit )
|
2004-06-15 02:52:03 +02:00
|
|
|
{
|
2008-06-20 09:19:09 +02:00
|
|
|
return RtlInitializeCriticalSectionEx( crit, 0, 0 );
|
2004-06-15 02:52:03 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/***********************************************************************
|
|
|
|
* RtlInitializeCriticalSectionAndSpinCount (NTDLL.@)
|
|
|
|
*
|
2004-09-06 22:26:23 +02:00
|
|
|
* Initialises a new critical section with a given spin count.
|
2004-06-15 02:52:03 +02:00
|
|
|
*
|
|
|
|
* PARAMS
|
|
|
|
* crit [O] Critical section to initialise
|
|
|
|
* spincount [I] Spin count for crit
|
|
|
|
*
|
|
|
|
* RETURNS
|
|
|
|
* STATUS_SUCCESS.
|
|
|
|
*
|
|
|
|
* NOTES
|
2004-09-06 22:26:23 +02:00
|
|
|
* Available on NT4 SP3 or later.
|
|
|
|
*
|
|
|
|
* SEE
|
2008-06-20 09:19:09 +02:00
|
|
|
* RtlInitializeCriticalSectionEx(),
|
2004-09-06 22:26:23 +02:00
|
|
|
* RtlInitializeCriticalSection(), RtlDeleteCriticalSection(),
|
|
|
|
* RtlEnterCriticalSection(), RtlLeaveCriticalSection(),
|
|
|
|
* RtlTryEnterCriticalSection(), RtlSetCriticalSectionSpinCount()
|
2004-06-15 02:52:03 +02:00
|
|
|
*/
|
|
|
|
NTSTATUS WINAPI RtlInitializeCriticalSectionAndSpinCount( RTL_CRITICAL_SECTION *crit, ULONG spincount )
|
2000-09-29 02:31:23 +02:00
|
|
|
{
|
2008-06-20 09:19:09 +02:00
|
|
|
return RtlInitializeCriticalSectionEx( crit, spincount, 0 );
|
|
|
|
}
|
|
|
|
|
|
|
|
/***********************************************************************
|
|
|
|
* RtlInitializeCriticalSectionEx (NTDLL.@)
|
|
|
|
*
|
|
|
|
* Initialises a new critical section with a given spin count and flags.
|
|
|
|
*
|
|
|
|
* PARAMS
|
|
|
|
* crit [O] Critical section to initialise.
|
|
|
|
* spincount [I] Number of times to spin upon contention.
|
|
|
|
* flags [I] RTL_CRITICAL_SECTION_FLAG_ flags from winnt.h.
|
|
|
|
*
|
|
|
|
* RETURNS
|
|
|
|
* STATUS_SUCCESS.
|
|
|
|
*
|
|
|
|
* NOTES
|
|
|
|
* Available on Vista or later.
|
|
|
|
*
|
|
|
|
* SEE
|
|
|
|
* RtlInitializeCriticalSection(), RtlDeleteCriticalSection(),
|
|
|
|
* RtlEnterCriticalSection(), RtlLeaveCriticalSection(),
|
|
|
|
* RtlTryEnterCriticalSection(), RtlSetCriticalSectionSpinCount()
|
|
|
|
*/
|
|
|
|
NTSTATUS WINAPI RtlInitializeCriticalSectionEx( RTL_CRITICAL_SECTION *crit, ULONG spincount, ULONG flags )
|
|
|
|
{
|
|
|
|
if (flags & (RTL_CRITICAL_SECTION_FLAG_DYNAMIC_SPIN|RTL_CRITICAL_SECTION_FLAG_STATIC_INIT))
|
|
|
|
FIXME("(%p,%u,0x%08x) semi-stub\n", crit, spincount, flags);
|
|
|
|
|
|
|
|
/* FIXME: if RTL_CRITICAL_SECTION_FLAG_STATIC_INIT is given, we should use
|
|
|
|
* memory from a static pool to hold the debug info. Then heap.c could pass
|
|
|
|
* this flag rather than initialising the process heap CS by hand. If this
|
|
|
|
* is done, then debug info should be managed through Rtlp[Allocate|Free]DebugInfo
|
|
|
|
* so (e.g.) MakeCriticalSectionGlobal() doesn't free it using HeapFree().
|
|
|
|
*/
|
|
|
|
if (flags & RTL_CRITICAL_SECTION_FLAG_NO_DEBUG_INFO)
|
|
|
|
crit->DebugInfo = NULL;
|
|
|
|
else
|
|
|
|
crit->DebugInfo = RtlAllocateHeap(GetProcessHeap(), 0, sizeof(RTL_CRITICAL_SECTION_DEBUG));
|
|
|
|
|
2005-08-03 18:03:15 +02:00
|
|
|
if (crit->DebugInfo)
|
2003-08-13 01:50:54 +02:00
|
|
|
{
|
2005-08-03 18:03:15 +02:00
|
|
|
crit->DebugInfo->Type = 0;
|
|
|
|
crit->DebugInfo->CreatorBackTraceIndex = 0;
|
|
|
|
crit->DebugInfo->CriticalSection = crit;
|
|
|
|
crit->DebugInfo->ProcessLocksList.Blink = &(crit->DebugInfo->ProcessLocksList);
|
|
|
|
crit->DebugInfo->ProcessLocksList.Flink = &(crit->DebugInfo->ProcessLocksList);
|
|
|
|
crit->DebugInfo->EntryCount = 0;
|
|
|
|
crit->DebugInfo->ContentionCount = 0;
|
2005-09-09 12:19:44 +02:00
|
|
|
memset( crit->DebugInfo->Spare, 0, sizeof(crit->DebugInfo->Spare) );
|
2003-08-13 01:50:54 +02:00
|
|
|
}
|
2000-09-29 02:31:23 +02:00
|
|
|
crit->LockCount = -1;
|
|
|
|
crit->RecursionCount = 0;
|
|
|
|
crit->OwningThread = 0;
|
|
|
|
crit->LockSemaphore = 0;
|
2005-07-13 13:38:08 +02:00
|
|
|
if (NtCurrentTeb()->Peb->NumberOfProcessors <= 1) spincount = 0;
|
|
|
|
crit->SpinCount = spincount & ~0x80000000;
|
2000-09-29 02:31:23 +02:00
|
|
|
return STATUS_SUCCESS;
|
|
|
|
}
|
|
|
|
|
2000-10-29 02:24:54 +01:00
|
|
|
/***********************************************************************
|
2004-06-15 02:52:03 +02:00
|
|
|
* RtlSetCriticalSectionSpinCount (NTDLL.@)
|
2003-03-18 19:35:48 +01:00
|
|
|
*
|
2004-06-15 02:52:03 +02:00
|
|
|
* Sets the spin count of a critical section.
|
2003-03-18 19:35:48 +01:00
|
|
|
*
|
|
|
|
* PARAMS
|
2004-09-06 22:26:23 +02:00
|
|
|
* crit [I/O] Critical section
|
2003-03-18 19:35:48 +01:00
|
|
|
* spincount [I] Spin count for crit
|
2004-06-15 02:52:03 +02:00
|
|
|
*
|
2003-03-18 19:35:48 +01:00
|
|
|
* RETURNS
|
2004-06-15 02:52:03 +02:00
|
|
|
* The previous spin count.
|
2003-03-18 19:35:48 +01:00
|
|
|
*
|
|
|
|
* NOTES
|
2004-06-15 02:52:03 +02:00
|
|
|
* If the system is not SMP, spincount is ignored and set to 0.
|
2004-09-06 22:26:23 +02:00
|
|
|
*
|
|
|
|
* SEE
|
2008-06-20 09:19:09 +02:00
|
|
|
* RtlInitializeCriticalSectionEx(),
|
2004-09-06 22:26:23 +02:00
|
|
|
* RtlInitializeCriticalSection(), RtlInitializeCriticalSectionAndSpinCount(),
|
|
|
|
* RtlDeleteCriticalSection(), RtlEnterCriticalSection(),
|
|
|
|
* RtlLeaveCriticalSection(), RtlTryEnterCriticalSection()
|
2000-10-29 02:24:54 +01:00
|
|
|
*/
|
2004-06-15 02:52:03 +02:00
|
|
|
ULONG WINAPI RtlSetCriticalSectionSpinCount( RTL_CRITICAL_SECTION *crit, ULONG spincount )
|
2000-10-29 02:24:54 +01:00
|
|
|
{
|
2004-06-15 02:52:03 +02:00
|
|
|
ULONG oldspincount = crit->SpinCount;
|
|
|
|
if (NtCurrentTeb()->Peb->NumberOfProcessors <= 1) spincount = 0;
|
2000-10-29 02:24:54 +01:00
|
|
|
crit->SpinCount = spincount;
|
2004-06-15 02:52:03 +02:00
|
|
|
return oldspincount;
|
2000-10-29 02:24:54 +01:00
|
|
|
}
|
|
|
|
|
2000-09-29 02:31:23 +02:00
|
|
|
/***********************************************************************
|
|
|
|
* RtlDeleteCriticalSection (NTDLL.@)
|
2003-03-18 19:35:48 +01:00
|
|
|
*
|
2004-09-06 22:26:23 +02:00
|
|
|
* Frees the resources used by a critical section.
|
2003-03-18 19:35:48 +01:00
|
|
|
*
|
|
|
|
* PARAMS
|
|
|
|
* crit [I/O] Critical section to free
|
|
|
|
*
|
|
|
|
* RETURNS
|
|
|
|
* STATUS_SUCCESS.
|
2004-09-06 22:26:23 +02:00
|
|
|
*
|
|
|
|
* SEE
|
2008-06-20 09:19:09 +02:00
|
|
|
* RtlInitializeCriticalSectionEx(),
|
2004-09-06 22:26:23 +02:00
|
|
|
* RtlInitializeCriticalSection(), RtlInitializeCriticalSectionAndSpinCount(),
|
|
|
|
* RtlDeleteCriticalSection(), RtlEnterCriticalSection(),
|
|
|
|
* RtlLeaveCriticalSection(), RtlTryEnterCriticalSection()
|
2000-09-29 02:31:23 +02:00
|
|
|
*/
|
|
|
|
NTSTATUS WINAPI RtlDeleteCriticalSection( RTL_CRITICAL_SECTION *crit )
|
|
|
|
{
|
|
|
|
crit->LockCount = -1;
|
|
|
|
crit->RecursionCount = 0;
|
|
|
|
crit->OwningThread = 0;
|
2003-08-13 01:50:54 +02:00
|
|
|
if (crit->DebugInfo)
|
|
|
|
{
|
|
|
|
/* only free the ones we made in here */
|
2005-09-09 12:19:44 +02:00
|
|
|
if (!crit->DebugInfo->Spare[0])
|
2003-08-13 01:50:54 +02:00
|
|
|
{
|
2004-03-12 02:59:35 +01:00
|
|
|
RtlFreeHeap( GetProcessHeap(), 0, crit->DebugInfo );
|
2003-08-13 01:50:54 +02:00
|
|
|
crit->DebugInfo = NULL;
|
|
|
|
}
|
2006-06-27 17:39:50 +02:00
|
|
|
close_semaphore( crit );
|
2003-08-13 01:50:54 +02:00
|
|
|
}
|
2006-06-27 17:39:50 +02:00
|
|
|
else NtClose( crit->LockSemaphore );
|
|
|
|
crit->LockSemaphore = 0;
|
2000-09-29 02:31:23 +02:00
|
|
|
return STATUS_SUCCESS;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/***********************************************************************
|
|
|
|
* RtlpWaitForCriticalSection (NTDLL.@)
|
2003-03-18 19:35:48 +01:00
|
|
|
*
|
2004-09-06 22:26:23 +02:00
|
|
|
* Waits for a busy critical section to become free.
|
2003-03-18 19:35:48 +01:00
|
|
|
*
|
|
|
|
* PARAMS
|
|
|
|
* crit [I/O] Critical section to wait for
|
|
|
|
*
|
|
|
|
* RETURNS
|
|
|
|
* STATUS_SUCCESS.
|
2004-09-06 22:26:23 +02:00
|
|
|
*
|
|
|
|
* NOTES
|
|
|
|
* Use RtlEnterCriticalSection() instead of this function as it is often much
|
|
|
|
* faster.
|
|
|
|
*
|
|
|
|
* SEE
|
2008-06-20 09:19:09 +02:00
|
|
|
* RtlInitializeCriticalSectionEx(),
|
2004-09-06 22:26:23 +02:00
|
|
|
* RtlInitializeCriticalSection(), RtlInitializeCriticalSectionAndSpinCount(),
|
|
|
|
* RtlDeleteCriticalSection(), RtlEnterCriticalSection(),
|
|
|
|
* RtlLeaveCriticalSection(), RtlTryEnterCriticalSection()
|
2000-09-29 02:31:23 +02:00
|
|
|
*/
|
|
|
|
NTSTATUS WINAPI RtlpWaitForCriticalSection( RTL_CRITICAL_SECTION *crit )
|
|
|
|
{
|
|
|
|
for (;;)
|
|
|
|
{
|
|
|
|
EXCEPTION_RECORD rec;
|
2005-09-28 20:21:48 +02:00
|
|
|
NTSTATUS status = wait_semaphore( crit, 5 );
|
2000-09-29 02:31:23 +02:00
|
|
|
|
2005-06-25 20:31:29 +02:00
|
|
|
if ( status == STATUS_TIMEOUT )
|
2000-09-29 02:31:23 +02:00
|
|
|
{
|
2003-08-13 01:50:54 +02:00
|
|
|
const char *name = NULL;
|
2005-09-09 12:19:44 +02:00
|
|
|
if (crit->DebugInfo) name = (char *)crit->DebugInfo->Spare[0];
|
2002-09-19 01:13:35 +02:00
|
|
|
if (!name) name = "?";
|
2006-10-16 13:49:06 +02:00
|
|
|
ERR( "section %p %s wait timed out in thread %04x, blocked by %04x, retrying (60 sec)\n",
|
2007-05-23 09:36:29 +02:00
|
|
|
crit, debugstr_a(name), GetCurrentThreadId(), HandleToULong(crit->OwningThread) );
|
2005-09-28 20:21:48 +02:00
|
|
|
status = wait_semaphore( crit, 60 );
|
2005-06-25 20:31:29 +02:00
|
|
|
if ( status == STATUS_TIMEOUT && TRACE_ON(relay) )
|
2000-09-29 02:31:23 +02:00
|
|
|
{
|
2006-10-16 13:49:06 +02:00
|
|
|
ERR( "section %p %s wait timed out in thread %04x, blocked by %04x, retrying (5 min)\n",
|
2007-05-23 09:36:29 +02:00
|
|
|
crit, debugstr_a(name), GetCurrentThreadId(), HandleToULong(crit->OwningThread) );
|
2005-09-28 20:21:48 +02:00
|
|
|
status = wait_semaphore( crit, 300 );
|
2000-09-29 02:31:23 +02:00
|
|
|
}
|
|
|
|
}
|
2005-09-28 20:21:48 +02:00
|
|
|
if (status == STATUS_WAIT_0) break;
|
2000-09-29 02:31:23 +02:00
|
|
|
|
2002-08-13 20:09:22 +02:00
|
|
|
/* Throw exception only for Wine internal locks */
|
2005-09-09 12:19:44 +02:00
|
|
|
if ((!crit->DebugInfo) || (!crit->DebugInfo->Spare[0])) continue;
|
2002-08-13 20:09:22 +02:00
|
|
|
|
2003-04-04 21:41:31 +02:00
|
|
|
rec.ExceptionCode = STATUS_POSSIBLE_DEADLOCK;
|
2000-09-29 02:31:23 +02:00
|
|
|
rec.ExceptionFlags = 0;
|
|
|
|
rec.ExceptionRecord = NULL;
|
|
|
|
rec.ExceptionAddress = RtlRaiseException; /* sic */
|
|
|
|
rec.NumberParameters = 1;
|
2005-02-24 14:15:36 +01:00
|
|
|
rec.ExceptionInformation[0] = (ULONG_PTR)crit;
|
2000-09-29 02:31:23 +02:00
|
|
|
RtlRaiseException( &rec );
|
|
|
|
}
|
2005-09-28 20:21:48 +02:00
|
|
|
if (crit->DebugInfo) crit->DebugInfo->ContentionCount++;
|
|
|
|
return STATUS_SUCCESS;
|
2000-09-29 02:31:23 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/***********************************************************************
|
|
|
|
* RtlpUnWaitCriticalSection (NTDLL.@)
|
2004-09-06 22:26:23 +02:00
|
|
|
*
|
|
|
|
* Notifies other threads waiting on the busy critical section that it has
|
|
|
|
* become free.
|
|
|
|
*
|
|
|
|
* PARAMS
|
|
|
|
* crit [I/O] Critical section
|
|
|
|
*
|
|
|
|
* RETURNS
|
|
|
|
* Success: STATUS_SUCCESS.
|
|
|
|
* Failure: Any error returned by NtReleaseSemaphore()
|
|
|
|
*
|
|
|
|
* NOTES
|
|
|
|
* Use RtlLeaveCriticalSection() instead of this function as it is often much
|
|
|
|
* faster.
|
|
|
|
*
|
|
|
|
* SEE
|
2008-06-20 09:19:09 +02:00
|
|
|
* RtlInitializeCriticalSectionEx(),
|
2004-09-06 22:26:23 +02:00
|
|
|
* RtlInitializeCriticalSection(), RtlInitializeCriticalSectionAndSpinCount(),
|
|
|
|
* RtlDeleteCriticalSection(), RtlEnterCriticalSection(),
|
|
|
|
* RtlLeaveCriticalSection(), RtlTryEnterCriticalSection()
|
2000-09-29 02:31:23 +02:00
|
|
|
*/
|
|
|
|
NTSTATUS WINAPI RtlpUnWaitCriticalSection( RTL_CRITICAL_SECTION *crit )
|
|
|
|
{
|
2006-06-27 17:39:50 +02:00
|
|
|
NTSTATUS ret;
|
|
|
|
|
|
|
|
/* debug info is cleared by MakeCriticalSectionGlobal */
|
|
|
|
if (!crit->DebugInfo || ((ret = fast_wake( crit )) == STATUS_NOT_IMPLEMENTED))
|
2005-09-28 20:21:48 +02:00
|
|
|
{
|
|
|
|
HANDLE sem = get_semaphore( crit );
|
2006-06-27 17:39:50 +02:00
|
|
|
ret = NtReleaseSemaphore( sem, 1, NULL );
|
2005-09-28 20:21:48 +02:00
|
|
|
}
|
2006-06-27 17:39:50 +02:00
|
|
|
if (ret) RtlRaiseStatus( ret );
|
|
|
|
return ret;
|
2000-09-29 02:31:23 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/***********************************************************************
|
|
|
|
* RtlEnterCriticalSection (NTDLL.@)
|
2003-03-18 19:35:48 +01:00
|
|
|
*
|
2004-09-06 22:26:23 +02:00
|
|
|
* Enters a critical section, waiting for it to become available if necessary.
|
2003-03-18 19:35:48 +01:00
|
|
|
*
|
|
|
|
* PARAMS
|
|
|
|
* crit [I/O] Critical section to enter
|
|
|
|
*
|
|
|
|
* RETURNS
|
|
|
|
* STATUS_SUCCESS. The critical section is held by the caller.
|
|
|
|
*
|
2004-09-06 22:26:23 +02:00
|
|
|
* SEE
|
2008-06-20 09:19:09 +02:00
|
|
|
* RtlInitializeCriticalSectionEx(),
|
2004-09-06 22:26:23 +02:00
|
|
|
* RtlInitializeCriticalSection(), RtlInitializeCriticalSectionAndSpinCount(),
|
|
|
|
* RtlDeleteCriticalSection(), RtlSetCriticalSectionSpinCount(),
|
|
|
|
* RtlLeaveCriticalSection(), RtlTryEnterCriticalSection()
|
2000-09-29 02:31:23 +02:00
|
|
|
*/
|
|
|
|
NTSTATUS WINAPI RtlEnterCriticalSection( RTL_CRITICAL_SECTION *crit )
|
|
|
|
{
|
2004-06-18 01:11:08 +02:00
|
|
|
if (crit->SpinCount)
|
|
|
|
{
|
|
|
|
ULONG count;
|
|
|
|
|
|
|
|
if (RtlTryEnterCriticalSection( crit )) return STATUS_SUCCESS;
|
|
|
|
for (count = crit->SpinCount; count > 0; count--)
|
|
|
|
{
|
|
|
|
if (crit->LockCount > 0) break; /* more than one waiter, don't bother spinning */
|
|
|
|
if (crit->LockCount == -1) /* try again */
|
|
|
|
{
|
2008-01-12 17:05:50 +01:00
|
|
|
if (interlocked_cmpxchg( &crit->LockCount, 0, -1 ) == -1) goto done;
|
2004-06-18 01:11:08 +02:00
|
|
|
}
|
|
|
|
small_pause();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2000-09-29 02:31:23 +02:00
|
|
|
if (interlocked_inc( &crit->LockCount ))
|
|
|
|
{
|
2007-05-23 09:36:29 +02:00
|
|
|
if (crit->OwningThread == ULongToHandle(GetCurrentThreadId()))
|
2000-09-29 02:31:23 +02:00
|
|
|
{
|
|
|
|
crit->RecursionCount++;
|
|
|
|
return STATUS_SUCCESS;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Now wait for it */
|
|
|
|
RtlpWaitForCriticalSection( crit );
|
|
|
|
}
|
2004-06-18 01:11:08 +02:00
|
|
|
done:
|
2007-05-23 09:36:29 +02:00
|
|
|
crit->OwningThread = ULongToHandle(GetCurrentThreadId());
|
2000-09-29 02:31:23 +02:00
|
|
|
crit->RecursionCount = 1;
|
|
|
|
return STATUS_SUCCESS;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/***********************************************************************
|
|
|
|
* RtlTryEnterCriticalSection (NTDLL.@)
|
2003-03-18 19:35:48 +01:00
|
|
|
*
|
2004-09-06 22:26:23 +02:00
|
|
|
* Tries to enter a critical section without waiting.
|
2003-03-18 19:35:48 +01:00
|
|
|
*
|
|
|
|
* PARAMS
|
|
|
|
* crit [I/O] Critical section to enter
|
|
|
|
*
|
|
|
|
* RETURNS
|
|
|
|
* Success: TRUE. The critical section is held by the caller.
|
|
|
|
* Failure: FALSE. The critical section is currently held by another thread.
|
2004-09-06 22:26:23 +02:00
|
|
|
*
|
|
|
|
* SEE
|
2008-06-20 09:19:09 +02:00
|
|
|
* RtlInitializeCriticalSectionEx(),
|
2004-09-06 22:26:23 +02:00
|
|
|
* RtlInitializeCriticalSection(), RtlInitializeCriticalSectionAndSpinCount(),
|
|
|
|
* RtlDeleteCriticalSection(), RtlEnterCriticalSection(),
|
|
|
|
* RtlLeaveCriticalSection(), RtlSetCriticalSectionSpinCount()
|
2000-09-29 02:31:23 +02:00
|
|
|
*/
|
|
|
|
BOOL WINAPI RtlTryEnterCriticalSection( RTL_CRITICAL_SECTION *crit )
|
|
|
|
{
|
|
|
|
BOOL ret = FALSE;
|
2008-01-12 17:05:50 +01:00
|
|
|
if (interlocked_cmpxchg( &crit->LockCount, 0, -1 ) == -1)
|
2000-09-29 02:31:23 +02:00
|
|
|
{
|
2007-05-23 09:36:29 +02:00
|
|
|
crit->OwningThread = ULongToHandle(GetCurrentThreadId());
|
2000-09-29 02:31:23 +02:00
|
|
|
crit->RecursionCount = 1;
|
|
|
|
ret = TRUE;
|
|
|
|
}
|
2007-05-23 09:36:29 +02:00
|
|
|
else if (crit->OwningThread == ULongToHandle(GetCurrentThreadId()))
|
2000-09-29 02:31:23 +02:00
|
|
|
{
|
|
|
|
interlocked_inc( &crit->LockCount );
|
|
|
|
crit->RecursionCount++;
|
|
|
|
ret = TRUE;
|
|
|
|
}
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/***********************************************************************
|
|
|
|
* RtlLeaveCriticalSection (NTDLL.@)
|
2003-03-18 19:35:48 +01:00
|
|
|
*
|
2004-09-06 22:26:23 +02:00
|
|
|
* Leaves a critical section.
|
2003-03-18 19:35:48 +01:00
|
|
|
*
|
|
|
|
* PARAMS
|
2004-09-06 22:26:23 +02:00
|
|
|
* crit [I/O] Critical section to leave.
|
2003-03-18 19:35:48 +01:00
|
|
|
*
|
|
|
|
* RETURNS
|
|
|
|
* STATUS_SUCCESS.
|
2004-09-06 22:26:23 +02:00
|
|
|
*
|
|
|
|
* SEE
|
2008-06-20 09:19:09 +02:00
|
|
|
* RtlInitializeCriticalSectionEx(),
|
2004-09-06 22:26:23 +02:00
|
|
|
* RtlInitializeCriticalSection(), RtlInitializeCriticalSectionAndSpinCount(),
|
|
|
|
* RtlDeleteCriticalSection(), RtlEnterCriticalSection(),
|
|
|
|
* RtlSetCriticalSectionSpinCount(), RtlTryEnterCriticalSection()
|
2000-09-29 02:31:23 +02:00
|
|
|
*/
|
|
|
|
NTSTATUS WINAPI RtlLeaveCriticalSection( RTL_CRITICAL_SECTION *crit )
|
|
|
|
{
|
|
|
|
if (--crit->RecursionCount) interlocked_dec( &crit->LockCount );
|
|
|
|
else
|
|
|
|
{
|
|
|
|
crit->OwningThread = 0;
|
|
|
|
if (interlocked_dec( &crit->LockCount ) >= 0)
|
|
|
|
{
|
|
|
|
/* someone is waiting */
|
|
|
|
RtlpUnWaitCriticalSection( crit );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return STATUS_SUCCESS;
|
|
|
|
}
|