* file.c (rb_thread_flock, rb_file_flock): use UBF feature.

git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@11865 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
This commit is contained in:
nobu 2007-02-24 09:45:41 +00:00
parent 145f0b0f50
commit 8ac59769bc
2 changed files with 24 additions and 46 deletions

68
file.c
View File

@ -3082,51 +3082,24 @@ rb_file_truncate(VALUE obj, VALUE len)
#ifdef __CYGWIN__ #ifdef __CYGWIN__
#include <winerror.h> #include <winerror.h>
static int #endif
cygwin_flock(int fd, int op)
static VALUE
rb_thread_flock(rb_thread_t *th, void *data)
{ {
#ifdef __CYGWIN__
int old_errno = errno; int old_errno = errno;
int ret = flock(fd, op); #endif
int *op = data, ret = flock(op[0], op[1]);
#ifdef __CYGWIN__
if (GetLastError() == ERROR_NOT_LOCKED) { if (GetLastError() == ERROR_NOT_LOCKED) {
ret = 0; ret = 0;
errno = old_errno; errno = old_errno;
} }
return ret; #endif
return (VALUE)ret;
} }
# define flock(fd, op) cygwin_flock(fd, op)
#endif
static int
rb_thread_flock(int fd, int op, OpenFile *fptr)
{
if (rb_thread_alone() || (op & LOCK_NB)) {
int ret;
TRAP_BEG;
ret = flock(fd, op);
TRAP_END;
return ret;
}
op |= LOCK_NB;
while (flock(fd, op) < 0) {
switch (errno) {
case EAGAIN:
case EACCES:
#if defined(EWOULDBLOCK) && EWOULDBLOCK != EAGAIN
case EWOULDBLOCK:
#endif
rb_thread_polling(); /* busy wait */
rb_io_check_closed(fptr);
continue;
default:
return -1;
}
}
return 0;
}
#ifdef __CYGWIN__
# undef flock
#endif
#define flock(fd, op) rb_thread_flock(fd, op, fptr)
/* /*
* call-seq: * call-seq:
@ -3162,31 +3135,36 @@ rb_file_flock(VALUE obj, VALUE operation)
{ {
#ifndef __CHECKER__ #ifndef __CHECKER__
OpenFile *fptr; OpenFile *fptr;
int op; int op[2];
rb_secure(2); rb_secure(2);
op = NUM2INT(operation); op[1] = NUM2INT(operation);
GetOpenFile(obj, fptr); GetOpenFile(obj, fptr);
op[0] = fptr->fd;
if (fptr->mode & FMODE_WRITABLE) { if (fptr->mode & FMODE_WRITABLE) {
rb_io_flush(obj); rb_io_flush(obj);
} }
retry: while ((int)rb_thread_blocking_region(rb_thread_flock, op, RB_UBF_DFL) < 0) {
if (flock(fptr->fd, op) < 0) {
switch (errno) { switch (errno) {
case EAGAIN: case EAGAIN:
case EACCES: case EACCES:
#if defined(EWOULDBLOCK) && EWOULDBLOCK != EAGAIN #if defined(EWOULDBLOCK) && EWOULDBLOCK != EAGAIN
case EWOULDBLOCK: case EWOULDBLOCK:
#endif #endif
return Qfalse; rb_thread_polling();
rb_io_check_closed(fptr);
continue;
case EINTR: case EINTR:
#if defined(ERESTART) #if defined(ERESTART)
case ERESTART: case ERESTART:
#endif #endif
goto retry; break;
default:
rb_sys_fail(fptr->path);
} }
rb_sys_fail(fptr->path);
} }
#endif #endif
return INT2FIX(0); return INT2FIX(0);

View File

@ -3851,7 +3851,7 @@ rb_w32_asynchronize(asynchronous_func_t func, VALUE self,
if (thr) { if (thr) {
yield_until(arg.stackaddr); yield_until(arg.stackaddr);
if (rb_w32_wait_events(&thr, 1, INFINITE) != WAIT_OBJECT_0) { if (rb_w32_wait_events_blocking(&thr, 1, INFINITE) != WAIT_OBJECT_0) {
interrupted = TRUE; interrupted = TRUE;
if (TerminateThread(thr, intrval)) { if (TerminateThread(thr, intrval)) {