tunu
tunu

Reputation: 72

Are semaphore functions down() and __down() same?

In the down function of semaphore code, are the two functions down(struct semaphore *sem) at line 53 and __down(sem) at line 61.

In the else part are we calling the down at line 53 in a recursive manner.

void down(struct semaphore *sem)
{
      unsigned long flags;

      raw_spin_lock_irqsave(&sem->lock, flags);
      if (likely(sem->count > 0))
              sem->count--;
      else
              __down(sem);
     raw_spin_unlock_irqrestore(&sem->lock, flags);
}
EXPORT_SYMBOL(down);

Link to semaphore.c

Upvotes: 1

Views: 1053

Answers (1)

Ortwin Angermeier
Ortwin Angermeier

Reputation: 6203

Are semaphore functions down() and __down() same?

No they are not. Functions with a __ prefix are for internal use and should generally not be used in your code.

Taking a short look at the code reveals that down() is a function calling __down() with a spinlock, protecting the count part of the struct semaphore.

The function __down() is only called if the smaphore's count is <= 0, __down() delegates to __down_common(), that handles the waiting tasks.

So you will never want to call __down() directly, if you do so, you will most likely create bugs.

Upvotes: 1

Related Questions