PageRenderTime 20ms CodeModel.GetById 14ms app.highlight 4ms RepoModel.GetById 1ms app.codeStats 0ms

/src/kilim/ReentrantLock.java

http://github.com/kilim/kilim
Java | 92 lines | 77 code | 13 blank | 2 comment | 18 complexity | 6d16cd5a59ff0d1b46ddf290fc14eca7 MD5 | raw file
 1// copyright 2006 sriram - offered under the terms of the MIT License
 2
 3package kilim;
 4
 5import java.util.concurrent.TimeUnit;
 6
 7public class ReentrantLock extends java.util.concurrent.locks.ReentrantLock {
 8    private static final long serialVersionUID = 1L;
 9
10    public ReentrantLock() {
11        super(false);
12    }
13
14    public ReentrantLock(boolean fair) {
15        super(fair);
16    }
17
18    public Thread getOwner() {
19        return super.getOwner();
20    }
21
22    Thread locker = null; // /***************************
23
24    public void preLock() throws Pausable {
25        Scheduler.getCurrentTask().prePin();
26    }
27    protected void checkPin() {
28        Scheduler.getCurrentTask().checkPin();
29    }
30    
31    @Override
32    public void lock() {
33        checkPin();
34        super.lock();
35        Thread t = Thread.currentThread();
36        locker = t;
37
38        if (t != null) {
39            Task tsk = Scheduler.getCurrentTask();
40            if (tsk != null)
41                tsk.pinToThread();
42        }
43    }
44
45    @Override
46    public boolean tryLock() {
47        checkPin();
48        // TODO Auto-generated method stub
49        boolean ret = super.tryLock();
50        Thread t = Thread.currentThread();
51        if (ret && (t != null)) {
52            locker = t;
53            Task tsk = Scheduler.getCurrentTask();
54            if (tsk != null)
55                tsk.pinToThread();
56        }
57        return ret;
58    }
59
60    @Override
61    public boolean tryLock(long timeout, TimeUnit unit)
62                                                       throws InterruptedException {
63        checkPin();
64        boolean ret = super.tryLock(timeout, unit);
65        Thread t = Thread.currentThread();
66        if (ret && (t != null)) {
67            locker = t;
68            Task tsk = Scheduler.getCurrentTask();
69            if (tsk != null)
70                tsk.pinToThread();
71        }
72        return ret;
73    }
74
75    @Override
76    public void unlock() {
77        try {
78            super.unlock();
79        } catch (IllegalMonitorStateException ims) {
80            System.err.println("Locking thread: " + locker.getName()
81                    + ", unlocking thread: " + Thread.currentThread().getName());
82            ims.printStackTrace();
83            System.exit(1);
84        }
85        Thread t = Thread.currentThread();
86        if (t != null) {
87            Task tsk = Scheduler.getCurrentTask();
88            if (tsk != null)
89                tsk.unpinFromThread();
90        }
91    }
92}