Merge branch 'master' of wikki.no-ip.biz:OpenFOAM-1.6-ext

This commit is contained in:
Hrvoje Jasak 2010-10-04 22:56:58 +01:00
commit 3aa57b52aa
4 changed files with 161 additions and 41 deletions

View file

@ -318,7 +318,7 @@ void multiThreader::addToWorkQueue
( (
void (*tFunction)(void*), void (*tFunction)(void*),
void *arg void *arg
) ) const
{ {
if (singleThreaded()) if (singleThreaded())
{ {
@ -494,7 +494,7 @@ int multiThreader::getNumThreads() const
//- Obtain the thread ID for a given index //- Obtain the thread ID for a given index
pthread_t multiThreader::getID(int index) pthread_t multiThreader::getID(int index) const
{ {
if (multiThreaded()) if (multiThreaded())
{ {
@ -536,7 +536,7 @@ int multiThreader::getMaxQueueSize() const
//- Set the maxQueueSize //- Set the maxQueueSize
void multiThreader::setMaxQueueSize(int size) void multiThreader::setMaxQueueSize(int size) const
{ {
if (size > 0) if (size > 0)
{ {

View file

@ -176,9 +176,9 @@ class multiThreader
{ {
// Private data // Private data
int numThreads_; mutable int numThreads_;
int maxQueueSize_; mutable int maxQueueSize_;
// Work-queue item: Holds a pointer to the method and its argument // Work-queue item: Holds a pointer to the method and its argument
struct workQueueItem struct workQueueItem
@ -189,7 +189,7 @@ class multiThreader
}; };
// Common structure for all threads in the pool. // Common structure for all threads in the pool.
struct threadPool mutable struct threadPool
{ {
multiThreader *threader; multiThreader *threader;
int numThreads; int numThreads;
@ -246,7 +246,7 @@ public:
int getNumThreads() const; int getNumThreads() const;
//- Obtain the thread ID for a given index //- 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. //- Return true if the number of threads is equal to one.
bool singleThreaded() const; bool singleThreaded() const;
@ -258,10 +258,10 @@ public:
int getMaxQueueSize() const; int getMaxQueueSize() const;
//- Set the maxQueueSize //- Set the maxQueueSize
void setMaxQueueSize(int size); void setMaxQueueSize(int size) const;
//- Add a function to the work queue //- Add a function to the work queue
void addToWorkQueue(void (*tFunction)(void*), void *arg); void addToWorkQueue(void (*tFunction)(void*), void *arg) const;
//- Conditional handling //- Conditional handling
void waitForCondition(Conditional&, Mutex&) const; void waitForCondition(Conditional&, Mutex&) const;

View file

@ -76,21 +76,21 @@ class threadHandler
pthread_t pthreadID_; pthread_t pthreadID_;
// Is this a master/slave thread // Is this a master/slave thread
bool master_; mutable bool master_;
// Synchronization mutexes // Synchronization mutexes
Mutex startMutex_; mutable Mutex startMutex_;
Mutex stopMutex_; mutable Mutex stopMutex_;
// Conditionals for synchronization // Conditionals for synchronization
Conditional startConditional_; mutable Conditional startConditional_;
Conditional stopConditional_; mutable Conditional stopConditional_;
// On some implementations, a conditional wait // On some implementations, a conditional wait
// might return prematurely due to a spurious // might return prematurely due to a spurious
// wake-up signal. Use a predicate to avoid this // wake-up signal. Use a predicate to avoid this
// behaviour. // behaviour.
FixedList<bool, 2> predicate_; mutable FixedList<bool, 2> predicate_;
public: public:
@ -121,43 +121,43 @@ public:
inline void set(const label index, void* argPtr); inline void set(const label index, void* argPtr);
// Return a reference to the multiThreader // Return a reference to the multiThreader
inline const multiThreader& threader(); inline const multiThreader& threader() const;
// Return the number of threads // Return the number of threads
inline label nThreads(); inline label nThreads() const;
// Designate as master thread // Designate as master thread
inline void setMaster(); inline void setMaster() const;
// Designate as slave thread // Designate as slave thread
inline void setSlave(); inline void setSlave() const;
// Is this a master thread? // Is this a master thread?
inline bool master(); inline bool master() const;
// Is this a slave thread? // Is this a slave thread?
inline bool slave(); inline bool slave() const;
// Lock this thread // Lock this thread
inline void lock(const signalType sType); inline void lock(const signalType sType) const;
// Unlock this thread // Unlock this thread
inline void unlock(const signalType sType); inline void unlock(const signalType sType) const;
// Send signal to a waiting conditional // Send signal to a waiting conditional
inline void sendSignal(const signalType sType); inline void sendSignal(const signalType sType) const;
// Wait for signal // Wait for signal
inline void waitForSignal(const signalType sType); inline void waitForSignal(const signalType sType) const;
// Return state of the predicate variable // Return state of the predicate variable
inline bool predicate(const signalType sType); inline bool predicate(const signalType sType) const;
// Set the predicate variable // Set the predicate variable
inline void setPredicate(const signalType sType); inline void setPredicate(const signalType sType) const;
// Unset the predicate variable // Unset the predicate variable
inline void unsetPredicate(const signalType sType); inline void unsetPredicate(const signalType sType) const;
// Set the ID // Set the ID
inline void setID(const pthread_t& pt); inline void setID(const pthread_t& pt);
@ -165,11 +165,41 @@ public:
// Return the ID // Return the ID
inline pthread_t ID() const; 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 // Return an argument pointer at a particular index
inline void * operator()(const label index); inline void * operator()(const label index);
}; };
// Lock all threads provided by sequence
template <class T>
void lockThreads
(
const List<label>& sequence,
const PtrList<threadHandler<T> >& handler
);
// Synchronize all threads provided by sequence
template <class T>
void synchronizeThreads
(
const List<label>& sequence,
const PtrList<threadHandler<T> >& handler
);
// Execute threads for the submitted static function by sequence
template <class T>
void executeThreads
(
const List<label>& sequence,
PtrList<threadHandler<T> >& handler,
void (*tFunction)(void*)
);
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
} // End namespace Foam } // End namespace Foam

View file

@ -113,7 +113,7 @@ inline void threadHandler<T>::set
// Return a reference to the multiThreader // Return a reference to the multiThreader
template<class T> template<class T>
inline const multiThreader& threadHandler<T>::threader() inline const multiThreader& threadHandler<T>::threader() const
{ {
return threader_; return threader_;
} }
@ -121,7 +121,7 @@ inline const multiThreader& threadHandler<T>::threader()
// Return the number of threads // Return the number of threads
template<class T> template<class T>
inline label threadHandler<T>::nThreads() inline label threadHandler<T>::nThreads() const
{ {
return nThreads_; return nThreads_;
} }
@ -129,7 +129,7 @@ inline label threadHandler<T>::nThreads()
// Designate as master thread // Designate as master thread
template<class T> template<class T>
inline void threadHandler<T>::setMaster() inline void threadHandler<T>::setMaster() const
{ {
master_ = true; master_ = true;
} }
@ -137,7 +137,7 @@ inline void threadHandler<T>::setMaster()
// Designate as slave thread // Designate as slave thread
template<class T> template<class T>
inline void threadHandler<T>::setSlave() inline void threadHandler<T>::setSlave() const
{ {
master_ = false; master_ = false;
} }
@ -145,7 +145,7 @@ inline void threadHandler<T>::setSlave()
// Is this a master thread? // Is this a master thread?
template<class T> template<class T>
inline bool threadHandler<T>::master() inline bool threadHandler<T>::master() const
{ {
return (master_ == true); return (master_ == true);
} }
@ -153,7 +153,7 @@ inline bool threadHandler<T>::master()
// Is this a slave thread? // Is this a slave thread?
template<class T> template<class T>
inline bool threadHandler<T>::slave() inline bool threadHandler<T>::slave() const
{ {
return !master(); return !master();
} }
@ -164,7 +164,7 @@ template<class T>
inline void threadHandler<T>::lock inline void threadHandler<T>::lock
( (
const signalType sType const signalType sType
) ) const
{ {
if (sType == START) if (sType == START)
{ {
@ -183,7 +183,7 @@ template<class T>
inline void threadHandler<T>::unlock inline void threadHandler<T>::unlock
( (
const signalType sType const signalType sType
) ) const
{ {
if (sType == START) if (sType == START)
{ {
@ -202,7 +202,7 @@ template<class T>
inline void threadHandler<T>::sendSignal inline void threadHandler<T>::sendSignal
( (
const signalType sType const signalType sType
) ) const
{ {
lock(sType); lock(sType);
@ -249,7 +249,7 @@ template<class T>
inline void threadHandler<T>::waitForSignal inline void threadHandler<T>::waitForSignal
( (
const signalType sType const signalType sType
) ) const
{ {
if (sType == START) if (sType == START)
{ {
@ -294,7 +294,7 @@ template<class T>
inline bool threadHandler<T>::predicate inline bool threadHandler<T>::predicate
( (
const signalType sType const signalType sType
) ) const
{ {
return predicate_[sType]; return predicate_[sType];
} }
@ -305,7 +305,7 @@ template<class T>
inline void threadHandler<T>::setPredicate inline void threadHandler<T>::setPredicate
( (
const signalType sType const signalType sType
) ) const
{ {
predicate_[sType] = true; predicate_[sType] = true;
} }
@ -316,7 +316,7 @@ template<class T>
inline void threadHandler<T>::unsetPredicate inline void threadHandler<T>::unsetPredicate
( (
const signalType sType const signalType sType
) ) const
{ {
predicate_[sType] = false; predicate_[sType] = false;
} }
@ -338,6 +338,14 @@ inline pthread_t threadHandler<T>::ID() const
} }
// Does the calling thread correspond to this handler?
template<class T>
inline bool threadHandler<T>::self() const
{
return pthread_equal(ID(), pthread_self());
}
// Return an argument pointer at a particular index // Return an argument pointer at a particular index
template<class T> template<class T>
inline void * threadHandler<T>::operator() inline void * threadHandler<T>::operator()
@ -371,6 +379,88 @@ inline void * threadHandler<T>::operator()
} }
// * * * * * * * * * * * * * * * Global Functions * * * * * * * * * * * * * //
// Lock all threads provided by sequence
template <class T>
void lockThreads
(
const List<label>& sequence,
const PtrList<threadHandler<T> >& handler
)
{
forAll(sequence, i)
{
handler[sequence[i]].lock(threadHandler<T>::START);
handler[sequence[i]].lock(threadHandler<T>::STOP);
handler[sequence[i]].unsetPredicate(threadHandler<T>::START);
handler[sequence[i]].unsetPredicate(threadHandler<T>::STOP);
}
}
// Synchronize all threads provided by sequence
template <class T>
void synchronizeThreads
(
const List<label>& sequence,
const PtrList<threadHandler<T> >& handler
)
{
forAll(sequence, i)
{
// Wait for a signal from this thread before moving on.
handler[sequence[i]].waitForSignal(threadHandler<T>::STOP);
}
}
// Execute threads for the submitted static function by sequence
template <class T>
void executeThreads
(
const List<label>& sequence,
PtrList<threadHandler<T> >& handler,
void (*tFunction)(void*)
)
{
if (!handler.size())
{
FatalErrorIn
(
"\n\n"
"template <class T>\n"
"void executeThreads\n"
"(\n"
" const List<label>& sequence,\n"
" PtrList<threadHandler<T> >& handler,\n"
" void (*tFunction)(void*)\n"
")\n"
)
<< "Empty handler list."
<< abort(FatalError);
}
// Fetch threader reference
const multiThreader& threader = handler[0].threader();
// Lock slave threads by sequence
lockThreads(sequence, handler);
forAll(sequence, i)
{
// Submit jobs to the work queue
threader.addToWorkQueue(tFunction, &(handler[sequence[i]]));
// Wait for a signal from this thread before moving on.
handler[sequence[i]].waitForSignal(threadHandler<T>::START);
}
// Synchronize threads
synchronizeThreads(sequence, handler);
}
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
} // End namespace Foam } // End namespace Foam