[haiku-development] correct way to acquire_sem_etc()

  • From: Stephan Assmus <superstippi@xxxxxx>
  • To: haiku-development@xxxxxxxxxxxxx
  • Date: Wed, 29 Oct 2008 22:34:27 +0100

Hi,

could someone help me with an acquire_sem_etc() problem? I am unsure about 
the B_CAN_INTERRUPT flag and handling B_INTERRUPTED as the error code.

When exactly do I want acquire_sem_etc() to be interruptable, and who can 
interrupt me?

As for the specific problem I am trying to fix: In the usb_hid driver, 
Michael and I use a USB interrupt transfer callback semaphore. In other 
words, the thread that does ioctl() on the driver schedules an USB 
interrupt transfer, then it blocks on a semaphore which has been created 
like this:

fTransferNotifySem = create_sem(0, "hid device transfer notify sem");

and the acquire_sem_etc() call looks like this:

tatus_t result = acquire_sem_etc(fTransferNotifySem, 1,
        B_CAN_INTERRUPT | B_RELATIVE_TIMEOUT, someTimeout);

The USB stack will call a hook when the transfer has taken place, put the 
transfered data somewhere safe and release the semaphore.

In the ioctl() the result is checked for several possible errors, among 
which is B_INTERRUPTED. All this happens in a while loop and in the 
B_INTERRUPTED case, the thread will simply go back into the loop.

Now I have learned that killing the thread that does the ioctl() will make 
it busy loop, because B_INTERRUPTED is returned from acquire_sem_etc().

How is this done the correct way?

Thanks a lot and best regards,
-Stephan

Other related posts: