nsprpub/pr/src/io/prpolevt.c

Wed, 31 Dec 2014 06:09:35 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:09:35 +0100
changeset 0
6474c204b198
permissions
-rw-r--r--

Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.

michael@0 1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
michael@0 2 /* This Source Code Form is subject to the terms of the Mozilla Public
michael@0 3 * License, v. 2.0. If a copy of the MPL was not distributed with this
michael@0 4 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
michael@0 5
michael@0 6 /*
michael@0 7 *********************************************************************
michael@0 8 *
michael@0 9 * Pollable events
michael@0 10 *
michael@0 11 * Pollable events are implemented using layered I/O. The only
michael@0 12 * I/O methods that are implemented for pollable events are poll
michael@0 13 * and close. No other methods can be invoked on a pollable
michael@0 14 * event.
michael@0 15 *
michael@0 16 * A pipe or socket pair is created and the pollable event layer
michael@0 17 * is pushed onto the read end. A pointer to the write end is
michael@0 18 * saved in the PRFilePrivate structure of the pollable event.
michael@0 19 *
michael@0 20 *********************************************************************
michael@0 21 */
michael@0 22
michael@0 23 #include "prinit.h"
michael@0 24 #include "prio.h"
michael@0 25 #include "prmem.h"
michael@0 26 #include "prerror.h"
michael@0 27 #include "prlog.h"
michael@0 28
michael@0 29 /*
michael@0 30 * These internal functions are declared in primpl.h,
michael@0 31 * but we can't include primpl.h because the definition
michael@0 32 * of struct PRFilePrivate in this file (for the pollable
michael@0 33 * event layer) will conflict with the definition of
michael@0 34 * struct PRFilePrivate in primpl.h (for the NSPR layer).
michael@0 35 */
michael@0 36 extern PRIntn _PR_InvalidInt(void);
michael@0 37 extern PRInt64 _PR_InvalidInt64(void);
michael@0 38 extern PRStatus _PR_InvalidStatus(void);
michael@0 39 extern PRFileDesc *_PR_InvalidDesc(void);
michael@0 40
michael@0 41 /*
michael@0 42 * PRFilePrivate structure for the NSPR pollable events layer
michael@0 43 */
michael@0 44 struct PRFilePrivate {
michael@0 45 PRFileDesc *writeEnd; /* the write end of the pipe/socketpair */
michael@0 46 };
michael@0 47
michael@0 48 static PRStatus PR_CALLBACK _pr_PolEvtClose(PRFileDesc *fd);
michael@0 49
michael@0 50 static PRInt16 PR_CALLBACK _pr_PolEvtPoll(
michael@0 51 PRFileDesc *fd, PRInt16 in_flags, PRInt16 *out_flags);
michael@0 52
michael@0 53 static PRIOMethods _pr_polevt_methods = {
michael@0 54 PR_DESC_LAYERED,
michael@0 55 _pr_PolEvtClose,
michael@0 56 (PRReadFN)_PR_InvalidInt,
michael@0 57 (PRWriteFN)_PR_InvalidInt,
michael@0 58 (PRAvailableFN)_PR_InvalidInt,
michael@0 59 (PRAvailable64FN)_PR_InvalidInt64,
michael@0 60 (PRFsyncFN)_PR_InvalidStatus,
michael@0 61 (PRSeekFN)_PR_InvalidInt,
michael@0 62 (PRSeek64FN)_PR_InvalidInt64,
michael@0 63 (PRFileInfoFN)_PR_InvalidStatus,
michael@0 64 (PRFileInfo64FN)_PR_InvalidStatus,
michael@0 65 (PRWritevFN)_PR_InvalidInt,
michael@0 66 (PRConnectFN)_PR_InvalidStatus,
michael@0 67 (PRAcceptFN)_PR_InvalidDesc,
michael@0 68 (PRBindFN)_PR_InvalidStatus,
michael@0 69 (PRListenFN)_PR_InvalidStatus,
michael@0 70 (PRShutdownFN)_PR_InvalidStatus,
michael@0 71 (PRRecvFN)_PR_InvalidInt,
michael@0 72 (PRSendFN)_PR_InvalidInt,
michael@0 73 (PRRecvfromFN)_PR_InvalidInt,
michael@0 74 (PRSendtoFN)_PR_InvalidInt,
michael@0 75 _pr_PolEvtPoll,
michael@0 76 (PRAcceptreadFN)_PR_InvalidInt,
michael@0 77 (PRTransmitfileFN)_PR_InvalidInt,
michael@0 78 (PRGetsocknameFN)_PR_InvalidStatus,
michael@0 79 (PRGetpeernameFN)_PR_InvalidStatus,
michael@0 80 (PRReservedFN)_PR_InvalidInt,
michael@0 81 (PRReservedFN)_PR_InvalidInt,
michael@0 82 (PRGetsocketoptionFN)_PR_InvalidStatus,
michael@0 83 (PRSetsocketoptionFN)_PR_InvalidStatus,
michael@0 84 (PRSendfileFN)_PR_InvalidInt,
michael@0 85 (PRConnectcontinueFN)_PR_InvalidStatus,
michael@0 86 (PRReservedFN)_PR_InvalidInt,
michael@0 87 (PRReservedFN)_PR_InvalidInt,
michael@0 88 (PRReservedFN)_PR_InvalidInt,
michael@0 89 (PRReservedFN)_PR_InvalidInt
michael@0 90 };
michael@0 91
michael@0 92 static PRDescIdentity _pr_polevt_id;
michael@0 93 static PRCallOnceType _pr_polevt_once_control;
michael@0 94 static PRStatus PR_CALLBACK _pr_PolEvtInit(void);
michael@0 95
michael@0 96 static PRInt16 PR_CALLBACK _pr_PolEvtPoll(
michael@0 97 PRFileDesc *fd, PRInt16 in_flags, PRInt16 *out_flags)
michael@0 98 {
michael@0 99 return (fd->lower->methods->poll)(fd->lower, in_flags, out_flags);
michael@0 100 }
michael@0 101
michael@0 102 static PRStatus PR_CALLBACK _pr_PolEvtInit(void)
michael@0 103 {
michael@0 104 _pr_polevt_id = PR_GetUniqueIdentity("NSPR pollable events");
michael@0 105 if (PR_INVALID_IO_LAYER == _pr_polevt_id) {
michael@0 106 return PR_FAILURE;
michael@0 107 }
michael@0 108 return PR_SUCCESS;
michael@0 109 }
michael@0 110
michael@0 111 #if !defined(XP_UNIX)
michael@0 112 #define USE_TCP_SOCKETPAIR
michael@0 113 #endif
michael@0 114
michael@0 115 PR_IMPLEMENT(PRFileDesc *) PR_NewPollableEvent(void)
michael@0 116 {
michael@0 117 PRFileDesc *event;
michael@0 118 PRFileDesc *fd[2]; /* fd[0] is the read end; fd[1] is the write end */
michael@0 119 #ifdef USE_TCP_SOCKETPAIR
michael@0 120 PRSocketOptionData socket_opt;
michael@0 121 PRStatus rv;
michael@0 122 #endif
michael@0 123
michael@0 124 fd[0] = fd[1] = NULL;
michael@0 125
michael@0 126 if (PR_CallOnce(&_pr_polevt_once_control, _pr_PolEvtInit) == PR_FAILURE) {
michael@0 127 return NULL;
michael@0 128 }
michael@0 129
michael@0 130 event = PR_CreateIOLayerStub(_pr_polevt_id, &_pr_polevt_methods);
michael@0 131 if (NULL == event) {
michael@0 132 goto errorExit;
michael@0 133 }
michael@0 134 event->secret = PR_NEW(PRFilePrivate);
michael@0 135 if (event->secret == NULL) {
michael@0 136 PR_SetError(PR_OUT_OF_MEMORY_ERROR, 0);
michael@0 137 goto errorExit;
michael@0 138 }
michael@0 139
michael@0 140 #ifndef USE_TCP_SOCKETPAIR
michael@0 141 if (PR_CreatePipe(&fd[0], &fd[1]) == PR_FAILURE) {
michael@0 142 fd[0] = fd[1] = NULL;
michael@0 143 goto errorExit;
michael@0 144 }
michael@0 145 #else
michael@0 146 if (PR_NewTCPSocketPair(fd) == PR_FAILURE) {
michael@0 147 fd[0] = fd[1] = NULL;
michael@0 148 goto errorExit;
michael@0 149 }
michael@0 150 /*
michael@0 151 * set the TCP_NODELAY option to reduce notification latency
michael@0 152 */
michael@0 153 socket_opt.option = PR_SockOpt_NoDelay;
michael@0 154 socket_opt.value.no_delay = PR_TRUE;
michael@0 155 rv = PR_SetSocketOption(fd[1], &socket_opt);
michael@0 156 PR_ASSERT(PR_SUCCESS == rv);
michael@0 157 #endif
michael@0 158
michael@0 159 event->secret->writeEnd = fd[1];
michael@0 160 if (PR_PushIOLayer(fd[0], PR_TOP_IO_LAYER, event) == PR_FAILURE) {
michael@0 161 goto errorExit;
michael@0 162 }
michael@0 163
michael@0 164 return fd[0];
michael@0 165
michael@0 166 errorExit:
michael@0 167 if (fd[0]) {
michael@0 168 PR_Close(fd[0]);
michael@0 169 PR_Close(fd[1]);
michael@0 170 }
michael@0 171 if (event) {
michael@0 172 PR_DELETE(event->secret);
michael@0 173 event->dtor(event);
michael@0 174 }
michael@0 175 return NULL;
michael@0 176 }
michael@0 177
michael@0 178 static PRStatus PR_CALLBACK _pr_PolEvtClose(PRFileDesc *fd)
michael@0 179 {
michael@0 180 PRFileDesc *event;
michael@0 181
michael@0 182 event = PR_PopIOLayer(fd, PR_TOP_IO_LAYER);
michael@0 183 PR_ASSERT(NULL == event->higher && NULL == event->lower);
michael@0 184 PR_Close(fd);
michael@0 185 PR_Close(event->secret->writeEnd);
michael@0 186 PR_DELETE(event->secret);
michael@0 187 event->dtor(event);
michael@0 188 return PR_SUCCESS;
michael@0 189 }
michael@0 190
michael@0 191 PR_IMPLEMENT(PRStatus) PR_DestroyPollableEvent(PRFileDesc *event)
michael@0 192 {
michael@0 193 return PR_Close(event);
michael@0 194 }
michael@0 195
michael@0 196 static const char magicChar = '\x38';
michael@0 197
michael@0 198 PR_IMPLEMENT(PRStatus) PR_SetPollableEvent(PRFileDesc *event)
michael@0 199 {
michael@0 200 if (PR_Write(event->secret->writeEnd, &magicChar, 1) != 1) {
michael@0 201 return PR_FAILURE;
michael@0 202 }
michael@0 203 return PR_SUCCESS;
michael@0 204 }
michael@0 205
michael@0 206 PR_IMPLEMENT(PRStatus) PR_WaitForPollableEvent(PRFileDesc *event)
michael@0 207 {
michael@0 208 char buf[1024];
michael@0 209 PRInt32 nBytes;
michael@0 210 #ifdef DEBUG
michael@0 211 PRIntn i;
michael@0 212 #endif
michael@0 213
michael@0 214 nBytes = PR_Read(event->lower, buf, sizeof(buf));
michael@0 215 if (nBytes == -1) {
michael@0 216 return PR_FAILURE;
michael@0 217 }
michael@0 218
michael@0 219 #ifdef DEBUG
michael@0 220 /*
michael@0 221 * Make sure people do not write to the pollable event fd
michael@0 222 * directly.
michael@0 223 */
michael@0 224 for (i = 0; i < nBytes; i++) {
michael@0 225 PR_ASSERT(buf[i] == magicChar);
michael@0 226 }
michael@0 227 #endif
michael@0 228
michael@0 229 return PR_SUCCESS;
michael@0 230 }

mercurial