I know that locking is generally discouraged unless absolutely
necessary, and in our driver I have tried to keep locking to a
minimum, only using it where serialization is really needed.
However, recently a new synchronization issue has come up.
We have a framework used by our user apps to communicate with the
device. The framework has an Init function which makes calls like so:
do some stuff
call driver (via userclient)
do more stuff
call driver
more stuff
call driver
The problem is that all the stuff in between the driver calls needs
to be serialized across user process instances (instances of our
framework), so the entire Init function needs to be serialized, not
just the driver calls. In the past the Init calls have never
happened from multiple processes simultaneously, since you typically
launch one app, then the other etc. But we now have a situation
where multiple running apps can need to reinitialize things at the
same time, and so this problem has shown up.
So I did something that makes me cringe. I added this at the
beginning and end of the Init function:
call driver to acquire lock
[do everything else]
call driver to release lock
This uses IOLockLock/Unlock. Something about this seems really
wrong... but it seems to work. Suppose process A loads the framework
and calls Init. It acquires the lock and starts doing other stuff.
Process B then loads, and process A gets switched out before
finishing Init. Process B calls Init, and when it tries to acquire
the lock it looks like it just blocks. Process A gets switched back
in, finishes its stuff, and releases the lock, so now process B is
unblocked.
So my question is, is this safe? Is there a better way to do this?
I have control over all the clients that will ever call this
function, so I can guarantee that any necessary cleanup will happen
(i.e. if the process crashes in Init while holding the lock that it
will release it etc.).
Thanks,
Jim
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Darwin-drivers mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
http://lists.apple.com/mailman/options/darwin-drivers/email@hidden