Mutant as synchronization object

Dear forum members,

Sorry for absolute offtopic, but can you
tell me ? Am I able to use KMUTANT for
synchronization between driver and user
mode application ? If I can what API and
naming should I use ?

Thanks in advance.

Best regards, Vladimir.

What particular task do you want to solve?

Maxim Shatskih, Windows DDK MVP
StorageCraft Corporation
xxxxx@storagecraft.com
http://www.storagecraft.com

----- Original Message -----
From: “Vladimir Magaziy”
To: “Windows System Software Devs Interest List”
Sent: Wednesday, December 17, 2003 12:24 PM
Subject: [ntdev] Mutant as synchronization object

> Dear forum members,
>
> Sorry for absolute offtopic, but can you
> tell me ? Am I able to use KMUTANT for
> synchronization between driver and user
> mode application ? If I can what API and
> naming should I use ?
>
> Thanks in advance.
>
> Best regards, Vladimir. й-늉 X!y -➮wz@Aa 0 ,w rjbrWq (r z{e ˛ mm{]z fo ځ+i
r 칻&ޱ iZGj)mW ׯ]w ± ,(
>

Dear Maxim,

Thanks for your answer and question.

The task I want to solve is not particular
moreover it’s very simple and primitive.

I just have to inform my user mode thread
to read information at the moment from kernel mode.
I uses classical event notification, but every time
I am not assured against repeated requests and
have to store information about last notification.
I guess I won’t have such probmes using syncronization
objects I can signale from user mode.
And during “Using sync events rather than spinlocks”
discussion Don Born wrote he prefer to use mutexes but
when I tried to find something like “KeCreateMutant”
I found nothing and decided to ask you about it.

Best regards, Vladimir.

----- Original Message -----
From: “Maxim S. Shatskih”
To: “Windows System Software Devs Interest List”
Sent: Wednesday, December 17, 2003 2:15 PM
Subject: [ntdev] Re: Mutant as synchronization object

What particular task do you want to solve?

Maxim Shatskih, Windows DDK MVP
StorageCraft Corporation
xxxxx@storagecraft.com
http://www.storagecraft.com

It sounds to me like your particular problem would be better solved by
the app using ReadFile asynchronously, pending the read IRP in the
driver if no data is available, and then completing the request when the
data becomes available.

A mutex doesn’t help the situation unless you’re trying to protect
access to memory shared between the driver and app, which is definitely
not “very simple and primitive”. Even there, there are almost always
better ways to deal with that problem (e.g. the use of “one-writer,
multiple-readers” synchronization methods such as filling the buffer
with information containing a length component before updating the
“buffer end” pointer).

Vladimir Magaziy wrote:

Dear Maxim,

Thanks for your answer and question.

The task I want to solve is not particular
moreover it’s very simple and primitive.

I just have to inform my user mode thread
to read information at the moment from kernel mode.
I uses classical event notification, but every time
I am not assured against repeated requests and
have to store information about last notification.
I guess I won’t have such probmes using syncronization
objects I can signale from user mode.
And during “Using sync events rather than spinlocks”
discussion Don Born wrote he prefer to use mutexes but
when I tried to find something like “KeCreateMutant”
I found nothing and decided to ask you about it.

Best regards, Vladimir.

----- Original Message -----
From: “Maxim S. Shatskih”
> To: “Windows System Software Devs Interest List”
> Sent: Wednesday, December 17, 2003 2:15 PM
> Subject: [ntdev] Re: Mutant as synchronization object
>
>
> What particular task do you want to solve?
>
> Maxim Shatskih, Windows DDK MVP
> StorageCraft Corporation
> xxxxx@storagecraft.com
> http://www.storagecraft.com


…/ray..

Dear Ray,

I am very grateful to you for your advice,
but I don’t know how can I realize it. Anyway,
I guess I have to learn interaction mechanisms
more deeply.

You’ve said that mutexes are used to protect
shared memory. I have to say I have never heard
how is it possible to do it and why mutexes, are
they special ?
Would you say a couple of words about it ?

Best regards, Vladimir.

----- Original Message -----
From: “Ray Trent”
Newsgroups: ntdev
To: “Windows System Software Devs Interest List”
Sent: Wednesday, December 17, 2003 8:37 PM
Subject: [ntdev] Re: Mutant as synchronization object

> It sounds to me like your particular problem would be better solved by
> the app using ReadFile asynchronously, pending the read IRP in the
> driver if no data is available, and then completing the request when the
> data becomes available.
>
> A mutex doesn’t help the situation unless you’re trying to protect
> access to memory shared between the driver and app, which is definitely
> not “very simple and primitive”. Even there, there are almost always
> better ways to deal with that problem (e.g. the use of “one-writer,
> multiple-readers” synchronization methods such as filling the buffer
> with information containing a length component before updating the
> “buffer end” pointer).

Then send lots of overlapped reads from this thread, they will be completed
one by one when the notifications will arrive.

Maxim Shatskih, Windows DDK MVP
StorageCraft Corporation
xxxxx@storagecraft.com
http://www.storagecraft.com

----- Original Message -----
From: “Vladimir Magaziy”
To: “Windows System Software Devs Interest List”
Sent: Wednesday, December 17, 2003 8:55 PM
Subject: [ntdev] Re: Mutant as synchronization object

> Dear Maxim,
>
> Thanks for your answer and question.
>
> The task I want to solve is not particular
> moreover it’s very simple and primitive.
>
> I just have to inform my user mode thread
> to read information at the moment from kernel mode.
> I uses classical event notification, but every time
> I am not assured against repeated requests and
> have to store information about last notification.
> I guess I won’t have such probmes using syncronization
> objects I can signale from user mode.
> And during “Using sync events rather than spinlocks”
> discussion Don Born wrote he prefer to use mutexes but
> when I tried to find something like “KeCreateMutant”
> I found nothing and decided to ask you about it.
>
> Best regards, Vladimir.
>
> ----- Original Message -----
> From: “Maxim S. Shatskih”
> To: “Windows System Software Devs Interest List”
> Sent: Wednesday, December 17, 2003 2:15 PM
> Subject: [ntdev] Re: Mutant as synchronization object
>
>
> What particular task do you want to solve?
>
> Maxim Shatskih, Windows DDK MVP
> StorageCraft Corporation
> xxxxx@storagecraft.com
> http://www.storagecraft.com й-늉 X!y -➮wz@Aa 0 ,w rjbrWq (r z{e ˛ mm{]z fo
ځ+i r 칻&ޱ iZGj)mW ׯ]w ± ,(
>

Not sure what you’re asking… but here goes (sorry if this is too
basic): Mutexes/mutants, critical sections, spinlocks, semaphores, and
other similar primitives are used to protect access to a shared resource
by multiple threads/processes. I tend to refer to this whole group of
objects as “locks”. Each of them has advantages and disadvantages and
are used in various different situations. Normally, the 2 threads
accessing a resource would do something like “Lock(); ;
Unlock();”. Since only 1 thread can “hold” the lock at a time, you avoid
conflicts in accessing the resource. However, since a thread will block
if until the lock is available, you also open up the possibility of
deadlocks where both threads are waiting for a lock that the other one has.

The reason I said that a mutex would only help in this case if you were
using shared memory to communicate between an app and a driver is that
memory is one of the very few “resources” you can actually share between
apps and drivers (though it’s complicated even at that). If all you’re
trying to do is signal the app, waiting on an event is no different from
waiting on a mutex or other lock, except that it’s much safer (harder to
introduce bugs that hang the system).

As for the app sending read requests and pending them in the driver
until your data arrives… any decent driver book would explain this
process. Usually people here recommend Walter Oney’s book.

Vladimir Magaziy wrote:

> Dear Ray,
>
> I am very grateful to you for your advice,
> but I don’t know how can I realize it. Anyway,
> I guess I have to learn interaction mechanisms
> more deeply.
>
> You’ve said that mutexes are used to protect
> shared memory. I have to say I have never heard
> how is it possible to do it and why mutexes, are
> they special ?
> Would you say a couple of words about it ?
>
> Best regards, Vladimir.
>
> ----- Original Message -----
> From: “Ray Trent”
> Newsgroups: ntdev
> To: “Windows System Software Devs Interest List”
> Sent: Wednesday, December 17, 2003 8:37 PM
> Subject: [ntdev] Re: Mutant as synchronization object
>
>
>
>>It sounds to me like your particular problem would be better solved by
>>the app using ReadFile asynchronously, pending the read IRP in the
>>driver if no data is available, and then completing the request when the
>>data becomes available.
>>
>>A mutex doesn’t help the situation unless you’re trying to protect
>>access to memory shared between the driver and app, which is definitely
>>not “very simple and primitive”. Even there, there are almost always
>>better ways to deal with that problem (e.g. the use of “one-writer,
>>multiple-readers” synchronization methods such as filling the buffer
>>with information containing a length component before updating the
>>“buffer end” pointer).


…/ray..

Well, the original poster needs to:

  • send some messages with some data from kernel to user when some events
    occurs in the kernel.

First of all, there is RegisterDeviceNotification(user) +
IoReportTargetDeviceChange(kernel).
The advantage: probably easiest to code.
The drawback: PnP PDO is necessary, not so good for FSDs (though the bottom
of Vpb->RealDevice stack is a PDO. For non-removable media, RealDevice is a PDO
itself, and, BTW, filesystems send events using this mechanism about volume
mounts and media change). Also requires either being a service - or a HWND.

Second: WMI events (user) + WMIFireEvent (kernel).
The advantage: events can be caught by scripts and .NET code easily.
The drawback: the unmanaged user code requires messy non-trivial C++/COM
stuff.

Third: pend the overlapped IRP (or lots of IRPs, and replenish the queue to
be not so drained automatically) to a file handle, then call
GetQueuedCompletionStatus. The driver must queue the IRP, and, when the event
occurs, fill the IRP with data and complete it.
The advantage: fastest, the only way for bulk amounts of data. Sound stack
and sockets work such.
The drawback: lots of logic-rich code to write (cancellation etc), though
without “syntax LSD”.

The original poster can choose one. How mutants are connected to all of
this - I cannot understand.

Maxim Shatskih, Windows DDK MVP
StorageCraft Corporation
xxxxx@storagecraft.com
http://www.storagecraft.com

----- Original Message -----
From: “Ray Trent”
Newsgroups: ntdev
To: “Windows System Software Devs Interest List”
Sent: Thursday, December 18, 2003 1:28 AM
Subject: [ntdev] Re: Mutant as synchronization object

> Not sure what you’re asking… but here goes (sorry if this is too

You have to use ‘KeInitializeMutex’.
Use below mentioned functions for your other implementations.
'KeReleaseMutex, KeWaitForMultipleObjects, KeWaitForMutexObject, KeWaitForSingleObject ’

Regards
saravanan

-----Original Message-----
From: xxxxx@lists.osr.com [mailto:xxxxx@lists.osr.com] On Behalf Of Vladimir Magaziy
Sent: Wednesday, December 17, 2003 11:26 PM
To: Windows System Software Devs Interest List
Subject: [ntdev] Re: Mutant as synchronization object

Dear Maxim,

Thanks for your answer and question.

The task I want to solve is not particular
moreover it’s very simple and primitive.

I just have to inform my user mode thread
to read information at the moment from kernel mode.
I uses classical event notification, but every time
I am not assured against repeated requests and
have to store information about last notification.
I guess I won’t have such probmes using syncronization
objects I can signale from user mode.
And during “Using sync events rather than spinlocks”
discussion Don Born wrote he prefer to use mutexes but
when I tried to find something like “KeCreateMutant”
I found nothing and decided to ask you about it.

Best regards, Vladimir.

----- Original Message -----
From: “Maxim S. Shatskih”
To: “Windows System Software Devs Interest List”
Sent: Wednesday, December 17, 2003 2:15 PM
Subject: [ntdev] Re: Mutant as synchronization object

What particular task do you want to solve?

Maxim Shatskih, Windows DDK MVP
StorageCraft Corporation
xxxxx@storagecraft.com
http://www.storagecraft.comB笶*'X!y-➮wz@Aa0Xy&^qt
jٲry۞׽jjvm*Nrzujyj׍I۲

Dear saravanan,

Thanks for your reply. And sorry for my delay.

I guess I don’t. Actually, I can use this API to create
some unnamed mutex and use it to provide
synchronization INSIDE KMD, but I have to provide
interaction between APP and KMD :frowning:

Best regards, Vladimir.

----- Original Message -----
From:
To: “Windows System Software Devs Interest List”
Sent: Thursday, December 18, 2003 6:02 AM
Subject: [ntdev] Re: Mutant as synchronization object

> You have to use ‘KeInitializeMutex’.
> Use below mentioned functions for your other implementations.
> 'KeReleaseMutex, KeWaitForMultipleObjects, KeWaitForMutexObject, KeWaitForSingleObject '
>
> Regards
> saravanan

Dear Ray,

I MUST thank you for your answer and your time
you have spent answering me in full (maybe it’s
basic, but it’s clearly) and please sorry me for my
delay.

You know I’ve got at the last that there are no
striking differences between lock" objects I am using. And I guess the main reason in this case is a result. And events -- they are useful because they are so lightweight".

You know, it’s strangely for me that there are no
documented APIs for using mutexes as for
instance events. How do you think why is it so ?
I can’t see what Microsoft can change here.

I’ve heard that Walter Oney’s “Programming the (M)WDM”
is perfect book. I will read it necessarily.

My best regards, Vladimir.

----- Original Message -----
From: “Ray Trent”
Newsgroups: ntdev
To: “Windows System Software Devs Interest List”
Sent: Thursday, December 18, 2003 12:28 AM
Subject: [ntdev] Re: Mutant as synchronization object

> Not sure what you’re asking… but here goes (sorry if this is too
> basic): Mutexes/mutants, critical sections, spinlocks, semaphores, and
> other similar primitives are used to protect access to a shared resource
> by multiple threads/processes. I tend to refer to this whole group of
> objects as “locks”. Each of them has advantages and disadvantages and
> are used in various different situations. Normally, the 2 threads
> accessing a resource would do something like “Lock(); ;
> Unlock();”. Since only 1 thread can “hold” the lock at a time, you avoid
> conflicts in accessing the resource. However, since a thread will block
> if until the lock is available, you also open up the possibility of
> deadlocks where both threads are waiting for a lock that the other one has.
>
> The reason I said that a mutex would only help in this case if you were
> using shared memory to communicate between an app and a driver is that
> memory is one of the very few “resources” you can actually share between
> apps and drivers (though it’s complicated even at that). If all you’re
> trying to do is signal the app, waiting on an event is no different from
> waiting on a mutex or other lock, except that it’s much safer (harder to
> introduce bugs that hang the system).
>
> As for the app sending read requests and pending them in the driver
> until your data arrives… any decent driver book would explain this
> process. Usually people here recommend Walter Oney’s book.