From c6a8c7496fec63be0f92568d68ac14d797b59e5a Mon Sep 17 00:00:00 2001 From: Thomas Bluemel Date: Sat, 19 Feb 2005 13:13:17 +0000 Subject: [PATCH] fixed RtlEnterCriticalSection not to fail on wrong assumptions and added comment on why the debug message "Critical section not initialized (guess)" is wrong svn path=/trunk/; revision=13641 --- reactos/lib/ntdll/rtl/critical.c | 22 +++++++++++++--------- 1 file changed, 13 insertions(+), 9 deletions(-) diff --git a/reactos/lib/ntdll/rtl/critical.c b/reactos/lib/ntdll/rtl/critical.c index 50f01d46b33..2caa58f187e 100644 --- a/reactos/lib/ntdll/rtl/critical.c +++ b/reactos/lib/ntdll/rtl/critical.c @@ -144,23 +144,27 @@ RtlEnterCriticalSection( */ if (Thread == CriticalSection->OwningThread) { - /* You own it, so you'll get it when you're done with it! */ + /* You own it, so you'll get it when you're done with it! No need to + use the interlocked functions as only the thread who already owns + the lock can modify this data. */ CriticalSection->RecursionCount++; return STATUS_SUCCESS; } - else if (CriticalSection->OwningThread == (HANDLE)0) - { - /* No one else owns it either! */ - DPRINT1("Critical section not initialized (guess)!\n"); - /* FIXME: raise exception */ - return STATUS_INVALID_PARAMETER; - } + + /* NOTE - CriticalSection->OwningThread can be NULL here because changing + this information is not serialized. This happens when thread a + acquires the lock (LockCount == 0) and thread b tries to + acquire it as well (LockCount == 1) but thread a hasn't had a + chance to set the OwningThread! So it's not an error when + OwningThread is NULL here! */ /* We don't own it, so we must wait for it */ RtlpWaitForCriticalSection(CriticalSection); } - /* Lock successful */ + /* Lock successful. Changing this information has not to be serialized because + only one thread at a time can actually change it (the one who acquired + the lock)! */ CriticalSection->OwningThread = Thread; CriticalSection->RecursionCount = 1; return STATUS_SUCCESS;