You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@maven.apache.org by Oleg Gusakov <ol...@gmail.com> on 2008/09/11 22:45:34 UTC

NIO problem, any help appreciated!

I modified Mercury LocalRepositoryWriter to allow multiple builds to 
write to the same repository without trashing metadata, but hit several 
obstacles - http://jira.codehaus.org/browse/MERCURY-7

Please help to find holes in the approach

*Problem*: find a way to sync multiple JVMs on a single box. This - to 
me - means that if I can sync multiple threads inside one JVM without 
using *synchronized*, *wait*() and *notify*(), I achieve the goal.

*First* I tried creating a lock file in the file system, but without 
using *synchronized* - I can always vary parameters and have two threads 
obtain the lock: it's the infamous double-check locking problem: 
http://www.cs.umd.edu/~pugh/java/memoryModel/DoubleCheckedLocking.html 
And having to think about multiple JVMs only aggravates the situation.

*Then* I tried java.nio approach, because "This file-locking API is 
intended to map directly to the native locking facility of the 
underlying operating system. Thus the locks held on a file should be 
visible to all programs that have access to the file, regardless of the 
language in which those programs are written." - javadocs on FileLock - 
http://java.sun.com/j2se/1.5.0/docs/api/java/nio/channels/FileLock.html

But this does not work, at least the way I understand it should: 
http://jira.codehaus.org/secure/attachment/36986/NioTest.java - this 
unit test shows how java.nio allows to have two valid exclusive locks on 
the same file [region].

Does anyone have any insights on the problem? Maybe a hint to some 3rd 
party library that can help with inter-process synchronization?

Thanks,
Oleg

Re: NIO problem, any help appreciated!

Posted by Oleg Gusakov <ol...@gmail.com>.
FYI - solution found!

Dain Sundstrom tried it and found out that while NIO does not work 
inside single JVM, it works across processes. So the first take ugly 
solution seem to use Java synchronization inside JVM and NIO locks 
between them. This brings a lot of complications to the table, I'd 
rather  wait and see how good/bad is the current workaround - "OS file 
exists" lock.

Thanks,
Oleg

Christian Edward Gruber wrote:
> Sorry - re-reading this, I realize I wasn't clear.  I'm assuming using 
> some sort of .lock file as a way to lock the artifact, rather than a 
> locking the files at the filesystem level.  That is more likely to 
> have broad support, even on network mounted filesystems whereas 
> locking semantics of underlying OSs are often different.
>
> Christian.
>
> On 12-Sep-08, at 01:13 , Christian Edward Gruber wrote:
>
>> Well I think creating the folders up to the file is not so difficult, 
>> because the folder creation can have a race, as long as it's not a 
>> destructive creation.  I think it's just the file you need to worry 
>> about, and my examination of the c files underlying the 
>> createFileExclusively suggests that they use the native atomic 
>> create, with any subsequent call returning false (since the file 
>> exists).  That semantic should work for inter or intra JVM checks, 
>> since it's not about locking the file, but about the file's existence.
>>
>> Christian.
>>
>> On 12-Sep-08, at 00:54 , Oleg Gusakov wrote:
>>
>>>
>>>
>>> Christian Edward Gruber wrote:
>>>> BTW, my suggestion isn't as robust as java.nio.channels.FileLock is 
>>>> supposed to be, but in basic testing it definitely returns "false" 
>>>> if the lock file exists on two platforms (MacOS and BSD).  I can't 
>>>> generate a race condition so easily, so I can't vouch for that, but 
>>>> it would at least reduce the race condition to the lock creation 
>>>> moment, which is pretty darned small if you use this facility.
>>> Devil is in the details: create file file is very fast, check if 
>>> file exists is a little slower, check if parent directory exists, 
>>> create one if it does not. So this time is not so small. I can 
>>> always tune test parameters so that two threads obtain the lock, and 
>>> I can also tune it so that test passes with "reasonable" request 
>>> volume.
>>>
>>> So I left this implementation in for now, but it does not completely 
>>> eliminate the problem, it just reduces it.
>>>>
>>>> Christian.
>>>>
>>>> On 11-Sep-08, at 17:06 , Christian Edward Gruber wrote:
>>>>
>>>>> So is File.createNewFile() not actually back-ended by the 
>>>>> platform's atomic file creation capability (that is, if there is 
>>>>> one, it uses it, else there's a possible race)?  If so, I'd just 
>>>>> use that.  Looking through the 1.5 source, it at least calls to 
>>>>> FileSystem.createFileExclusively, which in Win32FileSystem, 
>>>>> UnixFileSystem, and WinNTFileSystem are all implemented in 
>>>>> native.  I haven't looked at the c source to validate the 
>>>>> mechanism, but I think it would be safe to use that and rely on 
>>>>> operating system capability for the specific semantic.
>>>>>
>>>>> Christian.
>>>>>
>>>>> On 11-Sep-08, at 16:45 , Oleg Gusakov wrote:
>>>>>
>>>>>> I modified Mercury LocalRepositoryWriter to allow multiple builds 
>>>>>> to write to the same repository without trashing metadata, but 
>>>>>> hit several obstacles - http://jira.codehaus.org/browse/MERCURY-7
>>>>>>
>>>>>> Please help to find holes in the approach
>>>>>>
>>>>>> *Problem*: find a way to sync multiple JVMs on a single box. This 
>>>>>> - to me - means that if I can sync multiple threads inside one 
>>>>>> JVM without using *synchronized*, *wait*() and *notify*(), I 
>>>>>> achieve the goal.
>>>>>>
>>>>>> *First* I tried creating a lock file in the file system, but 
>>>>>> without using *synchronized* - I can always vary parameters and 
>>>>>> have two threads obtain the lock: it's the infamous double-check 
>>>>>> locking problem: 
>>>>>> http://www.cs.umd.edu/~pugh/java/memoryModel/DoubleCheckedLocking.html And 
>>>>>> having to think about multiple JVMs only aggravates the situation.
>>>>>>
>>>>>> *Then* I tried java.nio approach, because "This file-locking API 
>>>>>> is intended to map directly to the native locking facility of the 
>>>>>> underlying operating system. Thus the locks held on a file should 
>>>>>> be visible to all programs that have access to the file, 
>>>>>> regardless of the language in which those programs are written." 
>>>>>> - javadocs on FileLock - 
>>>>>> http://java.sun.com/j2se/1.5.0/docs/api/java/nio/channels/FileLock.html 
>>>>>>
>>>>>>
>>>>>> But this does not work, at least the way I understand it should: 
>>>>>> http://jira.codehaus.org/secure/attachment/36986/NioTest.java - 
>>>>>> this unit test shows how java.nio allows to have two valid 
>>>>>> exclusive locks on the same file [region].
>>>>>>
>>>>>> Does anyone have any insights on the problem? Maybe a hint to 
>>>>>> some 3rd party library that can help with inter-process 
>>>>>> synchronization?
>>>>>>
>>>>>> Thanks,
>>>>>> Oleg
>>>>>
>>>>
>>>>
>>>> ---------------------------------------------------------------------
>>>> To unsubscribe, e-mail: dev-unsubscribe@maven.apache.org
>>>> For additional commands, e-mail: dev-help@maven.apache.org
>>>>
>>>>
>>>
>>
>>
>> ---------------------------------------------------------------------
>> To unsubscribe, e-mail: dev-unsubscribe@maven.apache.org
>> For additional commands, e-mail: dev-help@maven.apache.org
>>
>
>

---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@maven.apache.org
For additional commands, e-mail: dev-help@maven.apache.org


Re: NIO problem, any help appreciated!

Posted by Oleg Gusakov <ol...@gmail.com>.
Christian,

We are on the same page: I am using .lock file in Mercury. But I do it 
at GA level, because I need to change both GA and GAV level metadata 
files, so I chose to lock entire artifact.

This still leaves a window on 5-10 millis - lock file processing - when 
two threads or processes can obtain the same lock, but it's magnitudes 
better that leaving it wide open for entire update duration - hundreds 
of milliseconds. So solution greatly mitigates the problem but does not 
eliminate it.

Thanks,
Oleg

Christian Edward Gruber wrote:
> Sorry - re-reading this, I realize I wasn't clear.  I'm assuming using 
> some sort of .lock file as a way to lock the artifact, rather than a 
> locking the files at the filesystem level.  That is more likely to 
> have broad support, even on network mounted filesystems whereas 
> locking semantics of underlying OSs are often different.
>
> Christian.
>
>

---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@maven.apache.org
For additional commands, e-mail: dev-help@maven.apache.org


Re: NIO problem, any help appreciated!

Posted by Christian Edward Gruber <ch...@gmail.com>.
Sorry - re-reading this, I realize I wasn't clear.  I'm assuming using  
some sort of .lock file as a way to lock the artifact, rather than a  
locking the files at the filesystem level.  That is more likely to  
have broad support, even on network mounted filesystems whereas  
locking semantics of underlying OSs are often different.

Christian.

On 12-Sep-08, at 01:13 , Christian Edward Gruber wrote:

> Well I think creating the folders up to the file is not so  
> difficult, because the folder creation can have a race, as long as  
> it's not a destructive creation.  I think it's just the file you  
> need to worry about, and my examination of the c files underlying  
> the createFileExclusively suggests that they use the native atomic  
> create, with any subsequent call returning false (since the file  
> exists).  That semantic should work for inter or intra JVM checks,  
> since it's not about locking the file, but about the file's existence.
>
> Christian.
>
> On 12-Sep-08, at 00:54 , Oleg Gusakov wrote:
>
>>
>>
>> Christian Edward Gruber wrote:
>>> BTW, my suggestion isn't as robust as java.nio.channels.FileLock  
>>> is supposed to be, but in basic testing it definitely returns  
>>> "false" if the lock file exists on two platforms (MacOS and BSD).   
>>> I can't generate a race condition so easily, so I can't vouch for  
>>> that, but it would at least reduce the race condition to the lock  
>>> creation moment, which is pretty darned small if you use this  
>>> facility.
>> Devil is in the details: create file file is very fast, check if  
>> file exists is a little slower, check if parent directory exists,  
>> create one if it does not. So this time is not so small. I can  
>> always tune test parameters so that two threads obtain the lock,  
>> and I can also tune it so that test passes with "reasonable"  
>> request volume.
>>
>> So I left this implementation in for now, but it does not  
>> completely eliminate the problem, it just reduces it.
>>>
>>> Christian.
>>>
>>> On 11-Sep-08, at 17:06 , Christian Edward Gruber wrote:
>>>
>>>> So is File.createNewFile() not actually back-ended by the  
>>>> platform's atomic file creation capability (that is, if there is  
>>>> one, it uses it, else there's a possible race)?  If so, I'd just  
>>>> use that.  Looking through the 1.5 source, it at least calls to  
>>>> FileSystem.createFileExclusively, which in Win32FileSystem,  
>>>> UnixFileSystem, and WinNTFileSystem are all implemented in  
>>>> native.  I haven't looked at the c source to validate the  
>>>> mechanism, but I think it would be safe to use that and rely on  
>>>> operating system capability for the specific semantic.
>>>>
>>>> Christian.
>>>>
>>>> On 11-Sep-08, at 16:45 , Oleg Gusakov wrote:
>>>>
>>>>> I modified Mercury LocalRepositoryWriter to allow multiple  
>>>>> builds to write to the same repository without trashing  
>>>>> metadata, but hit several obstacles - http://jira.codehaus.org/browse/MERCURY-7
>>>>>
>>>>> Please help to find holes in the approach
>>>>>
>>>>> *Problem*: find a way to sync multiple JVMs on a single box.  
>>>>> This - to me - means that if I can sync multiple threads inside  
>>>>> one JVM without using *synchronized*, *wait*() and *notify*(), I  
>>>>> achieve the goal.
>>>>>
>>>>> *First* I tried creating a lock file in the file system, but  
>>>>> without using *synchronized* - I can always vary parameters and  
>>>>> have two threads obtain the lock: it's the infamous double-check  
>>>>> locking problem: http://www.cs.umd.edu/~pugh/java/memoryModel/DoubleCheckedLocking.html 
>>>>>  And having to think about multiple JVMs only aggravates the  
>>>>> situation.
>>>>>
>>>>> *Then* I tried java.nio approach, because "This file-locking API  
>>>>> is intended to map directly to the native locking facility of  
>>>>> the underlying operating system. Thus the locks held on a file  
>>>>> should be visible to all programs that have access to the file,  
>>>>> regardless of the language in which those programs are written."  
>>>>> - javadocs on FileLock - http://java.sun.com/j2se/1.5.0/docs/api/java/nio/channels/FileLock.html
>>>>>
>>>>> But this does not work, at least the way I understand it should: http://jira.codehaus.org/secure/attachment/36986/NioTest.java 
>>>>>  - this unit test shows how java.nio allows to have two valid  
>>>>> exclusive locks on the same file [region].
>>>>>
>>>>> Does anyone have any insights on the problem? Maybe a hint to  
>>>>> some 3rd party library that can help with inter-process  
>>>>> synchronization?
>>>>>
>>>>> Thanks,
>>>>> Oleg
>>>>
>>>
>>>
>>> ---------------------------------------------------------------------
>>> To unsubscribe, e-mail: dev-unsubscribe@maven.apache.org
>>> For additional commands, e-mail: dev-help@maven.apache.org
>>>
>>>
>>
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: dev-unsubscribe@maven.apache.org
> For additional commands, e-mail: dev-help@maven.apache.org
>


---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@maven.apache.org
For additional commands, e-mail: dev-help@maven.apache.org


Re: NIO problem, any help appreciated!

Posted by Christian Edward Gruber <cg...@israfil.net>.
Well I think creating the folders up to the file is not so difficult,  
because the folder creation can have a race, as long as it's not a  
destructive creation.  I think it's just the file you need to worry  
about, and my examination of the c files underlying the  
createFileExclusively suggests that they use the native atomic create,  
with any subsequent call returning false (since the file exists).   
That semantic should work for inter or intra JVM checks, since it's  
not about locking the file, but about the file's existence.

Christian.

On 12-Sep-08, at 00:54 , Oleg Gusakov wrote:

>
>
> Christian Edward Gruber wrote:
>> BTW, my suggestion isn't as robust as java.nio.channels.FileLock is  
>> supposed to be, but in basic testing it definitely returns "false"  
>> if the lock file exists on two platforms (MacOS and BSD).  I can't  
>> generate a race condition so easily, so I can't vouch for that, but  
>> it would at least reduce the race condition to the lock creation  
>> moment, which is pretty darned small if you use this facility.
> Devil is in the details: create file file is very fast, check if  
> file exists is a little slower, check if parent directory exists,  
> create one if it does not. So this time is not so small. I can  
> always tune test parameters so that two threads obtain the lock, and  
> I can also tune it so that test passes with "reasonable" request  
> volume.
>
> So I left this implementation in for now, but it does not completely  
> eliminate the problem, it just reduces it.
>>
>> Christian.
>>
>> On 11-Sep-08, at 17:06 , Christian Edward Gruber wrote:
>>
>>> So is File.createNewFile() not actually back-ended by the  
>>> platform's atomic file creation capability (that is, if there is  
>>> one, it uses it, else there's a possible race)?  If so, I'd just  
>>> use that.  Looking through the 1.5 source, it at least calls to  
>>> FileSystem.createFileExclusively, which in Win32FileSystem,  
>>> UnixFileSystem, and WinNTFileSystem are all implemented in  
>>> native.  I haven't looked at the c source to validate the  
>>> mechanism, but I think it would be safe to use that and rely on  
>>> operating system capability for the specific semantic.
>>>
>>> Christian.
>>>
>>> On 11-Sep-08, at 16:45 , Oleg Gusakov wrote:
>>>
>>>> I modified Mercury LocalRepositoryWriter to allow multiple builds  
>>>> to write to the same repository without trashing metadata, but  
>>>> hit several obstacles - http://jira.codehaus.org/browse/MERCURY-7
>>>>
>>>> Please help to find holes in the approach
>>>>
>>>> *Problem*: find a way to sync multiple JVMs on a single box. This  
>>>> - to me - means that if I can sync multiple threads inside one  
>>>> JVM without using *synchronized*, *wait*() and *notify*(), I  
>>>> achieve the goal.
>>>>
>>>> *First* I tried creating a lock file in the file system, but  
>>>> without using *synchronized* - I can always vary parameters and  
>>>> have two threads obtain the lock: it's the infamous double-check  
>>>> locking problem: http://www.cs.umd.edu/~pugh/java/memoryModel/DoubleCheckedLocking.html 
>>>>  And having to think about multiple JVMs only aggravates the  
>>>> situation.
>>>>
>>>> *Then* I tried java.nio approach, because "This file-locking API  
>>>> is intended to map directly to the native locking facility of the  
>>>> underlying operating system. Thus the locks held on a file should  
>>>> be visible to all programs that have access to the file,  
>>>> regardless of the language in which those programs are written."  
>>>> - javadocs on FileLock - http://java.sun.com/j2se/1.5.0/docs/api/java/nio/channels/FileLock.html
>>>>
>>>> But this does not work, at least the way I understand it should: http://jira.codehaus.org/secure/attachment/36986/NioTest.java 
>>>>  - this unit test shows how java.nio allows to have two valid  
>>>> exclusive locks on the same file [region].
>>>>
>>>> Does anyone have any insights on the problem? Maybe a hint to  
>>>> some 3rd party library that can help with inter-process  
>>>> synchronization?
>>>>
>>>> Thanks,
>>>> Oleg
>>>
>>
>>
>> ---------------------------------------------------------------------
>> To unsubscribe, e-mail: dev-unsubscribe@maven.apache.org
>> For additional commands, e-mail: dev-help@maven.apache.org
>>
>>
>


---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@maven.apache.org
For additional commands, e-mail: dev-help@maven.apache.org


Re: NIO problem, any help appreciated!

Posted by Oleg Gusakov <ol...@gmail.com>.

Christian Edward Gruber wrote:
> BTW, my suggestion isn't as robust as java.nio.channels.FileLock is 
> supposed to be, but in basic testing it definitely returns "false" if 
> the lock file exists on two platforms (MacOS and BSD).  I can't 
> generate a race condition so easily, so I can't vouch for that, but it 
> would at least reduce the race condition to the lock creation moment, 
> which is pretty darned small if you use this facility.
Devil is in the details: create file file is very fast, check if file 
exists is a little slower, check if parent directory exists, create one 
if it does not. So this time is not so small. I can always tune test 
parameters so that two threads obtain the lock, and I can also tune it 
so that test passes with "reasonable" request volume.

So I left this implementation in for now, but it does not completely 
eliminate the problem, it just reduces it.
>
> Christian.
>
> On 11-Sep-08, at 17:06 , Christian Edward Gruber wrote:
>
>> So is File.createNewFile() not actually back-ended by the platform's 
>> atomic file creation capability (that is, if there is one, it uses 
>> it, else there's a possible race)?  If so, I'd just use that.  
>> Looking through the 1.5 source, it at least calls to 
>> FileSystem.createFileExclusively, which in Win32FileSystem, 
>> UnixFileSystem, and WinNTFileSystem are all implemented in native.  I 
>> haven't looked at the c source to validate the mechanism, but I think 
>> it would be safe to use that and rely on operating system capability 
>> for the specific semantic.
>>
>> Christian.
>>
>> On 11-Sep-08, at 16:45 , Oleg Gusakov wrote:
>>
>>> I modified Mercury LocalRepositoryWriter to allow multiple builds to 
>>> write to the same repository without trashing metadata, but hit 
>>> several obstacles - http://jira.codehaus.org/browse/MERCURY-7
>>>
>>> Please help to find holes in the approach
>>>
>>> *Problem*: find a way to sync multiple JVMs on a single box. This - 
>>> to me - means that if I can sync multiple threads inside one JVM 
>>> without using *synchronized*, *wait*() and *notify*(), I achieve the 
>>> goal.
>>>
>>> *First* I tried creating a lock file in the file system, but without 
>>> using *synchronized* - I can always vary parameters and have two 
>>> threads obtain the lock: it's the infamous double-check locking 
>>> problem: 
>>> http://www.cs.umd.edu/~pugh/java/memoryModel/DoubleCheckedLocking.html And 
>>> having to think about multiple JVMs only aggravates the situation.
>>>
>>> *Then* I tried java.nio approach, because "This file-locking API is 
>>> intended to map directly to the native locking facility of the 
>>> underlying operating system. Thus the locks held on a file should be 
>>> visible to all programs that have access to the file, regardless of 
>>> the language in which those programs are written." - javadocs on 
>>> FileLock - 
>>> http://java.sun.com/j2se/1.5.0/docs/api/java/nio/channels/FileLock.html
>>>
>>> But this does not work, at least the way I understand it should: 
>>> http://jira.codehaus.org/secure/attachment/36986/NioTest.java - this 
>>> unit test shows how java.nio allows to have two valid exclusive 
>>> locks on the same file [region].
>>>
>>> Does anyone have any insights on the problem? Maybe a hint to some 
>>> 3rd party library that can help with inter-process synchronization?
>>>
>>> Thanks,
>>> Oleg
>>
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: dev-unsubscribe@maven.apache.org
> For additional commands, e-mail: dev-help@maven.apache.org
>
>

---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@maven.apache.org
For additional commands, e-mail: dev-help@maven.apache.org


Re: NIO problem, any help appreciated!

Posted by Christian Edward Gruber <cg...@israfil.net>.
BTW, my suggestion isn't as robust as java.nio.channels.FileLock is  
supposed to be, but in basic testing it definitely returns "false" if  
the lock file exists on two platforms (MacOS and BSD).  I can't  
generate a race condition so easily, so I can't vouch for that, but it  
would at least reduce the race condition to the lock creation moment,  
which is pretty darned small if you use this facility.

Christian.

On 11-Sep-08, at 17:06 , Christian Edward Gruber wrote:

> So is File.createNewFile() not actually back-ended by the platform's  
> atomic file creation capability (that is, if there is one, it uses  
> it, else there's a possible race)?  If so, I'd just use that.   
> Looking through the 1.5 source, it at least calls to  
> FileSystem.createFileExclusively, which in Win32FileSystem,  
> UnixFileSystem, and WinNTFileSystem are all implemented in native.   
> I haven't looked at the c source to validate the mechanism, but I  
> think it would be safe to use that and rely on operating system  
> capability for the specific semantic.
>
> Christian.
>
> On 11-Sep-08, at 16:45 , Oleg Gusakov wrote:
>
>> I modified Mercury LocalRepositoryWriter to allow multiple builds  
>> to write to the same repository without trashing metadata, but hit  
>> several obstacles - http://jira.codehaus.org/browse/MERCURY-7
>>
>> Please help to find holes in the approach
>>
>> *Problem*: find a way to sync multiple JVMs on a single box. This -  
>> to me - means that if I can sync multiple threads inside one JVM  
>> without using *synchronized*, *wait*() and *notify*(), I achieve  
>> the goal.
>>
>> *First* I tried creating a lock file in the file system, but  
>> without using *synchronized* - I can always vary parameters and  
>> have two threads obtain the lock: it's the infamous double-check  
>> locking problem: http://www.cs.umd.edu/~pugh/java/memoryModel/DoubleCheckedLocking.html 
>>  And having to think about multiple JVMs only aggravates the  
>> situation.
>>
>> *Then* I tried java.nio approach, because "This file-locking API is  
>> intended to map directly to the native locking facility of the  
>> underlying operating system. Thus the locks held on a file should  
>> be visible to all programs that have access to the file, regardless  
>> of the language in which those programs are written." - javadocs on  
>> FileLock - http://java.sun.com/j2se/1.5.0/docs/api/java/nio/channels/FileLock.html
>>
>> But this does not work, at least the way I understand it should: http://jira.codehaus.org/secure/attachment/36986/NioTest.java 
>>  - this unit test shows how java.nio allows to have two valid  
>> exclusive locks on the same file [region].
>>
>> Does anyone have any insights on the problem? Maybe a hint to some  
>> 3rd party library that can help with inter-process synchronization?
>>
>> Thanks,
>> Oleg
>


---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@maven.apache.org
For additional commands, e-mail: dev-help@maven.apache.org


Re: NIO problem, any help appreciated!

Posted by Christian Edward Gruber <cg...@israfil.net>.
So is File.createNewFile() not actually back-ended by the platform's  
atomic file creation capability (that is, if there is one, it uses it,  
else there's a possible race)?  If so, I'd just use that.  Looking  
through the 1.5 source, it at least calls to  
FileSystem.createFileExclusively, which in Win32FileSystem,  
UnixFileSystem, and WinNTFileSystem are all implemented in native.  I  
haven't looked at the c source to validate the mechanism, but I think  
it would be safe to use that and rely on operating system capability  
for the specific semantic.

Christian.

On 11-Sep-08, at 16:45 , Oleg Gusakov wrote:

> I modified Mercury LocalRepositoryWriter to allow multiple builds to  
> write to the same repository without trashing metadata, but hit  
> several obstacles - http://jira.codehaus.org/browse/MERCURY-7
>
> Please help to find holes in the approach
>
> *Problem*: find a way to sync multiple JVMs on a single box. This -  
> to me - means that if I can sync multiple threads inside one JVM  
> without using *synchronized*, *wait*() and *notify*(), I achieve the  
> goal.
>
> *First* I tried creating a lock file in the file system, but without  
> using *synchronized* - I can always vary parameters and have two  
> threads obtain the lock: it's the infamous double-check locking  
> problem: http://www.cs.umd.edu/~pugh/java/memoryModel/DoubleCheckedLocking.html 
>  And having to think about multiple JVMs only aggravates the  
> situation.
>
> *Then* I tried java.nio approach, because "This file-locking API is  
> intended to map directly to the native locking facility of the  
> underlying operating system. Thus the locks held on a file should be  
> visible to all programs that have access to the file, regardless of  
> the language in which those programs are written." - javadocs on  
> FileLock - http://java.sun.com/j2se/1.5.0/docs/api/java/nio/channels/FileLock.html
>
> But this does not work, at least the way I understand it should: http://jira.codehaus.org/secure/attachment/36986/NioTest.java 
>  - this unit test shows how java.nio allows to have two valid  
> exclusive locks on the same file [region].
>
> Does anyone have any insights on the problem? Maybe a hint to some  
> 3rd party library that can help with inter-process synchronization?
>
> Thanks,
> Oleg


---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@maven.apache.org
For additional commands, e-mail: dev-help@maven.apache.org