1 /*---------------------------------------------------------------------------*\
3 \\ / F ield | foam-extend: Open Source CFD
4 \\ / O peration | Version: 3.2
5 \\ / A nd | Web: http://www.foam-extend.org
6 \\/ M anipulation | For copyright notice see file Copyright
7 -------------------------------------------------------------------------------
9 This file is part of foam-extend.
11 foam-extend is free software: you can redistribute it and/or modify it
12 under the terms of the GNU General Public License as published by the
13 Free Software Foundation, either version 3 of the License, or (at your
14 option) any later version.
16 foam-extend is distributed in the hope that it will be useful, but
17 WITHOUT ANY WARRANTY; without even the implied warranty of
18 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19 General Public License for more details.
21 You should have received a copy of the GNU General Public License
22 along with foam-extend. If not, see <http://www.gnu.org/licenses/>.
28 Helper class used to assist in multi-threading and synchronization.
30 Use this class to pass information to a threaded static member-function
31 belonging to the class T.
35 University of Massachusetts Amherst
41 \*---------------------------------------------------------------------------*/
43 #ifndef threadHandler_H
44 #define threadHandler_H
46 #include "FixedList.H"
48 #include "multiThreader.H"
50 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
55 /*---------------------------------------------------------------------------*\
56 Class threadHandler Declaration
57 \*---------------------------------------------------------------------------*/
62 // Reference to the template class
65 // Reference to the multiThreader class
66 const multiThreader& threader_;
68 // List of function argument pointers
69 List<void *> argList_;
71 // Total number of threads
72 const label nThreads_;
74 // ID generated by the pthreads API
77 // Is this a master/slave thread
80 // Synchronization mutexes
81 mutable Mutex startMutex_;
82 mutable Mutex stopMutex_;
84 // Conditionals for synchronization
85 mutable Conditional startConditional_;
86 mutable Conditional stopConditional_;
88 // On some implementations, a conditional wait
89 // might return prematurely due to a spurious
90 // wake-up signal. Use a predicate to avoid this
92 mutable FixedList<bool, 2> predicate_;
96 // Enumerants for signalling
107 const multiThreader& threader
113 // Return a reference to the template class
114 inline T& reference();
116 // Set a size for the argument list
117 inline void setSize(const label size);
119 // Set a argument pointer for a particular index
120 inline void set(const label index, void* argPtr);
122 // Return a reference to the multiThreader
123 inline const multiThreader& threader() const;
125 // Return the number of threads
126 inline label nThreads() const;
128 // Designate as master thread
129 inline void setMaster() const;
131 // Designate as slave thread
132 inline void setSlave() const;
134 // Is this a master thread?
135 inline bool master() const;
137 // Is this a slave thread?
138 inline bool slave() const;
141 inline void lock(const signalType sType) const;
143 // Unlock this thread
144 inline void unlock(const signalType sType) const;
146 // Send signal to a waiting conditional
147 inline void sendSignal(const signalType sType) const;
150 inline void waitForSignal(const signalType sType) const;
152 // Return state of the predicate variable
153 inline bool predicate(const signalType sType) const;
155 // Set the predicate variable
156 inline void setPredicate(const signalType sType) const;
158 // Unset the predicate variable
159 inline void unsetPredicate(const signalType sType) const;
162 inline void setID(const pthread_t& pt);
165 inline pthread_t ID() const;
167 // Does the calling thread correspond to this handler?
168 inline bool self() const;
170 // Return an argument pointer at a particular index
171 inline void * operator()(const label index);
175 // Lock all threads provided by sequence
179 const List<label>& sequence,
180 const PtrList<threadHandler<T> >& handler
184 // Synchronize all threads provided by sequence
186 void synchronizeThreads
188 const List<label>& sequence,
189 const PtrList<threadHandler<T> >& handler
193 // Execute threads for the submitted static function by sequence
197 const List<label>& sequence,
198 PtrList<threadHandler<T> >& handler,
199 void (*tFunction)(void*)
202 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
204 } // End namespace Foam
206 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
208 #include "threadHandlerI.H"
210 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
214 // ************************************************************************* //