Upload Tizen:Base source
[toolchains/nspr.git] / mozilla / nsprpub / pr / include / private / pprmwait.h
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 #if defined(_PPRMWAIT_H)
39 #else
40 #define _PPRMWAIT_H
41
42 #include "prlock.h"
43 #include "prcvar.h"
44 #include "prclist.h"
45 #include "prthread.h"
46
47 #define MAX_POLLING_INTERVAL 100
48 #define _PR_POLL_COUNT_FUDGE 64
49 #define _PR_DEFAULT_HASH_LENGTH 59
50
51 /*
52  * Our hash table resolves collisions by open addressing with
53  * double hashing.  See Cormen, Leiserson, and Rivest,
54  * Introduction to Algorithms, p. 232, The MIT Press, 1990.
55  */
56
57 #define _MW_HASH(a, m) ((((PRUptrdiff)(a) >> 4) ^ ((PRUptrdiff)(a) >> 10)) % (m))
58 #define _MW_HASH2(a, m) (1 + ((((PRUptrdiff)(a) >> 4) ^ ((PRUptrdiff)(a) >> 10)) % (m - 2)))
59 #define _MW_ABORTED(_rv) \
60     ((PR_FAILURE == (_rv)) && (PR_PENDING_INTERRUPT_ERROR == PR_GetError()))
61
62 typedef enum {_prmw_success, _prmw_rehash, _prmw_error} _PR_HashStory;
63
64 typedef struct _PRWaiterHash
65 {
66     PRUint16 count;             /* current number in hash table */
67     PRUint16 length;            /* current size of the hash table */
68     PRRecvWait *recv_wait;      /* hash table of receive wait objects */
69 } _PRWaiterHash;
70
71 typedef enum {_prmw_running, _prmw_stopping, _prmw_stopped} PRMWGroupState;
72
73 struct PRWaitGroup
74 {
75     PRCList group_link;         /* all groups are linked to each other */
76     PRCList io_ready;           /* list of I/O requests that are ready */
77     PRMWGroupState state;       /* state of this group (so we can shut down) */
78
79     PRLock *ml;                 /* lock for synchronizing this wait group */
80     PRCondVar *io_taken;        /* calling threads notify when they take I/O */
81     PRCondVar *io_complete;     /* calling threads wait here for completions */
82     PRCondVar *new_business;    /* polling thread waits here more work */
83     PRCondVar *mw_manage;       /* used to manage group lists */
84     PRThread* poller;           /* thread that's actually doing the poll() */
85     PRUint16 waiting_threads;   /* number of threads waiting for recv */
86     PRUint16 polling_count;     /* number of elements in the polling list */
87     PRUint32 p_timestamp;       /* pseudo-time group had element removed */
88     PRPollDesc *polling_list;   /* list poller builds for polling */
89     PRIntervalTime last_poll;   /* last time we polled */
90     _PRWaiterHash *waiter;      /* pointer to hash table of wait receive objects */
91
92 #ifdef WINNT
93     /*
94      * On NT, idle threads are responsible for getting completed i/o.
95      * They need to add completed i/o to the io_ready list.  Since
96      * idle threads cannot use nspr locks, we have to use an md lock
97      * to protect the io_ready list.
98      */
99     _MDLock mdlock;             /* protect io_ready, waiter, and wait_list */
100     PRCList wait_list;          /* used in place of io_complete.  reuse
101                                  * waitQLinks in the PRThread structure. */
102 #endif /* WINNT */
103 };
104
105 /**********************************************************************
106 ***********************************************************************
107 ******************** Wait group enumerations **************************
108 ***********************************************************************
109 **********************************************************************/
110 typedef struct _PRGlobalState
111 {
112     PRCList group_list;         /* master of the group list */
113     PRWaitGroup *group;         /* the default (NULL) group */
114 } _PRGlobalState;
115
116 #ifdef WINNT
117 extern PRStatus NT_HashRemoveInternal(PRWaitGroup *group, PRFileDesc *fd);
118 #endif
119
120 typedef enum {_PR_ENUM_UNSEALED=0, _PR_ENUM_SEALED=0x0eadface} _PREnumSeal;
121
122 struct PRMWaitEnumerator
123 {
124     PRWaitGroup *group;       /* group this enumerator is bound to */
125     PRThread *thread;               /* thread in midst of an enumeration */
126     _PREnumSeal seal;               /* trying to detect deleted objects */
127     PRUint32 p_timestamp;           /* when enumeration was (re)started */
128     PRRecvWait **waiter;            /* pointer into hash table */
129     PRUintn index;                  /* position in hash table */
130     void *pad[4];                   /* some room to grow */
131 };
132
133 #endif /* defined(_PPRMWAIT_H) */
134
135 /* pprmwait.h */