This is the mail archive of the cygwin-talk@cygwin.com mailing list for the cygwin project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

Re: lockf replacement?


Christopher Faylor wrote:

On Tue, Oct 26, 2004 at 09:10:17PM +0200, Gerrit P. Haase wrote:

I found some sources where the guy uses _locking from the WIN API to
replace lockf, I adapted this and linked the Cygwin executable against
/usr/lib/mingw/libcrtdll.a.

Is this a possible way to go?  Or does libcrtdll conflict with
libcygwin?  At least the application seems to run basically, I have not
tested the relevant parts though.


Are you just trying to implement lockf?

Why are you talking about linking libcrtdll.a with a cygwin executable
rather than just modifying the cygwin DLL?

Yes, this is the right idea. If it could be done via a similar Windows function it should be trivial. Is it trivial? Usually the _locking is in msvcrt but I found that it is also exported from crtdll. From where gets cygwin the _locking function in this case?


Would require the definitions from mingw/sys/locking.h maybe best place would be unistd.h, at least according to opengroup specs the lockf macros should go there? And maybe a lockf.h header which just includes unistd.h?

#define	_LK_UNLCK	0	/* Unlock */
#define	_LK_LOCK	1	/* Lock */
#define	_LK_NBLCK	2	/* Non-blocking lock */
#define	_LK_RLCK	3	/* Lock for read only */
#define	_LK_NBRLCK	4	/* Non-blocking lock for read only */

#define	LK_UNLCK	_LK_UNLCK
#define	LK_LOCK		_LK_LOCK
#define	LK_NBLCK	_LK_NBLCK
#define	LK_RLCK		_LK_RLCK
#define	LK_NBRLCK	_LK_NBRLCK


/* mappings for lockf */


#define lockf _locking

#define F_ULOCK _LK_UNLCK /* Unlock locked sections. */
#define F_LOCK  _LK_LOCK  /* Lock a section for exclusive use. */
#define F_TLOCK _LK_NBLCK /* Test and lock a section for exclusive use */
#define F_TEST  _LK_RLCK  /* Test section for locks by other processes. */



Actually it looks to me like they just took BSD source and renamed lockf to _locking.

From msdn.microsoft.com/library/en-us/vclib/html/_crt__locking.asp:

_locking

Locks or unlocks bytes of a file.

int _locking(
   int fd,
   int mode,
   long nbytes
);

Parameters

fd
    File descriptor.
mode
    Locking action to perform.
nbytes
    Number of bytes to lock.

Return Value

_locking returns 0 if successful. A return value of â1 indicates failure, in which case errno is set to one of the following values:

EACCES
    Locking violation (file already locked or unlocked).
EBADF
    Invalid file descriptor.
EDEADLOCK
    Locking violation. Returned when the _LK_LOCK or _LK_RLCK flag is
    specified and the file cannot be locked after 10 attempts.
EINVAL
    An invalid argument was given to _locking.

Remarks

The _locking function locks or unlocks nbytes bytes of the file specified by fd. Locking bytes in a file prevents access to those bytes by other processes. All locking or unlocking begins at the current position of the file pointer and proceeds for the next nbytes bytes. It is possible to lock bytes past end of file.

mode must be one of the following manifest constants, which are defined in LOCKING.H:

_LK_LOCK
    Locks the specified bytes. If the bytes cannot be locked, the
    program immediately tries again after 1 second. If, after 10
    attempts, the bytes cannot be locked, the constant returns an error.
_LK_NBLCK
    Locks the specified bytes. If the bytes cannot be locked, the
    constant returns an error.
_LK_NBRLCK
    Same as _LK_NBLCK.
_LK_RLCK
    Same as _LK_LOCK.
_LK_UNLCK
    Unlocks the specified bytes, which must have been previously locked.

Multiple regions of a file that do not overlap can be locked. A region being unlocked must have been previously locked. _locking does not merge adjacent regions; if two locked regions are adjacent, each region must be unlocked separately. Regions should be locked only briefly and should be unlocked before closing a file or exiting the program.

Requirements

Routine 	Required header 	Optional headers 	
_locking 	<io.h> and <sys/locking.h> 	<errno.h> 	

Compatibility
Win 98, Win Me, Win NT, Win 2000, Win XP




-- =^..^=


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]