001/**
002 * Logback: the reliable, generic, fast and flexible logging framework.
003 * Copyright (C) 1999-2015, QOS.ch. All rights reserved.
004 *
005 * This program and the accompanying materials are dual-licensed under
006 * either the terms of the Eclipse Public License v1.0 as published by
007 * the Eclipse Foundation
008 *
009 *   or (per the licensee's choosing)
010 *
011 * under the terms of the GNU Lesser General Public License version 2.1
012 * as published by the Free Software Foundation.
013 */
014package ch.qos.logback.core.issue;
015
016import java.util.concurrent.locks.Lock;
017import java.util.concurrent.locks.ReentrantLock;
018
019import ch.qos.logback.core.contention.RunnableWithCounterAndDone;
020
021/**
022 * A runnable which behaves differently depending on the desired locking model.
023 * 
024 * @author Joern Huxhorn
025 * @author Ceki Gulcu
026 */
027public class SelectiveLockRunnable extends RunnableWithCounterAndDone {
028
029    enum LockingModel {
030        NOLOCK, SYNC, FAIR, UNFAIR;
031    }
032
033    static Object LOCK = new Object();
034    static Lock FAIR_LOCK = new ReentrantLock(true);
035    static Lock UNFAIR_LOCK = new ReentrantLock(false);
036
037    LockingModel model;
038
039    SelectiveLockRunnable(LockingModel model) {
040        this.model = model;
041    }
042
043    public void run() {
044        switch (model) {
045        case NOLOCK:
046            nolockRun();
047            break;
048        case SYNC:
049            synchronizedRun();
050            break;
051        case FAIR:
052            fairLockRun();
053            break;
054        case UNFAIR:
055            unfairLockRun();
056            break;
057        }
058    }
059
060    void fairLockRun() {
061        for (;;) {
062            FAIR_LOCK.lock();
063            counter++;
064            FAIR_LOCK.unlock();
065            if (done) {
066                return;
067            }
068        }
069    }
070
071    void unfairLockRun() {
072        for (;;) {
073            UNFAIR_LOCK.lock();
074            counter++;
075            UNFAIR_LOCK.unlock();
076            if (done) {
077                return;
078            }
079        }
080    }
081
082    void nolockRun() {
083        for (;;) {
084            counter++;
085            if (done) {
086                return;
087            }
088        }
089    }
090
091    void synchronizedRun() {
092        for (;;) {
093            synchronized (LOCK) {
094                counter++;
095            }
096            if (done) {
097                return;
098            }
099        }
100    }
101
102    @Override
103    public String toString() {
104        return "SelectiveLockRunnable " + model;
105    }
106}