* in file LICENSE in the top-level directory of the distribution.
*/
-/* File locking methods */
-/* only available if fcntl() locking is required */
+/* File locking methods
+ *
+ * lutil_lockf() will block until an exclusive lock is acquired.
+ */
#ifndef _LUTIL_LOCKF_H_
#define _LUTIL_LOCKF_H_
* - flock
*
* Other implementations will be added as needed.
+ *
+ * NOTE: lutil_lockf() MUST block until an exclusive lock is acquired.
*/
#include "portable.h"
#ifdef USE_LOCKF
int lutil_lockf ( int fd ) {
+ /* use F_LOCK instead of F_TLOCK, ie: block */
return lockf( fd, F_LOCK, 0 );
}
#ifdef USE_FCNTL
int lutil_lockf ( int fd ) {
struct flock file_lock;
+
memset( &file_lock, 0, sizeof( file_lock ) );
file_lock.l_type = F_WRLCK;
file_lock.l_whence = SEEK_SET;
file_lock.l_start = 0;
file_lock.l_len = 0;
+
+ /* use F_SETLKW instead of F_SETLK, ie: block */
return( fcntl( fd, F_SETLKW, &file_lock ) );
}
int lutil_unlockf ( int fd ) {
struct flock file_lock;
+
memset( &file_lock, 0, sizeof( file_lock ) );
file_lock.l_type = F_UNLCK;
file_lock.l_whence = SEEK_SET;
file_lock.l_start = 0;
file_lock.l_len = 0;
- return( fcntl ( fd, F_SETLK, &file_lock ) );
+
+ return( fcntl ( fd, F_SETLKW, &file_lock ) );
}
#endif
#ifdef USE_FLOCK
int lutil_lockf ( int fd ) {
+ /* use LOCK_EX instead of LOCK_EX|LOCK_NB, ie: block */
return flock( fd, LOCK_EX );
}