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

Tim Peierls tim at peierls.net
Thu Jul 7 11:32:53 EDT 2005


Your arithmetic is OK, but the acquires argument is always 1 for this RRWL 
method (though not in general with AQS), so nextc < c only on overflow of 
the shared count "field".

The documentation *is* wrong about 65536; it should be 65535. I think this 
has been fixed for Mustang.

--tim


Hu, Jinsong wrote:
> 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 

You meant 0x8000 here, right?

> 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.





More information about the Concurrency-interest mailing list