自旋锁与互斥锁有点类似,只是自旋锁不会引起调用者睡眠,如果自旋锁已经被别的执行单元保持,调用者就一直循环在那里看是否该自旋锁的保持者已经释放了锁,“自旋锁”的作用
在用自旋锁时有可能造成死锁,当递归调用时有可能造成死锁,调用有些其他函数也可能造成死锁,如 copy_to_user()、copy_from_user()、kmalloc()等。
互斥锁:
用于保护临界区,确保同一时间只有一个线程访问数据。对共享资源的访问,先对互斥量进行加锁,如果互斥量已经上锁,调用线程会阻塞,直到互斥量被解锁。在完成了对共享资源的访问后,要对互斥量进行解锁。
public class SpinLock implements Lock {
/**
* 锁持有线程, null表示锁未被任何线程持有
*/
private final AtomicReference<Thread> owner = new AtomicReference<Thread>();
/**
* owner持有锁次数
*/
private int holdCount;
@Override
public void lock() {
final AtomicReference<Thread> owner = this.owner;
final Thread current = Thread.currentThread();
if (owner.get() == current) { // 当前线程已持有锁, 增加持有计数即可
++holdCount;
return;
}
while (!owner.compareAndSet(null, current)) {
}
holdCount = 1;
}
@Override
public void lockInterruptibly() throws InterruptedException {
final AtomicReference<Thread> owner = this.owner;
final Thread current = Thread.currentThread();
if (owner.get() == current) {
++holdCount;
return;
}
while (!owner.compareAndSet(null, current)) {
// 响应中断
if (current.isInterrupted()) {
current.interrupt(); // 重设中断标志
throw new InterruptedException();
}
}
holdCount = 1;
}
@Override
public boolean tryLock() {
boolean locked = owner.compareAndSet(null, Thread.currentThread());
if (locked) {
holdCount = 1;
}
return locked;
}
@Override
public boolean tryLock(long time, TimeUnit unit) throws InterruptedException {
final AtomicReference<Thread> owner = this.owner;
final Thread current = Thread.currentThread();
if (owner.get() == current) {
++holdCount;
return true;
}
final long start = System.nanoTime();
final long timeoutNanos = unit.toNanos(time);
while (!owner.compareAndSet(null, current)) {
// 响应中断
if (current.isInterrupted()) {
current.interrupt();
throw new InterruptedException();
}
// 判断是否超时
long elapsed = System.nanoTime() - start;
if (elapsed >= timeoutNanos) {
return false;
}
}
holdCount = 1;
return true;
}
@Override
public void unlock() {
final AtomicReference<Thread> owner = this.owner;
final Thread current = Thread.currentThread();
if (owner.get() != current) {
throw new IllegalMonitorStateException();
}
// 持有多少次, 就必须释放多少次
if (--holdCount == 0) {
owner.set(null);
}
}
@Override
public Condition newCondition() {
throw new UnsupportedOperationException();
}
}