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