dibyendumajumdar / simpledbm

SimpleDBM is an Open Source Multi-Threaded Embeddable Transactional Database Engine in Java.
52 stars 11 forks source link

The timer code has a bug whereby in the no timeout scenario it can still timeout occasionally if the underlying park() times out #93

Closed GoogleCodeExporter closed 9 years ago

GoogleCodeExporter commented 9 years ago
Hi,majumdar. I found there may be a small mistake in locking in SimpleDBM. 
Please check and confirm.

I used your complete jar to help implementing the locking in my project, but I 
found that the no timeout lock can throw timeout exception sometimes,
I read your codes carefully, and found there may be a small mistake when 
timeToWait = -1. The red color code is the change i try to solve the problem. 
Will you please
 check and fix it in your best way you think and release an new version ? For your license reason, I can not change your code.

Thanks a lot!
Sincerely Water Chow.

--------------------------------------------------------------------------------
-----------------------------

public boolean isExpired() { 

        if (expired) 
            return true;

        if (timeToWait == -1)
            return false;

        long now = System.nanoTime(); 

        long timeWaited = now - startTime; 

        if (timeWaited >= timeToWait) { 

            expired = true; 

        } 

        return expired; 

    } 

    public void await() { 

        if (expired) { 

            return; 

        } 

        if (timeToWait == -1) { 

            LockSupport.park(); 

//            expired = true; 

            return; 

        } 

        long now = System.nanoTime(); 

        long timeWaited = now - startTime; 

        if (timeWaited >= timeToWait) { 

            expired = true; 

            return; 

        } 

        long timeToWaitThisTime = timeToWait - timeWaited; 

        assert timeToWaitThisTime > 0 && timeToWaitThisTime <= timeToWait; 

        LockSupport.parkNanos(timeToWaitThisTime); 

    }

Original issue reported on code.google.com by d.majum...@gmail.com on 19 Dec 2011 at 10:10

GoogleCodeExporter commented 9 years ago
Fixed in 1.0.19

Original comment by d.majum...@gmail.com on 2 Jun 2012 at 11:37