1
0
Fork 0
mirror of https://github.com/ruby/ruby.git synced 2022-11-09 12:17:21 -05:00

win32.c: extract setup_overlapped and finish_overlapped

* win32/win32.c (setup_overlapped, finish_overlapped): extract from
  rb_w32_read() and rb_w32_write().


git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@40887 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
This commit is contained in:
nobu 2013-05-22 06:17:33 +00:00
parent 5aece50163
commit 38302ca719
2 changed files with 49 additions and 56 deletions

View file

@ -1,3 +1,8 @@
Wed May 22 15:17:30 2013 Nobuyoshi Nakada <nobu@ruby-lang.org>
* win32/win32.c (setup_overlapped, finish_overlapped): extract from
rb_w32_read() and rb_w32_write().
Wed May 22 14:19:56 2013 Koichi Sasada <ko1@atdot.net>
* gc.c (gc_prepare_free_objects, rest_sweep, lazy_sweep): fix position

View file

@ -6016,6 +6016,46 @@ rb_w32_close(int fd)
return 0;
}
static int
setup_overlapped(OVERLAPPED *ol, int fd)
{
memset(ol, 0, sizeof(*ol));
if (!(_osfile(fd) & (FDEV | FPIPE))) {
LONG high = 0;
DWORD method = _osfile(fd) & FAPPEND ? FILE_END : FILE_CURRENT;
DWORD low = SetFilePointer((HANDLE)_osfhnd(fd), 0, &high, method);
#ifndef INVALID_SET_FILE_POINTER
#define INVALID_SET_FILE_POINTER ((DWORD)-1)
#endif
if (low == INVALID_SET_FILE_POINTER) {
errno = map_errno(GetLastError());
return -1;
}
ol->Offset = low;
ol->OffsetHigh = high;
}
ol->hEvent = CreateEvent(NULL, TRUE, TRUE, NULL);
if (!ol->hEvent) {
errno = map_errno(GetLastError());
return -1;
}
return 0;
}
static void
finish_overlapped(OVERLAPPED *ol, int fd, DWORD size)
{
CloseHandle(ol->hEvent);
if (!(_osfile(fd) & (FDEV | FPIPE))) {
LONG high = ol->OffsetHigh;
DWORD low = ol->Offset + size;
if (low < ol->Offset)
++high;
SetFilePointer((HANDLE)_osfhnd(fd), low, &high, FILE_BEGIN);
}
}
#undef read
/* License: Ruby's */
ssize_t
@ -6076,25 +6116,7 @@ rb_w32_read(int fd, void *buf, size_t size)
/* if have cancel_io, use Overlapped I/O */
if (cancel_io) {
memset(&ol, 0, sizeof(ol));
if (!(_osfile(fd) & (FDEV | FPIPE))) {
LONG high = 0;
DWORD low = SetFilePointer((HANDLE)_osfhnd(fd), 0, &high,
FILE_CURRENT);
#ifndef INVALID_SET_FILE_POINTER
#define INVALID_SET_FILE_POINTER ((DWORD)-1)
#endif
if (low == INVALID_SET_FILE_POINTER) {
errno = map_errno(GetLastError());
MTHREAD_ONLY(LeaveCriticalSection(&_pioinfo(fd)->lock));
return -1;
}
ol.Offset = low;
ol.OffsetHigh = high;
}
ol.hEvent = CreateEvent(NULL, TRUE, TRUE, NULL);
if (!ol.hEvent) {
errno = map_errno(GetLastError());
if (setup_overlapped(&ol, fd)) {
MTHREAD_ONLY(LeaveCriticalSection(&_pioinfo(fd)->lock));
return -1;
}
@ -6152,15 +6174,7 @@ rb_w32_read(int fd, void *buf, size_t size)
}
if (pol) {
CloseHandle(ol.hEvent);
if (!(_osfile(fd) & (FDEV | FPIPE))) {
LONG high = ol.OffsetHigh;
DWORD low = ol.Offset + read;
if (low < ol.Offset)
++high;
SetFilePointer((HANDLE)_osfhnd(fd), low, &high, FILE_BEGIN);
}
finish_overlapped(&ol, fd, read);
}
ret += read;
@ -6220,25 +6234,7 @@ rb_w32_write(int fd, const void *buf, size_t size)
/* if have cancel_io, use Overlapped I/O */
if (cancel_io) {
memset(&ol, 0, sizeof(ol));
if (!(_osfile(fd) & (FDEV | FPIPE))) {
LONG high = 0;
DWORD method = _osfile(fd) & FAPPEND ? FILE_END : FILE_CURRENT;
DWORD low = SetFilePointer((HANDLE)_osfhnd(fd), 0, &high, method);
#ifndef INVALID_SET_FILE_POINTER
#define INVALID_SET_FILE_POINTER ((DWORD)-1)
#endif
if (low == INVALID_SET_FILE_POINTER) {
errno = map_errno(GetLastError());
MTHREAD_ONLY(LeaveCriticalSection(&_pioinfo(fd)->lock));
return -1;
}
ol.Offset = low;
ol.OffsetHigh = high;
}
ol.hEvent = CreateEvent(NULL, TRUE, TRUE, NULL);
if (!ol.hEvent) {
errno = map_errno(GetLastError());
if (setup_overlapped(&ol, fd)) {
MTHREAD_ONLY(LeaveCriticalSection(&_pioinfo(fd)->lock));
return -1;
}
@ -6284,15 +6280,7 @@ rb_w32_write(int fd, const void *buf, size_t size)
}
if (pol) {
CloseHandle(ol.hEvent);
if (!(_osfile(fd) & (FDEV | FPIPE))) {
LONG high = ol.OffsetHigh;
DWORD low = ol.Offset + written;
if (low < ol.Offset)
++high;
SetFilePointer((HANDLE)_osfhnd(fd), low, &high, FILE_BEGIN);
}
finish_overlapped(&ol, fd, written);
}
ret += written;