home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / nspr30-v.zip / nspr30-v / include / private / pprmwait.h < prev    next >
C/C++ Source or Header  |  1998-10-05  |  5KB  |  118 lines

  1. /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
  2. /*
  3.  * The contents of this file are subject to the Netscape Public License
  4.  * Version 1.0 (the "NPL"); you may not use this file except in
  5.  * compliance with the NPL.  You may obtain a copy of the NPL at
  6.  * http://www.mozilla.org/NPL/
  7.  * 
  8.  * Software distributed under the NPL is distributed on an "AS IS" basis,
  9.  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the NPL
  10.  * for the specific language governing rights and limitations under the
  11.  * NPL.
  12.  * 
  13.  * The Initial Developer of this code under the NPL is Netscape
  14.  * Communications Corporation.  Portions created by Netscape are
  15.  * Copyright (C) 1998 Netscape Communications Corporation.  All Rights
  16.  * Reserved.
  17.  */
  18.  
  19. #if defined(_PPRMWAIT_H)
  20. #else
  21. #define _PPRMWAIT_H
  22.  
  23. #include "prlock.h"
  24. #include "prcvar.h"
  25. #include "prclist.h"
  26. #include "prthread.h"
  27.  
  28. #define MAX_POLLING_INTERVAL 100
  29. #define _PR_POLL_COUNT_FUDGE 64
  30. #define MAX_POLLING_INTERVAL 100
  31. #define _PR_DEFAULT_HASH_LENGTH 59
  32.  
  33. /*
  34.  * Our hash table resolves collisions by open addressing with
  35.  * double hashing.  See Cormen, Leiserson, and Rivest,
  36.  * Introduction to Algorithms, p. 232, The MIT Press, 1990.
  37.  */
  38.  
  39. #define _MW_HASH(a, m) ((((PRUptrdiff)(a) >> 4) ^ ((PRUptrdiff)(a) >> 10)) % (m))
  40. #define _MW_HASH2(a, m) (1 + ((((PRUptrdiff)(a) >> 4) ^ ((PRUptrdiff)(a) >> 10)) % (m - 2)))
  41. #define _MW_ABORTED(_rv) \
  42.     ((PR_FAILURE == (_rv)) && (PR_PENDING_INTERRUPT_ERROR == PR_GetError()))
  43.  
  44. typedef enum {_prmw_success, _prmw_rehash, _prmw_error} _PR_HashStory;
  45.  
  46. typedef struct _PRWaiterHash
  47. {
  48.     PRUint16 count;             /* current number in hash table */
  49.     PRUint16 length;            /* current size of the hash table */
  50.     PRRecvWait *recv_wait;      /* hash table of receive wait objects */
  51. } _PRWaiterHash;
  52.  
  53. typedef enum {_prmw_running, _prmw_stopping, _prmw_stopped} PRMWGroupState;
  54.  
  55. struct PRWaitGroup
  56. {
  57.     PRCList group_link;         /* all groups are linked to each other */
  58.     PRCList io_ready;           /* list of I/O requests that are ready */
  59.     PRMWGroupState state;       /* state of this group (so we can shut down) */
  60.  
  61.     PRLock *ml;                 /* lock for synchronizing this wait group */
  62.     PRCondVar *io_taken;        /* calling threads notify when they take I/O */
  63.     PRCondVar *io_complete;     /* calling threads wait here for completions */
  64.     PRCondVar *new_business;    /* polling thread waits here more work */
  65.     PRCondVar *mw_manage;       /* used to manage group lists */
  66.     PRThread* poller;           /* thread that's actually doing the poll() */
  67.     PRUint16 waiting_threads;   /* number of threads waiting for recv */
  68.     PRUint16 polling_count;     /* number of elements in the polling list */
  69.     PRUint32 p_timestamp;       /* pseudo-time group had element removed */
  70.     PRPollDesc *polling_list;   /* list poller builds for polling */
  71.     PRIntervalTime last_poll;   /* last time we polled */
  72.     _PRWaiterHash *waiter;      /* pointer to hash table of wait receive objects */
  73.  
  74. #ifdef WINNT
  75.     /*
  76.      * On NT, idle threads are responsible for getting completed i/o.
  77.      * They need to add completed i/o to the io_ready list.  Since
  78.      * idle threads cannot use nspr locks, we have to use an md lock
  79.      * to protect the io_ready list.
  80.      */
  81.     _MDLock mdlock;             /* protect io_ready, waiter, and wait_list */
  82.     PRCList wait_list;          /* used in place of io_complete.  reuse
  83.                                  * waitQLinks in the PRThread structure. */
  84. #endif /* WINNT */
  85. };
  86.  
  87. /**********************************************************************
  88. ***********************************************************************
  89. ******************** Wait group enumerations **************************
  90. ***********************************************************************
  91. **********************************************************************/
  92. typedef struct _PRGlobalState
  93. {
  94.     PRCList group_list;         /* master of the group list */
  95.     PRWaitGroup *group;         /* the default (NULL) group */
  96. } _PRGlobalState;
  97.  
  98. #ifdef WINNT
  99. extern PRStatus NT_HashRemoveInternal(PRWaitGroup *group, PRFileDesc *fd);
  100. #endif
  101.  
  102. typedef enum {_PR_ENUM_UNSEALED=0, _PR_ENUM_SEALED=0x0eadface} _PREnumSeal;
  103.  
  104. struct PRMWaitEnumerator
  105. {
  106.     PRWaitGroup *group;       /* group this enumerator is bound to */
  107.     PRThread *thread;               /* thread in midst of an enumeration */
  108.     _PREnumSeal seal;               /* trying to detect deleted objects */
  109.     PRUint32 p_timestamp;           /* when enumeration was (re)started */
  110.     PRRecvWait **waiter;            /* pointer into hash table */
  111.     PRUintn index;                  /* position in hash table */
  112.     void *pad[4];                   /* some room to grow */
  113. };
  114.  
  115. #endif /* defined(_PPRMWAIT_H) */
  116.  
  117. /* pprmwait.h */
  118.