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
|
|
|
|
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 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>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include "winerror.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
|
|
|
|
|
|
|
inline static LONG interlocked_inc( PLONG dest )
|
|
|
|
{
|
2002-04-25 23:40:56 +02:00
|
|
|
return interlocked_xchg_add( dest, 1 ) + 1;
|
2000-09-29 02:31:23 +02:00
|
|
|
}
|
|
|
|
|
2002-04-25 23:40:56 +02:00
|
|
|
inline static LONG interlocked_dec( PLONG dest )
|
2000-09-29 02:31:23 +02:00
|
|
|
{
|
2002-04-25 23:40:56 +02:00
|
|
|
return interlocked_xchg_add( dest, -1 ) - 1;
|
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;
|
2002-04-25 23:40:56 +02:00
|
|
|
if (!(ret = (HANDLE)interlocked_cmpxchg_ptr( (PVOID *)&crit->LockSemaphore,
|
|
|
|
(PVOID)sem, 0 )))
|
2000-09-29 02:31:23 +02:00
|
|
|
ret = sem;
|
|
|
|
else
|
|
|
|
NtClose(sem); /* somebody beat us to it */
|
|
|
|
}
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
/***********************************************************************
|
|
|
|
* RtlInitializeCriticalSection (NTDLL.@)
|
2003-03-18 19:35:48 +01:00
|
|
|
*
|
|
|
|
* Initialise a new RTL_CRITICAL_SECTION.
|
|
|
|
*
|
|
|
|
* PARAMS
|
|
|
|
* crit [O] Critical section to initialise
|
|
|
|
*
|
|
|
|
* RETURN
|
|
|
|
* STATUS_SUCCESS.
|
2000-09-29 02:31:23 +02:00
|
|
|
*/
|
|
|
|
NTSTATUS WINAPI RtlInitializeCriticalSection( RTL_CRITICAL_SECTION *crit )
|
|
|
|
{
|
2003-08-13 03:09:45 +02:00
|
|
|
if (!ntdll_get_process_heap()) crit->DebugInfo = NULL;
|
|
|
|
else
|
2003-08-13 01:50:54 +02:00
|
|
|
{
|
2003-08-13 03:09:45 +02:00
|
|
|
crit->DebugInfo = RtlAllocateHeap(ntdll_get_process_heap(), 0, sizeof(CRITICAL_SECTION_DEBUG));
|
|
|
|
if (crit->DebugInfo)
|
|
|
|
{
|
|
|
|
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;
|
|
|
|
crit->DebugInfo->Spare[0] = 0;
|
|
|
|
crit->DebugInfo->Spare[1] = 0;
|
|
|
|
}
|
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;
|
|
|
|
return STATUS_SUCCESS;
|
|
|
|
}
|
|
|
|
|
2000-10-29 02:24:54 +01:00
|
|
|
/***********************************************************************
|
|
|
|
* RtlInitializeCriticalSectionAndSpinCount (NTDLL.@)
|
2003-03-18 19:35:48 +01:00
|
|
|
*
|
|
|
|
* Initialise a new RTL_CRITICAL_SECTION with a given spin count.
|
|
|
|
*
|
|
|
|
* PARAMS
|
|
|
|
* crit [O] Critical section to initialise
|
|
|
|
* spincount [I] Spin count for crit
|
|
|
|
*
|
|
|
|
* RETURNS
|
|
|
|
* STATUS_SUCCESS.
|
|
|
|
*
|
|
|
|
* NOTES
|
|
|
|
* The InitializeCriticalSectionAndSpinCount() (KERNEL32) function is
|
2000-10-29 02:24:54 +01:00
|
|
|
* available on NT4SP3 or later, and Win98 or later.
|
|
|
|
* I am assuming that this is the correct definition given the MSDN
|
|
|
|
* docs for the kernel32 functions.
|
|
|
|
*/
|
|
|
|
NTSTATUS WINAPI RtlInitializeCriticalSectionAndSpinCount( RTL_CRITICAL_SECTION *crit, DWORD spincount )
|
|
|
|
{
|
2001-01-22 03:18:13 +01:00
|
|
|
if(spincount) TRACE("critsection=%p: spincount=%ld not supported\n", crit, spincount);
|
2000-10-29 02:24:54 +01:00
|
|
|
crit->SpinCount = spincount;
|
|
|
|
return RtlInitializeCriticalSection( crit );
|
|
|
|
}
|
|
|
|
|
2000-09-29 02:31:23 +02:00
|
|
|
|
|
|
|
/***********************************************************************
|
|
|
|
* RtlDeleteCriticalSection (NTDLL.@)
|
2003-03-18 19:35:48 +01:00
|
|
|
*
|
|
|
|
* Free the resources used by an RTL_CRITICAL_SECTION.
|
|
|
|
*
|
|
|
|
* PARAMS
|
|
|
|
* crit [I/O] Critical section to free
|
|
|
|
*
|
|
|
|
* RETURNS
|
|
|
|
* STATUS_SUCCESS.
|
2000-09-29 02:31:23 +02:00
|
|
|
*/
|
|
|
|
NTSTATUS WINAPI RtlDeleteCriticalSection( RTL_CRITICAL_SECTION *crit )
|
|
|
|
{
|
|
|
|
crit->LockCount = -1;
|
|
|
|
crit->RecursionCount = 0;
|
|
|
|
crit->OwningThread = 0;
|
|
|
|
if (crit->LockSemaphore) NtClose( crit->LockSemaphore );
|
|
|
|
crit->LockSemaphore = 0;
|
2003-08-13 01:50:54 +02:00
|
|
|
if (crit->DebugInfo)
|
|
|
|
{
|
|
|
|
/* only free the ones we made in here */
|
|
|
|
if (!crit->DebugInfo->Spare[1])
|
|
|
|
{
|
|
|
|
|
|
|
|
RtlFreeHeap( ntdll_get_process_heap(), 0, crit->DebugInfo );
|
|
|
|
crit->DebugInfo = NULL;
|
|
|
|
}
|
|
|
|
}
|
2000-09-29 02:31:23 +02:00
|
|
|
return STATUS_SUCCESS;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/***********************************************************************
|
|
|
|
* RtlpWaitForCriticalSection (NTDLL.@)
|
2003-03-18 19:35:48 +01:00
|
|
|
*
|
|
|
|
* Wait for an RTL_CRITICAL_SECTION to become free.
|
|
|
|
*
|
|
|
|
* PARAMS
|
|
|
|
* crit [I/O] Critical section to wait for
|
|
|
|
*
|
|
|
|
* RETURNS
|
|
|
|
* STATUS_SUCCESS.
|
2000-09-29 02:31:23 +02:00
|
|
|
*/
|
|
|
|
NTSTATUS WINAPI RtlpWaitForCriticalSection( RTL_CRITICAL_SECTION *crit )
|
|
|
|
{
|
|
|
|
for (;;)
|
|
|
|
{
|
|
|
|
EXCEPTION_RECORD rec;
|
|
|
|
HANDLE sem = get_semaphore( crit );
|
2003-04-04 01:57:11 +02:00
|
|
|
LARGE_INTEGER time;
|
|
|
|
DWORD status;
|
2000-09-29 02:31:23 +02:00
|
|
|
|
2003-04-04 01:57:11 +02:00
|
|
|
time.QuadPart = -5000 * 10000; /* 5 seconds */
|
|
|
|
status = NtWaitForSingleObject( sem, FALSE, &time );
|
|
|
|
if ( status == WAIT_TIMEOUT )
|
2000-09-29 02:31:23 +02:00
|
|
|
{
|
2003-08-13 01:50:54 +02:00
|
|
|
const char *name = NULL;
|
|
|
|
if (crit->DebugInfo) name = (char *)crit->DebugInfo->Spare[1];
|
2002-09-19 01:13:35 +02:00
|
|
|
if (!name) name = "?";
|
2003-06-16 21:36:22 +02:00
|
|
|
ERR( "section %p %s wait timed out in thread %04lx, blocked by %04lx, retrying (60 sec)\n",
|
|
|
|
crit, debugstr_a(name), GetCurrentThreadId(), (DWORD)crit->OwningThread );
|
2003-04-04 01:57:11 +02:00
|
|
|
time.QuadPart = -60000 * 10000;
|
|
|
|
status = NtWaitForSingleObject( sem, FALSE, &time );
|
|
|
|
if ( status == WAIT_TIMEOUT && TRACE_ON(relay) )
|
2000-09-29 02:31:23 +02:00
|
|
|
{
|
2003-06-16 21:36:22 +02:00
|
|
|
ERR( "section %p %s wait timed out in thread %04lx, blocked by %04lx, retrying (5 min)\n",
|
|
|
|
crit, debugstr_a(name), GetCurrentThreadId(), (DWORD) crit->OwningThread );
|
2003-04-04 01:57:11 +02:00
|
|
|
time.QuadPart = -300000 * (ULONGLONG)10000;
|
|
|
|
status = NtWaitForSingleObject( sem, FALSE, &time );
|
2000-09-29 02:31:23 +02:00
|
|
|
}
|
|
|
|
}
|
2003-04-04 01:57:11 +02:00
|
|
|
if (status == STATUS_WAIT_0) return STATUS_SUCCESS;
|
2000-09-29 02:31:23 +02:00
|
|
|
|
2002-08-13 20:09:22 +02:00
|
|
|
/* Throw exception only for Wine internal locks */
|
2003-08-13 01:50:54 +02:00
|
|
|
if ((!crit->DebugInfo) || (!crit->DebugInfo->Spare[1])) 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;
|
|
|
|
rec.ExceptionInformation[0] = (DWORD)crit;
|
|
|
|
RtlRaiseException( &rec );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/***********************************************************************
|
|
|
|
* RtlpUnWaitCriticalSection (NTDLL.@)
|
|
|
|
*/
|
|
|
|
NTSTATUS WINAPI RtlpUnWaitCriticalSection( RTL_CRITICAL_SECTION *crit )
|
|
|
|
{
|
|
|
|
HANDLE sem = get_semaphore( crit );
|
|
|
|
NTSTATUS res = NtReleaseSemaphore( sem, 1, NULL );
|
|
|
|
if (res) RtlRaiseStatus( res );
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/***********************************************************************
|
|
|
|
* RtlEnterCriticalSection (NTDLL.@)
|
2003-03-18 19:35:48 +01:00
|
|
|
*
|
|
|
|
* Enter an RTL_CRITICAL_SECTION.
|
|
|
|
*
|
|
|
|
* PARAMS
|
|
|
|
* crit [I/O] Critical section to enter
|
|
|
|
*
|
|
|
|
* RETURNS
|
|
|
|
* STATUS_SUCCESS. The critical section is held by the caller.
|
|
|
|
*
|
|
|
|
* NOTES
|
|
|
|
* The caller will wait until the critical section is availale.
|
2000-09-29 02:31:23 +02:00
|
|
|
*/
|
|
|
|
NTSTATUS WINAPI RtlEnterCriticalSection( RTL_CRITICAL_SECTION *crit )
|
|
|
|
{
|
|
|
|
if (interlocked_inc( &crit->LockCount ))
|
|
|
|
{
|
2002-07-31 21:26:03 +02:00
|
|
|
if (crit->OwningThread == (HANDLE)GetCurrentThreadId())
|
2000-09-29 02:31:23 +02:00
|
|
|
{
|
|
|
|
crit->RecursionCount++;
|
|
|
|
return STATUS_SUCCESS;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Now wait for it */
|
|
|
|
RtlpWaitForCriticalSection( crit );
|
|
|
|
}
|
2002-07-31 21:26:03 +02:00
|
|
|
crit->OwningThread = (HANDLE)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
|
|
|
*
|
|
|
|
* Enter an RTL_CRITICAL_SECTION without waiting.
|
|
|
|
*
|
|
|
|
* 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.
|
2000-09-29 02:31:23 +02:00
|
|
|
*/
|
|
|
|
BOOL WINAPI RtlTryEnterCriticalSection( RTL_CRITICAL_SECTION *crit )
|
|
|
|
{
|
|
|
|
BOOL ret = FALSE;
|
2002-04-25 23:40:56 +02:00
|
|
|
if (interlocked_cmpxchg( &crit->LockCount, 0L, -1 ) == -1)
|
2000-09-29 02:31:23 +02:00
|
|
|
{
|
2002-07-31 21:26:03 +02:00
|
|
|
crit->OwningThread = (HANDLE)GetCurrentThreadId();
|
2000-09-29 02:31:23 +02:00
|
|
|
crit->RecursionCount = 1;
|
|
|
|
ret = TRUE;
|
|
|
|
}
|
2002-07-31 21:26:03 +02:00
|
|
|
else if (crit->OwningThread == (HANDLE)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
|
|
|
*
|
|
|
|
* Leave an RTL_CRITICAL_SECTION.
|
|
|
|
*
|
|
|
|
* PARAMS
|
|
|
|
* crit [I/O] Critical section to enter
|
|
|
|
*
|
|
|
|
* RETURNS
|
|
|
|
* STATUS_SUCCESS.
|
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;
|
|
|
|
}
|