14#include "arccore/base/NotImplementedException.h"
15#include "arccore/base/IFunctor.h"
17#include "arccore/base/NotSupportedException.h"
20#include "arccore/concurrency/IThreadBarrier.h"
21#include "arccore/concurrency/Mutex.h"
25#include <condition_variable>
31namespace Arccore::Concurrency
52 void initialize()
override;
56 ThreadImpl* createThread(
IFunctor* f)
override;
57 void joinThread(ThreadImpl* t)
override;
58 void destroyThread(ThreadImpl* t)
override;
60 void createSpinLock(
Int64* spin_lock_addr)
override;
61 void lockSpinLock(
Int64* spin_lock_addr,
Int64* scoped_spin_lock_addr)
override;
62 void unlockSpinLock(
Int64* spin_lock_addr,
Int64* scoped_spin_lock_addr)
override;
64 MutexImpl* createMutex()
override;
65 void destroyMutex(MutexImpl*)
override;
66 void lockMutex(MutexImpl* mutex)
override;
67 void unlockMutex(MutexImpl* mutex)
override;
69 Int64 currentThread()
override;
75 void addReference()
override { ReferenceCounterImpl::addReference(); }
76 void removeReference()
override { ReferenceCounterImpl::removeReference(); }
80 MutexImpl* m_global_mutex_impl =
nullptr;
88 void* _StdStartFunc(
void* f)
106 m_nb_thread = nb_thread;
107 m_current_reached = 0;
113 m_current_reached = 0;
119 bool is_last =
false;
121 std::unique_lock<std::mutex> lk(m_wait_mutex);
124 if (m_current_reached == m_nb_thread) {
125 m_current_reached = 0;
138 std::mutex m_wait_mutex;
139 std::condition_variable m_wait;
147StdThreadImplementation::
148StdThreadImplementation()
149: m_global_mutex_impl(nullptr)
153StdThreadImplementation::
154~StdThreadImplementation()
156 GlobalMutex::destroy();
157 if (m_global_mutex_impl)
158 destroyMutex(m_global_mutex_impl);
161void StdThreadImplementation::
164 m_global_mutex_impl = createMutex();
168ThreadImpl* StdThreadImplementation::
169createThread(IFunctor* f)
171 return reinterpret_cast<ThreadImpl*
>(
new std::thread(&_StdStartFunc, f));
174void StdThreadImplementation::
175joinThread(ThreadImpl* t)
177 std::thread* tt =
reinterpret_cast<std::thread*
>(t);
181void StdThreadImplementation::
182destroyThread(ThreadImpl* t)
184 std::thread* tt =
reinterpret_cast<std::thread*
>(t);
188void StdThreadImplementation::
189createSpinLock(Int64* spin_lock_addr)
191 ARCCORE_THROW(NotSupportedException,
"Spin lock. Use std::atomic_flag instead()");
194void StdThreadImplementation::
195lockSpinLock(Int64* spin_lock_addr, Int64* scoped_spin_lock_addr)
197 ARCCORE_THROW(NotSupportedException,
"Spin lock. Use std::atomic_flag instead()");
200void StdThreadImplementation::
201unlockSpinLock(Int64* spin_lock_addr, Int64* scoped_spin_lock_addr)
203 ARCCORE_THROW(NotSupportedException,
"Spin lock. Use std::atomic_flag instead()");
206MutexImpl* StdThreadImplementation::
209 std::mutex* m =
new std::mutex();
210 return reinterpret_cast<MutexImpl*
>(m);
213void StdThreadImplementation::
214destroyMutex(MutexImpl* mutex)
216 std::mutex* m =
reinterpret_cast<std::mutex*
>(mutex);
220void StdThreadImplementation::
221lockMutex(MutexImpl* mutex)
223 std::mutex* m =
reinterpret_cast<std::mutex*
>(mutex);
227void StdThreadImplementation::
228unlockMutex(MutexImpl* mutex)
230 std::mutex* m =
reinterpret_cast<std::mutex*
>(mutex);
234Int64 StdThreadImplementation::
237 Int64 v = std::hash<std::thread::id>{}(std::this_thread::get_id());
241IThreadBarrier* StdThreadImplementation::
244 return new StdThreadBarrier();
250Ref<IThreadImplementation>
251createStdThreadImplementation()
253 return makeRef<IThreadImplementation>(
new Concurrency::StdThreadImplementation());
#define ARCCORE_DEFINE_REFERENCE_COUNTED_INCLASS_METHODS()
Macro pour définir les méthodes gérant les compteurs de référence.
Gestion des références à une classe C++.
void init(Integer nb_thread) override
Initialise la barrière pour nb_thread.
void destroy() override
Détruit la barrière.
bool wait() override
Bloque et attend que tous les threads appellent cette méthode.
Implémentation de ITreadImplementation avec la bibliothèque standard C++.
static void init(MutexImpl *p)
Initialise le mutex global. Interne a Arccore. Doit être alloué par new.
virtual void executeFunctor()=0
Exécute la méthode associé
Interface d'une barrière entre threads.
Interface d'un service implémentant le support des threads.
Implémentation thread-safe d'un compteur de référence.
Int32 Integer
Type représentant un entier.
std::int64_t Int64
Type entier signé sur 64 bits.