Project Home
Project Home
Documents
Documents
Wiki
Wiki
Discussion Forums
Discussions
Project Information
Project Info
Forum Topic - Device Locks and Unblocking: (3 Items)
   
Device Locks and Unblocking  
Hi...

We're currently designing a Resource Manager that offers two devices to the user.
Each of the devices can be opened by multiple processes.
The data pumped into the devices is passed down via a serial bus.

The idea is that the user locks his device, does some operations, then unlocks it again.
In case the device is already locked, the lock() syscall blocks until the lock is aquired (or it returns right away, in 
case the device was opened non-blocking).

Now if the device is locked, another client who tries to lock it will block. Now if you try to stop that blocked client,
 for example via Ctrl-C, it won't work, because it is blocked in the MsgSend inside the call responsible for locking the
 device. So we need to implement an unblock handler in the Resource Manager.

Currently the blocking is implemented via waiting on a condvar in the ResMgr. Obviously, this is a problem for 
implementing an unblock handler.

How is this usually solved? Obviously I want a performant solution, i.e. the client should really block, not repeatedly 
poll. I could do the blocking in the ResMgr by means of building a blocking table that contains a list of the remaining 
to-do MsgReplys. But then it would be up to me to decide which client is to aquire the lock next, and I think this is 
best left to the Scheduler (e.g. by using a mutex).

Any suggestions?

Greetings,
 Marc
Re: Device Locks and Unblocking  
Hi Marc,

as I understand your explanation, you currently block on the condvar within your resmgr's lock-handler? That indeed 
makes it hard to handle unblocking.

Usually, if you want to handle blocking clients, you'll implement a list of currently blocked clients. You add any 
client about to be blocked to this list and return from the io-function right away, with a return value of 
_RESMGR_NOREPLY.

When the blocking request can be serviced, you remove the client from the list and do a MsgReply() to it, maybe handing 
over any reply data acquired.

If an unblock request arrives for a client, you remove it from the list, remove all associated ressources and pending 
actions, and, e.g.,  return EINTR.

Hope this helps,
- Thomas
Re: Device Locks and Unblocking  
Thomas, exactly.

Of course I can do this via a list of blocked clients that I maintain in the ResMgr.
But in this case, how do I decide which client to unblock? Easiest would be to just unblock the next client in the list,
 but I think this should be a scheduler decision.

This is why we had chosen to block on a condvar: because then it is up to the scheduler to decide which thread to 
unblock. But now it turns out that unblocking then becomes a problem when using condvars/blocking ResMgr threads...

Is something similar (i.e. scheduler decides which thread to unblock) possible with a list of blocked clients? I suppose
 there is a standard way to do this, as I'd expect this problem to pop up quite often?

Greetings,
 Marc