Back to index

lightning-sunbird  0.9+nobinonly
pprmwait.h
Go to the documentation of this file.
00001 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
00002 /* ***** BEGIN LICENSE BLOCK *****
00003  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
00004  *
00005  * The contents of this file are subject to the Mozilla Public License Version
00006  * 1.1 (the "License"); you may not use this file except in compliance with
00007  * the License. You may obtain a copy of the License at
00008  * http://www.mozilla.org/MPL/
00009  *
00010  * Software distributed under the License is distributed on an "AS IS" basis,
00011  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
00012  * for the specific language governing rights and limitations under the
00013  * License.
00014  *
00015  * The Original Code is the Netscape Portable Runtime (NSPR).
00016  *
00017  * The Initial Developer of the Original Code is
00018  * Netscape Communications Corporation.
00019  * Portions created by the Initial Developer are Copyright (C) 1998-2000
00020  * the Initial Developer. All Rights Reserved.
00021  *
00022  * Contributor(s):
00023  *
00024  * Alternatively, the contents of this file may be used under the terms of
00025  * either the GNU General Public License Version 2 or later (the "GPL"), or
00026  * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
00027  * in which case the provisions of the GPL or the LGPL are applicable instead
00028  * of those above. If you wish to allow use of your version of this file only
00029  * under the terms of either the GPL or the LGPL, and not to allow others to
00030  * use your version of this file under the terms of the MPL, indicate your
00031  * decision by deleting the provisions above and replace them with the notice
00032  * and other provisions required by the GPL or the LGPL. If you do not delete
00033  * the provisions above, a recipient may use your version of this file under
00034  * the terms of any one of the MPL, the GPL or the LGPL.
00035  *
00036  * ***** END LICENSE BLOCK ***** */
00037 
00038 #if defined(_PPRMWAIT_H)
00039 #else
00040 #define _PPRMWAIT_H
00041 
00042 #include "prlock.h"
00043 #include "prcvar.h"
00044 #include "prclist.h"
00045 #include "prthread.h"
00046 
00047 #define MAX_POLLING_INTERVAL 100
00048 #define _PR_POLL_COUNT_FUDGE 64
00049 #define _PR_DEFAULT_HASH_LENGTH 59
00050 
00051 /*
00052  * Our hash table resolves collisions by open addressing with
00053  * double hashing.  See Cormen, Leiserson, and Rivest,
00054  * Introduction to Algorithms, p. 232, The MIT Press, 1990.
00055  */
00056 
00057 #define _MW_HASH(a, m) ((((PRUptrdiff)(a) >> 4) ^ ((PRUptrdiff)(a) >> 10)) % (m))
00058 #define _MW_HASH2(a, m) (1 + ((((PRUptrdiff)(a) >> 4) ^ ((PRUptrdiff)(a) >> 10)) % (m - 2)))
00059 #define _MW_ABORTED(_rv) \
00060     ((PR_FAILURE == (_rv)) && (PR_PENDING_INTERRUPT_ERROR == PR_GetError()))
00061 
00062 typedef enum {_prmw_success, _prmw_rehash, _prmw_error} _PR_HashStory;
00063 
00064 typedef struct _PRWaiterHash
00065 {
00066     PRUint16 count;             /* current number in hash table */
00067     PRUint16 length;            /* current size of the hash table */
00068     PRRecvWait *recv_wait;      /* hash table of receive wait objects */
00069 } _PRWaiterHash;
00070 
00071 typedef enum {_prmw_running, _prmw_stopping, _prmw_stopped} PRMWGroupState;
00072 
00073 struct PRWaitGroup
00074 {
00075     PRCList group_link;         /* all groups are linked to each other */
00076     PRCList io_ready;           /* list of I/O requests that are ready */
00077     PRMWGroupState state;       /* state of this group (so we can shut down) */
00078 
00079     PRLock *ml;                 /* lock for synchronizing this wait group */
00080     PRCondVar *io_taken;        /* calling threads notify when they take I/O */
00081     PRCondVar *io_complete;     /* calling threads wait here for completions */
00082     PRCondVar *new_business;    /* polling thread waits here more work */
00083     PRCondVar *mw_manage;       /* used to manage group lists */
00084     PRThread* poller;           /* thread that's actually doing the poll() */
00085     PRUint16 waiting_threads;   /* number of threads waiting for recv */
00086     PRUint16 polling_count;     /* number of elements in the polling list */
00087     PRUint32 p_timestamp;       /* pseudo-time group had element removed */
00088     PRPollDesc *polling_list;   /* list poller builds for polling */
00089     PRIntervalTime last_poll;   /* last time we polled */
00090     _PRWaiterHash *waiter;      /* pointer to hash table of wait receive objects */
00091 
00092 #ifdef WINNT
00093     /*
00094      * On NT, idle threads are responsible for getting completed i/o.
00095      * They need to add completed i/o to the io_ready list.  Since
00096      * idle threads cannot use nspr locks, we have to use an md lock
00097      * to protect the io_ready list.
00098      */
00099     _MDLock mdlock;             /* protect io_ready, waiter, and wait_list */
00100     PRCList wait_list;          /* used in place of io_complete.  reuse
00101                                  * waitQLinks in the PRThread structure. */
00102 #endif /* WINNT */
00103 };
00104 
00105 /**********************************************************************
00106 ***********************************************************************
00107 ******************** Wait group enumerations **************************
00108 ***********************************************************************
00109 **********************************************************************/
00110 typedef struct _PRGlobalState
00111 {
00112     PRCList group_list;         /* master of the group list */
00113     PRWaitGroup *group;         /* the default (NULL) group */
00114 } _PRGlobalState;
00115 
00116 #ifdef WINNT
00117 extern PRStatus NT_HashRemoveInternal(PRWaitGroup *group, PRFileDesc *fd);
00118 #endif
00119 
00120 typedef enum {_PR_ENUM_UNSEALED=0, _PR_ENUM_SEALED=0x0eadface} _PREnumSeal;
00121 
00122 struct PRMWaitEnumerator
00123 {
00124     PRWaitGroup *group;       /* group this enumerator is bound to */
00125     PRThread *thread;               /* thread in midst of an enumeration */
00126     _PREnumSeal seal;               /* trying to detect deleted objects */
00127     PRUint32 p_timestamp;           /* when enumeration was (re)started */
00128     PRRecvWait **waiter;            /* pointer into hash table */
00129     PRUintn index;                  /* position in hash table */
00130     void *pad[4];                   /* some room to grow */
00131 };
00132 
00133 #endif /* defined(_PPRMWAIT_H) */
00134 
00135 /* pprmwait.h */