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 Arcane::Concurrency
39class ARCCORE_CONCURRENCY_EXPORT StdThreadImplementation
47 StdThreadImplementation();
48 ~StdThreadImplementation()
override;
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);
123 Int32 generation = m_generation;
125 if (m_current_reached == m_nb_thread) {
127 m_current_reached = 0;
133 while (generation == m_generation)
141 std::mutex m_wait_mutex;
142 std::condition_variable m_wait;
145 Int32 m_generation = 0;
151StdThreadImplementation::
152StdThreadImplementation()
153: m_global_mutex_impl(nullptr)
157StdThreadImplementation::
158~StdThreadImplementation()
160 GlobalMutex::destroy();
161 if (m_global_mutex_impl)
162 destroyMutex(m_global_mutex_impl);
165void StdThreadImplementation::
168 m_global_mutex_impl = createMutex();
172ThreadImpl* StdThreadImplementation::
173createThread(IFunctor* f)
175 return reinterpret_cast<ThreadImpl*
>(
new std::thread(&_StdStartFunc, f));
178void StdThreadImplementation::
179joinThread(ThreadImpl* t)
181 std::thread* tt =
reinterpret_cast<std::thread*
>(t);
185void StdThreadImplementation::
186destroyThread(ThreadImpl* t)
188 std::thread* tt =
reinterpret_cast<std::thread*
>(t);
192void StdThreadImplementation::
193createSpinLock(
Int64* spin_lock_addr)
195 ARCCORE_THROW(NotSupportedException,
"Spin lock. Use std::atomic_flag instead()");
198void StdThreadImplementation::
199lockSpinLock(
Int64* spin_lock_addr,
Int64* scoped_spin_lock_addr)
201 ARCCORE_THROW(NotSupportedException,
"Spin lock. Use std::atomic_flag instead()");
204void StdThreadImplementation::
205unlockSpinLock(
Int64* spin_lock_addr,
Int64* scoped_spin_lock_addr)
207 ARCCORE_THROW(NotSupportedException,
"Spin lock. Use std::atomic_flag instead()");
210MutexImpl* StdThreadImplementation::
213 std::mutex* m =
new std::mutex();
214 return reinterpret_cast<MutexImpl*
>(m);
217void StdThreadImplementation::
218destroyMutex(MutexImpl* mutex)
220 std::mutex* m =
reinterpret_cast<std::mutex*
>(mutex);
224void StdThreadImplementation::
225lockMutex(MutexImpl* mutex)
227 std::mutex* m =
reinterpret_cast<std::mutex*
>(mutex);
231void StdThreadImplementation::
232unlockMutex(MutexImpl* mutex)
234 std::mutex* m =
reinterpret_cast<std::mutex*
>(mutex);
238Int64 StdThreadImplementation::
241 Int64 v = std::hash<std::thread::id>{}(std::this_thread::get_id());
245IThreadBarrier* StdThreadImplementation::
248 return new StdThreadBarrier();
254Ref<IThreadImplementation>
255createStdThreadImplementation()
#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 destroy() override
Détruit la barrière.
void init(Integer nb_thread) override
Initialise la barrière pour nb_thread.
bool wait() override
Bloque et attend que tous les threads appellent cette méthode.
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.
std::int64_t Int64
Type entier signé sur 64 bits.
Int32 Integer
Type représentant un entier.
auto makeRef(InstanceType *t) -> Ref< InstanceType >
Créé une référence sur un pointeur.
std::int32_t Int32
Type entier signé sur 32 bits.