1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377
|
/* sqUnixAsynchFile.c -- non-blocking file i/o
*
* Copyright (C) 1996-2004 by Ian Piumarta and other authors/contributors
* listed elsewhere in this file.
* All rights reserved.
*
* This file is part of Unix Squeak.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*
* Author: Ian.Piumarta@INRIA.Fr
*
* Last edited: 2006-10-18 10:10:25 by piumarta on emilia.local
*/
/*
Experimental support for asynchronous file reading and writing.
When a read or write operation is initiated, control is returned to Squeak
immediately. A semaphore is signaled when the operation completes, at which
time the client can find out how many bytes were actually read or written
and copy the results of the read operation from the file buffer into a Squeak
buffer. Only one operation may be in progress on a given file at a given time,
but operations on different files may be done in parallel.
The semaphore is signalled once for each transfer operation that is successfully
started, even if that operation later fails. Write operations always write
their entire buffer if they succeed, but read operations may transfer less than
their buffer size if they are started less than a buffer's size from the end
of the file.
The state of a file is kept in the following structure, which is stored directly
in a Squeak ByteArray object:
typedef struct {
int sessionID;
void *state; // private to the implementation
} AsyncFile;
The session ID is used to detect stale files--files that were open
when the image was saved. The state pointer of such files is meaningless.
Async file handles use the same session ID as ordinary file handles.
Note: These primitives are experimental! They need not be implemented on
every platform, and they may be withdrawn or replaced in a future release.
*/
#include "sq.h"
#include "AsynchFilePlugin.h"
#include "sqUnixAsynchFile.h"
/*** module initialisation ***/
#include "sqVirtualMachine.h"
#include "sqaio.h"
#include <sys/types.h>
#include <unistd.h>
#include <time.h>
int sqUnixAsyncFileSessionID= 0;
static struct VirtualMachine *vm= 0;
static fd_set fds;
static int nfd= 0;
#define isValid(f) (f->sessionID == sqUnixAsyncFileSessionID)
#define validate(f) if ((!isValid(f)) || (!(f->state))) return vm->primitiveFail()
int asyncFileInit(void)
{
vm= sqGetInterpreterProxy();
sqUnixAsyncFileSessionID= clock() + time(0);
FD_ZERO(&fds);
nfd= 0;
return 1;
}
int asyncFileShutdown(void)
{
/* protect against calling stale aio handlers */
int i;
for (i= 0; i < nfd; ++i)
if (FD_ISSET(i, &fds))
aioDisable(i);
nfd= 0;
FD_ZERO(&fds);
sqUnixAsyncFileSessionID= 0;
return 1;
}
/*** module ***/
#include <stdlib.h>
#include <string.h>
#include <fcntl.h>
#include <errno.h>
#ifdef __GNUC__
# define INLINE inline
#else
# define INLINE
#endif
#define min(a,b) ((a) < (b) ? (a) : (b))
#define max(a,b) ((a) > (b) ? (a) : (b))
static void readHandler(int fd, void *data, int flags);
static void writeHandler(int fd, void *data, int flags);
INLINE static FilePtr newFileRec(int fd, int sema)
{
FilePtr fp= (FilePtr)calloc(1, sizeof(FileRec));
if (fp)
{
fp->fd= fd;
fp->sema= sema;
fp->rd.status= Busy; /* read not ready */
fp->wr.status= Busy; /* write not complete */
}
return fp;
}
INLINE static allocateBuffer(struct FileBuf *buf, int size)
{
if (buf->capacity >= size)
return 1;
if (buf->capacity > 0)
{
free(buf->bytes);
buf->capacity= 0;
}
buf->bytes= (char *)malloc(size);
if (!buf->bytes)
{
fprintf(stderr, "out of memory\n");
return 0;
}
buf->capacity= size;
return 1;
}
FilePtr asyncFileAttach(AsyncFile *f, int fd, int semaIndex)
{
FilePtr fp= newFileRec(fd, semaIndex);
if (fp)
{
f->sessionID= sqUnixAsyncFileSessionID;
f->state= (void *)fp;
aioEnable(fd, (void *)fp, 0);
FD_SET(fd, &fds);
nfd= max(nfd, fd + 1);
return fp; /* success */
}
fprintf(stderr, "out of memory\n");
f->sessionID= 0;
f->state= 0;
return 0;
}
/*** public functions ***/
int asyncFileOpen(AsyncFile *f, int fileNamePtr, int fileNameSize,
int writeFlag, int semaIndex)
{
int fd= 0;
char *name= alloca(fileNameSize + 1);
memcpy((void *)name, (void *)fileNamePtr, fileNameSize);
name[fileNameSize]= '\0';
/* if opening for wr then open for rw so that we can use these primitives
to read bidirectional files (e.g., master ptys for interactive child
processes) */
fd= (writeFlag
? open(name, O_RDWR | O_CREAT, 0644)
: open(name, O_RDONLY));
if (fd >= 0)
{
if (asyncFileAttach(f, fd, semaIndex))
return 0; /* success */
close(fd);
}
vm->primitiveFail();
return 0; /* failure */
}
int asyncFileClose(AsyncFile *f)
{
FilePtr fp= 0;
validate(f);
if ((fp= (FilePtr)f->state))
{
if (fp->fd >= 0)
{
aioDisable(fp->fd);
FD_CLR(fp->fd, &fds);
close(fp->fd);
}
if (fp->buf.bytes)
free((void *)fp->buf.bytes);
free((void *)fp);
f->state= 0;
}
return 0; /* success */
}
/* this no longer appears to be used */
int asyncFileRecordSize(void)
{
fprintf(stderr, "asyncFileRecordSize() called -- why?\n");
vm->primitiveFail();
return 0;
}
int asyncFileReadResult(AsyncFile *f, int bufferPtr, int bufferSize)
{
FilePtr fp= 0;
int n= 0;
validate(f);
fp= (FilePtr)f->state;
n= read(fp->fd, (void *)bufferPtr, bufferSize);
if ((n < 0) && (errno == EWOULDBLOCK))
return fp->rd.status= Busy;
else if (n <= 0)
return fp->rd.status= Error;
else /* (n > 0) */
fp->rd.pos += n;
return fp->rd.status= n;
}
static void readHandler(int fd, void *data, int flags)
{
signalSemaphoreWithIndex(((FilePtr)data)->sema);
}
int asyncFileReadStart(AsyncFile *f, int fPosition, int count)
{
FilePtr fp= 0;
validate(f);
fp= (FilePtr)f->state;
if (( (fPosition >= 0)) /* (fPos < 0) => current position */
&& (fp->rd.pos != fPosition)) /* avoid EPIPE on pty */
{
if (lseek(fp->fd, fPosition, SEEK_SET) < 0)
{
perror("lseek");
goto fail;
}
fp->rd.pos= fPosition;
}
fp->rd.status= Busy;
aioHandle(fp->fd, readHandler, AIO_R);
return 0;
fail:
fp->rd.status= Error;
vm->primitiveFail();
return 0;
}
int asyncFileWriteResult(AsyncFile *f)
{
int n= 0;
FilePtr fp= 0;
validate(f);
fp= (FilePtr)f->state;
n= fp->wr.status;
fp->wr.status= Busy;
return n;
}
static void writeBuffer(FilePtr fp)
{
int n= 0;
while ((n= fp->buf.size - fp->buf.pos) > 0)
{
n= write(fp->fd, (void *)(fp->buf.bytes + fp->buf.pos), n);
if (n < 0)
switch (errno)
{
case EWOULDBLOCK:
aioHandle(fp->fd, writeHandler, AIO_W);
return;
default:
fp->wr.status= Error;
return;
}
fp->buf.pos += n;
fp->wr.pos += n;
}
/* completed */
fp->wr.status= fp->buf.size;
signalSemaphoreWithIndex(fp->sema);
}
static void writeHandler(int fd, void *data, int flags)
{
writeBuffer((FilePtr)data);
}
int asyncFileWriteStart(AsyncFile *f, int fPosition, int bufferPtr, int count)
{
FilePtr fp= 0;
validate(f);
fp= (FilePtr)f->state;
if (( (fPosition >= 0)) /* (fPos < 0) => current position */
&& (fp->wr.pos != fPosition)) /* avoid EPIPE on tty */
{
if (lseek(fp->fd, fPosition, SEEK_SET) < 0)
{
perror("lseek");
goto fail;
}
fp->wr.pos= fPosition;
}
if (count < 1)
{
fp->wr.status= 0;
signalSemaphoreWithIndex(fp->sema);
return 0;
}
if (!allocateBuffer(&fp->buf, count))
{
fprintf(stderr, "out of memory\n");
goto fail;
}
memcpy((void *)fp->buf.bytes, (void *)bufferPtr, count);
fp->buf.pos= 0; /* current output pointer */
fp->buf.size= count; /* bytes to transfer */
fp->wr.status= Busy; /* transfer in progress */
writeBuffer(fp); /* begin transfer */
return 0;
fail:
fp->wr.status= Error;
vm->primitiveFail();
return 0;
}
|