/*
Spin on a ReentrantLock before blocking. Default behavior is not to spin.
498193
public static void spinLock(ReentrantLock lock) {
        if (LOCK_SPIN_MICROSECONDS > 0) {
            long nanos = -1;
            for (;;) {
                if (lock.tryLock()) return;
                if (nanos == -1) {
                    nanos = System.nanoTime();
                } else if (System.nanoTime() - nanos > LOCK_SPIN_MICROSECONDS) {
                    lock.lock();
                    return;
                }
            }
        } else {
            lock.lock();
        }
    }
1066782
private void signalNotEmpty() {
        final ReentrantLock takeLock = this.takeLock;
        takeLock.lock();
        try {
            notEmpty.signal();
        } finally {
            takeLock.unlock();
        }
    }
1287860
function Locker(timeout, logger) {
  this.rwlock = new Rwlock()
  this.activeLock = null
  this.count = 0
  this.logger = logger || { warn: function() {} }
  this.timeout = timeout 
}
1365689
private boolean isAllOrDirtyOptLocking() {
		EntityMetamodel entityMetamodel = getEntityMetamodel();
		return entityMetamodel.getOptimisticLockStyle() == OptimisticLockStyle.DIRTY
				|| entityMetamodel.getOptimisticLockStyle() == OptimisticLockStyle.ALL;
	}
1128193
private static void lockBoth(Lock sourceWriteLock, Lock destWriteLock) {
    while (true) {
      sourceWriteLock.lock();
      if (destWriteLock.tryLock()) {
        return;
      } else {
        sourceWriteLock.unlock();
      }

      destWriteLock.lock();
      if (sourceWriteLock.tryLock()) {
        return;
      } else {
        destWriteLock.unlock();
      }
    }
  }
566914
public boolean tryLock(String key, String request) {
        //get connection
        Object connection = getConnection();
        String result ;
        if (connection instanceof Jedis){
            result =  ((Jedis) connection).set(lockPrefix + key, request, SET_IF_NOT_EXIST, SET_WITH_EXPIRE_TIME, 10 * TIME);
            ((Jedis) connection).close();
        }else {
            result = ((JedisCluster) connection).set(lockPrefix + key, request, SET_IF_NOT_EXIST, SET_WITH_EXPIRE_TIME, 10 * TIME);

        }

        if (LOCK_MSG.equals(result)) {
            return true;
        } else {
            return false;
        }
    }
285500
public function spin($lambda, $wait = 15,$errorCallback = null)
   {
       $time = time();
       $stopTime = $time + $wait;
       while (time() < $stopTime)
       {
           try {
               if ($lambda($this)) {
                   return;
               }
           } catch (\Exception $e) {
               // do nothing
           }

           usleep(250000);
       }
       if($errorCallback !== null){
           $errorCallback($this);
       }
       throw new \Exception("Spin function timed out after {$wait} seconds");
   }
902207
private void handleWhereBlock(Method method) {
    Block block = method.getLastBlock();
    if (!(block instanceof WhereBlock)) return;

    new DeepBlockRewriter(this).visit(block);
    WhereBlockRewriter.rewrite((WhereBlock) block, this);
  }
652950
public static void setBlocking(FileDescriptor fd, boolean blocking) throws IOException {
        try {
            int flags = Libcore.os.fcntlVoid(fd, F_GETFL);
            if (!blocking) {
                flags |= O_NONBLOCK;
            } else {
                flags &= ~O_NONBLOCK;
            }
            Libcore.os.fcntlLong(fd, F_SETFL, flags);
        } catch (ErrnoException errnoException) {
            throw errnoException.rethrowAsIOException();
        }
    }
971428
function isLockHolder(task, requestorId) {
  if(!task.semaphore) {
    throw new LockError('TaskNotLocked');
  }
  var requestKey  = hashKey(requestorId, task.id);

  return requestKey === task.semaphore.key;
}
950933
private boolean addStoredBlock(Block block,
                               DatanodeDescriptor node,
                               DatanodeDescriptor delNodeHint) throws IOException {
    return addStoredBlockInternal(block, node, delNodeHint, false, null);
  }
1088016
@Override
  public void block(final Function<FastFuture<T>, T> safeJoin) {

    if (active.size() == 0)
      return;
    active.stream()
      .peek(cf -> safeJoin.apply(cf))
      .forEach(a -> {
      });

  }
1020623
void lock(String serverId) {
        Assert.notNull(serverId);
        if (this.locked) {
            throw new IllegalStateException("Cannot lock already locked mutex: " + this);
        }
        this.locked = true;
        this.lockStart = new Date();
        this.lastUpdate = this.lockStart;
        this.serverId = serverId;
    }
1364894
private void signalNotFull() {
        final ReentrantLock putLock = this.putLock;
        putLock.lock();
        try {
            notFull.signal();
        } finally {
            putLock.unlock();
        }
    }
974073
private void blockMe(int cyclesToBlock) {
	long unblock = cycles + cyclesToBlock;

	BlockedEntry newEntry = new BlockedEntry(unblock);
	blockedCollection.add(newEntry);

	while (unblock > cycles) {
	    try {
		newEntry.getSync().acquire(); // blocks
	    } catch (InterruptedException exc) {
		log.error("[temporaryBlock] Spurious wakeup", exc);
	    }
	}

    }
707429
public static Lock getLock(String key) {
        Lock lock = container.get(key);
        if (lock == null) {
            synchronized (container) {
                lock = container.computeIfAbsent(key, k -> new ReentrantLock());
            }
        }
        return lock;
    }
180938
public final T blockResult(long timeout) throws TError, CancelException {
		Thread t;
		BlockedThreadHandler blockedHandler;
		synchronized (this) {
			if (unblocked && listenersInline == null) {
				if (error != null) throw error;
				if (cancel != null) throw cancel;
				return result;
			}
			t = Thread.currentThread();
			blockedHandler = Threading.getBlockedThreadHandler(t);
			if (blockedHandler == null)
				while (!unblocked || listenersInline != null)
					try { this.wait(timeout < 0 ? 0 : timeout); }
					catch (InterruptedException e) { return null; }
		}
		if (blockedHandler != null)
			blockedHandler.blocked(this, timeout);
		if (error != null) throw error;
		if (cancel != null) throw cancel;
		return result;
	}