ArtOfMP--自旋锁

关键词 缓存一致性流量

  • TASLock
  • TTASLock
  • BackOffLock
  • ArrayLock
  • CLHLock

认识队列锁:

  • In a queue, each thread can learn if its turn has arrived by checking whether its predecessor has finished.

  • Cache-coherence traffic is reduced by having each thread spin on a different location.

  • A queue also allows for better utilization of the critical section, since there is no need to guess when to attempt to access it: each thread is notified directly by its predecessor in the queue.

队列锁示例

  1. 基于数组实现的队列锁
/*
 * ALock.java
 *
 * Created on January 20, 2006, 11:02 PM
 *
 * From "Multiprocessor Synchronization and Concurrent Data Structures",
 * by Maurice Herlihy and Nir Shavit.
 * Copyright 2006 Elsevier Inc. All rights reserved.
 */

package spin;

/**
 * Anderson lock
 * @author Maurice Herlihy
 */
import java.util.concurrent.locks.Lock;
import java.util.concurrent.atomic.AtomicInteger;
import java.lang.ThreadLocal;
import java.util.concurrent.locks.Condition;
import java.util.concurrent.TimeUnit;
public class ALock implements Lock {
  // thread-local variable
  ThreadLocal mySlotIndex = new ThreadLocal (){
    protected Integer initialValue() {
      return 0;
    }
  };
  AtomicInteger tail;
  boolean[] flag;
  int size;
  /**
   * Constructor
   * @param capacity max number of array slots
   */
  public ALock(int capacity) {
    size = capacity;
    tail = new AtomicInteger(0);
    flag = new boolean[capacity];
    flag[0] = true;
  }
  public void lock() {
    int slot = tail.getAndIncrement() % size;
    mySlotIndex.set(slot);
    while (! flag[mySlotIndex.get()]) {}; // spin
  }
  public void unlock() {
    flag[mySlotIndex.get()] = false;
    flag[(mySlotIndex.get() + 1) % size] = true;
  }
  // any class implementing Lock must provide these methods
  public Condition newCondition() {
    throw new UnsupportedOperationException();
  }
  public boolean tryLock(long time,
      TimeUnit unit)
      throws InterruptedException {
    throw new UnsupportedOperationException();
  }
  public boolean tryLock() {
    throw new UnsupportedOperationException();
  }
  public void lockInterruptibly() throws InterruptedException {
    throw new UnsupportedOperationException();
  }
}

你可能感兴趣的:(ArtOfMP--自旋锁)