[concurrency-interest] maximum number of shared lock forReentrantReadWriteLock

Hu, Jinsong jhu at glog.com
Thu Jul 7 12:03:29 EDT 2005


Let's say that current state is 0x7FFF0000, hence the current total
shared lock count is 0x7FFF. Now, let's acquire one more shared lock,
hence following are values of current variables:

int c = 0x7FFF0000
int i = 1 << 16 = 0x00010000
int nextc = c + i = 0x80000000 < 0

hence nextc < c and according to the code, the lock overflow exception
is still thrown out.

Jinsong



-----Original Message-----
From: Tim Peierls [mailto:tim at peierls.net] 
Sent: Thursday, July 07, 2005 11:33 AM
To: Hu, Jinsong
Cc: concurrency-interest at altair.cs.oswego.edu
Subject: Re: [concurrency-interest] maximum number of shared lock
forReentrantReadWriteLock

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