PDA

View Full Version : Critical section coding with OSX 10.6




multinode
Aug 30, 2013, 01:00 AM
I have N SDCards connected to my MacBook via USB. This is an OSX 10.6 MacBook business app with as many as N customers randomly inserting SDCards. My app recognizes each insertion with a Disk Arbitration DiskAppearedCallback and that callback processes the information in a file on the card and determines the USB locationID to identify which customer did the mount.

My question is how to have the multiple callbacks be processed SEQUENTIALLY, i.e. I don't want any callback to be interrupted by another mount callback. I think that this is a critical section problem ... locks, semaphores, etc.

Does anybody here have any suggestions?



gnasher729
Aug 30, 2013, 03:02 AM
I have N SDCards connected to my MacBook via USB. This is an OSX 10.6 MacBook business app with as many as N customers randomly inserting SDCards. My app recognizes each insertion with a Disk Arbitration DiskAppearedCallback and that callback processes the information in a file on the card and determines the USB locationID to identify which customer did the mount.

My question is how to have the multiple callbacks be processed SEQUENTIALLY, i.e. I don't want any callback to be interrupted by another mount callback. I think that this is a critical section problem ... locks, semaphores, etc.

Does anybody here have any suggestions?

Enter the debugger. Set a breakpoint on the callback. Is it called on the main thread or on a background thread? Better yet, check the documentation. What does it say? Main thread is single threaded.

multinode
Aug 30, 2013, 10:35 AM
Enter the debugger. Set a breakpoint on the callback. Is it called on the main thread or on a background thread? Better yet, check the documentation. What does it say? Main thread is single threaded.

Thanks GNASHER. I'm not certain that this is a multithreading issue??? I usually think of multithreading as the processor giving each thread a quantum of time one after the other. In my case, I'm thinking that ONE thread is doing all the mount processing, BUT while one mount callback is executing the callback code block, another mount callback cannot interrupt to enter that same code block. I think this is a matter of locking the code block to prevent non lock holders from entering.

I don't understand your statement "main thread is single threaded" ... please elaborate. Your statement seems to imply that a DA notification spawns a new thread and that execution in the main thread doesn't allow that. If that is what you mean, then that begs the question of how any DA notification can execute. Again, I'm not sure that my issue is about multi threading.

multinode
Aug 30, 2013, 10:59 AM
Hello GNASHER ...

I have an idea ... maybe I do want multiple threads. Maybe each time a DA Disk Appears notification happens the callback should explicitly spawn a new thread and then call the actual mount processing code to execute in the new (child) thread (HOW?). The beginning of the actual mount processing code could spin on a lock which would get released whenever a player completes/exits the execution of the mount processing code.

Does this make sense? Did you understand me?

robbieduncan
Aug 30, 2013, 11:35 AM
Sounds like a job for Grand Central Dispatch. If you want to ensure that the dispatched events (mounts) are handled one at a time in the order they happened dispatch against your own queue that you create with DISPATCH_QUEUE_SERIAL

multinode
Aug 30, 2013, 11:50 AM
Sounds like a job for Grand Central Dispatch. If you want to ensure that the dispatched events (mounts) are handled one at a time in the order they happened dispatch against your own queue that you create with DISPATCH_QUEUE_SERIAL

Yeah ... I was thinking about queues and GCD too. In other words, the DA callback should go to some intermediate code ... queues, locks, etc. ... which would then go to the actual mount processing code.

Thanx.