LCOV - app.info - nsprpub/pr/src/io/prpolevt.c

LCOV - code coverage report
| Current view: | directory - nsprpub/pr/src/io - prpolevt.c (source / functions) |  | Found | Hit | Coverage |
| Test: | app.info | Lines: | 53 | 0 | 0.0 %
| Date: | 2012-04-07 | Functions: | 7 | 0 | 0.0 %

           1                 : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
           2                 : /* ***** BEGIN LICENSE BLOCK *****
           3                 :  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
           4                 :  *
           5                 :  * The contents of this file are subject to the Mozilla Public License Version
           6                 :  * 1.1 (the "License"); you may not use this file except in compliance with
           7                 :  * the License. You may obtain a copy of the License at
           8                 :  * http://www.mozilla.org/MPL/
           9                 :  *
          10                 :  * Software distributed under the License is distributed on an "AS IS" basis,
          11                 :  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
          12                 :  * for the specific language governing rights and limitations under the
          13                 :  * License.
          14                 :  *
          15                 :  * The Original Code is the Netscape Portable Runtime (NSPR).
          16                 :  *
          17                 :  * The Initial Developer of the Original Code is
          18                 :  * Netscape Communications Corporation.
          19                 :  * Portions created by the Initial Developer are Copyright (C) 1998-2000
          20                 :  * the Initial Developer. All Rights Reserved.
          21                 :  *
          22                 :  * Contributor(s):
          23                 :  *
          24                 :  * Alternatively, the contents of this file may be used under the terms of
          25                 :  * either the GNU General Public License Version 2 or later (the "GPL"), or
          26                 :  * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
          27                 :  * in which case the provisions of the GPL or the LGPL are applicable instead
          28                 :  * of those above. If you wish to allow use of your version of this file only
          29                 :  * under the terms of either the GPL or the LGPL, and not to allow others to
          30                 :  * use your version of this file under the terms of the MPL, indicate your
          31                 :  * decision by deleting the provisions above and replace them with the notice
          32                 :  * and other provisions required by the GPL or the LGPL. If you do not delete
          33                 :  * the provisions above, a recipient may use your version of this file under
          34                 :  * the terms of any one of the MPL, the GPL or the LGPL.
          35                 :  *
          36                 :  * ***** END LICENSE BLOCK ***** */
          37                 :
          38                 : /*
          39                 :  *********************************************************************
          40                 :  *
          41                 :  * Pollable events
          42                 :  *
          43                 :  * Pollable events are implemented using layered I/O.  The only
          44                 :  * I/O methods that are implemented for pollable events are poll
          45                 :  * and close.  No other methods can be invoked on a pollable
          46                 :  * event.
          47                 :  *
          48                 :  * A pipe or socket pair is created and the pollable event layer
          49                 :  * is pushed onto the read end.  A pointer to the write end is
          50                 :  * saved in the PRFilePrivate structure of the pollable event.
          51                 :  *
          52                 :  *********************************************************************
          53                 :  */
          54                 :
          55                 : #include "prinit.h"
          56                 : #include "prio.h"
          57                 : #include "prmem.h"
          58                 : #include "prerror.h"
          59                 : #include "prlog.h"
          60                 :
          61                 : /*
          62                 :  * These internal functions are declared in primpl.h,
          63                 :  * but we can't include primpl.h because the definition
          64                 :  * of struct PRFilePrivate in this file (for the pollable
          65                 :  * event layer) will conflict with the definition of
          66                 :  * struct PRFilePrivate in primpl.h (for the NSPR layer).
          67                 :  */
          68                 : extern PRIntn _PR_InvalidInt(void);
          69                 : extern PRInt64 _PR_InvalidInt64(void);
          70                 : extern PRStatus _PR_InvalidStatus(void);
          71                 : extern PRFileDesc *_PR_InvalidDesc(void);
          72                 :
          73                 : /*
          74                 :  * PRFilePrivate structure for the NSPR pollable events layer
          75                 :  */
          76                 : struct PRFilePrivate {
          77                 :     PRFileDesc *writeEnd;  /* the write end of the pipe/socketpair */
          78                 : };
          79                 :
          80                 : static PRStatus PR_CALLBACK _pr_PolEvtClose(PRFileDesc *fd);
          81                 :
          82                 : static PRInt16 PR_CALLBACK _pr_PolEvtPoll(
          83                 :     PRFileDesc *fd, PRInt16 in_flags, PRInt16 *out_flags);
          84                 :
          85                 : static PRIOMethods _pr_polevt_methods = {
          86                 :     PR_DESC_LAYERED,
          87                 :     _pr_PolEvtClose,
          88                 :     (PRReadFN)_PR_InvalidInt,
          89                 :     (PRWriteFN)_PR_InvalidInt,
          90                 :     (PRAvailableFN)_PR_InvalidInt,
          91                 :     (PRAvailable64FN)_PR_InvalidInt64,
          92                 :     (PRFsyncFN)_PR_InvalidStatus,
          93                 :     (PRSeekFN)_PR_InvalidInt,
          94                 :     (PRSeek64FN)_PR_InvalidInt64,
          95                 :     (PRFileInfoFN)_PR_InvalidStatus,
          96                 :     (PRFileInfo64FN)_PR_InvalidStatus,
          97                 :     (PRWritevFN)_PR_InvalidInt,
          98                 :     (PRConnectFN)_PR_InvalidStatus,
          99                 :     (PRAcceptFN)_PR_InvalidDesc,
         100                 :     (PRBindFN)_PR_InvalidStatus,
         101                 :     (PRListenFN)_PR_InvalidStatus,
         102                 :     (PRShutdownFN)_PR_InvalidStatus,
         103                 :     (PRRecvFN)_PR_InvalidInt,
         104                 :     (PRSendFN)_PR_InvalidInt,
         105                 :     (PRRecvfromFN)_PR_InvalidInt,
         106                 :     (PRSendtoFN)_PR_InvalidInt,
         107                 :     _pr_PolEvtPoll,
         108                 :     (PRAcceptreadFN)_PR_InvalidInt,
         109                 :     (PRTransmitfileFN)_PR_InvalidInt,
         110                 :     (PRGetsocknameFN)_PR_InvalidStatus,
         111                 :     (PRGetpeernameFN)_PR_InvalidStatus,
         112                 :     (PRReservedFN)_PR_InvalidInt,
         113                 :     (PRReservedFN)_PR_InvalidInt,
         114                 :     (PRGetsocketoptionFN)_PR_InvalidStatus,
         115                 :     (PRSetsocketoptionFN)_PR_InvalidStatus,
         116                 :     (PRSendfileFN)_PR_InvalidInt,
         117                 :     (PRConnectcontinueFN)_PR_InvalidStatus,
         118                 :     (PRReservedFN)_PR_InvalidInt,
         119                 :     (PRReservedFN)_PR_InvalidInt,
         120                 :     (PRReservedFN)_PR_InvalidInt,
         121                 :     (PRReservedFN)_PR_InvalidInt
         122                 : };
         123                 :
         124                 : static PRDescIdentity _pr_polevt_id;
         125                 : static PRCallOnceType _pr_polevt_once_control;
         126                 : static PRStatus PR_CALLBACK _pr_PolEvtInit(void);
         127                 :
         128               0 : static PRInt16 PR_CALLBACK _pr_PolEvtPoll(
         129                 :     PRFileDesc *fd, PRInt16 in_flags, PRInt16 *out_flags)
         130                 : {
         131               0 :     return (fd->lower->methods->poll)(fd->lower, in_flags, out_flags);
         132                 : }
         133                 :
         134               0 : static PRStatus PR_CALLBACK _pr_PolEvtInit(void)
         135                 : {
         136               0 :     _pr_polevt_id = PR_GetUniqueIdentity("NSPR pollable events");
         137               0 :     if (PR_INVALID_IO_LAYER == _pr_polevt_id) {
         138               0 :         return PR_FAILURE;
         139                 :     }
         140               0 :     return PR_SUCCESS;
         141                 : }
         142                 :
         143                 : #if !defined(XP_UNIX)
         144                 : #define USE_TCP_SOCKETPAIR
         145                 : #endif
         146                 :
         147               0 : PR_IMPLEMENT(PRFileDesc *) PR_NewPollableEvent(void)
         148                 : {
         149                 :     PRFileDesc *event;
         150                 :     PRFileDesc *fd[2]; /* fd[0] is the read end; fd[1] is the write end */
         151                 : #ifdef USE_TCP_SOCKETPAIR
         152                 :     PRSocketOptionData socket_opt;
         153                 :     PRStatus rv;
         154                 : #endif
         155                 :
         156               0 :     fd[0] = fd[1] = NULL;
         157                 :
         158               0 :     if (PR_CallOnce(&_pr_polevt_once_control, _pr_PolEvtInit) == PR_FAILURE) {
         159               0 :         return NULL;
         160                 :     }
         161                 :
         162               0 :     event = PR_CreateIOLayerStub(_pr_polevt_id, &_pr_polevt_methods);
         163               0 :     if (NULL == event) {
         164               0 :         goto errorExit;
         165                 :     }
         166               0 :     event->secret = PR_NEW(PRFilePrivate);
         167               0 :     if (event->secret == NULL) {
         168               0 :         PR_SetError(PR_OUT_OF_MEMORY_ERROR, 0);
         169               0 :         goto errorExit;
         170                 :     }
         171                 :
         172                 : #ifndef USE_TCP_SOCKETPAIR
         173               0 :     if (PR_CreatePipe(&fd[0], &fd[1]) == PR_FAILURE) {
         174               0 :         fd[0] = fd[1] = NULL;
         175               0 :         goto errorExit;
         176                 :     }
         177                 : #else
         178                 :     if (PR_NewTCPSocketPair(fd) == PR_FAILURE) {
         179                 :         fd[0] = fd[1] = NULL;
         180                 :         goto errorExit;
         181                 :     }
         182                 :         /*
         183                 :          * set the TCP_NODELAY option to reduce notification latency
         184                 :          */
         185                 :     socket_opt.option = PR_SockOpt_NoDelay;
         186                 :     socket_opt.value.no_delay = PR_TRUE;
         187                 :     rv = PR_SetSocketOption(fd[1], &socket_opt);
         188                 :     PR_ASSERT(PR_SUCCESS == rv);
         189                 : #endif
         190                 :
         191               0 :     event->secret->writeEnd = fd[1];
         192               0 :     if (PR_PushIOLayer(fd[0], PR_TOP_IO_LAYER, event) == PR_FAILURE) {
         193               0 :         goto errorExit;
         194                 :     }
         195                 :
         196               0 :     return fd[0];
         197                 :
         198                 : errorExit:
         199               0 :     if (fd[0]) {
         200               0 :         PR_Close(fd[0]);
         201               0 :         PR_Close(fd[1]);
         202                 :     }
         203               0 :     if (event) {
         204               0 :         PR_DELETE(event->secret);
         205               0 :         event->dtor(event);
         206                 :     }
         207               0 :     return NULL;
         208                 : }
         209                 :
         210               0 : static PRStatus PR_CALLBACK _pr_PolEvtClose(PRFileDesc *fd)
         211                 : {
         212                 :     PRFileDesc *event;
         213                 :
         214               0 :     event = PR_PopIOLayer(fd, PR_TOP_IO_LAYER);
         215               0 :     PR_ASSERT(NULL == event->higher && NULL == event->lower);
         216               0 :     PR_Close(fd);
         217               0 :     PR_Close(event->secret->writeEnd);
         218               0 :     PR_DELETE(event->secret);
         219               0 :     event->dtor(event);
         220               0 :     return PR_SUCCESS;
         221                 : }
         222                 :
         223               0 : PR_IMPLEMENT(PRStatus) PR_DestroyPollableEvent(PRFileDesc *event)
         224                 : {
         225               0 :     return PR_Close(event);
         226                 : }
         227                 :
         228                 : static const char magicChar = '\x38';
         229                 :
         230               0 : PR_IMPLEMENT(PRStatus) PR_SetPollableEvent(PRFileDesc *event)
         231                 : {
         232               0 :     if (PR_Write(event->secret->writeEnd, &magicChar, 1) != 1) {
         233               0 :         return PR_FAILURE;
         234                 :     }
         235               0 :     return PR_SUCCESS;
         236                 : }
         237                 :
         238               0 : PR_IMPLEMENT(PRStatus) PR_WaitForPollableEvent(PRFileDesc *event)
         239                 : {
         240                 :     char buf[1024];
         241                 :     PRInt32 nBytes;
         242                 : #ifdef DEBUG
         243                 :     PRIntn i;
         244                 : #endif
         245                 :
         246               0 :     nBytes = PR_Read(event->lower, buf, sizeof(buf));
         247               0 :     if (nBytes == -1) {
         248               0 :         return PR_FAILURE;
         249                 :     }
         250                 :
         251                 : #ifdef DEBUG
         252                 :     /*
         253                 :      * Make sure people do not write to the pollable event fd
         254                 :      * directly.
         255                 :      */
         256               0 :     for (i = 0; i < nBytes; i++) {
         257               0 :         PR_ASSERT(buf[i] == magicChar);
         258                 :     }
         259                 : #endif
         260                 :
         261               0 :     return PR_SUCCESS;
         262                 : }

Generated by: LCOV version 1.7