[concurrency-interest] maximum number of shared lock for ReentrantReadWriteLock

Hu, Jinsong jhu at glog.com
Thu Jul 7 10:41:11 EDT 2005



I found my self stuck on a basic bit number operation here, and I hope I
am wrong here, following is the code I extracted from this class:



         * Perform nonfair tryLock for read. 


        final int nonfairTryAcquireShared(int acquires) {

            for (;;) {

                int c = getState();

                int nextc = c + (acquires << SHARED_SHIFT);

                if (nextc < c)

                    throw new Error("Maximum lock count exceeded");

                if (exclusiveCount(c) != 0 && 

                    owner != Thread.currentThread())

                    return -1;

                if (compareAndSetState(c, nextc)) 

                    return 1;

                // Recheck count if lost CAS




Let's say, initially, c is 0, while I tried to acquire 0x80000 shared
lock, then 0x8000 << 16 is a negative number, then c + 0x8000 << 16 is
definitely less than c, then the maximum lock count exceeded exception
will be thrown out. If this is true, then the shared lock count can not
be beyond 0x8000 (32768), not 65536.




-------------- next part --------------
An HTML attachment was scrubbed...
URL: /pipermail/attachments/20050707/1d70af6a/attachment.htm

More information about the Concurrency-interest mailing list