public bool Wait(int millisecondsTimeout)
{
lock (typeof(FileMutex))
{
if (handle == 0)
Open();
bool result = false;
ThreadStart placeLock = delegate()
{
// a write (exclusive) lock
wl.l_type = LockType.F_WRLCK;
int res = Syscall.fcntl(handle, FcntlCommand.F_SETLKW, ref wl);
if (res == 0 && Syscall.GetLastError() != Errno.EAGAIN)
result = true;
};
if (millisecondsTimeout == -1)
{
// Console.WriteLine("waiting in the calling thread");
placeLock();
}
else
{
// Console.WriteLine("waiting in the separate thread");
Thread t = new Thread(placeLock);
t.IsBackground = true;
t.Start();
if (!t.Join(millisecondsTimeout))
{
//timeout
t.Abort();
}
}
return result;
}
}