Changeset 11096


Ignore:
Timestamp:
Oct 14, 2008, 11:59:28 PM (11 years ago)
Author:
gb
Message:

Stop supporting file descriptors for Windows file/device I/O.
(A handle on win64 is 64 bits wide; since a handle is an offset into
a kernel table, it seens very unlikely that this would be an issue,
but we should probably change the callers.)

Use the per-thread event handle in tcr->io_datum in the overlapped
struct on reads and writes; this should mean that things like
GetOverlappedResult?() return when that event is signaled, not when
the file handle is signaled because of other I/O activity in other
threads.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/source/lisp-kernel/windows-calls.c

    r11085 r11096  
    178178#define MAX_FD 32
    179179
    180 int
     180HANDLE
    181181lisp_open(wchar_t *path, int flag, int mode)
    182182{
     
    262262  if (hfile == ((HANDLE)-1)) {
    263263    _dosmaperr(GetLastError());
    264     return -1;
    265   }
    266   return (int)hfile;
     264    return (HANDLE)-1;
     265  }
     266  return hfile;
    267267}
    268268
    269269int
    270 lisp_close(int fd)
    271 {
    272   if (fd < MAX_FD) {
    273     return close(fd);
    274   }
    275   if (CloseHandle((HANDLE)fd)) {
     270lisp_close(HANDLE hfile)
     271{
     272  if (CloseHandle(hfile)) {
    276273    return 0;
    277274  }
     
    280277}
    281278
     279extern TCR *get_tcr(int);
     280
    282281ssize_t
    283 lisp_read(int fd, void *buf, unsigned int count)
    284 {
    285   HANDLE hfile;
     282lisp_read(HANDLE hfile, void *buf, unsigned int count)
     283{
     284  HANDLE hevent;
    286285  OVERLAPPED overlapped;
    287286  DWORD err, nread;
    288287  pending_io pending;
    289288  TCR *tcr;
    290   extern TCR *get_tcr(int);
    291 
    292   if (fd < MAX_FD) {
    293     hfile = (HANDLE) _get_osfhandle(fd);
    294     if (hfile == ((HANDLE)-1)) {
    295       errno = EBADF;
    296       return -1;
    297     }
    298   } else {
    299     hfile = (HANDLE) fd;
    300   }
     289
    301290 
    302291  memset(&overlapped,0,sizeof(overlapped));
     
    309298  pending.h = hfile;
    310299  pending.o = &overlapped;
    311   tcr->foreign_exception_status = (signed_natural)&pending;
    312 
     300  tcr->pending_io_info = &pending;
     301  hevent = (HANDLE)(tcr->io_datum);
     302  overlapped.hEvent = hevent;
    313303  do {
     304    ResetEvent(hevent);
    314305    if (ReadFile(hfile, buf, count, &nread, &overlapped)) {
    315       tcr->foreign_exception_status = 0;
     306      tcr->pending_io_info = NULL;
    316307      return nread;
    317308    }
     
    319310
    320311    if (err == ERROR_HANDLE_EOF) {
    321       tcr->foreign_exception_status = 0;
     312      tcr->pending_io_info = NULL;
    322313      return 0;
    323314    }
     
    325316    if (err != ERROR_IO_PENDING) {
    326317      _dosmaperr(err);
    327       tcr->foreign_exception_status = 0;
     318      tcr->pending_io_info = NULL;
    328319      return -1;
    329320    }
     
    333324    if (GetOverlappedResult(hfile, &overlapped, &nread, TRUE)) {
    334325      if (nread) {
    335         tcr->foreign_exception_status = 0;
     326        tcr->pending_io_info = NULL;
    336327        return nread;
    337328      }
     
    340331    }
    341332  } while (!err);
    342   tcr->foreign_exception_status = 0;
     333  tcr->pending_io_info = NULL;
    343334
    344335  switch (err) {
     
    355346
    356347ssize_t
    357 lisp_write(int fd, void *buf, ssize_t count)
    358 {
    359   HANDLE hfile;
     348lisp_write(HANDLE hfile, void *buf, ssize_t count)
     349{
     350  HANDLE hevent;
    360351  OVERLAPPED overlapped;
    361352  DWORD err, nwritten;
    362 
    363   if (fd < MAX_FD) {
    364     hfile = (HANDLE) _get_osfhandle(fd);
    365 
    366     if (hfile == ((HANDLE)-1)) {
    367       errno = EBADF;
    368       return -1;
    369     }
    370   } else {
    371     hfile = (HANDLE) fd;
    372   }
     353  TCR *tcr = (TCR *)get_tcr(1);
     354
     355  hevent = (HANDLE)tcr->io_datum;
     356
    373357
    374358  memset(&overlapped,0,sizeof(overlapped));
     
    378362  }
    379363
     364  overlapped.hEvent = hevent;
     365  ResetEvent(hevent);
    380366  if (WriteFile(hfile, buf, count, &nwritten, &overlapped)) {
    381367    return nwritten;
     
    388374
    389375int
    390 lisp_fchmod(int fd, int mode)
     376lisp_fchmod(HANDLE hfile, int mode)
    391377{
    392378  errno = ENOSYS;
     
    395381
    396382__int64
    397 lisp_lseek(int fd, __int64 offset, int whence)
    398 {
    399   HANDLE hfile;
     383lisp_lseek(HANDLE hfile, __int64 offset, int whence)
     384{
    400385  DWORD high, low;
    401386
    402   if (fd < MAX_FD) {
    403     hfile = (HANDLE) _get_osfhandle(fd);
    404 
    405     if (hfile == ((HANDLE)-1)) {
    406       errno = EBADF;
    407       return -1;
    408     }
    409   } else {
    410     hfile = (HANDLE) fd;
    411   }
    412387  high = ((__int64)offset)>>32;
    413388  low = offset & 0xffffffff;
     
    441416
    442417int
    443 lisp_fstat(int fd, struct __stat64 *buf)
    444 {
    445   HANDLE hfile;
     418lisp_fstat(HANDLE hfile, struct __stat64 *buf)
     419{
    446420  int filetype;
    447421
    448   if (fd < MAX_FD) {
    449     hfile = (HANDLE) _get_osfhandle(fd);
    450 
    451     if (hfile == ((HANDLE)-1)) {
    452       errno = EBADF;
    453       return -1;
    454     }
    455   } else {
    456     hfile = (HANDLE) fd;
    457   }
    458422  filetype = GetFileType(hfile) & ~FILE_TYPE_REMOTE;
    459423
     
    514478
    515479__int64
    516 lisp_ftruncate(int fd, off_t new_size)
    517 {
    518   HANDLE hfile;
     480lisp_ftruncate(HANDLE hfile, off_t new_size)
     481{
    519482  __int64 oldpos;
    520483
    521   if (fd < MAX_FD) {
    522     hfile = (HANDLE) _get_osfhandle(fd);
    523 
    524     if (hfile == ((HANDLE)-1)) {
    525       errno = EBADF;
    526       return -1;
    527     }
    528   } else {
    529     hfile = (HANDLE) fd;
    530   }
    531 
    532   oldpos = lisp_lseek((int)hfile, 0, SEEK_END);
     484
     485  oldpos = lisp_lseek(hfile, 0, SEEK_END);
    533486  if (oldpos == -1) {
    534487    return 0;
     
    554507    return 0;
    555508  }
    556   lisp_lseek((int)hfile, new_size, SEEK_SET);
     509  lisp_lseek(hfile, new_size, SEEK_SET);
    557510  if (SetEndOfFile(hfile)) {
    558511    return 0;
     
    596549      return -1;
    597550    }
    598   fd[0] = (int) input;
    599   fd[1] = (int) output;
     551  fd[0] = (int) ((intptr_t)input);
     552  fd[1] = (int) ((intptr_t)output);
    600553  return 0;
    601554}
Note: See TracChangeset for help on using the changeset viewer.