From 69816377708a917752c1edb3e02678868dfd9b75 Mon Sep 17 00:00:00 2001 From: Sandeep Menon Date: Mon, 4 Oct 2010 16:43:16 -0400 Subject: [PATCH] Adapting multiThreader for const-correctness, and bringing files into sync with r1822/r1823 of the SVN for 1.5-dev. --- .../POSIX/multiThreader/multiThreader.C | 6 +- .../POSIX/multiThreader/multiThreader.H | 12 +- .../POSIX/multiThreader/threadHandler.H | 68 +++++++--- .../POSIX/multiThreader/threadHandlerI.H | 116 ++++++++++++++++-- 4 files changed, 161 insertions(+), 41 deletions(-) diff --git a/src/OSspecific/POSIX/multiThreader/multiThreader.C b/src/OSspecific/POSIX/multiThreader/multiThreader.C index b20310ae6..a9b1c4c13 100644 --- a/src/OSspecific/POSIX/multiThreader/multiThreader.C +++ b/src/OSspecific/POSIX/multiThreader/multiThreader.C @@ -318,7 +318,7 @@ void multiThreader::addToWorkQueue ( void (*tFunction)(void*), void *arg -) +) const { if (singleThreaded()) { @@ -494,7 +494,7 @@ int multiThreader::getNumThreads() const //- Obtain the thread ID for a given index -pthread_t multiThreader::getID(int index) +pthread_t multiThreader::getID(int index) const { if (multiThreaded()) { @@ -536,7 +536,7 @@ int multiThreader::getMaxQueueSize() const //- Set the maxQueueSize -void multiThreader::setMaxQueueSize(int size) +void multiThreader::setMaxQueueSize(int size) const { if (size > 0) { diff --git a/src/OSspecific/POSIX/multiThreader/multiThreader.H b/src/OSspecific/POSIX/multiThreader/multiThreader.H index 874b0833a..1e0cb62b8 100644 --- a/src/OSspecific/POSIX/multiThreader/multiThreader.H +++ b/src/OSspecific/POSIX/multiThreader/multiThreader.H @@ -176,9 +176,9 @@ class multiThreader { // Private data - int numThreads_; + mutable int numThreads_; - int maxQueueSize_; + mutable int maxQueueSize_; // Work-queue item: Holds a pointer to the method and its argument struct workQueueItem @@ -189,7 +189,7 @@ class multiThreader }; // Common structure for all threads in the pool. - struct threadPool + mutable struct threadPool { multiThreader *threader; int numThreads; @@ -246,7 +246,7 @@ public: int getNumThreads() const; //- Obtain the thread ID for a given index - pthread_t getID(int index); + pthread_t getID(int index) const; //- Return true if the number of threads is equal to one. bool singleThreaded() const; @@ -258,10 +258,10 @@ public: int getMaxQueueSize() const; //- Set the maxQueueSize - void setMaxQueueSize(int size); + void setMaxQueueSize(int size) const; //- Add a function to the work queue - void addToWorkQueue(void (*tFunction)(void*), void *arg); + void addToWorkQueue(void (*tFunction)(void*), void *arg) const; //- Conditional handling void waitForCondition(Conditional&, Mutex&) const; diff --git a/src/OSspecific/POSIX/multiThreader/threadHandler.H b/src/OSspecific/POSIX/multiThreader/threadHandler.H index dc11b5633..590450465 100644 --- a/src/OSspecific/POSIX/multiThreader/threadHandler.H +++ b/src/OSspecific/POSIX/multiThreader/threadHandler.H @@ -76,21 +76,21 @@ class threadHandler pthread_t pthreadID_; // Is this a master/slave thread - bool master_; + mutable bool master_; // Synchronization mutexes - Mutex startMutex_; - Mutex stopMutex_; + mutable Mutex startMutex_; + mutable Mutex stopMutex_; // Conditionals for synchronization - Conditional startConditional_; - Conditional stopConditional_; + mutable Conditional startConditional_; + mutable Conditional stopConditional_; // On some implementations, a conditional wait // might return prematurely due to a spurious // wake-up signal. Use a predicate to avoid this // behaviour. - FixedList predicate_; + mutable FixedList predicate_; public: @@ -121,43 +121,43 @@ public: inline void set(const label index, void* argPtr); // Return a reference to the multiThreader - inline const multiThreader& threader(); + inline const multiThreader& threader() const; // Return the number of threads - inline label nThreads(); + inline label nThreads() const; // Designate as master thread - inline void setMaster(); + inline void setMaster() const; // Designate as slave thread - inline void setSlave(); + inline void setSlave() const; // Is this a master thread? - inline bool master(); + inline bool master() const; // Is this a slave thread? - inline bool slave(); + inline bool slave() const; // Lock this thread - inline void lock(const signalType sType); + inline void lock(const signalType sType) const; // Unlock this thread - inline void unlock(const signalType sType); + inline void unlock(const signalType sType) const; // Send signal to a waiting conditional - inline void sendSignal(const signalType sType); + inline void sendSignal(const signalType sType) const; // Wait for signal - inline void waitForSignal(const signalType sType); + inline void waitForSignal(const signalType sType) const; // Return state of the predicate variable - inline bool predicate(const signalType sType); + inline bool predicate(const signalType sType) const; // Set the predicate variable - inline void setPredicate(const signalType sType); + inline void setPredicate(const signalType sType) const; // Unset the predicate variable - inline void unsetPredicate(const signalType sType); + inline void unsetPredicate(const signalType sType) const; // Set the ID inline void setID(const pthread_t& pt); @@ -165,11 +165,41 @@ public: // Return the ID inline pthread_t ID() const; + // Does the calling thread correspond to this handler? + inline bool self() const; + // Return an argument pointer at a particular index inline void * operator()(const label index); }; +// Lock all threads provided by sequence +template +void lockThreads +( + const List