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