flock(p1)
public
Locks or unlocks a file according to locking_constant (a logical
or of the values in the table below). Returns false if
File::LOCK_NB is specified and the operation would otherwise have blocked.
Not available on all platforms.
Locking constants (in class File):
LOCK_EX | Exclusive lock. Only one process may hold an
| exclusive lock for a given file at a time.
----------+------------------------------------------------
LOCK_NB | Don't block when locking. May be combined
| with other lock options using logical or.
----------+------------------------------------------------
LOCK_SH | Shared lock. Multiple processes may each hold a
| shared lock for a given file at the same time.
----------+------------------------------------------------
LOCK_UN | Unlock.
Example:
File.new("testfile").flock(File::LOCK_UN)
Show source
/*
* call-seq:
* file.flock (locking_constant ) => 0 or false
*
* Locks or unlocks a file according to <i>locking_constant</i> (a
* logical <em>or</em> of the values in the table below).
* Returns <code>false</code> if <code>File::LOCK_NB</code> is
* specified and the operation would otherwise have blocked. Not
* available on all platforms.
*
* Locking constants (in class File):
*
* LOCK_EX | Exclusive lock. Only one process may hold an
* | exclusive lock for a given file at a time.
* ----------+------------------------------------------------
* LOCK_NB | Don't block when locking. May be combined
* | with other lock options using logical or.
* ----------+------------------------------------------------
* LOCK_SH | Shared lock. Multiple processes may each hold a
* | shared lock for a given file at the same time.
* ----------+------------------------------------------------
* LOCK_UN | Unlock.
*
* Example:
*
* File.new("testfile").flock(File::LOCK_UN) #=> 0
*
*/
static VALUE
rb_file_flock(obj, operation)
VALUE obj;
VALUE operation;
{
rb_io_t *fptr;
int op;
rb_secure(2);
op = NUM2INT(operation);
GetOpenFile(obj, fptr);
if (fptr->mode & FMODE_WRITABLE) {
fflush(GetWriteFile(fptr));
}
retry:
if (flock(fileno(fptr->f), op) < 0) {
switch (errno) {
case EAGAIN:
case EACCES:
case EWOULDBLOCK:
return Qfalse;
case EINTR:
case ERESTART:
goto retry;
}
rb_sys_fail(fptr->path);
}
return INT2FIX(0);
}