??????????????????????????????????????豸??????????????
static atomic_t xxx_available = ATOMIC_INIT(1); // ??????????
static int xxx_open(struct inode *inode?? struct file *filp)
{
...
if(!atomic_dec_and_test(&xxx_available))
{
atomic_inc(&xxx_availble);
return - EBUSY; // ?????
}
...
return 0; // ???
}
static int xxx_release(struct inode *inode?? struct file *filp)
{
atomic_inc(&xxx_available); // ????豸
return 0;
}
???????????????:
??????????VS?????
?????????????????????????????????????????ε??????Σ?????????????????????CPU???????????????????????????????????????????????????????????????У????????????·???????????????????????????????????????????????л?????????????????????????????????????????????????????????????????????
?????????????????????????????????????????????????????????????????л??????????CPU?ò????????????CPU??????????????????е???????????????????????????????????????????
??????????????????裺
????1.??????????
????spinlock_t lock;
????2.???????????
????spin_lock_init(lock);??????????????????????????lock??
????3.?????????
????spin_lock(lock);?ú???????????????????????????????????????????????????????????????????????????????
????spin_trylock(lock);????????????棬????????????????????????????
????4.?????????
????spin_unlock(lock);
????????????????????????????????? ???????????????????????CPU???CPU???????????????????????????·?????????????????????????ж????(BH)????????????????????????????????????
????spin_lock_irq() = spin_lock() + local_irq_disable()
????spin_unlock_irq() = spin_unlock() + local_irq_enable()
????spin_lock_irqsave() = spin_lock() + local_irq_save()
????spin_unlock_irqrestore() = spin_unlock() + local_irq_restore()
????spin_lock_bh() = spin_lock() + local_bh_disable()
????spin_unlock_bh() = spin_unlock() + local_bh_enable()
????????????????????????????????????????佇???????£?????????????????????????????????????????????????????????????????????????
?????????
????spinlock_t lock;
????spin_lock_init(&lock);
????spin_lock(&lock); //????????????????????
?????????????????
????spin_unlock(&lock);//?????????
??????????????????????????????????????е?????????????????д???????????????????????ж???????????????????????е?????????????????????????????????????????????????????д???????????????????????????????????????????ж??????????????????????????д????????????С?
????????????????????????????????д???????????????????????????д???????????????????д????????????????????????С??????????????????д????????????????????
????????????????????????????????????????????
??????д??????(rwlock)??????????????д???????棬?????????д???????????????棬???????ж??????е?????????????д??????????С?
// ???????????д??????
rwlock_t my_rwlock = RW_LOCK_UNLOCKED; // ????????
rwlock_t my_rwlock;
rwlock)init(&my_rwlock); // ????????
// ????????????????????ж?????????????????????????????????????????
void read_lock(rwlock_t *lock);
void read_lock_irqsave(rwlock_t *lock?? unsigned long flags);
void read_lock_irq(rwlock_t *lock);
void read_lock_bh(rwlock_t *lock);
// ??????
void read_unlock(rwlock_t *lock);
void read_unlock_irqrestore(rwlock_t *lock?? unsigned long flags);
void read_unlock_irq(rwlock_t *lock);
void read_unlock_bh(rwlock_t *lock);
// д???????????????????д??????????д???????????????????д????????
void write_lock(rwlock_t *lock);
void write_lock_irqsave(rwlock_t *lock?? unsigned long flags);
void write_lock_irq(rwlock_t *lock);
void write_lock_bh(rwlock_t *lock);
int write_trylock(rwlock_t *lock);
// д????
void write_unlock(rwlock_t *lock);
void write_unlock_irqsave(rwlock_t *lock?? unsigned long flags);
void write_unlock_irq(rwlock_t *lock);
void write_unlock_bh(rwlock_t *lock);