????(3)??????????????????????л??????????????1????????????????????????
???????????V????
void up(struct semaphore *sem);
/**
* up - release the semaphore
* @sem: the semaphore to release
*
* Release the semaphore.  Unlike mutexes?? up() may be called from any
* context and even by tasks which have never called down().
*/
void up(struct semaphore *sem)
{
unsigned long flags;
spin_lock_irqsave(&sem->lock?? flags);
if (likely(list_empty(&sem->wait_list)))
sem->count++;
else
__up(sem);
spin_unlock_irqrestore(&sem->lock?? flags);
}
????????????????????????????????????????????軔count++???ɡ?????????????????????????????????????????__up.
????__up????壺
????static noinline void __sched __up(struct semaphore *sem)
????{
????struct semaphore_waiter *waiter = list_first_entry(&sem->wait_list??    struct semaphore_waiter?? list);
????list_del(&waiter->list);
????waiter->up = 1;
????wake_up_process(waiter->task);
????}
?????????????????sem?????wait_list?????????????????Ч????????????н?????????????y????????????
???????????????sem??????down_interruptible???????????sem??wait_list????β????????μ???????sem??????up??????????????wait_list?????е???????Ч??????????????y?????????
??????????
???????????????????

????1???????????????????
?????????????????????????????????????????????????????????????????????????????????y????????????е?????????????????????á?????????????????“????-???-????”?????????????磬???????????????????????????????-??????????
????2????????????????????
?????????????????????????????????????????????????????????????????????Щ?????????????????????????????????????????????????“????-????”????????????????????????????????????????????????????????????????????????????????????????
????????????????????????????????????????????????????????
???????????????????????????????????
???????????????????????????????????Σ???????Ρ?
?????????????????????????????????????????????????????????????????????????м?飬??????????????????????????????????????????????????????????????
????Linux 2.6.26??mutex????壺
struct mutex {
/* 1: unlocked?? 0: locked?? negative: locked?? possible waiters */
atomic_t                  count;//?????????????
spinlock_t                wait_lock;//?????????????
struct list_head          wait_list;
#ifdef CONFIG_DEBUG_MUTEXES
struct thread_info        *owner;
const char                *name;
void                      *magic;
#endif
#ifdef CONFIG_DEBUG_LOCK_ALLOC
struct lockdep_map         dep_map;
#endif
};
???????????struct semaphore??struct mutex????????????????debug???????????????semaphore?????????????????mutex????????????????????????????????????????????????????С?
?????????????????count=1??semaphore?????????????DECLARE_MUTEX(name)??DECLARE_MUTEX(name)?????????????semaphore?????????????????????????P??V????.
??????????????????mutex?????????????DEFINE_MUTEX
???????????????????????????mutex?????????????mutex_init??mutex????mutex_init???????ú??????????????__mutex_init??????
#define mutex_init(mutex)                                                  
do {                                                                       
static struct lock_class_key __key;                                

__mutex_init((mutex)?? #mutex?? &__key);                             
} while (0)
????__mutex_init???????£?
/***
* mutex_init - initialize the mutex
* @lock: the mutex to be initialized
*
* Initialize the mutex to unlocked state.
*
* It is not allowed to initialize an already locked mutex.
*/
void
__mutex_init(struct mutex *lock?? const char *name?? struct lock_class_key *key)
{
atomic_set(&lock->count?? 1);
spin_lock_init(&lock->wait_lock);
INIT_LIST_HEAD(&lock->wait_list);
debug_mutex_init(lock?? name?? key);
}
??????__mutex_init??????????????????mutex_init????????????mutex?????????????mutex????????mutex???P??V??????void mutex_lock(struct mutex *lock)??void __sched mutex_unlock(struct mutex *lock)??????????mutex???????count=1????μ?semaphore????????PV??????ú?semaphore??????????????????Linux???????????????????????????????????????down_interruptible??up???????ARM????mutex_lock?????????????mutex_lock?????????????fast path??slow path??????????????????????????????????£???????????????????????????á?????Linux????????????????ARM ??
??????????
??????????????????????????????????????????????????????????????????????????á???????????????????????????κ?????????????????????????κ?????????????е?????????????????????????????????????????????????????????????????????????sleep???????????????????????????sleep?????????????????????е????????????????????????????????????????????????
????????????????
??????????????????????е??????????????????????????????????????????????????????????????????????????????????????κ???е???????????????????????????????????????????????б????????????????????????????????????????????????????????????????????????????????????????????????????????ε?????????????????????????????????????????cpu?????
?????????????????
??????????????????????????????????????????????????????????????????????????????????????????????????????????????????Ч??????????????????????д????????????????????????????????μ?????sleep?????????????????????á???????????????????????????????????????κ?????????á???????????????????????????????????????????????ù?????????????????????????????????????????????????????????????????????????????ж?????????????????????ж????????????????ж??????????????????????????????????????Ч?????????????д????????????????????????????????????????????SMP????????????????2?????????????CPU??????????????£????????????в????????????????????????????????????????á?
??????????????弰???API
????????????????????Linux/Spinlock.h??
typedef struct spinlock {
union { //????
struct raw_spinlock rlock;
#ifdef CONFIG_DEBUG_LOCK_ALLOC
# define LOCK_PADSIZE (offsetof(struct raw_spinlock?? dep_map))
struct{
u8 __padding[LOCK_PADSIZE];
struct lockdep_map dep_map;
};
#endif
};
} spinlock_t;
???????????????????
????spinlock_t my_lock = SPIN_LOCK_UNLOCKED;
????void spin_lock_init(spinlock_t *lock);
????????????????
//?????????????????
void spin_lock(spinlock_t *lock);                                   //??????????????
void spin_lock_irq(spinlock_t *lock);                               //????????ж??????????
void spin_lock_irqsave(spinlock_t *lock?? unsigned long flags);      //???汾???ж??????????????ж??????????????
void spin_lock_bh(spinlock_t *lock)                                 //?????????????? ?????????????ж??????
//????????????????
void spin_unlock(spinlock_t *lock);                                 //??????????
void spin_unlock_irq(spinlock_t *lock);                             //?????????????????????ж?
void spin_unlock_irqrestore(spinlock_t *lock?? unsigned long flags); //????????????????????ж????????????
void spin_unlock_bh(spinlock_t *lock);                              //?????spin_lock_bh
//????????
int spin_trylock(spinlock_t *lock);                  //????????????????????????????????????????÷?????????????????0???
//??????????????????????????????????????????????0.
int spin_trylock_bh(spinlock_t *lock);
//??Щ???????????????( ??????? )?? ???? 0. ???"try"?汾??????ж?.
//????
int spin_is_locked(spinlock_t *lock);               //??try_lock()????
???????????????????????????????
????????????????????????sleep????????????????????????????sleep????????????????????????????????????
????????????д???????????????????????????????μ???????????????????????????????????????
???????????????????ж???????????????????(??????????)?????????????????????ж??У?????????????????????
???????????????????????Ч????????????????????????????????????????д????????????????????????
??????????????????
????????????????????????????????????????????????????????????????????????????????????????????????ζ??????н?????л????????????л???????????????????????????????????????
???????????????????????????????????????????????????????????????????????????????
??????????????????????
????1?????????????
??????????????????????????????????????????????????????????????????????????????????????????????Щ???????ж???????????????
?????????????????以??????????????????????????????????????????????????????????????????????????????????????????????????????????????????
????2?????????
?????????: ?????????value????0????????????sem_wait?????????????????value???????value???????0????sem_wait?????????sem_post????value????????仰?????????value>=0??
??????????: ??????????????κ???????????????????????????????????????????????????????????????????á???仰????????????vlaue???????????
????3????ó?????
?????????????????????????????????????????????????????????????????????