Drop unused resource strings
[TortoiseGit.git] / ext / OGDF / src / energybased / FMEThread.h
blob149e6ef60c0c71e891e4bf45aa13afe495153afe
1 /*
2 * $Revision: 2559 $
4 * last checkin:
5 * $Author: gutwenger $
6 * $Date: 2012-07-06 15:04:28 +0200 (Fr, 06. Jul 2012) $
7 ***************************************************************/
9 /** \file
10 * \brief Declaration of class FMEThread.
12 * \author Martin Gronemann
14 * \par License:
15 * This file is part of the Open Graph Drawing Framework (OGDF).
17 * \par
18 * Copyright (C)<br>
19 * See README.txt in the root directory of the OGDF installation for details.
21 * \par
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
26 * for details.
28 * \par
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.
34 * \par
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 ***************************************************************/
43 #ifdef _MSC_VER
44 #pragma once
45 #endif
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"
54 namespace ogdf {
56 class ArrayGraph;
57 class LinearQuadtree;
58 class LinearQuadtreeExpansion;
59 class FMEThreadPool;
60 class FMEThread;
61 class WSPD;
63 //! AutoLock class to easily lock a scope
64 class AutoLock
66 public:
67 AutoLock(CriticalSection* cs) : m_pCriticalSection(cs) { m_pCriticalSection->enter(); }
69 ~AutoLock() { m_pCriticalSection->leave(); }
71 private:
72 CriticalSection* m_pCriticalSection;
76 /*!
77 * The thread task class
78 * used only as an interface
80 class FMETask
82 public:
83 virtual ~FMETask() { }
84 // the doWork method to override
85 virtual void doWork() = 0;
89 /*!
90 * Class used to invoke a functor or function inside a thread.
92 template<typename FuncInvokerType>
93 class FMEFuncInvokerTask : public FMETask
95 public:
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(); }
101 private:
102 //! the invoker
103 FuncInvokerType funcInvoker;
108 * The fast multipole embedder work thread class
110 class FMEThread : public Thread
112 public:
113 //! construtor
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; }
128 //! thread sync call
129 void sync();
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; };
135 #else
136 //! the main work function
137 void doWork() { m_pTask->doWork(); delete m_pTask; m_pTask = 0; }
138 #endif
139 //! sets the actual task
140 void setTask(FMETask* pTask) { m_pTask = pTask; }
142 private:
143 __uint32 m_threadNr;
145 __uint32 m_numThreads;
147 FMEThreadPool* m_pThreadPool;
149 FMETask* m_pTask;
153 class FMEThreadPool
155 public:
156 FMEThreadPool(__uint32 numThreads);
158 ~FMEThreadPool();
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
170 void runThreads();
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));
181 runThreads();
184 private:
185 void allocate();
187 void deallocate();
189 __uint32 m_numThreads;
191 FMEThread** m_pThreads;
193 Barrier* m_pSyncBarrier;
198 #endif