Forward compatibility: flex
[foam-extend-3.2.git] / src / OSspecific / POSIX / multiThreader / threadHandler.H
blobed39d1f9ec36381f453d5dbbc10536f72d808a76
1 /*---------------------------------------------------------------------------*\
2   =========                 |
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 -------------------------------------------------------------------------------
8 License
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/>.
24 Class
25     threadHandler
27 Description
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.
33 Author
34     Sandeep Menon
35     University of Massachusetts Amherst
36     All rights reserved
38 SourceFiles
39     threadHandlerI.H
41 \*---------------------------------------------------------------------------*/
43 #ifndef threadHandler_H
44 #define threadHandler_H
46 #include "FixedList.H"
47 #include "List.H"
48 #include "multiThreader.H"
50 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
52 namespace Foam
55 /*---------------------------------------------------------------------------*\
56                            Class threadHandler Declaration
57 \*---------------------------------------------------------------------------*/
59 template<class T>
60 class threadHandler
62     // Reference to the template class
63     T& tRef_;
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
75     pthread_t pthreadID_;
77     // Is this a master/slave thread
78     mutable bool master_;
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
91     // behaviour.
92     mutable FixedList<bool, 2> predicate_;
94 public:
96     // Enumerants for signalling
97     enum signalType
98     {
99         START,
100         STOP
101     };
103     // Constructor
104     threadHandler
105     (
106         T& tPtr,
107         const multiThreader& threader
108     );
110     // Destructor
111     ~threadHandler();
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;
140     // Lock this thread
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;
149     // Wait for signal
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;
161     // Set the ID
162     inline void setID(const pthread_t& pt);
164     // Return the ID
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
176 template <class T>
177 void lockThreads
179     const List<label>& sequence,
180     const PtrList<threadHandler<T> >& handler
184 // Synchronize all threads provided by sequence
185 template <class T>
186 void synchronizeThreads
188     const List<label>& sequence,
189     const PtrList<threadHandler<T> >& handler
193 // Execute threads for the submitted static function by sequence
194 template <class T>
195 void executeThreads
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 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
212 #endif
214 // ************************************************************************* //