6 * $Date: 2012-07-06 15:04:28 +0200 (Fr, 06. Jul 2012) $
7 ***************************************************************/
10 * \brief Declaration of class FMEThread.
12 * \author Martin Gronemann
15 * This file is part of the Open Graph Drawing Framework (OGDF).
19 * See README.txt in the root directory of the OGDF installation for details.
22 * This program is free software; you can redistribute it and/or
23 * modify it under the terms of the GNU General Public License
24 * Version 2 or 3 as published by the Free Software Foundation;
25 * see the file LICENSE.txt included in the packaging of this file
29 * This program is distributed in the hope that it will be useful,
30 * but WITHOUT ANY WARRANTY; without even the implied warranty of
31 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
32 * GNU General Public License for more details.
35 * You should have received a copy of the GNU General Public
36 * License along with this program; if not, write to the Free
37 * Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
38 * Boston, MA 02110-1301, USA.
40 * \see http://www.gnu.org/copyleft/gpl.html
41 ***************************************************************/
47 #ifndef OGDF_FME_THREAD_H
48 #define OGDF_FME_THREAD_H
50 #include <ogdf/basic/Thread.h>
51 #include <ogdf/basic/Barrier.h>
52 #include "FastUtils.h"
58 class LinearQuadtreeExpansion
;
63 //! AutoLock class to easily lock a scope
67 AutoLock(CriticalSection
* cs
) : m_pCriticalSection(cs
) { m_pCriticalSection
->enter(); }
69 ~AutoLock() { m_pCriticalSection
->leave(); }
72 CriticalSection
* m_pCriticalSection
;
77 * The thread task class
78 * used only as an interface
83 virtual ~FMETask() { }
84 // the doWork method to override
85 virtual void doWork() = 0;
90 * Class used to invoke a functor or function inside a thread.
92 template<typename FuncInvokerType
>
93 class FMEFuncInvokerTask
: public FMETask
96 //! constructor with an invoker
97 FMEFuncInvokerTask(FuncInvokerType f
) : funcInvoker(f
) { }
99 //! overrides the task doWork() method and invokes the function or functor
100 void doWork() { funcInvoker(); }
103 FuncInvokerType funcInvoker
;
108 * The fast multipole embedder work thread class
110 class FMEThread
: public Thread
114 FMEThread(FMEThreadPool
* pThreadPool
, __uint32 threadNr
);
116 //! returns the index of the thread ( 0.. numThreads()-1 )
117 inline __uint32
threadNr() const { return m_threadNr
; }
119 //! returns the total number of threads in the pool
120 inline __uint32
numThreads() const { return m_numThreads
; }
122 //! returns true if this is the main thread ( the main thread is always the first thread )
123 inline bool isMainThread() const { return (m_threadNr
== 0); }
125 //! returns the ThreadPool this thread belongs to
126 inline FMEThreadPool
* threadPool() const { return m_pThreadPool
; }
130 #if defined(OGDF_SYSTEM_UNIX) && defined(OGDF_FME_THREAD_AFFINITY)
131 void unixSetAffinity();
133 //! the main work function
134 void doWork() { unixSetAffinity(); m_pTask
->doWork(); delete m_pTask
; m_pTask
= 0; };
136 //! the main work function
137 void doWork() { m_pTask
->doWork(); delete m_pTask
; m_pTask
= 0; }
139 //! sets the actual task
140 void setTask(FMETask
* pTask
) { m_pTask
= pTask
; }
145 __uint32 m_numThreads
;
147 FMEThreadPool
* m_pThreadPool
;
156 FMEThreadPool(__uint32 numThreads
);
160 //! returns the number of threads in this pool
161 inline __uint32
numThreads() const { return m_numThreads
; }
163 //! returns the threadNr-th thread
164 inline FMEThread
* thread(__uint32 threadNr
) const { return m_pThreads
[threadNr
]; }
166 //! returns the barrier instance used to sync the threads during execution
167 inline Barrier
* syncBarrier() const { return m_pSyncBarrier
; }
169 //! runs one iteration. This call blocks the main thread
172 template<typename KernelType
, typename ArgType1
>
173 void runKernel(ArgType1 arg1
)
175 for (__uint32 i
=0; i
< numThreads(); i
++)
177 KernelType
kernel(thread(i
));
178 FuncInvoker
<KernelType
, ArgType1
> invoker(kernel
, arg1
);
179 thread(i
)->setTask(new FMEFuncInvokerTask
< FuncInvoker
< KernelType
, ArgType1
> >(invoker
));
189 __uint32 m_numThreads
;
191 FMEThread
** m_pThreads
;
193 Barrier
* m_pSyncBarrier
;