Changeset 9654


Ignore:
Timestamp:
Jun 2, 2008, 6:40:58 AM (11 years ago)
Author:
gb
Message:

Fail in attempts to use overlapped I/O.
Use an OVERLAPPED structure in windows_read() and note its use in
the TCR, so that blocking reads (at least those that happen here)
can be canceled.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • branches/win64/lisp-kernel/windows-calls.c

    r9334 r9654  
    3232*/
    3333
     34#include "lisp.h"
     35#include "x86-exceptions.h"
    3436#include <io.h>
    3537#include <unistd.h>
     
    4042#include <psapi.h>
    4143#include <dirent.h>
     44#include <stdio.h>
     45
     46
     47
     48#define _dosmaperr mingw_dosmaperr
     49
    4250
    4351#define WSYSCALL_RETURN(form) \
     
    5260
    5361
    54 #if 1
     62
    5563__int64
    5664windows_open(wchar_t *path, int flag, int mode)
    5765{
    58   WSYSCALL_RETURN(_wopen(path, flag, mode));
    59 }
    60 #else
    61 __int64
    62 windows_open(char *path, int flag, int mode)
    63 {
    64   WSYSCALL_RETURN(_open(path, flag, mode));
    65 }
     66  int fd;
     67  HANDLE hfile;
     68  DWORD dwDesiredAccess = 0;
     69  DWORD dwShareMode = 0;
     70  DWORD dwCreationDistribution = 0;
     71  DWORD dwFlagsAndAttributes = 0;
     72  SECURITY_ATTRIBUTES sa = {sizeof(SECURITY_ATTRIBUTES), NULL, TRUE};
     73
     74  if ((flag & S_IREAD) == S_IREAD) {
     75    dwShareMode = FILE_SHARE_READ;
     76  } else {
     77    if ((flag & S_IWRITE) == S_IWRITE) {
     78      dwShareMode = FILE_SHARE_READ | FILE_SHARE_WRITE;
     79    }
     80  }
     81
     82  if ((flag & _O_WRONLY) == _O_WRONLY) {
     83    dwDesiredAccess |= GENERIC_WRITE | FILE_WRITE_DATA |
     84      FILE_READ_ATTRIBUTES | FILE_WRITE_ATTRIBUTES;
     85  } else if ((flag & _O_RDWR) == _O_RDWR) {
     86    dwDesiredAccess |= GENERIC_WRITE|GENERIC_READ | FILE_READ_DATA |
     87      FILE_WRITE_DATA | FILE_READ_ATTRIBUTES | FILE_WRITE_ATTRIBUTES;
     88  } else {
     89    dwDesiredAccess |= GENERIC_READ | FILE_READ_DATA | FILE_READ_ATTRIBUTES |
     90      FILE_WRITE_ATTRIBUTES;
     91  }
     92   
     93  if ((flag & S_IREAD) == S_IREAD) {
     94    dwShareMode |= FILE_SHARE_READ;
     95  }
     96  if ((flag & S_IWRITE) == S_IWRITE) {
     97    dwShareMode |= FILE_SHARE_WRITE;
     98  }
     99
     100  if ((flag & (_O_CREAT | _O_EXCL)) == (_O_CREAT | _O_EXCL)) {
     101    dwCreationDistribution |= CREATE_NEW;
     102  } else if ((flag &  O_TRUNC) == O_TRUNC) {
     103    if ((flag &  O_CREAT) ==  O_CREAT) {
     104      dwCreationDistribution |= CREATE_ALWAYS;
     105    } else if ((flag & O_RDONLY) != O_RDONLY) {
     106      dwCreationDistribution |= TRUNCATE_EXISTING;
     107    }
     108  } else if ((flag & _O_APPEND) == _O_APPEND) {
     109    dwCreationDistribution |= OPEN_EXISTING;
     110  } else if ((flag &  _O_CREAT) == _O_CREAT) {
     111    dwCreationDistribution |= OPEN_ALWAYS;
     112  } else {
     113    dwCreationDistribution |= OPEN_EXISTING;
     114  }
     115  if ((flag &  _O_RANDOM) == _O_RANDOM) {
     116    dwFlagsAndAttributes |= FILE_FLAG_RANDOM_ACCESS;
     117  }
     118  if ((flag &  _O_SEQUENTIAL) == _O_SEQUENTIAL) {
     119    dwFlagsAndAttributes |= FILE_FLAG_SEQUENTIAL_SCAN;
     120  }
     121
     122  if ((flag &  _O_TEMPORARY) == _O_TEMPORARY) {
     123    dwFlagsAndAttributes |= FILE_FLAG_DELETE_ON_CLOSE;
     124  }
     125
     126  if ((flag &  _O_SHORT_LIVED) == _O_SHORT_LIVED) {
     127    dwFlagsAndAttributes |= FILE_FLAG_DELETE_ON_CLOSE;
     128  }
     129
     130  if (flag & _O_NOINHERIT) {
     131    sa.bInheritHandle = FALSE;
     132  }
     133
     134#if 0
     135  dwFlagsAndAttributes |= FILE_FLAG_OVERLAPPED;
    66136#endif
     137   
     138
     139  hfile = CreateFileW(path,
     140                      dwDesiredAccess,
     141                      dwShareMode,
     142                      &sa,
     143                      dwCreationDistribution,
     144                      dwFlagsAndAttributes,
     145                      NULL);
     146  if (hfile == ((HANDLE)-1)) {
     147    _dosmaperr(GetLastError());
     148    return -errno;
     149  }
     150  fd = _open_osfhandle((intptr_t)hfile, flag);
     151
     152  if (fd < 0) {
     153    CloseHandle(hfile);
     154    return -fd;
     155  }
     156  return fd;
     157}
     158
    67159__int64
    68160windows_close(int fd)
    69161{
    70   WSYSCALL_RETURN(_close(fd));
     162  WSYSCALL_RETURN(close(fd));
    71163}
    72164
     
    74166windows_read(int fd, void *buf, unsigned int count)
    75167{
    76   WSYSCALL_RETURN(_read(fd, buf, count));
     168  HANDLE hfile;
     169  OVERLAPPED overlapped;
     170  DWORD err, nread;
     171  pending_io pending;
     172  TCR *tcr;
     173  extern TCR *get_tcr(int);
     174
     175  hfile = (HANDLE) _get_osfhandle(fd);
     176
     177  if (hfile == ((HANDLE)-1)) {
     178    return -EBADF;
     179  }
     180 
     181  memset(&overlapped,0,sizeof(overlapped));
     182
     183  if (GetFileType(hfile) == FILE_TYPE_DISK) {
     184    overlapped.Offset = SetFilePointer(hfile, 0, &(overlapped.OffsetHigh), FILE_CURRENT);
     185  }
     186
     187  tcr = (TCR *)get_tcr(1);
     188  pending.h = hfile;
     189  pending.o = &overlapped;
     190  tcr->foreign_exception_status = (signed_natural)&pending;
     191
     192  if (ReadFile(hfile, buf, count, &nread, &overlapped)) {
     193    tcr->foreign_exception_status = 0;
     194    return nread;
     195  }
     196  err = GetLastError();
     197
     198  if (err == ERROR_HANDLE_EOF) {
     199    tcr->foreign_exception_status = 0;
     200    return 0;
     201  }
     202
     203  if (err != ERROR_IO_PENDING) {
     204    _dosmaperr(err);
     205    tcr->foreign_exception_status = 0;
     206    return -errno;
     207  }
     208 
     209  /* We block here */
     210  if (GetOverlappedResult(hfile, &overlapped, &nread, TRUE)) {
     211    tcr->foreign_exception_status = 0;
     212    return nread;
     213  }
     214  err = GetLastError();
     215  tcr->foreign_exception_status = 0;
     216
     217  switch (err) {
     218  case ERROR_HANDLE_EOF:
     219    return 0;
     220  case ERROR_OPERATION_ABORTED:
     221    return -EINTR;
     222  default:
     223    _dosmaperr(err);
     224    return -errno;
     225  }
    77226}
    78227
     
    211360
    212361void
     362init_windows_io()
     363{
     364#if 0
     365  int fd;
     366  HANDLE hfile0, hfile1;
     367
     368  hfile0 = (HANDLE) _get_osfhandle(0);
     369  hfile1 = ReOpenFile(hfile0,GENERIC_READ,FILE_SHARE_READ,FILE_FLAG_OVERLAPPED);
     370  if (hfile1 != ((HANDLE)-1)) {
     371    fd = _open_osfhandle(hfile1,O_RDONLY);
     372    dup2(fd,0);
     373    _close(fd);
     374    SetStdHandle(STD_INPUT_HANDLE,hfile1);
     375    CloseHandle(hfile0);
     376  } else {
     377    wperror("ReOpenFile");
     378  }
     379
     380  hfile0 = (HANDLE) _get_osfhandle(1);
     381  hfile1 = ReOpenFile(hfile0,GENERIC_WRITE,FILE_SHARE_WRITE,FILE_FLAG_OVERLAPPED);
     382  if (hfile1 != ((HANDLE)-1)) {
     383    fd = _open_osfhandle(hfile1,O_WRONLY);
     384    dup2(fd,1);
     385    _close(fd);
     386    SetStdHandle(STD_OUTPUT_HANDLE,hfile1);
     387    CloseHandle(hfile0);
     388  }
     389
     390  hfile0 = (HANDLE) _get_osfhandle(2);
     391  hfile1 = ReOpenFile(hfile0,GENERIC_WRITE,FILE_SHARE_WRITE,FILE_FLAG_OVERLAPPED);
     392  if (hfile1 != ((HANDLE)-1)) {
     393    fd = _open_osfhandle(hfile1,O_WRONLY);
     394    dup2(fd,2);
     395    _close(fd);
     396    SetStdHandle(STD_ERROR_HANDLE,hfile1);
     397    CloseHandle(hfile0);
     398  }
     399#endif 
     400}
     401
     402void
    213403init_winsock()
    214404{
Note: See TracChangeset for help on using the changeset viewer.