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