How to use atomic package with memory mapped file

classic Classic list List threaded Threaded
2 messages Options
Reply | Threaded
Open this post in threaded view
|

How to use atomic package with memory mapped file

wangchao_csg



Hi,

It seems that there is no way to use java.nio.MappedByteBuffer to map a disk file into memory, and at the same time, use java.util.concurrent.atomic.* objects' atomic methods to update the direct bufer sections' content. Is that true for JDK1.5?

If it is true, how to implement a thread safe shared memory based common facility in a performant way. Does it mean we need to write native code to do necessary synchronization among threads? Anyone can give me an hint?

Thanks in advance.

Chao Wang

 



Express yourself instantly with MSN Messenger! MSN Messenger Download today it's FREE!
_______________________________________________
Concurrency-interest mailing list
[hidden email]
http://altair.cs.oswego.edu/mailman/listinfo/concurrency-interest
Reply | Threaded
Open this post in threaded view
|

RE: How to use atomic package with memory mappedfile

Ernst, Matthias
Chao,
 
indeed there is no way to use j.u.c.atomic.* with mapped byte buffers.
 
An undocumented and non-portable (between VM implementations) way I figured out
is be the class sun.misc.Unsafe#compareAndSwapInt, passing null as the object and the
buffer address as field offset. You would need to get the buffer address through JNI or private
field access in the buffer object. I do not know about the memory effects, i.e. whether #compareAndSwap inserts the necessary memory barrier in order to not reorder previous #puts. In order to access the Unsafe singleton you need to have your code in the boot class path.
 
I think a few compareAndSwap methods on buffers might make a neat addition.
 
Matthias


Hi,

It seems that there is no way to use java.nio.MappedByteBuffer to map a disk file into memory, and at the same time, use java.util.concurrent.atomic.* objects' atomic methods to update the direct bufer sections' content. Is that true for JDK1.5?

If it is true, how to implement a thread safe shared memory based common facility in a performant way. Does it mean we need to write native code to do necessary synchronization among threads? Anyone can give me an hint?


_______________________________________________
Concurrency-interest mailing list
[hidden email]
http://altair.cs.oswego.edu/mailman/listinfo/concurrency-interest