michael@0: /* $NetBSD: ev_streams.c,v 1.2 2004/05/20 19:52:31 christos Exp $ */ michael@0: michael@0: /* michael@0: * Copyright (c) 2004 by Internet Systems Consortium, Inc. ("ISC") michael@0: * Copyright (c) 1996-1999 by Internet Software Consortium michael@0: * michael@0: * Permission to use, copy, modify, and distribute this software for any michael@0: * purpose with or without fee is hereby granted, provided that the above michael@0: * copyright notice and this permission notice appear in all copies. michael@0: * michael@0: * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES michael@0: * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF michael@0: * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR michael@0: * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES michael@0: * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN michael@0: * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT michael@0: * OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. michael@0: */ michael@0: michael@0: /* ev_streams.c - implement asynch stream file IO for the eventlib michael@0: * vix 04mar96 [initial] michael@0: */ michael@0: michael@0: /* michael@0: * This version of this file is derived from Android 2.3 "Gingerbread", michael@0: * which contains uncredited changes by Android/Google developers. It has michael@0: * been modified in 2011 for use in the Android build of Mozilla Firefox by michael@0: * Mozilla contributors (including Michael Edwards , michael@0: * and Steve Workman ). michael@0: * These changes are offered under the same license as the original NetBSD michael@0: * file, whose copyright and license are unchanged above. michael@0: */ michael@0: michael@0: #define ANDROID_CHANGES 1 michael@0: #define MOZILLA_NECKO_EXCLUDE_CODE 1 michael@0: michael@0: #include michael@0: #if !defined(LINT) && !defined(CODECENTER) && !defined(lint) michael@0: #ifdef notdef michael@0: static const char rcsid[] = "Id: ev_streams.c,v 1.2.206.2 2004/03/17 00:29:51 marka Exp"; michael@0: #else michael@0: __RCSID("$NetBSD: ev_streams.c,v 1.2 2004/05/20 19:52:31 christos Exp $"); michael@0: #endif michael@0: #endif michael@0: michael@0: #include michael@0: #include michael@0: michael@0: #include michael@0: michael@0: #include "eventlib.h" michael@0: #include "eventlib_p.h" michael@0: michael@0: #ifndef MOZILLA_NECKO_EXCLUDE_CODE michael@0: #ifndef _LIBC michael@0: static int copyvec(evStream *str, const struct iovec *iov, int iocnt); michael@0: static void consume(evStream *str, size_t bytes); michael@0: static void done(evContext opaqueCtx, evStream *str); michael@0: static void writable(evContext opaqueCtx, void *uap, int fd, int evmask); michael@0: static void readable(evContext opaqueCtx, void *uap, int fd, int evmask); michael@0: #endif michael@0: #endif michael@0: michael@0: struct iovec michael@0: evConsIovec(void *buf, size_t cnt) { michael@0: struct iovec ret; michael@0: michael@0: memset(&ret, 0xf5, sizeof ret); michael@0: ret.iov_base = buf; michael@0: ret.iov_len = cnt; michael@0: return (ret); michael@0: } michael@0: michael@0: #ifndef MOZILLA_NECKO_EXCLUDE_CODE michael@0: #ifndef _LIBC michael@0: int michael@0: evWrite(evContext opaqueCtx, int fd, const struct iovec *iov, int iocnt, michael@0: evStreamFunc func, void *uap, evStreamID *id) michael@0: { michael@0: evContext_p *ctx = opaqueCtx.opaque; michael@0: evStream *new; michael@0: int save; michael@0: michael@0: OKNEW(new); michael@0: new->func = func; michael@0: new->uap = uap; michael@0: new->fd = fd; michael@0: new->flags = 0; michael@0: if (evSelectFD(opaqueCtx, fd, EV_WRITE, writable, new, &new->file) < 0) michael@0: goto free; michael@0: if (copyvec(new, iov, iocnt) < 0) michael@0: goto free; michael@0: new->prevDone = NULL; michael@0: new->nextDone = NULL; michael@0: if (ctx->streams != NULL) michael@0: ctx->streams->prev = new; michael@0: new->prev = NULL; michael@0: new->next = ctx->streams; michael@0: ctx->streams = new; michael@0: if (id != NULL) michael@0: id->opaque = new; michael@0: return (0); michael@0: free: michael@0: save = errno; michael@0: FREE(new); michael@0: errno = save; michael@0: return (-1); michael@0: } michael@0: michael@0: int michael@0: evRead(evContext opaqueCtx, int fd, const struct iovec *iov, int iocnt, michael@0: evStreamFunc func, void *uap, evStreamID *id) michael@0: { michael@0: evContext_p *ctx = opaqueCtx.opaque; michael@0: evStream *new; michael@0: int save; michael@0: michael@0: OKNEW(new); michael@0: new->func = func; michael@0: new->uap = uap; michael@0: new->fd = fd; michael@0: new->flags = 0; michael@0: if (evSelectFD(opaqueCtx, fd, EV_READ, readable, new, &new->file) < 0) michael@0: goto free; michael@0: if (copyvec(new, iov, iocnt) < 0) michael@0: goto free; michael@0: new->prevDone = NULL; michael@0: new->nextDone = NULL; michael@0: if (ctx->streams != NULL) michael@0: ctx->streams->prev = new; michael@0: new->prev = NULL; michael@0: new->next = ctx->streams; michael@0: ctx->streams = new; michael@0: if (id) michael@0: id->opaque = new; michael@0: return (0); michael@0: free: michael@0: save = errno; michael@0: FREE(new); michael@0: errno = save; michael@0: return (-1); michael@0: } michael@0: michael@0: int michael@0: evTimeRW(evContext opaqueCtx, evStreamID id, evTimerID timer) /*ARGSUSED*/ { michael@0: evStream *str = id.opaque; michael@0: michael@0: UNUSED(opaqueCtx); michael@0: michael@0: str->timer = timer; michael@0: str->flags |= EV_STR_TIMEROK; michael@0: return (0); michael@0: } michael@0: michael@0: int michael@0: evUntimeRW(evContext opaqueCtx, evStreamID id) /*ARGSUSED*/ { michael@0: evStream *str = id.opaque; michael@0: michael@0: UNUSED(opaqueCtx); michael@0: michael@0: str->flags &= ~EV_STR_TIMEROK; michael@0: return (0); michael@0: } michael@0: michael@0: int michael@0: evCancelRW(evContext opaqueCtx, evStreamID id) { michael@0: evContext_p *ctx = opaqueCtx.opaque; michael@0: evStream *old = id.opaque; michael@0: michael@0: /* michael@0: * The streams list is doubly threaded. First, there's ctx->streams michael@0: * that's used by evDestroy() to find and cancel all streams. Second, michael@0: * there's ctx->strDone (head) and ctx->strLast (tail) which thread michael@0: * through the potentially smaller number of "IO completed" streams, michael@0: * used in evGetNext() to avoid scanning the entire list. michael@0: */ michael@0: michael@0: /* Unlink from ctx->streams. */ michael@0: if (old->prev != NULL) michael@0: old->prev->next = old->next; michael@0: else michael@0: ctx->streams = old->next; michael@0: if (old->next != NULL) michael@0: old->next->prev = old->prev; michael@0: michael@0: /* michael@0: * If 'old' is on the ctx->strDone list, remove it. Update michael@0: * ctx->strLast if necessary. michael@0: */ michael@0: if (old->prevDone == NULL && old->nextDone == NULL) { michael@0: /* michael@0: * Either 'old' is the only item on the done list, or it's michael@0: * not on the done list. If the former, then we unlink it michael@0: * from the list. If the latter, we leave the list alone. michael@0: */ michael@0: if (ctx->strDone == old) { michael@0: ctx->strDone = NULL; michael@0: ctx->strLast = NULL; michael@0: } michael@0: } else { michael@0: if (old->prevDone != NULL) michael@0: old->prevDone->nextDone = old->nextDone; michael@0: else michael@0: ctx->strDone = old->nextDone; michael@0: if (old->nextDone != NULL) michael@0: old->nextDone->prevDone = old->prevDone; michael@0: else michael@0: ctx->strLast = old->prevDone; michael@0: } michael@0: michael@0: /* Deallocate the stream. */ michael@0: if (old->file.opaque) michael@0: evDeselectFD(opaqueCtx, old->file); michael@0: memput(old->iovOrig, sizeof (struct iovec) * old->iovOrigCount); michael@0: FREE(old); michael@0: return (0); michael@0: } michael@0: michael@0: /* Copy a scatter/gather vector and initialize a stream handler's IO. */ michael@0: static int michael@0: copyvec(evStream *str, const struct iovec *iov, int iocnt) { michael@0: int i; michael@0: michael@0: str->iovOrig = (struct iovec *)memget(sizeof(struct iovec) * iocnt); michael@0: if (str->iovOrig == NULL) { michael@0: errno = ENOMEM; michael@0: return (-1); michael@0: } michael@0: str->ioTotal = 0; michael@0: for (i = 0; i < iocnt; i++) { michael@0: str->iovOrig[i] = iov[i]; michael@0: str->ioTotal += iov[i].iov_len; michael@0: } michael@0: str->iovOrigCount = iocnt; michael@0: str->iovCur = str->iovOrig; michael@0: str->iovCurCount = str->iovOrigCount; michael@0: str->ioDone = 0; michael@0: return (0); michael@0: } michael@0: michael@0: /* Pull off or truncate lead iovec(s). */ michael@0: static void michael@0: consume(evStream *str, size_t bytes) { michael@0: while (bytes > 0U) { michael@0: if (bytes < (size_t)str->iovCur->iov_len) { michael@0: str->iovCur->iov_len -= bytes; michael@0: str->iovCur->iov_base = (void *) michael@0: ((u_char *)str->iovCur->iov_base + bytes); michael@0: str->ioDone += bytes; michael@0: bytes = 0; michael@0: } else { michael@0: bytes -= str->iovCur->iov_len; michael@0: str->ioDone += str->iovCur->iov_len; michael@0: str->iovCur++; michael@0: str->iovCurCount--; michael@0: } michael@0: } michael@0: } michael@0: michael@0: /* Add a stream to Done list and deselect the FD. */ michael@0: static void michael@0: done(evContext opaqueCtx, evStream *str) { michael@0: evContext_p *ctx = opaqueCtx.opaque; michael@0: michael@0: if (ctx->strLast != NULL) { michael@0: str->prevDone = ctx->strLast; michael@0: ctx->strLast->nextDone = str; michael@0: ctx->strLast = str; michael@0: } else { michael@0: INSIST(ctx->strDone == NULL); michael@0: ctx->strDone = ctx->strLast = str; michael@0: } michael@0: evDeselectFD(opaqueCtx, str->file); michael@0: str->file.opaque = NULL; michael@0: /* evDrop() will call evCancelRW() on us. */ michael@0: } michael@0: michael@0: /* Dribble out some bytes on the stream. (Called by evDispatch().) */ michael@0: static void michael@0: writable(evContext opaqueCtx, void *uap, int fd, int evmask) { michael@0: evStream *str = uap; michael@0: int bytes; michael@0: michael@0: UNUSED(evmask); michael@0: michael@0: bytes = writev(fd, str->iovCur, str->iovCurCount); michael@0: if (bytes > 0) { michael@0: if ((str->flags & EV_STR_TIMEROK) != 0) michael@0: evTouchIdleTimer(opaqueCtx, str->timer); michael@0: consume(str, bytes); michael@0: } else { michael@0: if (bytes < 0 && errno != EINTR) { michael@0: str->ioDone = -1; michael@0: str->ioErrno = errno; michael@0: } michael@0: } michael@0: if (str->ioDone == -1 || str->ioDone == str->ioTotal) michael@0: done(opaqueCtx, str); michael@0: } michael@0: michael@0: /* Scoop up some bytes from the stream. (Called by evDispatch().) */ michael@0: static void michael@0: readable(evContext opaqueCtx, void *uap, int fd, int evmask) { michael@0: evStream *str = uap; michael@0: int bytes; michael@0: michael@0: UNUSED(evmask); michael@0: michael@0: bytes = readv(fd, str->iovCur, str->iovCurCount); michael@0: if (bytes > 0) { michael@0: if ((str->flags & EV_STR_TIMEROK) != 0) michael@0: evTouchIdleTimer(opaqueCtx, str->timer); michael@0: consume(str, bytes); michael@0: } else { michael@0: if (bytes == 0) michael@0: str->ioDone = 0; michael@0: else { michael@0: if (errno != EINTR) { michael@0: str->ioDone = -1; michael@0: str->ioErrno = errno; michael@0: } michael@0: } michael@0: } michael@0: if (str->ioDone <= 0 || str->ioDone == str->ioTotal) michael@0: done(opaqueCtx, str); michael@0: } michael@0: #endif michael@0: #endif