dotgnu-general
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [DotGNU]implementing System.IO.FileStream.Lock/Unlock


From: Stephen Compall
Subject: Re: [DotGNU]implementing System.IO.FileStream.Lock/Unlock
Date: 29 Oct 2003 21:58:56 +0000
User-agent: Gnus/5.09 (Gnus v5.9.0) Emacs/21.2

Rhys Weatherley <address@hidden> writes:

> I think it is exclusive, using the normal locking primitives under
> Win32.  The "lockf" function is the one that you are after, falling
> back to "flock" if that isn't present.  Read the man pages for
> "lockf", "flock", and "fcntl" carefully first.

lockf and flock don't provide the needed options -- specifically, the
range.  Oh, lockf claims to, but *I* don't see a POS argument, not
even in the actual C declaration.  Besides, they aren't documented in
the glibc manual, which is much nicer than crap man pages, and fcntl
is, so I used that.

However, the code may be naive.  I did it on a Windows machine without
access to the source, testing, etc.  Most probably, I got some of the
IL types wrong (off_t probably doesn't match ILNativeInt).
Furthermore, calling fcntl normally assumes non-LFS, I think.  I will
test/compile/crash/repeat when I get back, but it may be faster for
someone else to find the problems in it:

int ILSysIOLock(ILSysIOHandle handle, ILInt64 position, ILInt64 length)
{
#ifdef HAVE_FCNTL
    struct flock cntl_data;
    /* set fields individually...who knows what extras are there? */
    cntl_data.l_type = F_WRLCK;
    cntl_data.l_whence = SEEK_SET;
    cntl_data.l_start = (off_t)(ILNativeInt) position;
    cntl_data.l_len = (off_t)(ILNativeInt) length;
    /* -1 is error, anything else is OK */
    if (fcntl ((int)(ILNativeInt) handle, F_SETLKW, &cntl_data) != -1)
        return 0;
    else
        return 1;
#else /* !defined(HAVE_FCNTL) */
    /* Locking is not supported w/o fcntl - TODO */
    return 1;
#endif /* !defined(HAVE_FCNTL) */
}

int ILSysIOUnlock(ILSysIOHandle handle, ILInt64 position, ILInt64 length)
{
#ifdef HAVE_FCNTL
    struct flock cntl_data;
    /* set fields individually...who knows what extras are there? */
    cntl_data.l_type = F_UNLCK;
    cntl_data.l_whence = SEEK_SET;
    cntl_data.l_start = (off_t)(ILNativeInt) position;
    cntl_data.l_len = (off_t)(ILNativeInt) length;
    /* -1 is error, anything else is OK */
    if (fcntl ((int)(ILNativeInt) handle, F_SETLKW, &cntl_data) != -1)
        return 0;
    else
        return 1;
#else /* !defined(HAVE_FCNTL) */
    /* Locking is not supported w/o fcntl - TODO */
    return 1;
#endif /* !defined(HAVE_FCNTL) */
}

Implementers of alternatives should note that 0 as the length means
"infinity".

--
Stephen Compall or s11 or sirian

The Law of the Letter:
        The best way to inspire fresh thoughts is to seal the envelope.

Ft. Meade Albanian Bosnia CBNRC Commecen plutonium supercomputer AK-47
bce UNSCOM Nazi Qaddafi Jiang Zemin hackers Pine Gap


reply via email to

[Prev in Thread] Current Thread [Next in Thread]