which lock should I use ?

Hi,

I am in a situation that I have to use a lock as everyone does. The lock should be enabled recursively but this function can be called in any IRQL( <= Dispatch ). What kind of lock is best for this?

I have read some papers and am looking at the MSFT doc as well as this forum and some questions on KMUTEX.

In MSFT help file ( http://msdn.microsoft.com/en-us/library/ff553344(VS.85).aspx ) it explains that this mutex can be used in Dispatch level, but in this forum I can see that Mutex is running on ( <= APC ). Which one is right ? or both are right ?

So go back to original question, I was originally thinking Mutex for the resolution but if Mutex can only run <= APC it wouldn’t solve the problem.

Any suggestion?

Thanks,
SAM

You need to read the fine print at the bottom of that link:

Callers of KeWaitForMutexObject must be running at IRQL <=
DISPATCH_LEVEL. However, if Timeout = NULL or *Timeout != 0, the caller must
be running at IRQL <= APC_LEVEL and in a nonarbitrary thread context. (If
Timeout != NULL and *Timeout = 0, the caller must be running at IRQL <=
DISPATCH_LEVEL.)

For general background/theory information, you might want to take a look at
this excellent paper:
http://www.microsoft.com/whdc/driver/kernel/locks.mspx, after first reading
this one that describes background material:
http://www.microsoft.com/whdc/driver/kernel/IRQL.mspx.

Good luck,

mm
-----Original Message-----
From: xxxxx@lists.osr.com
[mailto:xxxxx@lists.osr.com] On Behalf Of xxxxx@hotmail.com
Sent: Wednesday, February 16, 2011 3:12 AM
To: Windows System Software Devs Interest List
Subject: [ntdev] which lock should I use ?

Hi,

I am in a situation that I have to use a lock as everyone does. The lock
should be enabled recursively but this function can be called in any IRQL(
<= Dispatch ). What kind of lock is best for this?

I have read some papers and am looking at the MSFT doc as well as this forum
and some questions on KMUTEX.

In MSFT help file (
http://msdn.microsoft.com/en-us/library/ff553344(VS.85).aspx ) it explains
that this mutex can be used in Dispatch level, but in this forum I can see
that Mutex is running on ( <= APC ). Which one is right ? or both are right
?

So go back to original question, I was originally thinking Mutex for the
resolution but if Mutex can only run <= APC it wouldn’t solve the problem.

Any suggestion?

Thanks,
SAM


NTDEV is sponsored by OSR

For our schedule of WDF, WDM, debugging and other seminars visit:
http://www.osr.com/seminars

To unsubscribe, visit the List Server section of OSR Online at
http://www.osronline.com/page.cfm?name=ListServer

Generally speaking, the only recursively acquirable lock is a KMUTEX. I would strongly suggest that you reconsider the recursive acquisition requirement and pass down the state of the lock being held to the functions you call instead of requiring recursive acquisition. This lets you use a spin lock and in the end, I think the code is more clear about what is expected of the caller’s state.

d

-----Original Message-----
From: xxxxx@lists.osr.com [mailto:xxxxx@lists.osr.com] On Behalf Of Martin O’Brien
Sent: Wednesday, February 16, 2011 12:31 AM
To: Windows System Software Devs Interest List
Subject: RE: [ntdev] which lock should I use ?

You need to read the fine print at the bottom of that link:

Callers of KeWaitForMutexObject must be running at IRQL <= DISPATCH_LEVEL. However, if Timeout = NULL or *Timeout != 0, the caller must
be running at IRQL <= APC_LEVEL and in a nonarbitrary thread context. (If
Timeout != NULL and *Timeout = 0, the caller must be running at IRQL <=
DISPATCH_LEVEL.)

For general background/theory information, you might want to take a look at this excellent paper:
http://www.microsoft.com/whdc/driver/kernel/locks.mspx, after first reading this one that describes background material:
http://www.microsoft.com/whdc/driver/kernel/IRQL.mspx.

Good luck,

mm
-----Original Message-----
From: xxxxx@lists.osr.com
[mailto:xxxxx@lists.osr.com] On Behalf Of xxxxx@hotmail.com
Sent: Wednesday, February 16, 2011 3:12 AM
To: Windows System Software Devs Interest List
Subject: [ntdev] which lock should I use ?

Hi,

I am in a situation that I have to use a lock as everyone does. The lock should be enabled recursively but this function can be called in any IRQL( <= Dispatch ). What kind of lock is best for this?

I have read some papers and am looking at the MSFT doc as well as this forum and some questions on KMUTEX.

In MSFT help file (
http://msdn.microsoft.com/en-us/library/ff553344(VS.85).aspx ) it explains that this mutex can be used in Dispatch level, but in this forum I can see that Mutex is running on ( <= APC ). Which one is right ? or both are right ?

So go back to original question, I was originally thinking Mutex for the resolution but if Mutex can only run <= APC it wouldn’t solve the problem.

Any suggestion?

Thanks,
SAM


NTDEV is sponsored by OSR

For our schedule of WDF, WDM, debugging and other seminars visit:
http://www.osr.com/seminars

To unsubscribe, visit the List Server section of OSR Online at http://www.osronline.com/page.cfm?name=ListServer


NTDEV is sponsored by OSR

For our schedule of WDF, WDM, debugging and other seminars visit:
http://www.osr.com/seminars

To unsubscribe, visit the List Server section of OSR Online at http://www.osronline.com/page.cfm?name=ListServer

As Doron suggested getting rid of the recursive lock is a good idea.
Doron suggested using a flag, personally I look at a lot of those cases
with flags and find I can restructure the code so I take the lock in a
routine that calls the code that may be recursive. So take a good look
at your code structure, the problem can likely be simplified.

Don Burn (MVP, Windows DKD)
Windows Filesystem and Driver Consulting
Website: http://www.windrvr.com
Blog: http://msmvps.com/blogs/WinDrvr

xxxxx@hotmail.com” wrote in message
news:xxxxx@ntdev:

> Hi,
>
> I am in a situation that I have to use a lock as everyone does. The lock should be enabled recursively but this function can be called in any IRQL( <= Dispatch ). What kind of lock is best for this?
>
> I have read some papers and am looking at the MSFT doc as well as this forum and some questions on KMUTEX.
>
> In MSFT help file ( http://msdn.microsoft.com/en-us/library/ff553344(VS.85).aspx ) it explains that this mutex can be used in Dispatch level, but in this forum I can see that Mutex is running on ( <= APC ). Which one is right ? or both are right ?
>
> So go back to original question, I was originally thinking Mutex for the resolution but if Mutex can only run <= APC it wouldn’t solve the problem.
>
> Any suggestion?
>
> Thanks,
> SAM