michael@0: /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ michael@0: /* This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this michael@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: /* michael@0: ********************************************************************* michael@0: * michael@0: * Pollable events michael@0: * michael@0: * Pollable events are implemented using layered I/O. The only michael@0: * I/O methods that are implemented for pollable events are poll michael@0: * and close. No other methods can be invoked on a pollable michael@0: * event. michael@0: * michael@0: * A pipe or socket pair is created and the pollable event layer michael@0: * is pushed onto the read end. A pointer to the write end is michael@0: * saved in the PRFilePrivate structure of the pollable event. michael@0: * michael@0: ********************************************************************* michael@0: */ michael@0: michael@0: #include "prinit.h" michael@0: #include "prio.h" michael@0: #include "prmem.h" michael@0: #include "prerror.h" michael@0: #include "prlog.h" michael@0: michael@0: /* michael@0: * These internal functions are declared in primpl.h, michael@0: * but we can't include primpl.h because the definition michael@0: * of struct PRFilePrivate in this file (for the pollable michael@0: * event layer) will conflict with the definition of michael@0: * struct PRFilePrivate in primpl.h (for the NSPR layer). michael@0: */ michael@0: extern PRIntn _PR_InvalidInt(void); michael@0: extern PRInt64 _PR_InvalidInt64(void); michael@0: extern PRStatus _PR_InvalidStatus(void); michael@0: extern PRFileDesc *_PR_InvalidDesc(void); michael@0: michael@0: /* michael@0: * PRFilePrivate structure for the NSPR pollable events layer michael@0: */ michael@0: struct PRFilePrivate { michael@0: PRFileDesc *writeEnd; /* the write end of the pipe/socketpair */ michael@0: }; michael@0: michael@0: static PRStatus PR_CALLBACK _pr_PolEvtClose(PRFileDesc *fd); michael@0: michael@0: static PRInt16 PR_CALLBACK _pr_PolEvtPoll( michael@0: PRFileDesc *fd, PRInt16 in_flags, PRInt16 *out_flags); michael@0: michael@0: static PRIOMethods _pr_polevt_methods = { michael@0: PR_DESC_LAYERED, michael@0: _pr_PolEvtClose, michael@0: (PRReadFN)_PR_InvalidInt, michael@0: (PRWriteFN)_PR_InvalidInt, michael@0: (PRAvailableFN)_PR_InvalidInt, michael@0: (PRAvailable64FN)_PR_InvalidInt64, michael@0: (PRFsyncFN)_PR_InvalidStatus, michael@0: (PRSeekFN)_PR_InvalidInt, michael@0: (PRSeek64FN)_PR_InvalidInt64, michael@0: (PRFileInfoFN)_PR_InvalidStatus, michael@0: (PRFileInfo64FN)_PR_InvalidStatus, michael@0: (PRWritevFN)_PR_InvalidInt, michael@0: (PRConnectFN)_PR_InvalidStatus, michael@0: (PRAcceptFN)_PR_InvalidDesc, michael@0: (PRBindFN)_PR_InvalidStatus, michael@0: (PRListenFN)_PR_InvalidStatus, michael@0: (PRShutdownFN)_PR_InvalidStatus, michael@0: (PRRecvFN)_PR_InvalidInt, michael@0: (PRSendFN)_PR_InvalidInt, michael@0: (PRRecvfromFN)_PR_InvalidInt, michael@0: (PRSendtoFN)_PR_InvalidInt, michael@0: _pr_PolEvtPoll, michael@0: (PRAcceptreadFN)_PR_InvalidInt, michael@0: (PRTransmitfileFN)_PR_InvalidInt, michael@0: (PRGetsocknameFN)_PR_InvalidStatus, michael@0: (PRGetpeernameFN)_PR_InvalidStatus, michael@0: (PRReservedFN)_PR_InvalidInt, michael@0: (PRReservedFN)_PR_InvalidInt, michael@0: (PRGetsocketoptionFN)_PR_InvalidStatus, michael@0: (PRSetsocketoptionFN)_PR_InvalidStatus, michael@0: (PRSendfileFN)_PR_InvalidInt, michael@0: (PRConnectcontinueFN)_PR_InvalidStatus, michael@0: (PRReservedFN)_PR_InvalidInt, michael@0: (PRReservedFN)_PR_InvalidInt, michael@0: (PRReservedFN)_PR_InvalidInt, michael@0: (PRReservedFN)_PR_InvalidInt michael@0: }; michael@0: michael@0: static PRDescIdentity _pr_polevt_id; michael@0: static PRCallOnceType _pr_polevt_once_control; michael@0: static PRStatus PR_CALLBACK _pr_PolEvtInit(void); michael@0: michael@0: static PRInt16 PR_CALLBACK _pr_PolEvtPoll( michael@0: PRFileDesc *fd, PRInt16 in_flags, PRInt16 *out_flags) michael@0: { michael@0: return (fd->lower->methods->poll)(fd->lower, in_flags, out_flags); michael@0: } michael@0: michael@0: static PRStatus PR_CALLBACK _pr_PolEvtInit(void) michael@0: { michael@0: _pr_polevt_id = PR_GetUniqueIdentity("NSPR pollable events"); michael@0: if (PR_INVALID_IO_LAYER == _pr_polevt_id) { michael@0: return PR_FAILURE; michael@0: } michael@0: return PR_SUCCESS; michael@0: } michael@0: michael@0: #if !defined(XP_UNIX) michael@0: #define USE_TCP_SOCKETPAIR michael@0: #endif michael@0: michael@0: PR_IMPLEMENT(PRFileDesc *) PR_NewPollableEvent(void) michael@0: { michael@0: PRFileDesc *event; michael@0: PRFileDesc *fd[2]; /* fd[0] is the read end; fd[1] is the write end */ michael@0: #ifdef USE_TCP_SOCKETPAIR michael@0: PRSocketOptionData socket_opt; michael@0: PRStatus rv; michael@0: #endif michael@0: michael@0: fd[0] = fd[1] = NULL; michael@0: michael@0: if (PR_CallOnce(&_pr_polevt_once_control, _pr_PolEvtInit) == PR_FAILURE) { michael@0: return NULL; michael@0: } michael@0: michael@0: event = PR_CreateIOLayerStub(_pr_polevt_id, &_pr_polevt_methods); michael@0: if (NULL == event) { michael@0: goto errorExit; michael@0: } michael@0: event->secret = PR_NEW(PRFilePrivate); michael@0: if (event->secret == NULL) { michael@0: PR_SetError(PR_OUT_OF_MEMORY_ERROR, 0); michael@0: goto errorExit; michael@0: } michael@0: michael@0: #ifndef USE_TCP_SOCKETPAIR michael@0: if (PR_CreatePipe(&fd[0], &fd[1]) == PR_FAILURE) { michael@0: fd[0] = fd[1] = NULL; michael@0: goto errorExit; michael@0: } michael@0: #else michael@0: if (PR_NewTCPSocketPair(fd) == PR_FAILURE) { michael@0: fd[0] = fd[1] = NULL; michael@0: goto errorExit; michael@0: } michael@0: /* michael@0: * set the TCP_NODELAY option to reduce notification latency michael@0: */ michael@0: socket_opt.option = PR_SockOpt_NoDelay; michael@0: socket_opt.value.no_delay = PR_TRUE; michael@0: rv = PR_SetSocketOption(fd[1], &socket_opt); michael@0: PR_ASSERT(PR_SUCCESS == rv); michael@0: #endif michael@0: michael@0: event->secret->writeEnd = fd[1]; michael@0: if (PR_PushIOLayer(fd[0], PR_TOP_IO_LAYER, event) == PR_FAILURE) { michael@0: goto errorExit; michael@0: } michael@0: michael@0: return fd[0]; michael@0: michael@0: errorExit: michael@0: if (fd[0]) { michael@0: PR_Close(fd[0]); michael@0: PR_Close(fd[1]); michael@0: } michael@0: if (event) { michael@0: PR_DELETE(event->secret); michael@0: event->dtor(event); michael@0: } michael@0: return NULL; michael@0: } michael@0: michael@0: static PRStatus PR_CALLBACK _pr_PolEvtClose(PRFileDesc *fd) michael@0: { michael@0: PRFileDesc *event; michael@0: michael@0: event = PR_PopIOLayer(fd, PR_TOP_IO_LAYER); michael@0: PR_ASSERT(NULL == event->higher && NULL == event->lower); michael@0: PR_Close(fd); michael@0: PR_Close(event->secret->writeEnd); michael@0: PR_DELETE(event->secret); michael@0: event->dtor(event); michael@0: return PR_SUCCESS; michael@0: } michael@0: michael@0: PR_IMPLEMENT(PRStatus) PR_DestroyPollableEvent(PRFileDesc *event) michael@0: { michael@0: return PR_Close(event); michael@0: } michael@0: michael@0: static const char magicChar = '\x38'; michael@0: michael@0: PR_IMPLEMENT(PRStatus) PR_SetPollableEvent(PRFileDesc *event) michael@0: { michael@0: if (PR_Write(event->secret->writeEnd, &magicChar, 1) != 1) { michael@0: return PR_FAILURE; michael@0: } michael@0: return PR_SUCCESS; michael@0: } michael@0: michael@0: PR_IMPLEMENT(PRStatus) PR_WaitForPollableEvent(PRFileDesc *event) michael@0: { michael@0: char buf[1024]; michael@0: PRInt32 nBytes; michael@0: #ifdef DEBUG michael@0: PRIntn i; michael@0: #endif michael@0: michael@0: nBytes = PR_Read(event->lower, buf, sizeof(buf)); michael@0: if (nBytes == -1) { michael@0: return PR_FAILURE; michael@0: } michael@0: michael@0: #ifdef DEBUG michael@0: /* michael@0: * Make sure people do not write to the pollable event fd michael@0: * directly. michael@0: */ michael@0: for (i = 0; i < nBytes; i++) { michael@0: PR_ASSERT(buf[i] == magicChar); michael@0: } michael@0: #endif michael@0: michael@0: return PR_SUCCESS; michael@0: }