8 #include <argos3/core/simulator/simulator.h>
9 #include <argos3/core/utility/profiler/profiler.h>
24 static void CleanupThread(
void* p_data) {
29 SCleanupThreadData& sData =
30 *
reinterpret_cast<SCleanupThreadData*
>(p_data);
31 pthread_mutex_unlock(sData.FetchTaskMutex);
32 pthread_mutex_unlock(sData.StartSenseControlPhaseMutex);
33 pthread_mutex_unlock(sData.StartActPhaseMutex);
34 pthread_mutex_unlock(sData.StartPhysicsPhaseMutex);
35 pthread_mutex_unlock(sData.StartMediaPhaseMutex);
40 LOG.AddThreadSafeBuffer();
41 LOGERR.AddThreadSafeBuffer();
43 pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, NULL);
44 pthread_setcanceltype(PTHREAD_CANCEL_DEFERRED, NULL);
46 CSpaceMultiThreadBalanceLength::SThreadLaunchData* psData =
reinterpret_cast<CSpaceMultiThreadBalanceLength::SThreadLaunchData*
>(p_data);
54 pthread_cleanup_push(CleanupThread, &sCancelData);
55 psData->Space->SlaveThread();
57 pthread_cleanup_pop(1);
70 if((nErrors = pthread_mutex_init(&m_tStartSenseControlPhaseMutex, NULL)) ||
71 (nErrors = pthread_mutex_init(&m_tStartActPhaseMutex, NULL)) ||
72 (nErrors = pthread_mutex_init(&m_tStartPhysicsPhaseMutex, NULL)) ||
73 (nErrors = pthread_mutex_init(&m_tStartMediaPhaseMutex, NULL)) ||
74 (nErrors = pthread_mutex_init(&m_tFetchTaskMutex, NULL))) {
78 if((nErrors = pthread_cond_init(&m_tStartSenseControlPhaseCond, NULL)) ||
79 (nErrors = pthread_cond_init(&m_tStartActPhaseCond, NULL)) ||
80 (nErrors = pthread_cond_init(&m_tStartPhysicsPhaseCond, NULL)) ||
81 (nErrors = pthread_cond_init(&m_tStartMediaPhaseCond, NULL)) ||
82 (nErrors = pthread_cond_init(&m_tFetchTaskCond, NULL))) {
100 if(m_ptThreads != NULL) {
102 if((nErrors = pthread_cancel(m_ptThreads[i]))) {
108 if((nErrors = pthread_join(m_ptThreads[i], ppJoinResult + i))) {
111 if(ppJoinResult[i] != PTHREAD_CANCELED) {
112 LOGERR <<
"[WARNING] Thread #" << i<<
" not canceled" << std::endl;
115 delete[] ppJoinResult;
117 delete[] m_ptThreads;
119 if(m_psThreadData != NULL) {
121 delete m_psThreadData[i];
124 delete[] m_psThreadData;
125 pthread_mutex_destroy(&m_tStartSenseControlPhaseMutex);
126 pthread_mutex_destroy(&m_tStartActPhaseMutex);
127 pthread_mutex_destroy(&m_tStartPhysicsPhaseMutex);
128 pthread_mutex_destroy(&m_tStartMediaPhaseMutex);
129 pthread_mutex_destroy(&m_tFetchTaskMutex);
130 pthread_cond_destroy(&m_tStartSenseControlPhaseCond);
131 pthread_cond_destroy(&m_tStartActPhaseCond);
132 pthread_cond_destroy(&m_tStartPhysicsPhaseCond);
133 pthread_cond_destroy(&m_tStartMediaPhaseCond);
134 pthread_cond_destroy(&m_tFetchTaskCond);
156 #define MAIN_START_PHASE(PHASE) \
157 pthread_mutex_lock(&m_tStart ## PHASE ## PhaseMutex); \
158 m_un ## PHASE ## PhaseIdleCounter = 0; \
160 pthread_cond_broadcast(&m_tStart ## PHASE ## PhaseCond); \
161 pthread_mutex_unlock(&m_tStart ## PHASE ## PhaseMutex);
163 #define MAIN_WAIT_FOR_END_OF(PHASE) \
164 pthread_mutex_lock(&m_tStart ## PHASE ## PhaseMutex); \
165 while(m_un ## PHASE ## PhaseIdleCounter < CSimulator::GetInstance().GetNumThreads()) { \
166 pthread_cond_wait(&m_tStart ## PHASE ## PhaseCond, &m_tStart ## PHASE ## PhaseMutex); \
168 pthread_mutex_unlock(&m_tStart ## PHASE ## PhaseMutex);
186 (*m_ptPhysicsEngines)[i]->TransferEntities();
212 void CSpaceMultiThreadBalanceLength::StartThreads() {
219 m_psThreadData[i] =
new SThreadLaunchData(i,
this);
221 if((nErrors = pthread_create(m_ptThreads + i,
224 reinterpret_cast<void*>(m_psThreadData[i])))) {
233 #define THREAD_WAIT_FOR_START_OF(PHASE) \
234 pthread_mutex_lock(&m_tStart ## PHASE ## PhaseMutex); \
235 while(m_un ## PHASE ## PhaseIdleCounter == CSimulator::GetInstance().GetNumThreads()) { \
236 pthread_cond_wait(&m_tStart ## PHASE ## PhaseCond, &m_tStart ## PHASE ## PhaseMutex); \
238 pthread_mutex_unlock(&m_tStart ## PHASE ## PhaseMutex); \
239 pthread_testcancel();
241 #define THREAD_PERFORM_TASK(PHASE, TASKVEC, SNIPPET) \
243 pthread_mutex_lock(&m_tFetchTaskMutex); \
244 if(m_unTaskIndex < (TASKVEC).size()) { \
245 unTaskIndex = m_unTaskIndex; \
247 pthread_mutex_unlock(&m_tFetchTaskMutex); \
248 pthread_testcancel(); \
252 pthread_testcancel(); \
255 pthread_mutex_unlock(&m_tFetchTaskMutex); \
256 pthread_testcancel(); \
257 pthread_mutex_lock(&m_tStart ## PHASE ## PhaseMutex); \
258 ++m_un ## PHASE ## PhaseIdleCounter; \
259 pthread_cond_broadcast(&m_tStart ## PHASE ## PhaseCond); \
260 pthread_mutex_unlock(&m_tStart ## PHASE ## PhaseMutex); \
261 pthread_testcancel(); \
265 pthread_testcancel();
267 void CSpaceMultiThreadBalanceLength::SlaveThread() {
pthread_mutex_t * StartActPhaseMutex
pthread_mutex_t * StartMediaPhaseMutex
CMedium::TVector * m_ptMedia
A pointer to the list of media.
virtual void UpdateMedia()
CARGoSLog LOG(std::cout, SLogColor(ARGOS_LOG_ATTRIBUTE_BRIGHT, ARGOS_LOG_COLOR_GREEN))
#define THROW_ARGOSEXCEPTION(message)
This macro throws an ARGoS exception with the passed message.
UInt32 GetNumThreads() const
Returns the number of threads used during the experiment.
#define MAIN_START_PHASE(PHASE)
virtual void UpdatePhysics()
ticpp::Element TConfigurationNode
The ARGoS configuration XML node.
virtual void Destroy()
Destroys the space and all its entities.
#define MAIN_WAIT_FOR_END_OF(PHASE)
friend void * LaunchThreadBalanceLength(void *p_data)
pthread_mutex_t * StartSenseControlPhaseMutex
void CollectThreadResourceUsage()
unsigned int UInt32
32-bit unsigned integer.
CPhysicsEngine::TVector * m_ptPhysicsEngines
A pointer to the list of physics engines.
virtual void Init(TConfigurationNode &t_tree)
Initializes the space using the
section of the XML configuration file.
virtual void UpdateControllableEntitiesSenseStep()
#define THREAD_PERFORM_TASK(PHASE, TASKVEC, SNIPPET)
CARGoSLog LOGERR(std::cerr, SLogColor(ARGOS_LOG_ATTRIBUTE_BRIGHT, ARGOS_LOG_COLOR_RED))
pthread_mutex_t * FetchTaskMutex
virtual void Update()
Updates the space.
virtual void Init(TConfigurationNode &t_tree)
Initializes the space using the
section of the XML configuration file.
CProfiler & GetProfiler()
Returns a reference to the profiler.
void * LaunchThreadBalanceLength(void *p_data)
#define THREAD_WAIT_FOR_START_OF(PHASE)
bool IsProfiling() const
Returns true if ARGoS is being profiled.
virtual void Update()
Updates the space.
CControllableEntity::TVector m_vecControllableEntities
A vector of controllable entities.
pthread_mutex_t * StartPhysicsPhaseMutex
The namespace containing all the ARGoS related code.
virtual void UpdateControllableEntitiesAct()
virtual void Destroy()
Destroys the space and all its entities.
static CSimulator & GetInstance()
Returns the instance to the CSimulator class.