LDD-Cocurrency
spinlock_t my_lock = SPIN_LOCK_UNLOCKED; void spin_lock_init(spinlock_t *lock);
void spin_lock(spinlock_t *lock);
Note that all spinlock are uninterruptible, whick means once you call spin_lock, you will spin until the lock become available.
void spin_unlock(spinlock_t *lock);
DECLARE_COMPLETION(my_completion); struct completion my_completion; init_completion(&my_completion); void wait_for_completion(struct completion *c); void complete(struct completion *c); void complete_all(struct completion *c);/*more than one waiting processes*/
INIT_COMPLETION(struct completion c);
Semaphore
void sema_init(struct semaphore *sem, int val);
where val is the initial value to assign to a semaphore.
DECLARE_MUTEX(name);
DECLARE_MUTEX_LOCKED(name);
Here, the first semaphore is set to 1, while the second semaphore is set to 0.
Since we have our semaphores, we can increase and decrease its value.
void down(struct semaphore *sem); int down_interruptible(struct semaphore *sem); int down_trylock(struct semaphore *sem);
As the name implies, the first one decrements the value and waits as long as need be. The second one does the same, but interruptible, and is almost always the one we want. The last one never sleeps; if the semaphore is not available at the time of call, it returns immediately with a nonzero return value.
void up(struct semaphore *sem);
With the tools above, we can manage the critical section access. However, sometimes we don't need to constrain program so strictly. Many processes may just want to read the critical section, without making any changes. In this case, rwsem works well.
void init_rwsem(struct rw_semaphore *sem); void down_read(struct rw_semaphore *sem); int down_read_trylock(struct rw_semaphore *sem); void up_read(struct rw_semaphore *sem); void down_write(struct rw_semaphore *sem); int down_write_trylock(struct rw_semaphore *sem); void up_write(struct rw_semaphore *sem); void downgrade_write(struct rw_semaphore *sem);
If you have a situation where a writer lock is needed for a quick change, followed by a longer period of readonly access, you can use downgrade_write to allow other readers in once you have finished making changes.
作者:glob
出处:http://www.cnblogs.com/adera/
欢迎访问我的个人博客:https://blog.globs.site/
本文版权归作者和博客园共有,转载请注明出处。