Search in sources :

Example 1 with DeadlockException

use of suite.concurrent.Concurrent.DeadlockException in project suite by stupidsing.

the class Mutex method lock.

public void lock() {
    synchronized (bigLock) {
        Thread currentThread = Thread.currentThread();
        waitees.put(currentThread, this);
        try {
            while (!owner.compareAndSet(null, currentThread)) {
                Mutex mutex = this;
                while (mutex != null) {
                    Thread ownerThread = mutex.owner.get();
                    mutex = ownerThread != null ? waitees.get(ownerThread) : null;
                    if (mutex == this)
                        throw new DeadlockException();
                }
                Object_.wait(bigLock);
            }
            depth++;
        } finally {
            waitees.remove(currentThread);
        }
    }
}
Also used : DeadlockException(suite.concurrent.Concurrent.DeadlockException)

Example 2 with DeadlockException

use of suite.concurrent.Concurrent.DeadlockException in project suite by stupidsing.

the class MutexTest method isDeadlock.

private boolean isDeadlock(MutexTestRunnable... mtrs) throws InterruptedException {
    BooMutable result = BooMutable.false_();
    List<Thread> threads = // 
    Read.from(// 
    mtrs).map(mtr -> Thread_.newThread(() -> {
        try {
            mtr.run();
        } catch (DeadlockException ex1) {
            result.setTrue();
        }
    })).toList();
    Thread_.startJoin(threads);
    return result.isTrue();
}
Also used : BooMutable(suite.primitive.BooMutable) MutexLock(suite.concurrent.Mutex.MutexLock) List(java.util.List) Read(suite.streamlet.Read) DeadlockException(suite.concurrent.Concurrent.DeadlockException) BooMutable(suite.primitive.BooMutable) Assert.assertFalse(org.junit.Assert.assertFalse) Assert.assertTrue(org.junit.Assert.assertTrue) Thread_(suite.util.Thread_) Test(org.junit.Test) DeadlockException(suite.concurrent.Concurrent.DeadlockException)

Aggregations

DeadlockException (suite.concurrent.Concurrent.DeadlockException)2 List (java.util.List)1 Assert.assertFalse (org.junit.Assert.assertFalse)1 Assert.assertTrue (org.junit.Assert.assertTrue)1 Test (org.junit.Test)1 MutexLock (suite.concurrent.Mutex.MutexLock)1 BooMutable (suite.primitive.BooMutable)1 Read (suite.streamlet.Read)1 Thread_ (suite.util.Thread_)1