A Thread.MemoryBarrier() might do the trick on the first cacheRef = __cache;
The issue is that every reader imposes a yield. Under mine and most users
scenarios, the __cache will be rarely written to, yet a lock is imposed on
every reader.
Regarding volatile - we may still be OK. Under current code, the thread
must yield because lock() {} is called. Under the proposed code, a yield is
not mandated when the current cache has the item.
Assuming non-volatile __cache declaration, there are 2 dry-cache outcomes:
1 - that the __cache does not have the url
2 - that the "real" __cache (new one) has it, but the reader sees the old
__cache (dry)
In either event, the lock(){} then steps in, and imposes the isolation and
full sync with main memory. the __cach is then re-gotten , but never
modified. It is copied over and then the main __cache reference is
atomically swapped.
The assignment of the __cache object is always done with an effectively
read-only value. __cache is not modified because the reference to it is
grabbed once. If while a thread is reading __cache another is in the lock,
the __cache may be swapped mid code, but cacheRef variable has a point in
time reference to __cache. If cache is then modified by another thread, the
reader still has the old one, and at most would be compelled to call for a
lock and try creating a new MongoUrl() but that would not happen due to the
second check.
On Friday, August 10, 2012 4:10:07 AM UTC-7, craiggwilson wrote:
> We are constrained to .NET 3.5 at the moment, so we cannot use the
> concurrent collections from .NET 4.0.
> NukNuk, that is the proper procedure. We simply missed it. My apologies.
> Regarding your pull request, are you creating so many MongoServer's that
> this is important. It adds a bit of complexity that seems unnecessary to
> me. In addition, this only satisfies conditions a strong memory models.
> To account for a weak memory model (like Itanium processors), you'll need
> to add volatile to the __cache member or create a memory barrier before
> reading it.
> On Friday, August 10, 2012 5:16:07 AM UTC-5, Daniel Harman wrote:
>> No skin in this game, but wouldn't concurrent dictionary be more
>> appropriate, or is the driver held back to support those on .net 1.0? ;)
>> Sent from my iPad
>> On 10 Aug 2012, at 01:12, nuk nuk <shlu...@gmail.com> wrote:
>> > Hi,
>> > I submitted a pull request a while back - never got any action or
>> traction. https://github.com/mongodb/mongo-csharp-driver/pull/118
>> > Is there some other procedure to submit patches? Do i need to contact
>> someone directly?
>> > Thanks!