michael@0: /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ michael@0: /* This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this michael@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: #if defined(_PPRMWAIT_H) michael@0: #else michael@0: #define _PPRMWAIT_H michael@0: michael@0: #include "prlock.h" michael@0: #include "prcvar.h" michael@0: #include "prclist.h" michael@0: #include "prthread.h" michael@0: michael@0: #define MAX_POLLING_INTERVAL 100 michael@0: #define _PR_POLL_COUNT_FUDGE 64 michael@0: #define _PR_DEFAULT_HASH_LENGTH 59 michael@0: michael@0: /* michael@0: * Our hash table resolves collisions by open addressing with michael@0: * double hashing. See Cormen, Leiserson, and Rivest, michael@0: * Introduction to Algorithms, p. 232, The MIT Press, 1990. michael@0: */ michael@0: michael@0: #define _MW_HASH(a, m) ((((PRUptrdiff)(a) >> 4) ^ ((PRUptrdiff)(a) >> 10)) % (m)) michael@0: #define _MW_HASH2(a, m) (1 + ((((PRUptrdiff)(a) >> 4) ^ ((PRUptrdiff)(a) >> 10)) % (m - 2))) michael@0: #define _MW_ABORTED(_rv) \ michael@0: ((PR_FAILURE == (_rv)) && (PR_PENDING_INTERRUPT_ERROR == PR_GetError())) michael@0: michael@0: typedef enum {_prmw_success, _prmw_rehash, _prmw_error} _PR_HashStory; michael@0: michael@0: typedef struct _PRWaiterHash michael@0: { michael@0: PRUint16 count; /* current number in hash table */ michael@0: PRUint16 length; /* current size of the hash table */ michael@0: PRRecvWait *recv_wait; /* hash table of receive wait objects */ michael@0: } _PRWaiterHash; michael@0: michael@0: typedef enum {_prmw_running, _prmw_stopping, _prmw_stopped} PRMWGroupState; michael@0: michael@0: struct PRWaitGroup michael@0: { michael@0: PRCList group_link; /* all groups are linked to each other */ michael@0: PRCList io_ready; /* list of I/O requests that are ready */ michael@0: PRMWGroupState state; /* state of this group (so we can shut down) */ michael@0: michael@0: PRLock *ml; /* lock for synchronizing this wait group */ michael@0: PRCondVar *io_taken; /* calling threads notify when they take I/O */ michael@0: PRCondVar *io_complete; /* calling threads wait here for completions */ michael@0: PRCondVar *new_business; /* polling thread waits here more work */ michael@0: PRCondVar *mw_manage; /* used to manage group lists */ michael@0: PRThread* poller; /* thread that's actually doing the poll() */ michael@0: PRUint16 waiting_threads; /* number of threads waiting for recv */ michael@0: PRUint16 polling_count; /* number of elements in the polling list */ michael@0: PRUint32 p_timestamp; /* pseudo-time group had element removed */ michael@0: PRPollDesc *polling_list; /* list poller builds for polling */ michael@0: PRIntervalTime last_poll; /* last time we polled */ michael@0: _PRWaiterHash *waiter; /* pointer to hash table of wait receive objects */ michael@0: michael@0: #ifdef WINNT michael@0: /* michael@0: * On NT, idle threads are responsible for getting completed i/o. michael@0: * They need to add completed i/o to the io_ready list. Since michael@0: * idle threads cannot use nspr locks, we have to use an md lock michael@0: * to protect the io_ready list. michael@0: */ michael@0: _MDLock mdlock; /* protect io_ready, waiter, and wait_list */ michael@0: PRCList wait_list; /* used in place of io_complete. reuse michael@0: * waitQLinks in the PRThread structure. */ michael@0: #endif /* WINNT */ michael@0: }; michael@0: michael@0: /********************************************************************** michael@0: *********************************************************************** michael@0: ******************** Wait group enumerations ************************** michael@0: *********************************************************************** michael@0: **********************************************************************/ michael@0: typedef struct _PRGlobalState michael@0: { michael@0: PRCList group_list; /* master of the group list */ michael@0: PRWaitGroup *group; /* the default (NULL) group */ michael@0: } _PRGlobalState; michael@0: michael@0: #ifdef WINNT michael@0: extern PRStatus NT_HashRemoveInternal(PRWaitGroup *group, PRFileDesc *fd); michael@0: #endif michael@0: michael@0: typedef enum {_PR_ENUM_UNSEALED=0, _PR_ENUM_SEALED=0x0eadface} _PREnumSeal; michael@0: michael@0: struct PRMWaitEnumerator michael@0: { michael@0: PRWaitGroup *group; /* group this enumerator is bound to */ michael@0: PRThread *thread; /* thread in midst of an enumeration */ michael@0: _PREnumSeal seal; /* trying to detect deleted objects */ michael@0: PRUint32 p_timestamp; /* when enumeration was (re)started */ michael@0: PRRecvWait **waiter; /* pointer into hash table */ michael@0: PRUintn index; /* position in hash table */ michael@0: void *pad[4]; /* some room to grow */ michael@0: }; michael@0: michael@0: #endif /* defined(_PPRMWAIT_H) */ michael@0: michael@0: /* pprmwait.h */