dow_interruptible () function returns the source code and analysis

down_interruptible () is a function of the amount of processed signal. His return value in three 1. "0" 2. "-ETIME" 3. "- EINTR"

0 represents a normal return

-ETIME wait timeout

-EINTR break

 

Works function:

If sem-> count> 0 (to allow access to the semaphore)         

Return 0 (normal return)

Otherwise, wait.

Kernel source code for

Click (here) folded or unfolded

  1. int down_interruptible(struct semaphore *sem)
  2. {
  3.  unsigned long flags;
  4.  int result = 0;
  5.  
  6.  spin_lock_irqsave (& sem-> lock, flags); // semaphore spinlock
  7.  if (likely(sem->count > 0))
  8.   sem->count--;
  9.  else
  10.   result = __down_interruptible (sem); // wait to enter
  11.  spin_unlock_irqrestore(&sem->lock, flags);
  12.  
  13.  return result;
  14. }

__down_interruptible()

Call __down_common ()

__down_common () has a for loop

-EINTR returned when an interrupt signal to return -ETIME while waiting for a timeout.

Return 0 become accessible semaphore

Incidentally, the amount of the signal are different, need to be distinguished.

 

Image Description:

You come home from school, hungry, want to eat, went to the kitchen to see if there is nothing to eat (eat semaphore), if there are ready-made food (signal is not occupied), you immediately go to eat (return 0) If mom is doing (semaphores in use), you say that after he woke with mom said to do good, you will first go to bed (sleep), while the room has an alarm clock (other signals), if rice when not do, alarm clock rang, you woke up (return -EINTR), or else wait until my mom finally wake you up good food (also returns 0), the timeout status is not taken into account.

Released nine original articles · won praise 1 · views 6705

Guess you like

Origin blog.csdn.net/u014426028/article/details/96998630